This repository was archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
324 lines (272 loc) · 7.73 KB
/
index.js
File metadata and controls
324 lines (272 loc) · 7.73 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
var augmentor = (function (exports) {
'use strict';
var compat = typeof cancelAnimationFrame === 'function';
var cAF = compat ? cancelAnimationFrame : clearTimeout;
var rAF = compat ? requestAnimationFrame : setTimeout;
function reraf(limit) {
var force, timer, callback, self, args;
reset();
return function reschedule(_callback, _self, _args) {
callback = _callback;
self = _self;
args = _args;
if (!timer) timer = rAF(invoke);
if (--force < 0) stop(true);
return stop;
};
function invoke() {
reset();
callback.apply(self, args || []);
}
function reset() {
force = limit || Infinity;
timer = compat ? 0 : null;
}
function stop(flush) {
var didStop = !!timer;
if (didStop) {
cAF(timer);
if (flush) invoke();
}
return didStop;
}
}
var umap = (function (_) {
return {
// About: get: _.get.bind(_)
// It looks like WebKit/Safari didn't optimize bind at all,
// so that using bind slows it down by 60%.
// Firefox and Chrome are just fine in both cases,
// so let's use the approach that works fast everywhere 👍
get: function get(key) {
return _.get(key);
},
set: function set(key, value) {
return _.set(key, value), value;
}
};
});
/*! (c) Andrea Giammarchi - ISC */
var state = null; // main exports
var augmentor = function augmentor(fn) {
var stack = [];
return function hook() {
var prev = state;
var after = [];
state = {
hook: hook,
args: arguments,
stack: stack,
i: 0,
length: stack.length,
after: after
};
try {
return fn.apply(null, arguments);
} finally {
state = prev;
for (var i = 0, length = after.length; i < length; i++) {
after[i]();
}
}
};
};
var contextual = function contextual(fn) {
var check = true;
var context = null;
var augmented = augmentor(function () {
return fn.apply(context, arguments);
});
return function hook() {
var result = augmented.apply(context = this, arguments); // perform hasEffect check only once
if (check) {
check = !check; // and copy same Array if any FX was used
if (hasEffect(augmented)) effects.set(hook, effects.get(augmented));
}
return result;
};
}; // useReducer
var updates = umap(new WeakMap());
var hookdate = function hookdate(hook, ctx, args) {
hook.apply(ctx, args);
};
var defaults = {
async: false,
always: false
};
var getValue = function getValue(value, f) {
return typeof f == 'function' ? f(value) : f;
};
var useReducer = function useReducer(reducer, value, init, options) {
var i = state.i++;
var _state = state,
hook = _state.hook,
args = _state.args,
stack = _state.stack,
length = _state.length;
if (i === length) state.length = stack.push({});
var ref = stack[i];
ref.args = args;
if (i === length) {
var fn = typeof init === 'function';
var _ref = (fn ? options : init) || options || defaults,
asy = _ref.async,
always = _ref.always;
ref.$ = fn ? init(value) : getValue(void 0, value);
ref._ = asy ? updates.get(hook) || updates.set(hook, reraf()) : hookdate;
ref.f = function (value) {
var $value = reducer(ref.$, value);
if (always || ref.$ !== $value) {
ref.$ = $value;
ref._(hook, null, ref.args);
}
};
}
return [ref.$, ref.f];
}; // useState
var useState = function useState(value, options) {
return useReducer(getValue, value, void 0, options);
}; // useContext
var hooks = new WeakMap();
var invoke = function invoke(_ref2) {
var hook = _ref2.hook,
args = _ref2.args;
hook.apply(null, args);
};
var createContext = function createContext(value) {
var context = {
value: value,
provide: provide
};
hooks.set(context, []);
return context;
};
var useContext = function useContext(context) {
var _state2 = state,
hook = _state2.hook,
args = _state2.args;
var stack = hooks.get(context);
var info = {
hook: hook,
args: args
};
if (!stack.some(update, info)) stack.push(info);
return context.value;
};
function provide(value) {
if (this.value !== value) {
this.value = value;
hooks.get(this).forEach(invoke);
}
}
function update(_ref3) {
var hook = _ref3.hook;
return hook === this.hook;
} // dropEffect, hasEffect, useEffect, useLayoutEffect
var effects = new WeakMap();
var fx = umap(effects);
var stop = function stop() {};
var createEffect = function createEffect(asy) {
return function (effect, guards) {
var i = state.i++;
var _state3 = state,
hook = _state3.hook,
after = _state3.after,
stack = _state3.stack,
length = _state3.length;
if (i < length) {
var info = stack[i];
var _update = info.update,
values = info.values,
_stop = info.stop;
if (!guards || guards.some(different, values)) {
info.values = guards;
if (asy) _stop(asy);
var clean = info.clean;
if (clean) {
info.clean = null;
clean();
}
var _invoke = function _invoke() {
info.clean = effect();
};
if (asy) _update(_invoke);else after.push(_invoke);
}
} else {
var _update2 = asy ? reraf() : stop;
var _info = {
clean: null,
update: _update2,
values: guards,
stop: stop
};
state.length = stack.push(_info);
(fx.get(hook) || fx.set(hook, [])).push(_info);
var _invoke2 = function _invoke2() {
_info.clean = effect();
};
if (asy) _info.stop = _update2(_invoke2);else after.push(_invoke2);
}
};
};
var dropEffect = function dropEffect(hook) {
(effects.get(hook) || []).forEach(function (info) {
var clean = info.clean,
stop = info.stop;
stop();
if (clean) {
info.clean = null;
clean();
}
});
};
var hasEffect = effects.has.bind(effects);
var useEffect = createEffect(true);
var useLayoutEffect = createEffect(false); // useMemo, useCallback
var useMemo = function useMemo(memo, guards) {
var i = state.i++;
var _state4 = state,
stack = _state4.stack,
length = _state4.length;
if (i === length) state.length = stack.push({
$: memo(),
_: guards
});else if (!guards || guards.some(different, stack[i]._)) stack[i] = {
$: memo(),
_: guards
};
return stack[i].$;
};
var useCallback = function useCallback(fn, guards) {
return useMemo(function () {
return fn;
}, guards);
}; // useRef
var useRef = function useRef(value) {
var i = state.i++;
var _state5 = state,
stack = _state5.stack,
length = _state5.length;
if (i === length) state.length = stack.push({
current: value
});
return stack[i];
};
function different(value, i) {
return value !== this[i];
}
exports.augmentor = augmentor;
exports.contextual = contextual;
exports.createContext = createContext;
exports.dropEffect = dropEffect;
exports.hasEffect = hasEffect;
exports.useCallback = useCallback;
exports.useContext = useContext;
exports.useEffect = useEffect;
exports.useLayoutEffect = useLayoutEffect;
exports.useMemo = useMemo;
exports.useReducer = useReducer;
exports.useRef = useRef;
exports.useState = useState;
return exports;
}({}));