|
1 | 1 | /// <reference path="./index.d.ts" /> |
2 | 2 | import * as React from 'react' |
3 | | -import { PureComponent, useEffect, useState } from 'react' |
| 3 | +import { PureComponent, useEffect, useState, useRef } from 'react' |
4 | 4 | import Global from './global' |
5 | 5 | import { |
6 | 6 | Consumer, |
@@ -51,12 +51,12 @@ function Model<M extends Models, MT extends ModelType, E>( |
51 | 51 | subscribe: ( |
52 | 52 | actionName: keyof MT['actions'] | Array<keyof MT['actions']>, |
53 | 53 | callback: () => void |
54 | | - ) => subscribe(hash, actionName as (string | string[]), callback), |
| 54 | + ) => subscribe(hash, actionName as string | string[], callback), |
55 | 55 | unsubscribe: ( |
56 | 56 | actionName: keyof MT['actions'] | Array<keyof MT['actions']> |
57 | | - ) => unsubscribe(hash, actionName as (string | string[])), |
| 57 | + ) => unsubscribe(hash, actionName as string | string[]), |
58 | 58 | useStore: (depActions?: Array<keyof MT['actions']>) => |
59 | | - useStore(hash, depActions as (string[] | undefined)) |
| 59 | + useStore(hash, depActions as string[] | undefined) |
60 | 60 | } |
61 | 61 | } else { |
62 | 62 | if (models.actions) { |
@@ -151,7 +151,7 @@ const subscribe = ( |
151 | 151 | callback?: () => void |
152 | 152 | ) => { |
153 | 153 | if (Array.isArray(actions)) { |
154 | | - actions.forEach(actionName => { |
| 154 | + actions.forEach((actionName) => { |
155 | 155 | if (!Global.subscriptions[`${modelName}_${actionName}`]) { |
156 | 156 | Global.subscriptions[`${modelName}_${actionName}`] = [] |
157 | 157 | } |
@@ -207,21 +207,29 @@ const getActions = ( |
207 | 207 | } |
208 | 208 |
|
209 | 209 | const useStore = (modelName: string, depActions?: string[]) => { |
210 | | - const setState = useState(Global.State[modelName])[1] |
| 210 | + const setState = useState({})[1] |
| 211 | + const hash = useRef<string>('') |
211 | 212 |
|
212 | 213 | useEffect(() => { |
213 | 214 | Global.uid += 1 |
214 | | - const hash = '' + Global.uid |
| 215 | + const local_hash = '' + Global.uid |
| 216 | + hash.current = local_hash |
215 | 217 | if (!Global.Setter.functionSetter[modelName]) { |
216 | 218 | Global.Setter.functionSetter[modelName] = {} |
217 | 219 | } |
218 | | - Global.Setter.functionSetter[modelName][hash] = { setState, depActions } |
| 220 | + Global.Setter.functionSetter[modelName][local_hash] = { |
| 221 | + setState, |
| 222 | + depActions |
| 223 | + } |
219 | 224 | return function cleanup() { |
220 | | - delete Global.Setter.functionSetter[modelName][hash] |
| 225 | + delete Global.Setter.functionSetter[modelName][local_hash] |
221 | 226 | } |
222 | 227 | }, []) |
223 | 228 |
|
224 | | - const updaters = getActions(modelName, { setState, type: 'function' }) |
| 229 | + const updaters = getActions(modelName, { |
| 230 | + __hash: hash.current, |
| 231 | + type: 'function' |
| 232 | + }) |
225 | 233 | return [getState(modelName), updaters] |
226 | 234 | } |
227 | 235 |
|
@@ -249,7 +257,7 @@ const connect = ( |
249 | 257 | const { state: prevState = {}, actions: prevActions = {} } = this.props |
250 | 258 | return ( |
251 | 259 | <Consumer> |
252 | | - {models => { |
| 260 | + {(models) => { |
253 | 261 | const { [`${modelName}`]: state } = models as any |
254 | 262 | const actions = Global.Actions[modelName] |
255 | 263 | return ( |
|
0 commit comments