|
| 1 | +import { getMagicSelf } from './magicSelf'; |
| 2 | + |
| 3 | +export const devToolConfig = { |
| 4 | + active: false, |
| 5 | + stateKey: '__UNIVERSAL_HOOKS__', |
| 6 | + show: 'object' // object, array, map |
| 7 | +}; |
| 8 | + |
| 9 | +export function supportReactDevTools({ active, stateKey, show }) { |
| 10 | + if (stateKey) devToolConfig.stateKey = stateKey; |
| 11 | + if (show) devToolConfig.show = show; |
| 12 | + devToolConfig.active = !!active; |
| 13 | +} |
| 14 | + |
| 15 | +export function setDevToolsHookState(name, state) { |
| 16 | + if (devToolConfig.active) { |
| 17 | + const self = getMagicSelf(); |
| 18 | + const { stateKey, show } = devToolConfig; |
| 19 | + if (!self.state) self.state = {}; |
| 20 | + if (!self.state[stateKey]) self.state[stateKey] = show === 'map' ? new Map() : show === 'array' ? [] : {}; |
| 21 | + |
| 22 | + if (show === 'map') { |
| 23 | + self.state[stateKey].set(name, state); |
| 24 | + } else if (show === 'array') { |
| 25 | + const hookState = self.state[stateKey].find(h => h.hasOwnProperty(name)); |
| 26 | + if (hookState) { |
| 27 | + hookState[name] = state; |
| 28 | + } else { |
| 29 | + self.state[stateKey].push({ [name]: state }); |
| 30 | + } |
| 31 | + } else { |
| 32 | + const hookNames = Object.keys(self.state[stateKey]); |
| 33 | + const hookName = hookNames.find(s => s.split(':')[1] === name); |
| 34 | + self.state[stateKey][hookName || `${hookNames.length.toString().padStart(2, '0')}:${name}`] = state; |
| 35 | + } |
| 36 | + } |
| 37 | +} |
0 commit comments