Skip to content

Commit e28a46f

Browse files
committed
fix(reactant-module): fix subscribe issue
1 parent 943d20f commit e28a46f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/reactant-module/src/core/createStore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function createStore<T = any>({
8282
let isExistReducer = false;
8383
let store: ReactantStore | undefined = originalStore;
8484
let reducers: ReducersMapObject = {};
85-
const subscriptions: Subscriptions = [];
85+
const subscriptions: Subscriptions[] = [];
8686
// TODO: replace with `mutative`
8787
const enableAutoFreeze = devOptions.autoFreeze ?? true;
8888
const enableReduxDevTools = devOptions.reduxDevTools ?? __DEV__;
@@ -252,7 +252,7 @@ export function createStore<T = any>({
252252
}
253253
}
254254
if (Array.isArray(service[subscriptionsKey])) {
255-
subscriptions.push(...service[subscriptionsKey]!);
255+
subscriptions.push(service[subscriptionsKey]!);
256256
}
257257
Object.defineProperties(service, {
258258
[modulesKey]: {
@@ -360,6 +360,6 @@ export function createStore<T = any>({
360360
// TODO: refactor hook
361361
beforeReplaceReducer();
362362
}
363-
perform(subscriptions);
363+
perform(Array.prototype.concat.apply([], subscriptions));
364364
return store;
365365
}

packages/reactant-module/test/core/subscriber.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,22 @@ test('subscribe in constructor', () => {
111111
test('subscribe in non-constructor', () => {
112112
const storeSubscribeFn = jest.fn();
113113
const subscribeFn = jest.fn();
114+
const subscribeFn0 = jest.fn();
114115
const whenFn = jest.fn();
115116

117+
@injectable()
118+
class Foo0 {
119+
useSubscribe() {
120+
subscribe(this, subscribeFn0);
121+
}
122+
}
123+
116124
@injectable()
117125
class Foo {
126+
constructor(private foo0: Foo0) {
127+
this.foo0.useSubscribe();
128+
}
129+
118130
unsubscribe?: () => void;
119131

120132
dispose?: () => void;
@@ -175,31 +187,37 @@ test('subscribe in non-constructor', () => {
175187
foo.increase();
176188
expect(storeSubscribeFn.mock.calls.length).toBe(1);
177189
expect(subscribeFn.mock.calls.length).toBe(1);
190+
expect(subscribeFn0.mock.calls.length).toBe(1);
178191
expect(subscribeFn.mock.calls).toEqual([[]]);
192+
expect(subscribeFn0.mock.calls).toEqual([[]]);
179193
expect(whenFn.mock.calls).toEqual([[2, 1]]);
180194
foo.increase();
181195
expect(storeSubscribeFn.mock.calls.length).toBe(2);
182196
expect(subscribeFn.mock.calls.length).toBe(2);
197+
expect(subscribeFn0.mock.calls.length).toBe(2);
183198
expect(whenFn.mock.calls).toEqual([
184199
[2, 1],
185200
[3, 2],
186201
]);
187202
foo.increase1();
188203
expect(storeSubscribeFn.mock.calls.length).toBe(3);
189204
expect(subscribeFn.mock.calls.length).toBe(3);
205+
expect(subscribeFn0.mock.calls.length).toBe(3);
190206
expect(whenFn.mock.calls).toEqual([
191207
[2, 1],
192208
[3, 2],
193209
]);
194210
foo.increase1();
195211
expect(storeSubscribeFn.mock.calls.length).toBe(4);
196212
expect(subscribeFn.mock.calls.length).toBe(4);
213+
expect(subscribeFn0.mock.calls.length).toBe(4);
197214
expect(whenFn.mock.calls.length).toEqual(2);
198215

199216
foo.unsubscribe!();
200217
foo.increase();
201218
expect(storeSubscribeFn.mock.calls.length).toBe(5);
202219
expect(subscribeFn.mock.calls.length).toBe(4);
220+
expect(subscribeFn0.mock.calls.length).toBe(5);
203221
expect(whenFn.mock.calls.length).toEqual(3);
204222

205223
foo.dispose!();

0 commit comments

Comments
 (0)