Skip to content

Commit

Permalink
Adding valueChanged function to replace onValueChanged method on redu…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
soxtoby committed Oct 27, 2023
1 parent 3fbff91 commit c7e9e97
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/event-reduce-react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-reduce-react",
"description": "React integration for event-reduce: state management based on reducing observable events into state",
"version": "0.5.1",
"version": "0.5.2",
"author": "Simon Oxtoby",
"homepage": "https://github.com/soxtoby/event-reduce",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/event-reduce/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-reduce",
"description": "State management based on reducing observable events into state",
"version": "0.5.1",
"version": "0.5.2",
"author": "Simon Oxtoby",
"homepage": "https://github.com/soxtoby/event-reduce",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/event-reduce/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { AsyncError, AsyncResult, AsyncStart, IAsyncEvent, IAsyncObservables, IE
export { enableLogging } from "./logging";
export { derived, events, extend, model, reduced, state } from "./models";
export { IObservable, IObserver, Observable, Observe, allSources, merge } from "./observable";
export { IObservableValue } from "./observableValue";
export { IObservableValue, valueChanged } from "./observableValue";
export { IBoundReduction, IReduction, reduce } from "./reduction";
export { State, getState, setState } from "./state";
export { ISubject, Subject } from "./subject";
Expand Down
8 changes: 8 additions & 0 deletions packages/event-reduce/src/observableValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ export function startTrackingScope(): Unsubscribe {
}
}

/** Allows subscribing to the changes of the specified observable value. */
export function valueChanged<T>(observableValue: T) {
let observable = getUnderlyingObservable(observableValue);
if (observable)
return observable.values;
throw new ValueIsNotObservableError(observableValue);
}

export function getUnderlyingObservable<T>(value: T): ObservableValue<T> | undefined {
let lastAccessed = consumeLastAccessed();
if (lastAccessed && withInnerTrackingScope(() => lastAccessed!.value) == value)
Expand Down
8 changes: 3 additions & 5 deletions packages/event-reduce/src/reduction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log, sourceTree } from "./logging";
import { IObservable, allSources, isObservable } from "./observable";
import { IObservableValue, ObservableValue, ValueIsNotObservableError, getUnderlyingObservable, protectAgainstAccessingValueWithCommonSource } from "./observableValue";
import { IObservableValue, ObservableValue, ValueIsNotObservableError, getUnderlyingObservable, protectAgainstAccessingValueWithCommonSource, valueChanged } from "./observableValue";
import { State, setState } from "./state";
import { Subject } from "./subject";
import { Unsubscribe } from "./types";
Expand All @@ -18,6 +18,7 @@ type Reducer<TValue, TEvent> = (previous: TValue, eventValue: TEvent) => TValue;

export interface IReduction<T> extends IObservableValue<T> {
on<TEvent>(observable: IObservable<TEvent>, reduce: Reducer<T, TEvent>): this;
/** @deprecated use valueChanged function instead */
onValueChanged<TValue>(observableVaue: TValue, reduce: Reducer<T, TValue>): this;
onRestore(reduce: Reducer<T, State<T>>): this;
}
Expand Down Expand Up @@ -72,10 +73,7 @@ export class Reduction<T> extends ObservableValue<T> implements IReduction<T> {
}

onValueChanged<TValue>(observableValue: TValue, reduce: Reducer<T, TValue>) {
let observable = getUnderlyingObservable(observableValue);
if (observable)
return this.on(observable.values, reduce);
throw new ValueIsNotObservableError(observableValue);
return this.on(valueChanged(observableValue), reduce);
}

onRestore(reduce: Reducer<T, State<T>>): this {
Expand Down
6 changes: 3 additions & 3 deletions tests/ModelTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { asyncEvent, derive, derived, event, events, extend, model, reduce, reduced } from "event-reduce";
import { AccessedValueWithCommonSourceError } from "event-reduce/lib/observableValue";
import { AccessedValueWithCommonSourceError, valueChanged } from "event-reduce/lib/observableValue";
import { describe, it, test, then, when } from "wattle";

describe("models", function () {
Expand All @@ -15,7 +15,7 @@ describe("models", function () {

@reduced
dependentProperty = reduce(1)
.onValueChanged(this.property, (_, p) => p)
.on(valueChanged(this.property), (_, p) => p)
.value;

@derived
Expand All @@ -28,7 +28,7 @@ describe("models", function () {

@reduced
basedOnDerivedProperty = reduce(0)
.onValueChanged(this.derivedProperty, (_, d) => d)
.on(valueChanged(this.derivedProperty), (_, d) => d)
.value;
}
let testModel = new TestModel();
Expand Down

0 comments on commit c7e9e97

Please sign in to comment.