Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(signal-slice): pass initial state streams to source functions #486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions libs/ngxtension/signal-slice/src/signal-slice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ describe(signalSlice.name, () => {

expect(state().age).toEqual(initialState.age + incrementAge);
});

it('should allow using inital state stream in source', () => {
const ageSource$ = new Subject<number>();

TestBed.runInInjectionContext(() => {
state = signalSlice({
initialState,
sources: [
ageSource$.pipe(map((age) => ({ age }))),
(state) => state.age$.pipe(map((age) => ({ powerLevel: 2 * age }))),
],
});
});

ageSource$.next(5);
TestBed.flushEffects();
expect(state().powerLevel).toEqual(10);
});
});

describe('lazySources', () => {
Expand Down
34 changes: 25 additions & 9 deletions libs/ngxtension/signal-slice/src/signal-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type WritableSignal,
} from '@angular/core';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
import { connect, type PartialOrValue, type Reducer } from 'ngxtension/connect';
import { connect, type PartialOrValue } from 'ngxtension/connect';
import { createNotifier } from 'ngxtension/create-notifier';
import { Subject, isObservable, share, take, type Observable } from 'rxjs';

Expand Down Expand Up @@ -81,7 +81,10 @@ type ActionStreams<
TActionSources extends NamedActionSources<TSignalValue>,
> = {
[K in keyof TActionSources &
string as `${K}$`]: TActionSources[K] extends ActionSourceFn<TSignalValue, unknown>
string as `${K}$`]: TActionSources[K] extends ActionSourceFn<
TSignalValue,
unknown
>
? Observable<void>
: TActionSources[K] extends ActionSourceFn<TSignalValue, infer TValue>
? TValue extends Observable<any>
Expand All @@ -90,6 +93,10 @@ type ActionStreams<
: never;
};

type InitialStateStreams<TSignalValue> = {
[K in keyof TSignalValue & string as `${K}$`]: Observable<TSignalValue[K]>;
};

type ActionUpdates<
TSignalValue,
TActionSources extends NamedActionSources<TSignalValue>,
Expand All @@ -99,7 +106,10 @@ type ActionUpdates<

export type Source<TSignalValue> = Observable<PartialOrValue<TSignalValue>>;
type SourceConfig<TSignalValue> = Array<
Source<TSignalValue> | ((state: Signal<TSignalValue>) => Source<TSignalValue>)
| Source<TSignalValue>
| ((
state: Signal<TSignalValue> & InitialStateStreams<TSignalValue>,
) => Source<TSignalValue>)
>;

export type SignalSlice<
Expand Down Expand Up @@ -174,8 +184,6 @@ export function signalSlice<
TEffects
>;

connectSources(state, sources);

for (const [key, actionSource] of Object.entries(
actionSources as TActionSources,
)) {
Expand Down Expand Up @@ -206,15 +214,20 @@ export function signalSlice<
}

for (const key in initialState) {
const value = computed(() => readonlyState()[key]);
Object.defineProperty(readonlyState, key, {
value: computed(() => readonlyState()[key]),
value,
});
(readonlyState as any)[`${key}$`] = toObservable(value);
}

for (const [key, selector] of Object.entries(selectors(slice))) {
const value = computed(selector);

Object.defineProperty(readonlyState, key, {
value: computed(selector),
value,
});
(readonlyState as any)[`${key}$`] = toObservable(value);
}

for (const [key, namedEffect] of Object.entries(effects(slice))) {
Expand All @@ -231,14 +244,16 @@ export function signalSlice<
});
}

connectSources(state, sources, readonlyState);

destroyRef.onDestroy(() => {
subs.forEach((sub) => sub.complete());
});

const connectLazySources = () => {
if (!lazySourcesLoaded) {
lazySourcesLoaded = true;
connectSources(state, lazySources, injector, true);
connectSources(state, lazySources, readonlyState, injector, true);
}
};

Expand All @@ -257,14 +272,15 @@ export function signalSlice<
function connectSources<TSignalValue>(
state: WritableSignal<TSignalValue>,
sources: SourceConfig<TSignalValue>,
readonlyState: Signal<TSignalValue>,
injector?: Injector,
useUntracked = false,
) {
for (const source of sources) {
if (isObservable(source)) {
connect(state, source, injector, useUntracked);
} else {
connect(state, source(state.asReadonly()), injector, useUntracked);
connect(state, source(readonlyState as any), injector, useUntracked);
}
}
}
Expand Down
Loading