This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpiled.js
275 lines (220 loc) · 7.87 KB
/
transpiled.js
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
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
/* global m */
const hookup = (closure, addHooks) => () =>
/* internal vnode, unused */
{
let setup = false;
const states = [];
let statesIndex = 0;
const depsStates = [];
let depsIndex = 0;
const updates = [];
const teardowns = new Map(); // Keep track of teardowns even when the update was run only once
const scheduleRender = m.redraw;
const resetAfterUpdate = () => {
updates.length = 0;
depsIndex = 0;
statesIndex = 0;
};
const updateDeps = deps => {
const index = depsIndex++;
const prevDeps = depsStates[index] || [];
const shouldRecompute = deps === undefined ? true // Always compute
: Array.isArray(deps) ? deps.length > 0 ? !deps.every((x, i) => x === prevDeps[i]) // Only compute when one of the deps has changed
: !setup // Empty array: only compute at mount
: false; // Invalid value, do nothing
depsStates[index] = deps;
return shouldRecompute;
};
const effect = function effect() {
let isAsync = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
return (fn, deps) => {
const shouldRecompute = updateDeps(deps);
if (shouldRecompute) {
const runCallbackFn = () => {
const teardown = fn(); // A callback may return a function. If any, add it to the teardowns:
if (typeof teardown === "function") {
// Store this this function to be called at unmount
teardowns.set(fn, teardown); // At unmount, call re-render at least once
teardowns.set("_", scheduleRender);
}
};
updates.push(isAsync ? () => new Promise(resolve => requestAnimationFrame(resolve)).then(runCallbackFn) : runCallbackFn);
}
};
};
const updateState = function updateState(initialValue) {
let newValueFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : value => value;
const index = statesIndex++;
if (!setup) {
states[index] = initialValue;
}
return [states[index], value => {
const previousValue = states[index];
const newValue = newValueFn(value, index);
states[index] = newValue;
if (newValue !== previousValue) {
scheduleRender(); // Calling redraw multiple times: Mithril will drop extraneous redraw calls, so performance should not be an issue
}
}];
}; // Hook functions
const useState = initialValue => {
const newValueFn = (value, index) => typeof value === "function" ? value(states[index]) : value;
return updateState(initialValue, newValueFn);
};
const useReducer = (reducer, initialArg, initFn) => {
// From the React docs: You can also create the initial state lazily. To do this, you can pass an init function as the third argument. The initial state will be set to init(initialArg).
const initialState = !setup && initFn ? initFn(initialArg) : initialArg;
const _updateState = updateState(initialState),
_updateState2 = _slicedToArray(_updateState, 2),
state = _updateState2[0],
setState = _updateState2[1];
const dispatch = action => setState( // Next state:
reducer(state, action));
return [state, dispatch];
};
const useRef = initialValue => {
// A ref is a persisted object that will not be updated, so it has no setter
const _updateState3 = updateState({
current: initialValue
}),
_updateState4 = _slicedToArray(_updateState3, 1),
value = _updateState4[0];
return value;
};
const useMemo = (fn, deps) => {
const shouldRecompute = updateDeps(deps);
const _ref = !setup ? updateState(fn()) : updateState(),
_ref2 = _slicedToArray(_ref, 2),
memoized = _ref2[0],
setMemoized = _ref2[1];
if (setup && shouldRecompute) {
setMemoized(fn());
}
return memoized;
};
const useCallback = (fn, deps) => useMemo(() => fn, deps);
const defaultHooks = {
useState,
useEffect: effect(true),
useLayoutEffect: effect(),
useReducer,
useRef,
useMemo,
useCallback
};
const hooks = _objectSpread({}, defaultHooks, addHooks && addHooks(defaultHooks));
const update = () => {
updates.forEach(call);
resetAfterUpdate();
};
const teardown = () => {
[...teardowns.values()].forEach(call);
};
return {
view: vnode => closure(vnode, hooks),
oncreate: () => (update(), setup = true),
onupdate: update,
onremove: teardown
};
};
const call = Function.prototype.call.bind(Function.prototype.call);
const hookupComponent = component => hookup((vnode, hooks) => component(_objectSpread({}, vnode.attrs, hooks)));
const withHooks = function withHooks(component, customHooks) {
let rest = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const customHooksFn = customHooks !== undefined && customHooks !== null ? customHooks : () => {};
return hookupComponent(hooks => component(_objectSpread({}, hooks, customHooksFn(hooks), rest)));
};
/* global m */
const customHooks = ({ useState }) => ({
useCount: (initialValue = 0) => {
const [count, setCount] = useState(initialValue);
return [
count, // value
() => setCount(count + 1), // increment
() => setCount(count - 1) // decrement
];
}
});
const Counter = ({ initialCount, useCount, extra }) => {
const [count, setCount] = useCount(initialCount);
return m("div[data-test-id=counter]", [
m("div", m("span[data-test-id=extra]", extra)),
m("div", m("span[data-test-id=count]", count)),
m("button[data-test-id=add-count]", {
onclick: () => setCount(count + 1)
}, "More"),
]);
};
const HookedCounter = withHooks(Counter, customHooks, { initialCount: 99, extra: "extra" });
const App = {
view: () =>
m(HookedCounter)
};
m.mount(
document.querySelector("#root"),
App
);
}));