Skip to content

Commit

Permalink
refactor(signal-slice): use createNotifier for version signal
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamorony committed Jul 17, 2024
1 parent 45e84b6 commit f96bb0c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions libs/ngxtension/signal-slice/src/signal-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@angular/core';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
import { connect, type PartialOrValue, type Reducer } from 'ngxtension/connect';
import { createNotifier } from 'ngxtension/create-notifier';
import { Subject, isObservable, share, take, type Observable } from 'rxjs';

type ActionSourceFn<TSignalValue, TPayload> = (
Expand Down Expand Up @@ -277,23 +278,23 @@ function addReducerProperties(
subs: Subject<unknown>[],
observableFromActionSource?: Observable<any>,
) {
const version = signal(0);
const version = createNotifier();
Object.defineProperties(readonlyState, {
[key]: {
value: (nextValue: unknown) => {
if (isObservable(nextValue)) {
return new Promise((res, rej) => {
nextValue.pipe(takeUntilDestroyed(destroyRef)).subscribe({
next: (value) => {
version.update((v) => v + 1);
version.notify();
subject.next(value);
},
error: (err) => {
subject.error(err);
rej(err);
},
complete: () => {
version.update((v) => v + 1);
version.notify();
subject.complete();
res(readonlyState());
},
Expand All @@ -311,7 +312,7 @@ function addReducerProperties(
state$.pipe(take(1)).subscribe((val) => {
res(val);
});
version.update((v) => v + 1);
version.notify();
subject.next(nextValue);
});
},
Expand All @@ -320,7 +321,7 @@ function addReducerProperties(
value: subject.asObservable(),
},
[`${key}Updated`]: {
value: version.asReadonly(),
value: version.listen,
},
});
subs.push(subject);
Expand Down

0 comments on commit f96bb0c

Please sign in to comment.