Skip to content

Commit 60d092d

Browse files
committed
add support for React dev Tools!
1 parent 445adda commit 60d092d

File tree

7 files changed

+82
-7
lines changed

7 files changed

+82
-7
lines changed

src/core/devTools.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

src/core/useClassContext.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
*/
44

55
import invariant from 'tiny-invariant';
6-
import { getMagicDispatcher, getMagicSelf } from './magicSelf';
6+
import { checkSymbol, getMagicDispatcher, getMagicSelf } from './magicSelf';
7+
import { setDevToolsHookState } from './devTools';
8+
import { createHook } from './createHook';
79

8-
export const useClassContext = (context, observedBits) => {
10+
export function useClassContextKey(keySymbol, context, observedBits) {
11+
checkSymbol('useClassContext', keySymbol);
912
getMagicSelf(); // invariant hook outside render method
1013
invariant(context && context.Provider && context.Consumer, 'Context should be React.createContext object!');
11-
return getMagicDispatcher().readContext(context, observedBits);
12-
};
14+
15+
const contextValue = getMagicDispatcher().readContext(context, observedBits);
16+
17+
setDevToolsHookState(keySymbol.description, contextValue);
18+
19+
return contextValue;
20+
}
21+
22+
export const useClassContext = createHook('Contexts', useClassContextKey);

src/core/useClassDebugValue.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://github.com/salvoravida/react-class-hooks
3+
*/
4+
5+
import { createHook } from './createHook';
6+
import { checkSymbol } from './magicSelf';
7+
import { setDevToolsHookState } from './devTools';
8+
9+
export function useClassDebugValueKey(keySymbol, value, formatter) {
10+
checkSymbol('useDebugValueKey', keySymbol);
11+
const viewValue= typeof formatter==="function"? formatter(value) : value;
12+
setDevToolsHookState(keySymbol.description,viewValue)
13+
}
14+
15+
export const useClassDebugValue = createHook('DebugValue', useClassDebugValueKey);
16+

src/core/useClassMemo.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import invariant from 'tiny-invariant';
66
import { checkSymbol, getMagicSelf, MAGIC_MEMOS } from './magicSelf';
77
import { inputsArrayEqual } from './inputsEqual';
88
import { createHook, createNamedHook } from './createHook';
9+
import { setDevToolsHookState } from './devTools';
910

1011
export const useClassMemoKey = (keySymbol, creator, inputs) => {
1112
checkSymbol('useClassMemo', keySymbol);
@@ -43,7 +44,9 @@ export const useClassMemoKey = (keySymbol, creator, inputs) => {
4344
}
4445
}
4546

46-
return self[MAGIC_MEMOS][keySymbol].value;
47+
const returnValue = self[MAGIC_MEMOS][keySymbol].value;
48+
setDevToolsHookState(keySymbol.description, returnValue);
49+
return returnValue;
4750
};
4851

4952
export const useClassMemo = createHook('Memos', useClassMemoKey);

src/core/useClassRefKey.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { checkSymbol, getMagicSelf, MAGIC_REFS } from './magicSelf';
6+
import { setDevToolsHookState } from './devTools';
67

78
export function useClassRefKey(keySymbol, initialValue) {
89
checkSymbol('useClassRefKey', keySymbol);
@@ -19,5 +20,7 @@ export function useClassRefKey(keySymbol, initialValue) {
1920
self[MAGIC_REFS][keySymbol] = ref;
2021
}
2122

22-
return self[MAGIC_REFS][keySymbol];
23+
const returnValue = self[MAGIC_REFS][keySymbol];
24+
setDevToolsHookState(keySymbol.description, returnValue);
25+
return returnValue;
2326
}

src/core/useClassStateKey.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { getMagicSelf, checkSymbol, MAGIC_STATES } from './magicSelf';
6+
import { setDevToolsHookState } from './devTools';
67

78
export function useClassStateKey(keySymbol, initialValue) {
89
checkSymbol('useClassStateKey', keySymbol);
@@ -29,5 +30,6 @@ export function useClassStateKey(keySymbol, initialValue) {
2930
}
3031

3132
const { value, setValue } = self[MAGIC_STATES][keySymbol];
33+
setDevToolsHookState(keySymbol.description, value);
3234
return [value, setValue];
3335
}

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { useClassReducer } from './core/useClassReducer';
1010
import { useClassRef, refCallback } from './core/useClassRef';
1111
import { useClassContext } from './core/useClassContext';
1212
import { useClassImperativeHandle } from './core/useClassImperativeHandle';
13+
import { useClassDebugValue } from './core/useClassDebugValue';
14+
import { supportReactDevTools } from './core/devTools';
1315

1416
const useClassLayoutEffect = useClassEffect;
1517

@@ -23,5 +25,7 @@ export {
2325
useClassRef,
2426
refCallback,
2527
useClassContext,
26-
useClassImperativeHandle
28+
useClassImperativeHandle,
29+
useClassDebugValue,
30+
supportReactDevTools
2731
};

0 commit comments

Comments
 (0)