Skip to content

Commit

Permalink
feat: Introduced 'destroy()' method to Store to complete it.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Aug 3, 2021
1 parent 3d074dc commit 199cbb7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
File renamed without changes.
File renamed without changes.
24 changes: 23 additions & 1 deletion packages/rx-effects/src/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,37 @@ describe('Effect', () => {
const resultPromise = firstValueFrom(effect.result$.pipe(materialize()));
const errorPromise = firstValueFrom(effect.error$.pipe(materialize()));
const finalPromise = firstValueFrom(effect.final$.pipe(materialize()));
const pendingPromise = firstValueFrom(
effect.pending.value$.pipe(materialize(), toArray()),
);
const pendingCountPromise = firstValueFrom(
effect.pendingCount.value$.pipe(materialize(), toArray()),
);

effect.destroy();

const completedEvent = { hasValue: false, kind: 'C' };

expect(await donePromise).toEqual(completedEvent);
expect(await resultPromise).toEqual(completedEvent);
expect(await errorPromise).toEqual(completedEvent);
expect(await finalPromise).toEqual(completedEvent);

expect(await pendingPromise).toEqual([
{
hasValue: true,
kind: 'N',
value: false,
},
completedEvent,
]);
expect(await pendingCountPromise).toEqual([
{
hasValue: true,
kind: 'N',
value: 0,
},
completedEvent,
]);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/rx-effects/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function createEffect<Event = void, Result = void, ErrorType = Error>(
subscriptions.unsubscribe();
done$.complete();
error$.complete();
pendingCount.destroy();
},
};
}
30 changes: 19 additions & 11 deletions packages/rx-effects/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { Controller } from './controller';
import { StateMutation } from './stateMutation';
import { StateQuery } from './stateQuery';

Expand All @@ -15,20 +16,18 @@ export type StateReader<State> = StateQuery<State> & {
) => StateQuery<R>;
};

export type Store<State> = StateReader<State> & {
readonly set: (state: State) => void;
readonly update: (mutation: StateMutation<State>) => void;
};
export type Store<State> = StateReader<State> &
Controller<{
set: (state: State) => void;
update: (mutation: StateMutation<State>) => void;
}>;

export function createStore<State>(
initialState: State,
stateCompare: (s1: State, s2: State) => boolean = Object.is,
): Store<State> {
const store$: BehaviorSubject<State> = new BehaviorSubject(initialState);
const state$ = store$.pipe(
distinctUntilChanged(stateCompare),
shareReplay({ refCount: true, bufferSize: 1 }),
);
const state$ = store$.asObservable();

return {
value$: state$,
Expand All @@ -38,13 +37,18 @@ export function createStore<State>(
},

set(nextState: State) {
store$.next(nextState);
const prevState = store$.value;
if (!stateCompare(prevState, nextState)) {
store$.next(nextState);
}
},

update(mutation: StateMutation<State>) {
const prevState = store$.value;
const nextState = mutation(prevState);
store$.next(nextState);
if (!stateCompare(prevState, nextState)) {
store$.next(nextState);
}
},

select<R>(
Expand All @@ -63,5 +67,9 @@ export function createStore<State>(
value$: this.select(selector, valueCompare),
};
},

destroy() {
store$.complete();
},
};
}

0 comments on commit 199cbb7

Please sign in to comment.