Skip to content

Commit c6739a9

Browse files
committed
feat(lane): add subscribe, unsubscribe apis
1 parent 23449b8 commit c6739a9

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

__test__/lane/lane.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,44 @@ describe('lane model', () => {
6262
})
6363
})
6464

65+
test('subscribe model', () => {
66+
let subscribeTimes = 0
67+
const { subscribe, unsubscribe, getState } = createStore(() => {
68+
const [count, setCount] = useModel(1)
69+
return { count, setCount }
70+
})
71+
72+
const callback = () => {
73+
subscribeTimes += 1
74+
}
75+
76+
subscribe(callback)
77+
78+
act(() => {
79+
expect(subscribeTimes).toEqual(0)
80+
})
81+
82+
act(() => {
83+
getState().setCount(5)
84+
})
85+
86+
act(() => {
87+
expect(subscribeTimes).toEqual(1)
88+
expect(getState().count).toBe(5)
89+
})
90+
91+
unsubscribe(callback)
92+
93+
act(() => {
94+
getState().setCount(15)
95+
})
96+
97+
act(() => {
98+
expect(subscribeTimes).toEqual(1)
99+
expect(getState().count).toBe(15)
100+
})
101+
})
102+
65103
test('pass function to useModel ', async () => {
66104
const { useStore } = createStore(() => {
67105
const [count, setCount] = useModel(() => 1)

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ interface API<MT extends ModelType = ModelType<any, any, {}>> {
156156
interface LaneAPI<S> {
157157
useStore: () => S
158158
getState: () => S
159+
subscribe: (callback: () => void) => void
160+
unsubscribe: (callback: () => void) => void
159161
}
160162

161163
interface APIs<M extends Models> {

src/index.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,21 @@ function createStore<S>(n: any, u?: any): LaneAPI<S> {
113113
return {
114114
// TODO: support selector
115115
useStore: () => useStore(storeId, selector),
116-
getState: () => selector()
116+
getState: () => selector(),
117+
subscribe: (callback: () => void) => {
118+
if (!Global.subscriptions[storeId]) {
119+
Global.subscriptions[storeId] = []
120+
}
121+
Global.subscriptions[storeId].push(callback)
122+
},
123+
unsubscribe: (callback?: () => void) => {
124+
if (Global.subscriptions[storeId]) {
125+
if (callback) {
126+
const idx = Global.subscriptions[storeId].indexOf(callback)
127+
if (idx >= 0) Global.subscriptions[storeId].splice(idx)
128+
}
129+
}
130+
}
117131
}
118132
}
119133

src/middlewares.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ const stateUpdater: Middleware = async (context, restMiddlewares) => {
114114

115115
const subscription: Middleware = async (context, restMiddlewares) => {
116116
const { modelName, actionName, next, Global } = context
117-
const subscriptions = Global.subscriptions[`${modelName}_${actionName}`]
117+
const subscriptions =
118+
context.type === 'u'
119+
? Global.subscriptions[modelName]
120+
: Global.subscriptions[`${modelName}_${actionName}`]
118121
if (subscriptions) {
119122
subscriptions.forEach((callback) => {
120123
callback(context)

0 commit comments

Comments
 (0)