Skip to content

Commit bac8c81

Browse files
committed
feat(reactant-module): support subscribe with immediate options
1 parent 7ae8ddf commit bac8c81

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ import { storeKey, subscriptionsKey, unsubscriptionsKey } from '../constants';
3636
* });
3737
* ```
3838
*/
39-
const subscribe: Subscribe = (service, listener) => {
39+
const subscribe: Subscribe = (service, listener, { immediate } = {}) => {
4040
if (typeof listener !== 'function') {
4141
throw new Error(`The 'listener' should be a function.`);
4242
}
4343
let unsubscribe: Unsubscribe;
4444
if (service[storeKey]) {
45+
if (immediate) {
46+
listener();
47+
}
4548
unsubscribe = service[storeKey]?.subscribe(listener)!;
4649
} else {
4750
// When constructing
4851
const subscriptions = service[subscriptionsKey] ?? [];
4952
let _unsubscribe: Unsubscribe;
5053
subscriptions.push(() => {
54+
if (immediate) {
55+
listener();
56+
}
5157
_unsubscribe = service[storeKey]?.subscribe(listener)!;
5258
});
5359
unsubscribe = () => {

packages/reactant-module/src/interfaces.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ export type Subscribe = (
155155
/**
156156
* Redux's store subscription
157157
*/
158-
listener: () => void
158+
listener: () => void,
159+
/**
160+
* Redux's options subscription
161+
*/
162+
options?: {
163+
/**
164+
* Whether to trigger the listener immediately
165+
*/
166+
immediate?: boolean;
167+
}
159168
) => Unsubscribe;
160169

161170
type Selector<T> = () => T;

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

+20
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ import {
1111
test('subscribe in constructor', () => {
1212
const storeSubscribeFn = jest.fn();
1313
const subscribeFn = jest.fn();
14+
const subscribeFn1 = jest.fn();
15+
const subscribeFn2 = jest.fn();
16+
const subscribeFn3 = jest.fn();
1417
const whenFn = jest.fn();
1518

1619
@injectable()
1720
class Foo {
1821
unsubscribe: () => void;
1922

23+
unsubscribe1: () => void;
24+
2025
dispose: () => void;
2126

2227
constructor() {
2328
this.unsubscribe = subscribe(this, subscribeFn);
29+
this.unsubscribe1 = subscribe(this, subscribeFn1, { immediate: true });
2430
this.dispose = watch(this, () => this.count, whenFn);
2531
}
2632

@@ -76,37 +82,51 @@ test('subscribe in constructor', () => {
7682
foo.increase();
7783
expect(storeSubscribeFn.mock.calls.length).toBe(1);
7884
expect(subscribeFn.mock.calls.length).toBe(1);
85+
expect(subscribeFn1.mock.calls.length).toBe(2);
7986
expect(subscribeFn.mock.calls).toEqual([[]]);
8087
expect(whenFn.mock.calls).toEqual([[2, 1]]);
8188
foo.increase();
8289
expect(storeSubscribeFn.mock.calls.length).toBe(2);
8390
expect(subscribeFn.mock.calls.length).toBe(2);
91+
expect(subscribeFn1.mock.calls.length).toBe(3);
8492
expect(whenFn.mock.calls).toEqual([
8593
[2, 1],
8694
[3, 2],
8795
]);
8896
foo.increase1();
8997
expect(storeSubscribeFn.mock.calls.length).toBe(3);
9098
expect(subscribeFn.mock.calls.length).toBe(3);
99+
expect(subscribeFn1.mock.calls.length).toBe(4);
91100
expect(whenFn.mock.calls).toEqual([
92101
[2, 1],
93102
[3, 2],
94103
]);
95104
foo.increase1();
96105
expect(storeSubscribeFn.mock.calls.length).toBe(4);
97106
expect(subscribeFn.mock.calls.length).toBe(4);
107+
expect(subscribeFn1.mock.calls.length).toBe(5);
98108
expect(whenFn.mock.calls.length).toEqual(2);
99109

100110
foo.unsubscribe();
111+
foo.unsubscribe1();
101112
foo.increase();
102113
expect(storeSubscribeFn.mock.calls.length).toBe(5);
103114
expect(subscribeFn.mock.calls.length).toBe(4);
115+
expect(subscribeFn1.mock.calls.length).toBe(5);
104116
expect(whenFn.mock.calls.length).toEqual(3);
105117

118+
subscribe(foo, subscribeFn2);
119+
expect(subscribeFn2.mock.calls.length).toBe(0);
120+
121+
subscribe(foo, subscribeFn3, { immediate: true });
122+
expect(subscribeFn3.mock.calls.length).toBe(1);
123+
106124
foo.dispose();
107125
foo.increase();
108126
expect(storeSubscribeFn.mock.calls.length).toBe(6);
109127
expect(whenFn.mock.calls.length).toEqual(3);
128+
expect(subscribeFn2.mock.calls.length).toBe(1);
129+
expect(subscribeFn3.mock.calls.length).toBe(2);
110130
});
111131

112132
test('subscribe in non-constructor', () => {

0 commit comments

Comments
 (0)