Skip to content

Commit

Permalink
feat(common): easier calculate extensions and more minor bundle size …
Browse files Browse the repository at this point in the history
…improvements
  • Loading branch information
mini-rx committed Nov 28, 2023
1 parent a41044f commit 7b3f9e5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 39 deletions.
13 changes: 6 additions & 7 deletions libs/common/src/lib/beautify-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { isMiniRxAction } from './is-mini-rx-action';

// Only display type and payload in the LoggingExtension and Redux DevTools
export function beautifyAction(action: Action | MiniRxAction<any>): Action {
if (isMiniRxAction(action)) {
return {
type: action.type,
payload: action.stateOrCallback,
};
}
return action;
return isMiniRxAction(action)
? {
type: action.type,
payload: action.stateOrCallback,
}
: action;
}
3 changes: 1 addition & 2 deletions libs/common/src/lib/calc-next-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { StateOrCallback } from './models';
import { isFunction } from './is-function';

export function calcNextState<T>(state: T, stateOrCallback: StateOrCallback<T>): T {
const newPartialState = isFunction(stateOrCallback) ? stateOrCallback(state) : stateOrCallback;
return {
...state,
...newPartialState,
...(isFunction(stateOrCallback) ? stateOrCallback(state) : stateOrCallback), // new partial state
};
}
18 changes: 4 additions & 14 deletions libs/common/src/lib/calculate-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,9 @@ export function calculateExtensions(
localConfig?: ComponentStoreConfig,
globalConfig?: ComponentStoreConfig
): ComponentStoreExtension[] {
let extensions: ComponentStoreExtension[] = [];

if (localConfig?.extensions) {
if (localConfig.extensions && globalConfig?.extensions) {
extensions = mergeComponentStoreExtensions(
globalConfig.extensions,
localConfig.extensions
);
} else {
extensions = localConfig.extensions;
}
} else if (globalConfig?.extensions) {
extensions = globalConfig.extensions;
}
const extensions: ComponentStoreExtension[] = mergeComponentStoreExtensions(
globalConfig?.extensions ?? [],
localConfig?.extensions ?? []
);
return sortExtensions(extensions);
}
7 changes: 3 additions & 4 deletions libs/common/src/lib/create-component-store-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ export function createComponentStoreReducer<StateType>(
initialState: StateType
): Reducer<StateType> {
return (state: StateType = initialState, action: Action) => {
if (isMiniRxAction<StateType>(action)) {
return calcNextState(state, action.stateOrCallback);
}
return state;
return isMiniRxAction<StateType>(action)
? calcNextState(state, action.stateOrCallback)
: state;
};
}
7 changes: 3 additions & 4 deletions libs/common/src/lib/create-feature-store-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export function createFeatureStoreReducer<StateType>(
initialState: StateType
): Reducer<StateType> {
return (state: StateType = initialState, action: Action): StateType => {
if (isMiniRxAction<StateType>(action) && action.featureId === featureId) {
return calcNextState(state, action.stateOrCallback);
}
return state;
return isMiniRxAction<StateType>(action) && action.featureId === featureId
? calcNextState(state, action.stateOrCallback)
: state;
};
}
10 changes: 3 additions & 7 deletions libs/common/src/lib/create-mini-rx-action-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ export function createMiniRxActionType(
featureKey?: string,
name?: string
): string {
return (
miniRxNameSpace +
(featureKey ? '/' + featureKey : '') +
'/' +
operationType +
(name ? '/' + name : '')
);
return `${miniRxNameSpace}${featureKey ? '/' + featureKey : ''}/${operationType}${
name ? '/' + name : ''
}`;
}
2 changes: 1 addition & 1 deletion libs/common/src/lib/generate-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Simple alpha numeric ID: https://stackoverflow.com/a/12502559/453959
// Simple alphanumeric ID: https://stackoverflow.com/a/12502559/453959
// This isn't a real GUID!
export function generateId(): string {
return Math.random().toString(36).slice(2);
Expand Down

0 comments on commit 7b3f9e5

Please sign in to comment.