-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
dom.go
1303 lines (1179 loc) · 39.1 KB
/
dom.go
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vecty
import (
"reflect"
)
// batch renderer singleton
var batch = &batchRenderer{idx: make(map[Component]int)}
// Core implements the Context method of the Component interface, and is the
// core/central struct which all Component implementations should embed.
type Core struct {
prevRenderComponent Component
prevRender ComponentOrHTML
mounted, unmounted bool
}
// Context implements the Component interface.
func (c *Core) Context() *Core { return c }
// isMarkupOrChild implements MarkupOrChild
func (c *Core) isMarkupOrChild() {}
// isComponentOrHTML implements ComponentOrHTML
func (c *Core) isComponentOrHTML() {}
// Component represents a single visual component within an application. To
// define a new component simply implement the Render method and embed the Core
// struct:
//
// type MyComponent struct {
// vecty.Core
// ... additional component fields (state or properties) ...
// }
//
// func (c *MyComponent) Render() vecty.ComponentOrHTML {
// ... rendering ...
// }
//
type Component interface {
// Render is responsible for building HTML which represents the component.
//
// If Render returns nil, the component will render as nothing (in reality,
// a noscript tag, which has no display or action, and is compatible with
// Vecty's diffing algorithm).
Render() ComponentOrHTML
// Context returns the components context, which is used internally by
// Vecty in order to store the previous component render for diffing.
Context() *Core
isComponentOrHTML()
isMarkupOrChild()
}
// Copier is an optional interface that a Component can implement in order to
// copy itself. Vecty must internally copy components, and it does so by either
// invoking the Copy method of the Component or, if the component does not
// implement the Copier interface, a shallow copy is performed.
//
// TinyGo: If compiling your Vecty application using the experimental TinyGo
// support (https://github.com/hexops/vecty/pull/243) then all components must
// implement at least a shallow-copy Copier interface (this is not required
// otherwise):
//
// func (c *MyComponent) Copy() vecty.Component {
// cpy := *c
// return &cpy
// }
//
type Copier interface {
// Copy returns a copy of the component.
Copy() Component
}
// Mounter is an optional interface that a Component can implement in order
// to receive component mount events.
type Mounter interface {
// Mount is called after the component has been mounted, after the DOM node
// has been attached.
Mount()
}
// Unmounter is an optional interface that a Component can implement in order
// to receive component unmount events.
type Unmounter interface {
// Unmount is called before the component has been unmounted, before the
// DOM node has been removed.
Unmount()
}
// Keyer is an optional interface that a Component can implement in order to
// uniquely identify the component amongst its siblings. If implemented, all
// siblings, both components and HTML, must also be keyed.
//
// Implementing this interface allows siblings to be removed or re-ordered
// whilst retaining state, and improving render efficiency.
type Keyer interface {
// Key returns a value that uniquely identifies the component amongst its
// siblings. The returned type must be a valid map key, or rendering will
// panic.
Key() interface{}
}
// ComponentOrHTML represents one of:
//
// Component
// *HTML
// List
// KeyedList
// nil
//
// An unexported method on this interface ensures at compile time that the
// underlying value must be one of these types.
type ComponentOrHTML interface {
isComponentOrHTML()
isMarkupOrChild()
}
// RenderSkipper is an optional interface that Component's can implement in
// order to short-circuit the reconciliation of a Component's rendered body.
//
// This is purely an optimization, and does not need to be implemented by
// Components for correctness. Without implementing this interface, only the
// difference between renders will be applied to the browser DOM. This
// interface allows components to bypass calculating the difference altogether
// and quickly state "nothing has changed, do not re-render".
type RenderSkipper interface {
// SkipRender is called with a copy of the Component made the last time its
// Render method was invoked. If it returns true, rendering of the
// component will be skipped.
//
// The previous component may be of a different type than this
// RenderSkipper itself, thus a type assertion should be used and no action
// taken if the type does not match.
SkipRender(prev Component) bool
}
// HTML represents some form of HTML: an element with a specific tag, or some
// literal text (a TextNode).
type HTML struct {
node jsObject
namespace, tag, text, innerHTML string
classes map[string]struct{}
styles, dataset map[string]string
properties, attributes map[string]interface{}
eventListeners []*EventListener
children []ComponentOrHTML
key interface{}
// keyedChildren stores a map of keys to children, for keyed reconciliation.
keyedChildren map[interface{}]ComponentOrHTML
// insertBeforeNode tracks the DOM node that elements should be inserted
// before, across List boundaries.
insertBeforeNode jsObject
// lastRendered child tracks the last child that was rendered, across List
// boundaries.
lastRenderedChild *HTML
}
// Key implements the Keyer interface.
func (h *HTML) Key() interface{} {
return h.key
}
// isMarkupOrChild implements MarkupOrChild
func (h *HTML) isMarkupOrChild() {}
// isComponentOrHTML implements ComponentOrHTML
func (h *HTML) isComponentOrHTML() {}
// createNode creates a HTML node of the appropriate type and namespace.
func (h *HTML) createNode() {
switch {
case h.tag != "" && h.text != "":
panic("vecty: internal error (only one of HTML.tag or HTML.text may be set)")
case h.tag == "" && h.innerHTML != "":
panic("vecty: only HTML may have UnsafeHTML attribute")
case h.tag != "" && h.namespace == "":
h.node = global().Get("document").Call("createElement", h.tag)
case h.tag != "" && h.namespace != "":
h.node = global().Get("document").Call("createElementNS", h.namespace, h.tag)
default:
h.node = global().Get("document").Call("createTextNode", h.text)
}
}
// reconcileText replaces the content of a text node.
func (h *HTML) reconcileText(prev *HTML) {
h.node = prev.node
// Text modifications.
if h.text != prev.text {
h.node.Set("nodeValue", h.text)
}
}
func (h *HTML) reconcile(prev *HTML) []Mounter {
// Check for compatible tag and mutate previous instance on match, otherwise start fresh
switch {
case prev != nil && h.tag == "" && prev.tag == "":
// Compatible text node
h.reconcileText(prev)
return nil
case prev != nil && h.tag != "" && prev.tag != "" && h.tag == prev.tag && h.namespace == prev.namespace:
// Compatible element node
h.node = prev.node
default:
// Incompatible node, start fresh
if prev == nil {
prev = &HTML{}
}
h.createNode()
}
if !h.node.Equal(prev.node) {
// reconcile properties against empty prev for new nodes.
h.reconcileProperties(&HTML{})
} else {
h.reconcileProperties(prev)
}
return h.reconcileChildren(prev)
}
// reconcileProperties updates properties/attributes/etc to match the current
// element.
func (h *HTML) reconcileProperties(prev *HTML) {
// If nodes match, remove any outdated properties
if h.node.Equal(prev.node) {
h.removeProperties(prev)
}
h.tinyGoCannotIterateNilMaps()
// Wrap event listeners
for _, l := range h.eventListeners {
l := l
l.wrapper = funcOf(func(this jsObject, args []jsObject) interface{} {
jsEvent := args[0]
if l.callPreventDefault {
jsEvent.Call("preventDefault")
}
if l.callStopPropagation {
jsEvent.Call("stopPropagation")
}
l.Listener(&Event{
Value: jsEvent.(wrappedObject).j,
Target: jsEvent.Get("target").(wrappedObject).j,
})
return undefined()
})
}
// Properties
for name, value := range h.properties {
var oldValue interface{}
switch name {
case "value":
oldValue = h.node.Get("value").String()
case "checked":
oldValue = h.node.Get("checked").Bool()
default:
oldValue = prev.properties[name]
}
if value != oldValue {
h.node.Set(name, value)
}
}
// Attributes
for name, value := range h.attributes {
if value != prev.attributes[name] {
h.node.Call("setAttribute", name, value)
}
}
// Classes
classList := h.node.Get("classList")
for name := range h.classes {
if _, ok := prev.classes[name]; !ok {
classList.Call("add", name)
}
}
// Dataset
dataset := h.node.Get("dataset")
for name, value := range h.dataset {
if value != prev.dataset[name] {
dataset.Set(name, value)
}
}
// Styles
style := h.node.Get("style")
for name, value := range h.styles {
oldValue := prev.styles[name]
if value != oldValue {
style.Call("setProperty", name, value)
}
}
// Event listeners
for _, l := range h.eventListeners {
h.node.Call("addEventListener", l.Name, l.wrapper)
}
// InnerHTML
if h.innerHTML != prev.innerHTML {
h.node.Set("innerHTML", h.innerHTML)
}
}
// removeProperties removes properties/attributes/etc that are no longer
// present on the current element.
func (h *HTML) removeProperties(prev *HTML) {
// Properties
for name := range prev.properties {
if _, ok := h.properties[name]; !ok {
h.node.Delete(name)
}
}
// Attributes
for name := range prev.attributes {
if _, ok := h.attributes[name]; !ok {
h.node.Call("removeAttribute", name)
}
}
// Classes
classList := h.node.Get("classList")
for name := range prev.classes {
if _, ok := h.classes[name]; !ok {
classList.Call("remove", name)
}
}
// Dataset
dataset := h.node.Get("dataset")
for name := range prev.dataset {
if _, ok := h.dataset[name]; !ok {
dataset.Delete(name)
}
}
// Styles
style := h.node.Get("style")
for name := range prev.styles {
if _, ok := h.styles[name]; !ok {
style.Call("removeProperty", name)
}
}
// Event listeners
for _, l := range prev.eventListeners {
h.node.Call("removeEventListener", l.Name, l.wrapper)
l.wrapper.Release()
}
}
// reconcileChildren reconciles children of the current HTML against a previous
// render's DOM nodes.
func (h *HTML) reconcileChildren(prev *HTML) (pendingMounts []Mounter) {
hasKeyedChildren := len(h.keyedChildren) > 0
prevHadKeyedChildren := len(prev.keyedChildren) > 0
for i, nextChild := range h.children {
// Determine concrete type if necessary.
switch v := nextChild.(type) {
case *HTML:
// If the type of the child is *HTML, but its value is nil, replace
// the child with a concrete nil, to ensure consistent render
// handling.
if v == nil {
nextChild = nil
h.children[i] = nextChild
}
case List:
// Replace List with keyedList, which can handle nested keys and
// children.
nextChild = KeyedList{html: &HTML{children: v}}
h.children[i] = nextChild
}
// Ensure children implement the keyer interface consistently, and
// populate the keyedChildren map now.
//
// TODO(pdf): Add tests for node equality, keyed children
var (
new = !h.node.Equal(prev.node)
nextKey interface{}
)
keyer, isKeyer := nextChild.(Keyer)
if hasKeyedChildren && !isKeyer {
panic("vecty: all siblings must have keys when using keyed elements")
}
if isKeyer {
nextKey = keyer.Key()
if hasKeyedChildren && nextKey == nil {
panic("vecty: all siblings must have keys when using keyed elements")
}
if nextKey != nil {
if h.keyedChildren == nil {
h.keyedChildren = make(map[interface{}]ComponentOrHTML)
}
if _, exists := h.keyedChildren[nextKey]; exists {
panic("vecty: duplicate sibling key")
}
// Store the keyed child.
h.keyedChildren[nextKey] = nextChild
hasKeyedChildren = true
}
}
// If this is a new element (changed type, or did not exist previously),
// simply add the element directly. The existence of keyed children
// can not be determined by children index, so skip if keyed.
if (i >= len(prev.children) && !hasKeyedChildren) || new {
if nextChildList, ok := nextChild.(KeyedList); ok {
pendingMounts = append(pendingMounts, nextChildList.reconcile(h, nil)...)
continue
}
nextChildRender, skip, mounters := render(nextChild, nil)
if skip || nextChildRender == nil {
continue
}
pendingMounts = append(pendingMounts, mounters...)
if m, ok := nextChild.(Mounter); ok {
pendingMounts = append(pendingMounts, m)
}
h.lastRenderedChild = nextChildRender
// Note: we must insertBefore not appendChild because if we're
// rendering inside a list with unkeyed children, we will have an
// insertion node here.
h.insertBefore(h.insertBeforeNode, nextChildRender)
continue
}
var prevChild ComponentOrHTML
if len(prev.children) > i {
prevChild = prev.children[i]
}
// Find previous keyed sibling if exists, and mutate from there.
if hasKeyedChildren {
if prevKeyedChild, ok := prev.keyedChildren[nextKey]; ok {
prevChild = prevKeyedChild
} else {
prevChild = nil
}
}
var prevChildRender *HTML
// If the previous child was not a list, extract the previous child
// render.
if _, isList := prevChild.(KeyedList); !isList {
prevChildRender = extractHTML(prevChild)
}
// If the previous child render was nil try to find the next DOM node
// in the previous render so that we can insert this child at the
// correct location.
if prevChildRender == nil && h.insertBeforeNode == nil {
// If we have not rendered any children yet, take the insert
// position from the first child, if any, otherwise use the
// next sibling from the last rendered child.
if h.lastRenderedChild == nil {
h.insertBeforeNode = h.firstChild()
} else {
h.insertBeforeNode = h.lastRenderedChild.nextSibling()
}
}
// If our insertion node is the current previous child, advance to the
// next sibling.
if prevChildRender != nil && prevChildRender.node.Equal(h.insertBeforeNode) {
h.insertBeforeNode = h.insertBeforeNode.Get("nextSibling")
}
// If the next child is a list, reconcile its elements in-place, and
// we're done.
if nextChildList, ok := nextChild.(KeyedList); ok {
pendingMounts = append(pendingMounts, nextChildList.reconcile(h, prevChild)...)
continue
}
// If the previous child was a list, remove the list elements from the
// previous render, since we no longer have a list.
if prevChildList, ok := prevChild.(KeyedList); ok {
prevChildList.remove(h)
prevChild = nil
}
// If we're keyed, find the next DOM node from the previous render to
// insert before, for reordering.
var (
insertBeforeKeyedNode jsObject
stableKey bool
)
if hasKeyedChildren {
insertBeforeKeyedNode = h.lastRenderedChild.nextSibling()
// If the next node is our old node, mark key as stable, to avoid
// unnecessary insertion.
if prevChildRender != nil && prevChildRender.node.Equal(insertBeforeKeyedNode) {
stableKey = true
insertBeforeKeyedNode = nil
}
}
// Determine the next child render.
nextChildRender, skip, mounters := render(nextChild, prevChild)
if nextChildRender != nil && prevChildRender != nil && nextChildRender == prevChildRender {
panic("vecty: next child render must not equal previous child render (did the child Render illegally return a stored render variable?)")
}
// Store the last rendered child to determine insertion target for
// subsequent children.
if nextChildRender != nil {
h.lastRenderedChild = nextChildRender
}
// If the previous and next child are components of the same type, then
// keep prevChildComponent as our child so that the next time we are
// here prevChild will be the same pointer. We do this because
// prevChildComponent is the persistent component instance.
if prevChildComponent, ok := prevChild.(Component); ok {
if nextChildComponent, ok := nextChild.(Component); ok && sameType(prevChildComponent, nextChildComponent) {
h.children[i] = prevChild
nextChild = prevChild
if hasKeyedChildren {
h.keyedChildren[nextKey] = prevChild
}
}
}
if skip {
continue
}
pendingMounts = append(pendingMounts, mounters...)
// Perform the final reconciliation action for nextChildRender and
// prevChildRender. Replace, remove, insert or append the DOM nodes.
switch {
case nextChildRender == nil && prevChildRender == nil:
continue // nothing to do.
case nextChildRender != nil && prevChildRender != nil:
if m := mountUnmount(nextChild, prevChild); m != nil {
pendingMounts = append(pendingMounts, m)
}
if hasKeyedChildren && (prevChildRender != nil && prevChildRender.node.Equal(nextChildRender.node)) {
// We are re-using the name node. Remove the children from
// keyedChildren so that we don't remove it when we remove dangling
// children below.
delete(prev.keyedChildren, nextKey)
}
// If we do not have keyed siblings, or the key is stable, replace
// the previous node (may be NOOP for equivalent nodes).
if !hasKeyedChildren || stableKey {
replaceNode(nextChildRender.node, prevChildRender.node)
continue
}
// Moving keyed children need to be inserted (which moves existing
// nodes), rather than replacing the previous child at this
// position.
if insertBeforeKeyedNode != nil {
// Insert before the next sibling, if we have one.
h.insertBefore(insertBeforeKeyedNode, nextChildRender)
continue
}
h.insertBefore(h.insertBeforeNode, nextChildRender)
case nextChildRender == nil && prevChildRender != nil:
h.removeChild(prevChildRender)
case nextChildRender != nil && prevChildRender == nil:
if m, ok := nextChild.(Mounter); ok {
pendingMounts = append(pendingMounts, m)
}
if insertBeforeKeyedNode != nil {
// Insert before the next keyed sibling, if we have one.
h.insertBefore(insertBeforeKeyedNode, nextChildRender)
continue
}
h.insertBefore(h.insertBeforeNode, nextChildRender)
default:
panic("vecty: internal error (unexpected switch state)")
}
}
// If dealing with keyed siblings, remove all prev.keyedChildren which are
// leftovers / ones we did not find a match for above.
if prevHadKeyedChildren && hasKeyedChildren {
// Convert prev.keyedChildren map to slice, and invoke removeChildren.
prevChildren := make([]ComponentOrHTML, len(prev.keyedChildren))
i := 0
for _, c := range prev.keyedChildren {
prevChildren[i] = c
i++
}
h.removeChildren(prevChildren)
return pendingMounts
}
if len(prev.children) > len(h.children) {
// Remove every previous child that h.children does not have in common.
h.removeChildren(prev.children[len(h.children):])
}
return pendingMounts
}
// removeChildren removes child elements from the previous render pass that no
// longer exist on the current HTML children.
func (h *HTML) removeChildren(prevChildren []ComponentOrHTML) {
for _, prevChild := range prevChildren {
if prevChildList, ok := prevChild.(KeyedList); ok {
// Previous child was a list, so remove all DOM nodes in it.
prevChildList.remove(h)
continue
}
prevChildRender := extractHTML(prevChild)
if prevChildRender == nil {
continue
}
h.removeChild(prevChildRender)
}
}
// firstChild returns the first child DOM node of this element.
func (h *HTML) firstChild() jsObject {
if h == nil || h.node == nil {
return nil
}
return h.node.Get("firstChild")
}
// nextSibling returns the next sibling DOM node for this element.
func (h *HTML) nextSibling() jsObject {
if h == nil || h.node == nil {
return nil
}
return h.node.Get("nextSibling")
}
// removeChild removes the provided child element from this element, and
// triggers unmount handlers.
func (h *HTML) removeChild(child *HTML) {
// If we're removing the current insert target, use the next
// sibling, if any.
if h.insertBeforeNode != nil && h.insertBeforeNode.Equal(child.node) {
h.insertBeforeNode = h.insertBeforeNode.Get("nextSibling")
}
unmount(child)
if child.node == nil {
return
}
// Use the child's parent node here, in case our node is not a valid
// target by the time we're called.
child.node.Get("parentNode").Call("removeChild", child.node)
}
// appendChild appends a new child to this element.
func (h *HTML) appendChild(child *HTML) {
h.node.Call("appendChild", child.node)
}
// insertBefore inserts the provided child before the provided DOM node. If the
// DOM node is nil, the child will be appended instead.
func (h *HTML) insertBefore(node jsObject, child *HTML) {
if node == nil {
h.appendChild(child)
return
}
h.node.Call("insertBefore", child.node, node)
}
// List represents a list of components or HTML.
type List []ComponentOrHTML
// isMarkupOrChild implements MarkupOrChild
func (l List) isMarkupOrChild() {}
// isComponentOrHTML implements ComponentOrHTML
func (l List) isComponentOrHTML() {}
// WithKey wraps the List in a Keyer using the given key. List members are
// inaccessible within the returned value.
func (l List) WithKey(key interface{}) KeyedList {
return KeyedList{key: key, html: &HTML{children: l}}
}
// KeyedList is produced by calling List.WithKey. It has no public behaviour,
// and List members are no longer accessible once wrapped in this stucture.
type KeyedList struct {
// html is used to render a set of children into another element in a
// separate context, without requiring a structural element. Keyed children
// also occupy a separate keyspace to the parent element.
html *HTML
// key is optional, and only required when the KeyedList has keyed siblings.
key interface{}
}
// isMarkupOrChild implements MarkupOrChild
func (l KeyedList) isMarkupOrChild() {}
// isComponentOrHTML implements ComponentOrHTML
func (l KeyedList) isComponentOrHTML() {}
// Key implements the Keyer interface
func (l KeyedList) Key() interface{} {
return l.key
}
// reconcile reconciles the keyedList against the DOM node in a separate
// context, unless keyed. Uses the currently known insertion point from the
// parent to insert children at the correct position.
func (l KeyedList) reconcile(parent *HTML, prevChild ComponentOrHTML) (pendingMounts []Mounter) {
// Effectively become the parent (copy its scope) so that we can reconcile
// our children against the prev child.
l.html.node = parent.node
l.html.insertBeforeNode = parent.insertBeforeNode
l.html.lastRenderedChild = parent.lastRenderedChild
switch v := prevChild.(type) {
case KeyedList:
pendingMounts = l.html.reconcileChildren(v.html)
case *HTML, Component, nil:
if v == nil {
// No previous element, so reconcile against a parent with no
// children so all of our elements are added.
pendingMounts = l.html.reconcileChildren(&HTML{node: parent.node})
} else {
// Build a previous render containing just the prevChild to be
// replaced by this list
prev := &HTML{node: parent.node, children: []ComponentOrHTML{prevChild}}
if keyer, ok := prevChild.(Keyer); ok && keyer.Key() != nil {
prev.keyedChildren = map[interface{}]ComponentOrHTML{keyer.Key(): prevChild}
}
pendingMounts = l.html.reconcileChildren(prev)
}
default:
panic("vecty: internal error (unexpected ComponentOrHTML type " + reflect.TypeOf(v).String() + ")")
}
// Update the parent insertBeforeNode and lastRenderedChild values to be
// ours, since we acted as the parent and ours is now updated / theirs is
// outdated.
if parent.insertBeforeNode != nil {
parent.insertBeforeNode = l.html.insertBeforeNode
}
if l.html.lastRenderedChild != nil {
parent.lastRenderedChild = l.html.lastRenderedChild
}
return pendingMounts
}
// remove keyedList elements from the parent.
func (l KeyedList) remove(parent *HTML) {
// Become the parent so that we can remove all of our children and get an
// updated insertBeforeNode value.
l.html.node = parent.node
l.html.insertBeforeNode = parent.insertBeforeNode
l.html.removeChildren(l.html.children)
// Now that the children are removed, and our insertBeforeNode value has
// been updated, update the parent's insertBeforeNode value since it is now
// invalid and ours is correct.
if parent.insertBeforeNode != nil {
parent.insertBeforeNode = l.html.insertBeforeNode
}
}
// Tag returns an HTML element with the given tag name. Generally, this
// function is not used directly but rather the elem subpackage (which is type
// safe) is used instead.
func Tag(tag string, m ...MarkupOrChild) *HTML {
h := &HTML{
tag: tag,
}
for _, m := range m {
apply(m, h)
}
return h
}
// Text returns a TextNode with the given literal text. Because the returned
// HTML represents a TextNode, the text does not have to be escaped (arbitrary
// user input fed into this function will always be safely rendered).
func Text(text string, m ...MarkupOrChild) *HTML {
h := &HTML{
text: text,
}
for _, m := range m {
apply(m, h)
}
return h
}
// Rerender causes the body of the given Component (i.e. the HTML returned by
// the Component's Render method) to be re-rendered.
//
// If the Component has not been rendered before, Rerender panics. If the
// Component was previously unmounted, Rerender is no-op.
//
// Rerender operates efficiently by batching renders together. As a result,
// there is no guarantee that a calls to Rerender will map 1:1 with calls to
// the Component's Render method. For example, two calls to Rerender may
// result in only one call to the Component's Render method.
func Rerender(c Component) {
if c == nil {
panic("vecty: Rerender illegally called with a nil Component argument")
}
if c.Context().prevRender == nil {
panic("vecty: Rerender invoked on Component that has never been rendered")
}
if c.Context().unmounted {
return
}
batch.add(c)
}
// batchRenderer handles component re-renders by queueing and deduplicating
// them, to be rendered on the next animation frame (via requestAnimationFrame).
type batchRenderer struct {
// batch contains the list of pending components to render.
batch []Component
// idx maps components to batch indexes to allow dedup, retaining order.
idx map[Component]int
// scheduled tracks whether a batch has been scheduled for processing.
scheduled bool
}
// add a Component to the pending batch.
func (b *batchRenderer) add(c Component) {
if i, ok := b.idx[c]; ok {
// Shift idx for delete.
for j, c := range b.batch[i+1:] {
b.idx[c] = i + j
}
// Delete previously queued render.
copy(b.batch[i:], b.batch[i+1:])
b.batch[len(b.batch)-1] = nil
b.batch = b.batch[:len(b.batch)-1]
}
// Append and index component.
b.batch = append(b.batch, c)
b.idx[c] = len(b.batch) - 1
// If we're not already scheduled for a render batch, request a render on
// the next frame.
if !b.scheduled {
b.scheduled = true
requestAnimationFrame(b.render)
}
}
// render the pending batch.
// TODO(pdf): Add tests for time budget and multi-pass renders.
func (b *batchRenderer) render(startTime float64) {
// If the batch is empty, mark as unscheduled, and stop render cycle.
if len(b.batch) == 0 {
b.scheduled = false
return
}
// Drain the current batch.
pending := b.batch
b.batch = nil
b.idx = make(map[Component]int)
// Process batch.
for i, c := range pending {
// Skip unmounted components.
if c.Context().unmounted {
continue
}
// Check for remaining time budget, targeting 60fps (~16ms per frame).
if i > 0 {
elapsed := global().Get("performance").Call("now").Float() - startTime
budgetRemaining := (1000 / 60) - elapsed
avgRenderTime := elapsed / float64(i)
// If the budget remaining is less than 2 times the average
// Component render time, push the remainder of the batch to the
// next frame.
if budgetRemaining < avgRenderTime*2 {
b.batch = pending[i:]
for i, c := range b.batch {
b.idx[c] = i
}
break
}
}
// Perform render.
prevHTML := extractHTML(c.Context().prevRender)
nextHTML, skip, pendingMounts := renderComponent(c, c)
if skip {
continue
}
replaceNode(nextHTML.node, prevHTML.node)
mount(pendingMounts...)
}
// Schedule next frame.
requestAnimationFrame(b.render)
}
// extractHTML returns the *HTML from a ComponentOrHTML.
func extractHTML(e ComponentOrHTML) *HTML {
switch v := e.(type) {
case nil:
return nil
case *HTML:
return v
case Component:
return extractHTML(v.Context().prevRender)
default:
panic("vecty: internal error (unexpected ComponentOrHTML type " + reflect.TypeOf(e).String() + ")")
}
}
// sameType returns whether first and second ComponentOrHTML are of the same
// underlying type.
func sameType(first, second ComponentOrHTML) bool {
return reflect.TypeOf(first) == reflect.TypeOf(second)
}
// copyComponent makes a copy of the given component.
func copyComponent(c Component) Component {
if c == nil {
panic("vecty: internal error (cannot copy nil Component)")
}
// If the Component implements the Copier interface, then use that to
// perform the copy.
if copier, ok := c.(Copier); ok {
cpy := copier.Copy()
if cpy == c {
panic("vecty: Component.Copy illegally returned an identical *MyComponent pointer")
}
return cpy
}
tinyGoAssertCopier(c)
// Component does not implement the Copier interface, so perform a shallow
// copy.
v := reflect.ValueOf(c)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
panic("vecty: Component must be pointer to struct, found " + reflect.TypeOf(c).String())
}
cpy := reflect.New(v.Elem().Type())
cpy.Elem().Set(v.Elem())
return cpy.Interface().(Component)
}
// copyProps copies all struct fields from src to dst that are tagged with
// `vecty:"prop"`.
//
// If src and dst are different types or non-pointers, copyProps panics.
func copyProps(src, dst Component) {
if src == dst {
return
}
s := reflect.ValueOf(src)
d := reflect.ValueOf(dst)
if s.Type() != d.Type() {
panic("vecty: internal error (attempted to copy properties of incompatible structs)")
}
if s.Kind() != reflect.Ptr || d.Kind() != reflect.Ptr {
panic("vecty: internal error (attempted to copy properties of non-pointer)")
}
for i := 0; i < s.Elem().NumField(); i++ {
sf := s.Elem().Field(i)
if s.Elem().Type().Field(i).Tag.Get("vecty") == "prop" {
df := d.Elem().Field(i)
if sf.Type() != df.Type() {
panic("vecty: internal error (should never be possible, struct types are identical)")
}
df.Set(sf)
}
}
}
// render handles rendering the next child into HTML. If skip is returned,
// the component's SkipRender method has signaled to skip rendering.
//
// In specific, render handles six cases:
//
// 1. nextChild == *HTML && prevChild == *HTML
// 2. nextChild == *HTML && prevChild == Component
// 3. nextChild == *HTML && prevChild == nil
// 4. nextChild == Component && prevChild == Component
// 5. nextChild == Component && prevChild == *HTML
// 6. nextChild == Component && prevChild == nil
//
func render(next, prev ComponentOrHTML) (nextHTML *HTML, skip bool, pendingMounts []Mounter) {
switch v := next.(type) {
case *HTML:
// Cases 1, 2 and 3 above. Reconcile against the prevRender.
pendingMounts = v.reconcile(extractHTML(prev))
return v, false, pendingMounts
case Component:
// Cases 4, 5, and 6 above.