Skip to content

Commit

Permalink
refactor(mini-rx-store, signal-store, common): move store effect to c…
Browse files Browse the repository at this point in the history
…ommon
  • Loading branch information
mini-rx committed Oct 13, 2024
1 parent 16ddeb1 commit 6bb219b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 51 deletions.
1 change: 1 addition & 0 deletions libs/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
EffectConfig,
EFFECT_METADATA_KEY,
} from './lib/create-rx-effect';
export { createRxEffectForStore } from './lib/rx-effect';
export { LoggerExtension } from './lib/extensions/logger/logger.extension';
export { UndoExtension } from './lib/extensions/undo/undo-extension';
export { undo } from './lib/extensions/undo/undo';
Expand Down
30 changes: 30 additions & 0 deletions libs/common/src/lib/rx-effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Observable } from 'rxjs';
import {
EFFECT_METADATA_KEY,
EffectConfig,
hasEffectMetaData,
HasEffectMetadata,
} from './create-rx-effect';
import { Action } from './models';
import { defaultEffectsErrorHandler } from './default-effects-error-handler';

export function createRxEffectForStore(dispatchFn: (action: Action) => void) {
function rxEffect(effect$: Observable<any> & HasEffectMetadata): void;
function rxEffect(effect$: Observable<Action>): void;
function rxEffect(effect$: any): void {
const effectWithErrorHandler$: Observable<Action | any> =
defaultEffectsErrorHandler(effect$);
effectWithErrorHandler$.subscribe((action) => {
let shouldDispatch = true;
if (hasEffectMetaData(effect$)) {
const metaData: EffectConfig = effect$[EFFECT_METADATA_KEY];
shouldDispatch = !!metaData.dispatch;
}
if (shouldDispatch) {
dispatchFn(action);
}
});
}

return rxEffect;
}
38 changes: 9 additions & 29 deletions libs/mini-rx-store/src/lib/store-core.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Observable, Subscription } from 'rxjs';
import { Subscription } from 'rxjs';
import { createState } from './state';
import {
Action,
AppState,
createActionsOnQueue,
createMiniRxActionType,
createReducerManager,
defaultEffectsErrorHandler,
EFFECT_METADATA_KEY,
EffectConfig,
createRxEffectForStore,
ExtensionId,
HasEffectMetadata,
hasEffectMetaData,
MetaReducer,
miniRxError,
OperationType,
Expand All @@ -35,26 +30,26 @@ function getReducerManager(): ReducerManager {
}

// ACTIONS
export const { dispatch, actions$ } = createActionsOnQueue();
export const { actions$, dispatch } = createActionsOnQueue();

// APP STATE
export const appState = createState<AppState>();

// Wire up the Redux Store: subscribe to the actions stream, calc next state for every action
// Called by `configureStore` and `addReducer`
function initStore() {
function initStore(): void {
if (actionSubscription) {
return;
}

// Listen to the Actions stream and update state accordingly
// Listen to the Actions stream and update state
actionSubscription = actions$.subscribe((action) => {
const nextState: AppState = getReducerManager().reducer(appState.get() ?? {}, action);
appState.set(nextState);
});
}

export function configureStore(config: StoreConfig<AppState> = {}) {
export function configureStore(config: StoreConfig<AppState> = {}): void {
initStore();

if (getReducerManager().hasFeatureReducers()) {
Expand Down Expand Up @@ -100,29 +95,14 @@ export function addFeature<StateType extends object>(
dispatch({ type: createMiniRxActionType(OperationType.INIT, featureKey) });
}

export function removeFeature(featureKey: string) {
export function removeFeature(featureKey: string): void {
getReducerManager().removeFeatureReducer(featureKey);
dispatch({ type: createMiniRxActionType(OperationType.DESTROY, featureKey) });
}

export function effect(effect$: Observable<any> & HasEffectMetadata): void;
export function effect(effect$: Observable<Action>): void;
export function effect(effect$: any): void {
const effectWithErrorHandler$: Observable<Action> = defaultEffectsErrorHandler(effect$);
effectWithErrorHandler$.subscribe((action) => {
let shouldDispatch = true;
if (hasEffectMetaData(effect$)) {
const metaData: EffectConfig = effect$[EFFECT_METADATA_KEY];
shouldDispatch = !!metaData.dispatch;
}

if (shouldDispatch) {
dispatch(action);
}
});
}
export const effect = createRxEffectForStore(dispatch);

function addExtension(extension: StoreExtension) {
function addExtension(extension: StoreExtension): void {
const metaReducer: MetaReducer<AppState> | void = extension.init();

if (metaReducer) {
Expand Down
28 changes: 6 additions & 22 deletions libs/signal-store/src/lib/store-core.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { signal, WritableSignal } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { Subscription } from 'rxjs';
import {
Action,
AppState,
createActionsOnQueue,
createMiniRxActionType,
createReducerManager,
defaultEffectsErrorHandler,
EFFECT_METADATA_KEY,
EffectConfig,
createRxEffectForStore,
ExtensionId,
HasEffectMetadata,
hasEffectMetaData,
MetaReducer,
OperationType,
Reducer,
Expand Down Expand Up @@ -61,12 +56,15 @@ export function configureStore(config: StoreConfig<AppState> = {}): void {
if (config.metaReducers) {
getReducerManager().addMetaReducers(...config.metaReducers);
}

if (config.extensions) {
sortExtensions(config.extensions).forEach((extension) => addExtension(extension));
}

if (config.reducers) {
getReducerManager().setFeatureReducers(config.reducers);
}

if (config.initialState) {
appState.set(config.initialState);
}
Expand Down Expand Up @@ -97,21 +95,7 @@ export function removeFeature(featureKey: string): void {
dispatch({ type: createMiniRxActionType(OperationType.DESTROY, featureKey) });
}

export function rxEffect(effect$: Observable<any> & HasEffectMetadata): void;
export function rxEffect(effect$: Observable<Action>): void;
export function rxEffect(effect$: any): void {
const effectWithErrorHandler$: Observable<Action | any> = defaultEffectsErrorHandler(effect$);
effectWithErrorHandler$.subscribe((action) => {
let shouldDispatch = true;
if (hasEffectMetaData(effect$)) {
const metaData: EffectConfig = effect$[EFFECT_METADATA_KEY];
shouldDispatch = !!metaData.dispatch;
}
if (shouldDispatch) {
dispatch(action);
}
});
}
export const rxEffect = createRxEffectForStore(dispatch);

function addExtension(extension: StoreExtension): void {
const metaReducer: MetaReducer<AppState> | void = extension.init();
Expand Down

0 comments on commit 6bb219b

Please sign in to comment.