forked from revery-ui/reason-reactify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactify.re
610 lines (547 loc) · 17.5 KB
/
Reactify.re
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
open Reactify_Types;
module Make = (ReconcilerImpl: Reconciler) => {
/* Module to give us unique IDs for components */
type renderedElement =
| RenderedPrimitive(ReconcilerImpl.node)
and elementWithChildren = (list(element), Effects.effects, Context.t)
and render = unit => elementWithChildren
and element =
| Primitive(ReconcilerImpl.primitives, render)
| Component(ComponentId.t, render)
| Provider(render)
| Empty(render)
/*
An instance is a component that has been rendered.
We store some additional context for it, like the state,
effects that need to be run, and corresponding nodes.
*/
and instance = {
mutable element,
children: list(element),
node: option(ReconcilerImpl.node),
rootNode: ReconcilerImpl.node,
mutable childInstances,
mutable effectInstances: Effects.effectInstances,
state: State.HeterogenousMutableList.t,
context: Context.HeterogenousHashtbl.t,
container: t,
}
and container = {
onBeginReconcile: Event.t(ReconcilerImpl.node),
onEndReconcile: Event.t(ReconcilerImpl.node),
rootInstance: ref(option(instance)),
containerNode: ReconcilerImpl.node,
}
and t = container
and childInstances = list(instance)
and hook('t) =
| Hook(element, 't)
and state('s) =
| State('s)
and reducer('r) =
| Reducer('r)
and effect =
| Effect
and context('t) =
| Context('t)
and emptyHook = hook(unit);
type node = ReconcilerImpl.node;
type primitives = ReconcilerImpl.primitives;
/*
Internal helpers to aid the hooks type representation,
the wrapping variants could be optimized away in the future as they're not used at runtime
*/
let addHook = (newHook, Hook(element, h)) => Hook(element, (h, newHook));
let addState = (~state as s) => addHook(State(s));
let addEffect = element => addHook(Effect, element);
let addReducer = (~reducer as r) => addHook(Reducer(r));
let addContext = (~value as v) => addHook(Context(v));
let elementToHook = x => Hook(x, ());
let elementFromHook = (Hook(x, _)) => x;
/*
A global, non-pure container to hold effects
during the course of a render operation.
*/
let __globalEffects = Effects.create();
let _uniqueIdScope = ComponentId.createScope();
/*
A global, non-pure container to hold current
context during hte course of a render operation.
*/
let noContext = Context.create();
let __globalContext = ref(noContext);
/*
State management for reconciliation
*/
module ComponentStateContext = {
type t = instance;
};
module ComponentState = State.Make(ComponentStateContext);
let noState = ComponentState.create([]);
let __globalState = ref(noState);
/*
Container API
*/
type reconcileNotification = node => unit;
let createContainer =
(
~onBeginReconcile: option(reconcileNotification)=?,
~onEndReconcile: option(reconcileNotification)=?,
rootNode: ReconcilerImpl.node,
) => {
let be = Event.create();
let ee = Event.create();
switch (onBeginReconcile) {
| Some(x) =>
let _ = Event.subscribe(be, x);
();
| _ => ()
};
switch (onEndReconcile) {
| Some(x) =>
let _ = Event.subscribe(ee, x);
();
| _ => ()
};
let ret: container = {
onBeginReconcile: be,
onEndReconcile: ee,
containerNode: rootNode,
rootInstance: ref(None),
};
ret;
};
let empty: emptyHook =
elementToHook(Empty(() => ([], [], __globalContext^)));
let render = (id: ComponentId.t, lazyElement, ~children) => {
ignore(children);
let ret: emptyHook =
elementToHook(
Component(
id,
() => {
Effects.resetEffects(__globalEffects);
let childElement = lazyElement() |> elementFromHook;
let children = [childElement];
let effects = Effects.getEffects(__globalEffects);
let renderResult: elementWithChildren = (
children,
effects,
__globalContext^,
);
renderResult;
},
),
);
ret;
};
module type Component = {
type hooks;
type createElement;
let createElement: createElement;
};
let createComponent =
(
type c,
type h,
create:
((unit => hook(h), ~children: list(emptyHook)) => emptyHook) => c,
)
: (module Component with type createElement = c and type hooks = h) => {
let id = ComponentId.newId(_uniqueIdScope);
let boundFunc = create(render(id));
(module
{
type hooks = h;
type createElement = c;
let createElement = boundFunc;
});
};
let component = createComponent;
let primitiveComponent = (~children, prim) => {
let comp: emptyHook =
elementToHook(
Primitive(
prim,
() => (List.map(elementFromHook, children), [], __globalContext^),
),
);
comp;
};
/* Context */
let __contextId = ref(0);
type providerConstructor('t) =
(~children: list(emptyHook), ~value: 't, unit) => emptyHook;
type contextValue('t) = {
initialValue: 't,
id: int,
};
let createContext = (initialValue: 't) => {
let contextId = __contextId^;
__contextId := __contextId^ + 1;
let ret: contextValue('t) = {initialValue, id: contextId};
ret;
};
let getProvider = ctx => {
let provider = (~children, ~value, ()) => {
let ret: emptyHook =
elementToHook(
Provider(
() => {
let contextId = ctx.id;
let context = Context.clone(__globalContext^);
Context.set(context, contextId, Object.to_object(value));
(List.map(elementFromHook, children), [], context);
},
),
);
ret;
};
provider;
};
let useContext = (ctx: contextValue('t)) => {
let value =
switch (Context.get(__globalContext^, ctx.id)) {
| Some(x) => Object.of_object(x)
| None => ctx.initialValue
};
value;
};
let useContextExperimental = (ctx: contextValue('t), continuation) => {
let value = useContext(ctx);
continuation(value) |> addContext(~value);
};
let useEffect =
(
~condition: Effects.effectCondition=Effects.Always,
e: Effects.effectFunction,
) =>
Effects.addEffect(~condition, __globalEffects, e);
let useEffectExperimental =
(
~condition: Effects.effectCondition=Effects.Always,
e: Effects.effectFunction,
continuation,
) => {
useEffect(~condition, e);
continuation() |> addEffect;
};
let _getEffectsFromInstance = (instance: option(instance)) =>
switch (instance) {
| None => []
| Some(i) => i.effectInstances
};
let _getPreviousChildInstances = (instance: option(instance)) =>
switch (instance) {
| None => []
| Some(i) => i.childInstances
};
let _getCurrentStateFromInstance = (instance: option(instance)) =>
switch (instance) {
| None => []
| Some(i) => i.state
};
let rec getFirstNode = (node: instance) =>
switch (node.node) {
| Some(n) => Some(n)
| None =>
switch (node.childInstances) {
| [] => None
| [c] => getFirstNode(c)
| _ => None
}
};
let isInstanceOfComponent = (instance: option(instance), element: element) =>
switch (instance) {
| None => false
| Some(x) =>
switch (x.element, element) {
| (Primitive(a, _), Primitive(b, _)) =>
Utility.areConstructorsEqual(a, b)
| (Component(a, _), Component(b, _)) => a === b
| _ => x.element === element
}
};
/*
* Instantiate turns a component function into a live instance,
* and asks the reconciler to append it to the root node.
*/
let rec instantiate =
(
rootNode,
previousInstance: option(instance),
element: element,
context: Context.t,
container: t,
) => {
let previousState = ref([]);
/* Recycle any previous effect instances */
let previousEffectInstances = _getEffectsFromInstance(previousInstance);
let isSameInstanceAsBefore =
isInstanceOfComponent(previousInstance, element);
if (isSameInstanceAsBefore) {
/* Set up state for the component */
previousState := _getCurrentStateFromInstance(previousInstance);
};
let state = ComponentState.create(previousState^);
/* We hold onto a reference to the component instance - we need to set this _after_ the component is instantiated */
let stateContext = ComponentState.getCurrentContext(state);
/*
This is dirty, but we set the 'global' state so that the 'useState'
can have access to it, without additional binding or passing. This is
necessary to preserve the semantics of the React-style API
*/
__globalState := state;
__globalContext := context;
let (children, effects, newContext) =
switch (element) {
| Primitive(_, render)
| Component(_, render)
| Provider(render)
| Empty(render) => render()
};
/* Once rendering is complete, we don't need this anymore */
__globalContext := noContext;
__globalState := noState;
let newState = ComponentState.getNewState(state);
let newEffectCount = List.length(effects);
let newEffectInstances =
isSameInstanceAsBefore ?
Effects.runEffects(
~previousInstances=previousEffectInstances,
effects,
) :
{
Effects.drainEffects(previousEffectInstances);
let emptyInstances =
Effects.createEmptyEffectInstances(newEffectCount);
Effects.runEffects(~previousInstances=emptyInstances, effects);
};
let primitiveInstance =
switch (element) {
| Primitive(p, _render) => Some(ReconcilerImpl.createInstance(p))
| _ => None
};
let nextRootPrimitiveInstance =
switch (primitiveInstance) {
| Some(i) => i
| None => rootNode
};
let previousChildInstances = _getPreviousChildInstances(previousInstance);
let childInstances =
reconcileChildren(
nextRootPrimitiveInstance,
previousChildInstances,
children,
newContext,
container,
);
let instance: instance = {
element,
node: primitiveInstance,
rootNode: nextRootPrimitiveInstance,
children,
childInstances,
effectInstances: newEffectInstances,
state: newState,
context: newContext,
container,
};
/*
'context' is the instance that state needs when 'setState' is called
We set it here, after the instance is fully realized, so that the 'setState'
callback has the latest state for the component instance.
*/
stateContext := Some(instance);
instance;
}
and reconcile = (rootNode, instance, component, context, container) => {
let newInstance =
instantiate(rootNode, instance, component, context, container);
let r =
switch (instance) {
| None =>
switch (newInstance.node) {
| Some(n) => ReconcilerImpl.appendChild(rootNode, n)
| None => ()
};
newInstance;
| Some(i) =>
let ret =
switch (newInstance.node, i.node) {
| (Some(a), Some(b)) =>
/* Only both replacing node if the primitives are different */
switch (newInstance.element, i.element) {
| (Primitive(newPrim, _), Primitive(oldPrim, _)) =>
if (oldPrim !== newPrim) {
/* Check if the primitive type is the same - if it is, we can simply update the node */
/* If not, we'll replace the node */
if (Utility.areConstructorsEqual(oldPrim, newPrim)) {
ReconcilerImpl.updateInstance(b, oldPrim, newPrim);
i.element = newInstance.element;
i.effectInstances = newInstance.effectInstances;
i.childInstances =
reconcileChildren(
b,
i.childInstances,
newInstance.children,
context,
container,
);
i;
} else {
ReconcilerImpl.replaceChild(rootNode, a, b);
newInstance;
};
} else {
/* The node itself is unchanged, so we'll just reconcile the children */
i.effectInstances = newInstance.effectInstances;
i.childInstances =
reconcileChildren(
b,
i.childInstances,
newInstance.children,
context,
container,
);
i;
}
| _ =>
print_endline(
"ERROR: Should only be nodes if there are primitives!",
);
newInstance;
}
| (Some(a), None) =>
/* If there was a non-primitive instance, we need to get the top-level node - */
/* and then remove it */
let currentNode = getFirstNode(i);
switch (currentNode) {
| Some(c) => ReconcilerImpl.removeChild(rootNode, c)
| _ => ()
};
ReconcilerImpl.appendChild(rootNode, a);
newInstance;
| (None, Some(b)) =>
ReconcilerImpl.removeChild(rootNode, b);
newInstance;
| (None, None) => newInstance
};
ret;
};
r;
}
and reconcileChildren =
(
root: node,
currentChildInstances: childInstances,
newChildren: list(element),
context: Context.t,
container: t,
) => {
let currentChildInstances: array(instance) =
Array.of_list(currentChildInstances);
let newChildren = Array.of_list(newChildren);
let newChildInstances: ref(childInstances) = ref([]);
for (i in 0 to Array.length(newChildren) - 1) {
let childInstance =
i >= Array.length(currentChildInstances) ?
None : Some(currentChildInstances[i]);
let childComponent = newChildren[i];
let newChildInstance =
reconcile(root, childInstance, childComponent, context, container);
newChildInstances :=
List.append(newChildInstances^, [newChildInstance]);
};
/* Clean up existing children */
for (i in
Array.length(newChildren) to
Array.length(currentChildInstances) - 1) {
switch (currentChildInstances[i].node) {
| Some(n) => ReconcilerImpl.removeChild(root, n)
| _ => ()
};
};
newChildInstances^;
};
let useReducer =
(reducer: ('state, 'action) => 'state, initialState: 'state) => {
let globalState = __globalState^;
let componentState =
ComponentState.popOldState(globalState, initialState);
let (getState, updateState) =
ComponentState.pushNewState(globalState, componentState);
let currentContext = ComponentState.getCurrentContext(globalState);
let dispatch = (context: ref(option(instance)), action: 'action) => {
let newVal = reducer(getState(), action);
updateState(newVal);
switch (context^) {
| Some(i) =>
let {rootNode, element, _} = i;
Event.dispatch(i.container.onBeginReconcile, rootNode);
let _ =
reconcile(rootNode, Some(i), element, i.context, i.container);
Event.dispatch(i.container.onEndReconcile, rootNode);
();
| _ => print_endline("WARNING: Skipping reconcile!")
};
};
(componentState, dispatch(currentContext));
};
let _useReducerExperimental =
(
reducer: ('state, 'action) => 'state,
initialState: 'state,
continuation,
) =>
continuation(useReducer(reducer, initialState));
/*
There's an internal and a public version of `useReducer`. The internal version,
which has no "hooks types propagation", allows to keep the types in `useState`
(which reuses `useReducer`) simpler
*/
let useReducerExperimental = (reducer, initialState, continuation) =>
_useReducerExperimental(reducer, initialState, continuation)
|> addReducer(~reducer);
type useStateAction('a) =
| SetState('a);
let useStateReducer = (_state, action) =>
switch (action) {
| SetState(newState) => newState
};
let useState = initialState => {
let (componentState, dispatch) =
useReducer(useStateReducer, initialState);
let setState = newState => dispatch(SetState(newState));
(componentState, setState);
};
let useStateExperimental = (initialState, continuation) =>
_useReducerExperimental(
useStateReducer,
initialState,
((componentState, dispatch)) => {
let setState = newState => dispatch(SetState(newState));
continuation((componentState, setState))
|> addState(~state=initialState);
},
);
let updateContainer = (container, hook) => {
let {containerNode, rootInstance, onBeginReconcile, onEndReconcile} = container;
let prevInstance = rootInstance^;
Event.dispatch(onBeginReconcile, containerNode);
let nextInstance =
reconcile(
containerNode,
prevInstance,
elementFromHook(hook),
noContext,
container,
);
rootInstance := Some(nextInstance);
Event.dispatch(onEndReconcile, containerNode);
};
};
module State = State;
module Event = Event;
module Utility = Utility;
module Object = Object;