Skip to content

Commit

Permalink
fix(module): fix storage signal issue (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib authored Aug 18, 2024
1 parent fb54094 commit 2257cbd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/ts-bookstore/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const app = createApp({
render,
devOptions: {
reduxDevTools: true,
autoComputed: true,
},
});

Expand Down
15 changes: 14 additions & 1 deletion examples/ts-bookstore/src/modules/shoppingCart.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable, action, state } from 'reactant';
import { injectable, action, state, computed, watch } from 'reactant';
import { Storage } from 'reactant-storage';

interface IListItem {
Expand All @@ -16,6 +16,14 @@ class ShoppingCart {
this.storage.setStorage(this, {
whitelist: ['list'],
});

watch(
this,
() => this.bookCount,
() => {
console.log('bookCount:', this.bookCount);
}
);
}

@state
Expand All @@ -30,6 +38,11 @@ class ShoppingCart {
book.count += item.count;
}
}

@computed
get bookCount() {
return this.list.reduce((acc, item) => acc + item.count, 0);
}
}

export { ShoppingCart };
3 changes: 2 additions & 1 deletion packages/reactant-module/src/decorators/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export function computed(...args: any[]) {
...descriptor,
get(this: Service) {
if (!this[enableAutoComputedKey]) {
if (__DEV__) {
// if the auto computed feature is disabled, return the computed value directly.
if (__DEV__ && this[storeKey]) {
console.warn(
`You should enable auto computed feature by setting 'autoComputed' to 'true' in the dev options.`
);
Expand Down
46 changes: 24 additions & 22 deletions packages/reactant-storage/src/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
signalMapKey,
enableAutoComputedKey,
} from 'reactant-module';
import type { Reducer, ReducersMapObject, Store } from 'redux';
import type { Middleware, Reducer, ReducersMapObject, Store } from 'redux';
import {
persistStore,
persistReducer,
Expand Down Expand Up @@ -129,6 +129,29 @@ class ReactantStorage extends PluginModule {
});
}

middleware: Middleware = (store) => (next) => (_action) => {
if (
(this as Service)[enableAutoComputedKey] &&
_action.type === REHYDRATE &&
_action.key !== 'root'
) {
const target = getRef(this).modules![_action.key];
const persistStateKeys = Object.keys(_action.payload) ?? [];
persistStateKeys.forEach((persistStateKey) => {
if (
persistStateKey !== '_persist' &&
target[signalMapKey]?.[persistStateKey]
) {
// need to update the target signal value to the latest hydrated state
target[signalMapKey][persistStateKey].value =
_action.payload[persistStateKey];
}
});
}
const result = next(_action);
return result;
};

/**
* get every module rehydrated
*/
Expand Down Expand Up @@ -162,27 +185,6 @@ class ReactantStorage extends PluginModule {
}
const persistConfig = this.persistConfig[key];
if (persistConfig) {
if ((this as Service)[enableAutoComputedKey]) {
const target = getRef(this)!.modules![key];
const ref = getRef(target);
const stopWatching = watch(
this,
() => ref!.state?._persist?.rehydrated,
(rehydrated) => {
if (rehydrated) {
stopWatching();
const persistStateKeys = persistConfig.whitelist ?? [];
persistStateKeys.forEach((persistStateKey) => {
if (target[signalMapKey]?.[persistStateKey]) {
// need to update the target signal value to the latest hydrated state
target[signalMapKey][persistStateKey].value =
ref.state![persistStateKey];
}
});
}
}
);
}
const reducer = persistReducer(persistConfig, reducers[key]);
Object.assign(reducers, {
[key]: reducer,
Expand Down

0 comments on commit 2257cbd

Please sign in to comment.