-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
dispatcher.go
304 lines (249 loc) · 6.7 KB
/
dispatcher.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
package app
import (
"context"
"net/url"
"reflect"
"sync"
"github.com/maxence-charriere/go-app/v8/pkg/errors"
)
const (
dispatcherSize = 4096
)
// Dispatcher is the interface that describes an environment that synchronizes
// UI instructions and UI elements lifecycle.
type Dispatcher interface {
// Dispatch enqueues the given function to be executed on a goroutine
// dedicated to managing UI modifications.
Dispatch(func())
// Async launches the given function on a new goroutine.
//
// The difference versus just launching a goroutine is that it ensures that
// the asynchronous instructions are called before the dispatcher is closed.
//
// This is important during component prerendering since asynchronous
// operations need to complete before sending a pre-rendered page over HTTP.
Async(func())
// Wait waits for the asynchronous operations launched with Async() to
// complete.
Wait()
start(context.Context)
currentPage() Page
localStorage() BrowserStorage
sessionStorage() BrowserStorage
isServerSideMode() bool
resolveStaticResource(string) string
}
// ClientDispatcher is the interface that describes a dispatcher that emulates a
// client environment.
type ClientDispatcher interface {
Dispatcher
// Context returns the context associated with the root element.
Context() Context
// Mounts the given component as root element.
Mount(UI)
// Triggers OnNav from the root component.
Nav(*url.URL)
// Triggers OnAppUpdate from the root component.
AppUpdate()
// Triggers OnAppResize from the root component.
AppResize()
// Consume executes all the remaining UI instructions.
Consume()
// Close consumes all the remaining UI instruction and releases allocated
// resources.
Close()
}
// NewClientTester creates a testing dispatcher that simulates a
// client environment. The given UI element is mounted upon creation.
func NewClientTester(v UI) ClientDispatcher {
return newTestingDispatcher(v, false)
}
// ServerDispatcher is the interface that describes a dispatcher that emulates a server environment.
type ServerDispatcher interface {
Dispatcher
// Context returns the context associated with the root element.
Context() Context
// Pre-renders the given component.
PreRender()
// Consume executes all the remaining UI instructions.
Consume()
// Close consumes all the remaining UI instruction and releases allocated
// resources.
Close()
}
// NewServerTester creates a testing dispatcher that simulates a
// client environment.
func NewServerTester(v UI) ServerDispatcher {
return newTestingDispatcher(v, true)
}
func newTestingDispatcher(v UI, serverSide bool) *uiDispatcher {
u, _ := url.Parse("https://localhost")
disp := newUIDispatcher(serverSide, &requestPage{url: u}, func(url string) string {
return url
})
disp.body = Body().Body(
Div(),
).(elemWithChildren)
if err := mount(disp, disp.body); err != nil {
panic(errors.New("mounting body failed").
Tag("server-side-mode", disp.isServerSideMode()).
Tag("body-type", reflect.TypeOf(disp.body)).
Tag("ui-len", len(disp.ui)).
Tag("ui-cap", cap(disp.ui)).
Wrap(err))
}
disp.Mount(v)
disp.Consume()
return disp
}
type uiDispatcher struct {
ui chan func()
body elemWithChildren
page Page
mountedOnce bool
serverSideMode bool
wg sync.WaitGroup
resolveStaticResourceFunc func(string) string
localStore BrowserStorage
sessionStore BrowserStorage
}
func newUIDispatcher(serverSide bool, p Page, resolveStaticResource func(string) string) *uiDispatcher {
var localStorage BrowserStorage
var sessionStorage BrowserStorage
if IsClient {
localStorage = newJSStorage("localStorage")
sessionStorage = newJSStorage("sessionStorage")
} else {
localStorage = newMemoryStorage()
sessionStorage = newMemoryStorage()
}
disp := &uiDispatcher{
ui: make(chan func(), dispatcherSize),
serverSideMode: serverSide,
resolveStaticResourceFunc: resolveStaticResource,
localStore: localStorage,
sessionStore: sessionStorage,
}
if p, ok := p.(browserPage); ok {
p.dispatcher = disp
}
disp.page = p
return disp
}
func (d *uiDispatcher) Context() Context {
return makeContext(d.body.children()[0])
}
func (d *uiDispatcher) Dispatch(fn func()) {
d.ui <- fn
}
func (d *uiDispatcher) Async(fn func()) {
d.wg.Add(1)
go func() {
fn()
d.wg.Done()
}()
}
func (d *uiDispatcher) Wait() {
d.wg.Wait()
}
func (d *uiDispatcher) PreRender() {
d.Dispatch(func() {
d.body.preRender(d.currentPage())
})
}
func (d *uiDispatcher) Mount(v UI) {
d.Dispatch(func() {
if !d.mountedOnce {
if err := d.body.(elemWithChildren).replaceChildAt(0, v); err != nil {
panic(errors.New("mounting ui element failed").
Tag("server-side-mode", d.isServerSideMode()).
Tag("body-type", reflect.TypeOf(d.body)).
Tag("ui-len", len(d.ui)).
Tag("ui-cap", cap(d.ui)).
Wrap(err))
}
d.mountedOnce = true
return
}
err := update(d.body.children()[0], v)
if err == nil {
return
}
if !isErrReplace(err) {
panic(errors.New("mounting ui element failed").
Tag("server-side-mode", d.isServerSideMode()).
Tag("body-type", reflect.TypeOf(d.body)).
Tag("ui-len", len(d.ui)).
Tag("ui-cap", cap(d.ui)).
Wrap(err))
}
if err := d.body.(elemWithChildren).replaceChildAt(0, v); err != nil {
panic(errors.New("mounting ui element failed").
Tag("server-side-mode", d.isServerSideMode()).
Tag("body-type", reflect.TypeOf(d.body)).
Tag("ui-len", len(d.ui)).
Tag("ui-cap", cap(d.ui)).
Wrap(err))
}
})
}
func (d *uiDispatcher) Nav(u *url.URL) {
if p, ok := d.currentPage().(*requestPage); ok {
p.url = u
}
d.Dispatch(func() {
d.body.onNav(u)
})
}
func (d *uiDispatcher) AppUpdate() {
d.Dispatch(func() {
d.body.onAppUpdate()
})
}
func (d *uiDispatcher) AppResize() {
d.Dispatch(func() {
d.body.onResize()
})
}
func (d *uiDispatcher) Consume() {
for {
select {
case fn := <-d.ui:
fn()
default:
return
}
}
}
func (d *uiDispatcher) Close() {
d.Consume()
d.Wait()
dismount(d.body)
d.body = nil
close(d.ui)
}
func (d *uiDispatcher) start(ctx context.Context) {
for {
select {
case fn := <-d.ui:
fn()
case <-ctx.Done():
return
}
}
}
func (d *uiDispatcher) currentPage() Page {
return d.page
}
func (d *uiDispatcher) isServerSideMode() bool {
return d.serverSideMode
}
func (d *uiDispatcher) resolveStaticResource(url string) string {
return d.resolveStaticResourceFunc(url)
}
func (d *uiDispatcher) localStorage() BrowserStorage {
return d.localStore
}
func (d *uiDispatcher) sessionStorage() BrowserStorage {
return d.sessionStore
}