This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhooks.go
executable file
·141 lines (114 loc) · 2.6 KB
/
hooks.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
package gas
// Hooks component lifecycle hooks
type Hooks struct {
BeforeCreated HookWithControl // When parent already rendered (appended to DOM), but component Element don't yet (you can rerender childes)
Created Hook // When component has been created in golang only (Element isn't available)
Mounted Hook // When component has been mounted (Element is available)
BeforeDestroy Hook // Before component destroy (Element is available)
BeforeUpdate Hook // When component child don't updated
Updated Hook // After component child was updated
}
// Hook - lifecycle hook
type Hook func() error
// HookWithControl - lifecycle hook. Return true for rerender component childes
type HookWithControl func() (rerender bool, err error)
// CallBeforeCreated call component and it's childes BeforeCreated hook
func CallBeforeCreated(i interface{}) error {
e, ok := i.(*Element)
if !ok {
return nil
}
c := e.Component
if c != nil && c.Hooks.BeforeCreated != nil {
rerender, err := c.Hooks.BeforeCreated()
if err != nil {
return err
}
if rerender {
e.UpdateChildes()
}
}
for _, child := range e.Childes {
err := CallBeforeCreated(child)
if err != nil {
return err
}
}
return nil
}
// CallMounted call component and it's childes Mounted hook
func CallMounted(i interface{}) error {
e, ok := i.(*Element)
if !ok {
return nil
}
c := e.Component
if c != nil && c.Hooks.Mounted != nil {
err := c.Hooks.Mounted()
if err != nil {
return err
}
}
for _, child := range e.Childes {
err := CallMounted(child)
if err != nil {
return err
}
}
return nil
}
// CallBeforeDestroy call component and it's childes WillDestroy hook
func CallBeforeDestroy(i interface{}) error {
e, ok := i.(*Element)
if !ok {
return nil
}
c := e.Component
if c != nil && c.Hooks.BeforeDestroy != nil {
err := c.Hooks.BeforeDestroy()
if err != nil {
return err
}
}
for _, child := range e.Childes {
err := CallBeforeDestroy(child)
if err != nil {
return err
}
}
return nil
}
// CallUpdated call Updated hook
func CallUpdated(p *Element) error {
if p == nil {
return nil
}
c := p.Component
if c == nil {
c = p.ParentComponent().Component
}
if c.Hooks.Updated != nil {
err := c.Hooks.Updated()
if err != nil {
return err
}
}
return nil
}
// CallBeforeUpdate call BeforeUpdate hook
func CallBeforeUpdate(p *Element) error {
if p == nil {
return nil
}
c := p.Component
if c == nil {
c = p.ParentComponent().Component
}
if c.Hooks.BeforeUpdate != nil {
err := c.Hooks.BeforeUpdate()
if err != nil {
return err
}
}
return nil
}