-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (154 loc) · 4.13 KB
/
index.js
File metadata and controls
180 lines (154 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
self.uhooks = (function (exports) {
'use strict';
let info = null, schedule = new Set;
const invoke = effect => {
const {$, r, h} = effect;
if (isFunction(r)) {
fx.get(h).delete(effect);
r();
}
if (isFunction(effect.r = $()))
fx.get(h).add(effect);
};
const runSchedule = () => {
const previous = schedule;
schedule = new Set;
previous.forEach(({h, c, a, e}) => {
// avoid running schedules when the hook is
// re-executed before such schedule happens
if (e)
h.apply(c, a);
});
};
const fx = new WeakMap;
const effects = [];
const layoutEffects = [];
function different(value, i) {
return value !== this[i];
}
const dropEffect = hook => {
const effects = fx.get(hook);
if (effects)
wait.then(() => {
effects.forEach(effect => {
effect.r();
effect.r = null;
effect.d = true;
});
effects.clear();
});
};
const getInfo = () => info;
const hasEffect = hook => fx.has(hook);
const isFunction = f => typeof f === 'function';
const hooked = callback => {
const current = {h: hook, c: null, a: null, e: 0, i: 0, s: []};
return hook;
function hook() {
const prev = info;
info = current;
current.e = current.i = 0;
try {
return callback.apply(current.c = this, current.a = arguments);
}
finally {
info = prev;
if (effects.length)
wait.then(effects.forEach.bind(effects.splice(0), invoke));
if (layoutEffects.length)
layoutEffects.splice(0).forEach(invoke);
}
}
};
const reschedule = info => {
if (!schedule.has(info)) {
info.e = 1;
schedule.add(info);
wait.then(runSchedule);
}
};
const wait = Promise.resolve();
const createContext = value => ({
_: new Set,
provide,
value
});
const useContext = ({_, value}) => {
_.add(getInfo());
return value;
};
function provide(newValue) {
const {_, value} = this;
if (value !== newValue) {
this._ = new Set;
this.value = newValue;
_.forEach(({h, c, a}) => {
h.apply(c, a);
});
}
}
const useCallback = (fn, guards) => useMemo(() => fn, guards);
const useMemo = (memo, guards) => {
const info = getInfo();
const {i, s} = info;
if (i === s.length || !guards || guards.some(different, s[i]._))
s[i] = {$: memo(), _: guards};
return s[info.i++].$;
};
const createEffect = stack => (callback, guards) => {
const info = getInfo();
const {i, s, h} = info;
const call = i === s.length;
info.i++;
if (call) {
if (!fx.has(h))
fx.set(h, new Set);
s[i] = {$: callback, _: guards, r: null, d: false, h};
}
if (call || !guards || s[i].d || guards.some(different, s[i]._))
stack.push(s[i]);
s[i].$ = callback;
s[i]._ = guards;
s[i].d = false;
};
const useEffect = createEffect(effects);
const useLayoutEffect = createEffect(layoutEffects);
const getValue = (value, f) => isFunction(f) ? f(value) : f;
const useReducer = (reducer, value, init) => {
const info = getInfo();
const {i, s} = info;
if (i === s.length)
s.push({
$: isFunction(init) ?
init(value) : getValue(void 0, value),
set: value => {
s[i].$ = reducer(s[i].$, value);
reschedule(info);
}
});
const {$, set} = s[info.i++];
return [$, set];
};
const useState = value => useReducer(getValue, value);
const useRef = current => {
const info = getInfo();
const {i, s} = info;
if (i === s.length)
s.push({current});
return s[info.i++];
};
exports.createContext = createContext;
exports.dropEffect = dropEffect;
exports.hasEffect = hasEffect;
exports.hooked = hooked;
exports.useCallback = useCallback;
exports.useContext = useContext;
exports.useEffect = useEffect;
exports.useLayoutEffect = useLayoutEffect;
exports.useMemo = useMemo;
exports.useReducer = useReducer;
exports.useRef = useRef;
exports.useState = useState;
exports.wait = wait;
return exports;
})({});