Skip to content

Commit 609dad1

Browse files
committed
feat(subscribe): pass context to callback
1 parent 5848859 commit 609dad1

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ npm install react-model
9696
- [How can I add custom middleware](#how-can-i-add-custom-middleware)
9797
- [How can I make persist models](#how-can-i-make-persist-models)
9898
- [How can I deal with local state](#how-can-i-deal-with-local-state)
99-
- [How can I deal with huge dataset / circular dataset](#how-can-i-deal-with-huge-dataset-/-circular-dataset)
99+
- [How can I deal with huge dataset / circular dataset](#how-can-i-deal-with-huge-dataset--circular-dataset)
100100
- [actions throw error from immer.module.js](#actions-throw-error-from-immermodulejs)
101101
- [How can I customize each model's middlewares?](#how-can-i-customize-each-models-middlewares)
102102

__test__/middlewares/subscribe.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ describe('Subscribe middleware', () => {
77
test('run callback when specific action run', async () => {
88
let actions: any
99
let count = 0
10+
let incrementCount = 0
1011
const Counter = Model(NextCounter)
1112
const { useStore, subscribe } = Model({ Counter })
1213
subscribe('Counter', ['increment'], () => (count += 1))
1314
subscribe('Counter', 'add', () => (count += 10))
1415
subscribe('Counter', ['increment', 'add'], () => (count += 5))
16+
subscribe('Counter', 'increment', ({ params }) => {
17+
incrementCount += params || 1
18+
})
1519
renderHook(() => {
1620
;[, actions] = useStore('Counter')
1721
})
@@ -22,5 +26,10 @@ describe('Subscribe middleware', () => {
2226
expect(count).toBe(33)
2327
await actions.addCaller()
2428
expect(count).toBe(48)
29+
expect(incrementCount).toBe(3)
30+
await actions.increment(10)
31+
expect(incrementCount).toBe(13)
32+
await actions.increment(-3)
33+
expect(incrementCount).toBe(10)
2534
})
2635
})

src/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ interface ModelContext {
7070
modelName: string
7171
}
7272

73-
interface BaseContext<S = {}> {
73+
interface BaseContext<S = {}, P = any> {
7474
action: Action
7575
consumerActions: (
7676
actions: Actions,
7777
modelContext: ModelContext
7878
) => getConsumerActionsType<Actions>
79-
params: Object
79+
params: P
8080
middlewareConfig?: Object
8181
actionName: string
8282
modelName: string
@@ -135,7 +135,7 @@ interface API<MT extends ModelType = ModelType<any, any, {}>> {
135135
getState: () => Readonly<Get<MT, 'state'>>
136136
subscribe: (
137137
actionName: keyof MT['actions'] | Array<keyof MT['actions']>,
138-
callback: () => void
138+
callback: (context: BaseContext) => void
139139
) => void
140140
unsubscribe: (
141141
actionName: keyof Get<MT, 'actions'> | Array<keyof Get<MT, 'actions'>>
@@ -194,7 +194,7 @@ interface APIs<M extends Models> {
194194
subscribe: <K extends keyof M>(
195195
modelName: K,
196196
actionName: keyof Get<M[K], 'actions'> | Array<keyof Get<M[K], 'actions'>>,
197-
callback: () => void
197+
callback: (context: BaseContext) => void
198198
) => void
199199
unsubscribe: <K extends keyof M>(
200200
modelName: K,

src/middlewares.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const subscription: Middleware = async (context, restMiddlewares) => {
9696
const subscriptions = Global.subscriptions[`${modelName}_${actionName}`]
9797
if (subscriptions) {
9898
subscriptions.forEach((callback) => {
99-
callback()
99+
callback(context)
100100
})
101101
}
102102
await next(restMiddlewares)

0 commit comments

Comments
 (0)