@yuneta/gobj-ui 7.27.27 → 7.27.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -6
- package/dist/gobj-ui.cjs.js +737 -63
- package/dist/gobj-ui.es.js +738 -65
- package/index.js +1 -0
- package/package.json +2 -2
- package/src/c_yui_gclass.js +2 -2
- package/src/c_yui_gobj_tree_js.js +13 -5
- package/src/c_yui_update_watch.css +45 -0
- package/src/c_yui_update_watch.js +722 -0
- package/src/c_yui_update_watch.wiring.test.js +315 -0
- package/src/gclass_traces.wiring.test.js +6 -0
- package/src/gobj_tree_remote.wiring.test.js +14 -0
- package/src/shell_modals.js +8 -0
- package/src/vite_plugin_yuneta_html.test.js +34 -1
- package/src/yui_build_stamp.js +125 -0
- package/src/yui_build_stamp.test.js +56 -0
- package/vite-plugin-yuneta-html.js +60 -2
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* c_yui_update_watch.js
|
|
3
|
+
*
|
|
4
|
+
* Is this page still running the build that is deployed?
|
|
5
|
+
*
|
|
6
|
+
* Every route of these apps is a hash: moving around reloads
|
|
7
|
+
* nothing, and neither does a login or a logout. A page opened
|
|
8
|
+
* before a deploy runs the old bundle until somebody reloads it
|
|
9
|
+
* by hand, and the only symptom is that a fix "did not work" --
|
|
10
|
+
* or worse: the deploy deleted the old lazy chunks, and the old
|
|
11
|
+
* page breaks the first time it opens a view it had not loaded.
|
|
12
|
+
*
|
|
13
|
+
* This service compares the build of the page (yui_build_stamp.js,
|
|
14
|
+
* the stamp the build writes into index.html) with the one the
|
|
15
|
+
* server hands out now, fetching that same document with
|
|
16
|
+
* `cache: "no-store"`. A difference is SAID, never acted on: a
|
|
17
|
+
* bar with the new version, a reload button and a ✕. Reloading
|
|
18
|
+
* is the reader's call -- they may be in the middle of something.
|
|
19
|
+
*
|
|
20
|
+
* When it asks (every one an event, through the FSM):
|
|
21
|
+
* - at start;
|
|
22
|
+
* - the tab comes back to the front, or the window gets the
|
|
23
|
+
* focus (at most once each `min_interval_ms`);
|
|
24
|
+
* - the browser is online again;
|
|
25
|
+
* - a lazy chunk failed to load (`vite:preloadError`): the
|
|
26
|
+
* symptom of exactly this, so it asks at once;
|
|
27
|
+
* - every `period_ms`: a screen that is always in view (a
|
|
28
|
+
* control room, a wall display) never gets the focus back.
|
|
29
|
+
* A periodic query is a discarded pattern in Yuneta; THIS
|
|
30
|
+
* one is a deliberate exception approved by the user
|
|
31
|
+
* (2026-10-21), for every SPA. 0 turns it off;
|
|
32
|
+
* - whenever the app sends EV_CHECK_BUILD -- on its backend's
|
|
33
|
+
* EV_ON_OPEN, typically: a backend deploy often comes with
|
|
34
|
+
* a deploy of the SPA.
|
|
35
|
+
*
|
|
36
|
+
* A build that comes back to the running one (a rollback) takes
|
|
37
|
+
* the bar away again. A page with no stamp (`vite dev`) watches
|
|
38
|
+
* nothing.
|
|
39
|
+
*
|
|
40
|
+
* The bar lives in the shell's notification layer, above its
|
|
41
|
+
* dialogs; with no shell (the login, between a logout and the
|
|
42
|
+
* next login) in document.body. It follows the shell across a
|
|
43
|
+
* logout (yui_shell_watch_changes()), as the dock does.
|
|
44
|
+
*
|
|
45
|
+
* Usage (an app, once, after creating the yuno):
|
|
46
|
+
*
|
|
47
|
+
* register_c_yui_update_watch();
|
|
48
|
+
* const watch = gobj_create_service(
|
|
49
|
+
* "__update_watch__", "C_YUI_UPDATE_WATCH", {}, yuno
|
|
50
|
+
* );
|
|
51
|
+
* gobj_start(watch);
|
|
52
|
+
*
|
|
53
|
+
* // on its backend's EV_ON_OPEN:
|
|
54
|
+
* gobj_send_event(watch, "EV_CHECK_BUILD", {}, gobj);
|
|
55
|
+
*
|
|
56
|
+
* Input events:
|
|
57
|
+
* EV_CHECK_BUILD {force?} ask the server (force skips
|
|
58
|
+
* the minimum interval)
|
|
59
|
+
* EV_RELOAD {} the bar's reload button
|
|
60
|
+
* EV_DISMISS {} the bar's ✕: hidden for THIS
|
|
61
|
+
* build, a later one shows it
|
|
62
|
+
*
|
|
63
|
+
* Output events (optional subscribers):
|
|
64
|
+
* EV_NEW_BUILD {running, deployed, version}
|
|
65
|
+
* EV_BUILD_CURRENT {running} the deployed build is the
|
|
66
|
+
* running one again
|
|
67
|
+
*
|
|
68
|
+
* Copyright (c) 2026, ArtGins.
|
|
69
|
+
* All Rights Reserved.
|
|
70
|
+
***********************************************************************/
|
|
71
|
+
import {
|
|
72
|
+
SDATA,
|
|
73
|
+
SDATA_END,
|
|
74
|
+
data_type_t,
|
|
75
|
+
event_flag_t,
|
|
76
|
+
gclass_create,
|
|
77
|
+
log_error,
|
|
78
|
+
log_warning,
|
|
79
|
+
log_info,
|
|
80
|
+
createElement2,
|
|
81
|
+
refresh_language,
|
|
82
|
+
set_timeout_periodic,
|
|
83
|
+
clear_timeout,
|
|
84
|
+
gobj_create_pure_child,
|
|
85
|
+
gobj_name,
|
|
86
|
+
gobj_short_name,
|
|
87
|
+
gobj_start,
|
|
88
|
+
gobj_stop,
|
|
89
|
+
gobj_is_running,
|
|
90
|
+
gobj_is_destroying,
|
|
91
|
+
gobj_read_pointer_attr,
|
|
92
|
+
gobj_read_integer_attr,
|
|
93
|
+
gobj_read_bool_attr,
|
|
94
|
+
gobj_subscribe_event,
|
|
95
|
+
gobj_unsubscribe_event,
|
|
96
|
+
gobj_send_event,
|
|
97
|
+
gobj_publish_event,
|
|
98
|
+
gobj_change_state,
|
|
99
|
+
gobj_current_state,
|
|
100
|
+
} from "@yuneta/gobj-js";
|
|
101
|
+
|
|
102
|
+
import {t} from "i18next";
|
|
103
|
+
import {text_node} from "./yui_text.js";
|
|
104
|
+
import {yui_tint} from "./bulma_tint.js";
|
|
105
|
+
import {
|
|
106
|
+
yui_shell_of,
|
|
107
|
+
yui_shell_watch_changes,
|
|
108
|
+
yui_shell_unwatch_changes,
|
|
109
|
+
} from "./c_yui_shell.js";
|
|
110
|
+
import {yui_shell_notification_layer} from "./shell_modals.js";
|
|
111
|
+
import {running_build, parse_build, build_page_url} from "./yui_build_stamp.js";
|
|
112
|
+
import "./c_yui_update_watch.css";
|
|
113
|
+
|
|
114
|
+
/***************************************************************
|
|
115
|
+
* Constants
|
|
116
|
+
***************************************************************/
|
|
117
|
+
const GCLASS_NAME = "C_YUI_UPDATE_WATCH";
|
|
118
|
+
|
|
119
|
+
/***************************************************************
|
|
120
|
+
* Data
|
|
121
|
+
***************************************************************/
|
|
122
|
+
const attrs_table = [
|
|
123
|
+
SDATA(data_type_t.DTP_POINTER, "subscriber", 0, null, "Subscriber of output events"),
|
|
124
|
+
SDATA(data_type_t.DTP_INTEGER, "min_interval_ms", 0, "60000", "Minimum time between two checks, except a forced one"),
|
|
125
|
+
SDATA(data_type_t.DTP_INTEGER, "period_ms", 0, "3600000", "Periodic check, for a screen that never gets the focus back; 0 = off (user-approved exception to No polling)"),
|
|
126
|
+
SDATA(data_type_t.DTP_BOOLEAN, "with_bar", 0, "1", "Show the bar; false leaves EV_NEW_BUILD to the app"),
|
|
127
|
+
SDATA(data_type_t.DTP_POINTER, "before_reload", 0, null, "fn() -> bool|Promise<bool>, asked before the reload; false vetoes it (unsaved edits)"),
|
|
128
|
+
SDATA(data_type_t.DTP_POINTER, "$parent", 0, null, "Where the bar mounts (default: the shell's notification layer, document.body while there is none)"),
|
|
129
|
+
SDATA_END()
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
let PRIVATE_DATA = {
|
|
133
|
+
running: null, /* {stamp, version} of this page, null = watch nothing */
|
|
134
|
+
deployed: null, /* {stamp, version} last read from the server */
|
|
135
|
+
dismissed: "", /* stamp whose bar the reader closed */
|
|
136
|
+
in_flight: false, /* a fetch is out */
|
|
137
|
+
last_check: 0, /* performance.now() of the last fetch */
|
|
138
|
+
failing: false, /* the last check failed (log on the transition only) */
|
|
139
|
+
gobj_timer: null,
|
|
140
|
+
$bar: null,
|
|
141
|
+
lang_shell: null, /* the shell whose EV_LANGUAGE_CHANGED we hear */
|
|
142
|
+
dom_handlers: null, /* [[target, type, fn]] added in mt_start */
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
let __gclass__ = null;
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
/******************************
|
|
151
|
+
* Framework Methods
|
|
152
|
+
******************************/
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
/***************************************************************
|
|
158
|
+
* Framework Method: Create
|
|
159
|
+
***************************************************************/
|
|
160
|
+
function mt_create(gobj)
|
|
161
|
+
{
|
|
162
|
+
let priv = gobj.priv;
|
|
163
|
+
|
|
164
|
+
priv.running = running_build(typeof document !== "undefined" ? document : null);
|
|
165
|
+
priv.dom_handlers = [];
|
|
166
|
+
|
|
167
|
+
/*
|
|
168
|
+
* Create children
|
|
169
|
+
*/
|
|
170
|
+
priv.gobj_timer = gobj_create_pure_child(gobj_name(gobj), "C_TIMER", {}, gobj);
|
|
171
|
+
|
|
172
|
+
/* The bar lives in a shell and outlives it: a logout destroys the
|
|
173
|
+
* shell, the login builds another (ac_shell_changed). */
|
|
174
|
+
yui_shell_watch_changes(gobj);
|
|
175
|
+
follow_language(gobj);
|
|
176
|
+
|
|
177
|
+
/*
|
|
178
|
+
* SERVICE subscription model
|
|
179
|
+
*/
|
|
180
|
+
const subscriber = gobj_read_pointer_attr(gobj, "subscriber");
|
|
181
|
+
if(subscriber) {
|
|
182
|
+
gobj_subscribe_event(gobj, null, {}, subscriber);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/***************************************************************
|
|
187
|
+
* Framework Method: Start
|
|
188
|
+
***************************************************************/
|
|
189
|
+
function mt_start(gobj)
|
|
190
|
+
{
|
|
191
|
+
let priv = gobj.priv;
|
|
192
|
+
|
|
193
|
+
gobj_start(priv.gobj_timer);
|
|
194
|
+
|
|
195
|
+
if(!priv.running) {
|
|
196
|
+
log_info(`${gobj_short_name(gobj)}: this page carries no build stamp (vite dev?): nothing to watch`);
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* The OS notifications, translated into events and nothing else. */
|
|
201
|
+
listen_dom(gobj, document, "visibilitychange", () => {
|
|
202
|
+
if(document.visibilityState === "visible") {
|
|
203
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {}, gobj);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
listen_dom(gobj, window, "focus", () => {
|
|
207
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {}, gobj);
|
|
208
|
+
});
|
|
209
|
+
listen_dom(gobj, window, "online", () => {
|
|
210
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {force: true}, gobj);
|
|
211
|
+
});
|
|
212
|
+
listen_dom(gobj, window, "vite:preloadError", () => {
|
|
213
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {force: true}, gobj);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
let period = gobj_read_integer_attr(gobj, "period_ms");
|
|
217
|
+
if(period > 0) {
|
|
218
|
+
set_timeout_periodic(priv.gobj_timer, period);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {force: true}, gobj);
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/***************************************************************
|
|
226
|
+
* Framework Method: Stop
|
|
227
|
+
***************************************************************/
|
|
228
|
+
function mt_stop(gobj)
|
|
229
|
+
{
|
|
230
|
+
let priv = gobj.priv;
|
|
231
|
+
|
|
232
|
+
for(let [target, type, fn] of priv.dom_handlers) {
|
|
233
|
+
target.removeEventListener(type, fn);
|
|
234
|
+
}
|
|
235
|
+
priv.dom_handlers = [];
|
|
236
|
+
|
|
237
|
+
clear_timeout(priv.gobj_timer);
|
|
238
|
+
if(gobj_is_running(priv.gobj_timer)) {
|
|
239
|
+
gobj_stop(priv.gobj_timer);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/***************************************************************
|
|
244
|
+
* Framework Method: Destroy
|
|
245
|
+
***************************************************************/
|
|
246
|
+
function mt_destroy(gobj)
|
|
247
|
+
{
|
|
248
|
+
let priv = gobj.priv;
|
|
249
|
+
|
|
250
|
+
yui_shell_unwatch_changes(gobj);
|
|
251
|
+
if(priv.lang_shell) {
|
|
252
|
+
if(!gobj_is_destroying(priv.lang_shell)) {
|
|
253
|
+
gobj_unsubscribe_event(priv.lang_shell, "EV_LANGUAGE_CHANGED", {}, gobj);
|
|
254
|
+
}
|
|
255
|
+
priv.lang_shell = null;
|
|
256
|
+
}
|
|
257
|
+
remove_bar(gobj);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
/***************************
|
|
264
|
+
* Local Methods
|
|
265
|
+
***************************/
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
/************************************************************
|
|
271
|
+
* Add a DOM listener, kept to be removed in mt_stop.
|
|
272
|
+
************************************************************/
|
|
273
|
+
function listen_dom(gobj, target, type, fn)
|
|
274
|
+
{
|
|
275
|
+
target.addEventListener(type, fn);
|
|
276
|
+
gobj.priv.dom_handlers.push([target, type, fn]);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/************************************************************
|
|
280
|
+
* Hear the language of the shell that holds the bar -- the
|
|
281
|
+
* bar outlives a shell, so it follows them.
|
|
282
|
+
************************************************************/
|
|
283
|
+
function follow_language(gobj)
|
|
284
|
+
{
|
|
285
|
+
let priv = gobj.priv;
|
|
286
|
+
|
|
287
|
+
let shell = yui_shell_of(gobj);
|
|
288
|
+
if(shell && gobj_is_destroying(shell)) {
|
|
289
|
+
shell = null;
|
|
290
|
+
}
|
|
291
|
+
if(shell === priv.lang_shell) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if(priv.lang_shell && !gobj_is_destroying(priv.lang_shell)) {
|
|
295
|
+
gobj_unsubscribe_event(priv.lang_shell, "EV_LANGUAGE_CHANGED", {}, gobj);
|
|
296
|
+
}
|
|
297
|
+
priv.lang_shell = null;
|
|
298
|
+
if(shell) {
|
|
299
|
+
gobj_subscribe_event(shell, "EV_LANGUAGE_CHANGED", {}, gobj);
|
|
300
|
+
priv.lang_shell = shell;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/************************************************************
|
|
305
|
+
* Where the bar goes: the host's `$parent`, else the toasts'
|
|
306
|
+
* layer of the live shell, else the body.
|
|
307
|
+
************************************************************/
|
|
308
|
+
function bar_place(gobj)
|
|
309
|
+
{
|
|
310
|
+
let $parent = gobj_read_pointer_attr(gobj, "$parent");
|
|
311
|
+
if($parent) {
|
|
312
|
+
return $parent;
|
|
313
|
+
}
|
|
314
|
+
let shell = yui_shell_of(gobj);
|
|
315
|
+
if(shell && gobj_is_destroying(shell)) {
|
|
316
|
+
shell = null;
|
|
317
|
+
}
|
|
318
|
+
return yui_shell_notification_layer(shell) || document.body;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/************************************************************
|
|
322
|
+
* Show the bar for the deployed build, or move it to its
|
|
323
|
+
* place. Not shown for a build the reader dismissed.
|
|
324
|
+
************************************************************/
|
|
325
|
+
function show_bar(gobj)
|
|
326
|
+
{
|
|
327
|
+
let priv = gobj.priv;
|
|
328
|
+
|
|
329
|
+
if(!gobj_read_bool_attr(gobj, "with_bar")) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if(!priv.deployed || priv.deployed.stamp === priv.dismissed) {
|
|
333
|
+
remove_bar(gobj);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
remove_bar(gobj);
|
|
338
|
+
priv.$bar = createElement2(
|
|
339
|
+
["div", {class: `C_YUI_UPDATE_WATCH UPDATE_BAR notification ${yui_tint("info")}`, role: "status"}, [
|
|
340
|
+
["span", {class: "UPDATE_TEXT", i18n: "new version available"},
|
|
341
|
+
t("new version available")],
|
|
342
|
+
["span", {class: "UPDATE_VERSION"}, text_node(priv.deployed.version)],
|
|
343
|
+
["button", {class: "UPDATE_RELOAD button is-info", type: "button",
|
|
344
|
+
i18n: "reload",
|
|
345
|
+
title: t("reload"), "data-i18n-title": "reload",
|
|
346
|
+
"aria-label": t("reload"), "data-i18n-aria-label": "reload"},
|
|
347
|
+
t("reload"),
|
|
348
|
+
{click: () => gobj_send_event(gobj, "EV_RELOAD", {}, gobj)}],
|
|
349
|
+
["button", {class: "UPDATE_DISMISS delete", type: "button",
|
|
350
|
+
title: t("close"), "data-i18n-title": "close",
|
|
351
|
+
"aria-label": t("close"), "data-i18n-aria-label": "close"},
|
|
352
|
+
null,
|
|
353
|
+
{click: () => gobj_send_event(gobj, "EV_DISMISS", {}, gobj)}]
|
|
354
|
+
]]
|
|
355
|
+
);
|
|
356
|
+
bar_place(gobj).appendChild(priv.$bar);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/************************************************************
|
|
360
|
+
* Take the bar off the page.
|
|
361
|
+
************************************************************/
|
|
362
|
+
function remove_bar(gobj)
|
|
363
|
+
{
|
|
364
|
+
let priv = gobj.priv;
|
|
365
|
+
|
|
366
|
+
if(priv.$bar && priv.$bar.parentNode) {
|
|
367
|
+
priv.$bar.parentNode.removeChild(priv.$bar);
|
|
368
|
+
}
|
|
369
|
+
priv.$bar = null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/************************************************************
|
|
373
|
+
* Ask the server which build it hands out. The answer comes
|
|
374
|
+
* back as an event (a resolved promise is an OS notification).
|
|
375
|
+
************************************************************/
|
|
376
|
+
function fetch_deployed_build(gobj)
|
|
377
|
+
{
|
|
378
|
+
let priv = gobj.priv;
|
|
379
|
+
|
|
380
|
+
priv.in_flight = true;
|
|
381
|
+
priv.last_check = performance.now();
|
|
382
|
+
|
|
383
|
+
/* no-store, or the check would be answered from the very cache
|
|
384
|
+
* it has to see past. */
|
|
385
|
+
fetch(build_page_url(window.location), {cache: "no-store", credentials: "same-origin"})
|
|
386
|
+
.then((response) => {
|
|
387
|
+
if(!response.ok) {
|
|
388
|
+
throw {status: response.status};
|
|
389
|
+
}
|
|
390
|
+
return response.text();
|
|
391
|
+
})
|
|
392
|
+
.then((html) => {
|
|
393
|
+
if(gobj_is_destroying(gobj)) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
let deployed = parse_build(html);
|
|
397
|
+
if(!deployed) {
|
|
398
|
+
gobj_send_event(gobj, "EV_CHECK_FAILED", {
|
|
399
|
+
reason: "the page served carries no build stamp", network: false
|
|
400
|
+
}, gobj);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
gobj_send_event(gobj, "EV_DEPLOYED_BUILD", deployed, gobj);
|
|
404
|
+
})
|
|
405
|
+
.catch((err) => {
|
|
406
|
+
if(gobj_is_destroying(gobj)) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
let http = err && err.status;
|
|
410
|
+
gobj_send_event(gobj, "EV_CHECK_FAILED", {
|
|
411
|
+
reason: http ? `HTTP ${err.status}` : String((err && err.message) || err),
|
|
412
|
+
network: !http
|
|
413
|
+
}, gobj);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
/***************************
|
|
421
|
+
* Actions
|
|
422
|
+
***************************/
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
/************************************************************
|
|
428
|
+
* Ask the server, unless a question is out or the last one
|
|
429
|
+
* is too recent (a focus and a visibilitychange come
|
|
430
|
+
* together), or the browser says it is offline -- false is
|
|
431
|
+
* the one answer of navigator.onLine that can be trusted.
|
|
432
|
+
************************************************************/
|
|
433
|
+
function ac_check_build(gobj, event, kw, src)
|
|
434
|
+
{
|
|
435
|
+
let priv = gobj.priv;
|
|
436
|
+
|
|
437
|
+
if(!priv.running) {
|
|
438
|
+
return 0; // a page with no stamp watches nothing (logged in mt_start)
|
|
439
|
+
}
|
|
440
|
+
if(priv.in_flight) {
|
|
441
|
+
return 0;
|
|
442
|
+
}
|
|
443
|
+
if(!kw.force) {
|
|
444
|
+
let min_interval = gobj_read_integer_attr(gobj, "min_interval_ms");
|
|
445
|
+
if(priv.last_check && performance.now() - priv.last_check < min_interval) {
|
|
446
|
+
return 0;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if(navigator.onLine === false) {
|
|
450
|
+
return 0;
|
|
451
|
+
}
|
|
452
|
+
fetch_deployed_build(gobj);
|
|
453
|
+
return 0;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/************************************************************
|
|
457
|
+
* EV_TIMEOUT_PERIODIC -- the screen that never gets the
|
|
458
|
+
* focus back asks too.
|
|
459
|
+
************************************************************/
|
|
460
|
+
function ac_periodic(gobj, event, kw, src)
|
|
461
|
+
{
|
|
462
|
+
gobj_send_event(gobj, "EV_CHECK_BUILD", {}, gobj);
|
|
463
|
+
return 0;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/************************************************************
|
|
467
|
+
* The server answered with a build.
|
|
468
|
+
************************************************************/
|
|
469
|
+
function ac_deployed_build(gobj, event, kw, src)
|
|
470
|
+
{
|
|
471
|
+
let priv = gobj.priv;
|
|
472
|
+
|
|
473
|
+
priv.in_flight = false;
|
|
474
|
+
if(priv.failing) {
|
|
475
|
+
log_info(`${gobj_short_name(gobj)}: the deployed build can be read again`);
|
|
476
|
+
priv.failing = false;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
let deployed = {stamp: kw.stamp, version: kw.version || ""};
|
|
480
|
+
|
|
481
|
+
if(deployed.stamp === priv.running.stamp) {
|
|
482
|
+
if(gobj_current_state(gobj) === "ST_STALE") {
|
|
483
|
+
priv.deployed = null;
|
|
484
|
+
remove_bar(gobj);
|
|
485
|
+
gobj_change_state(gobj, "ST_CURRENT");
|
|
486
|
+
gobj_publish_event(gobj, "EV_BUILD_CURRENT", {running: priv.running.stamp});
|
|
487
|
+
}
|
|
488
|
+
return 0;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if(priv.deployed && priv.deployed.stamp === deployed.stamp) {
|
|
492
|
+
return 0; // the same newer build, already said
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
priv.deployed = deployed;
|
|
496
|
+
log_info(
|
|
497
|
+
`${gobj_short_name(gobj)}: a new build is deployed: ` +
|
|
498
|
+
`${deployed.version} ${deployed.stamp} (this page runs ` +
|
|
499
|
+
`${priv.running.version} ${priv.running.stamp})`
|
|
500
|
+
);
|
|
501
|
+
gobj_change_state(gobj, "ST_STALE");
|
|
502
|
+
show_bar(gobj);
|
|
503
|
+
gobj_publish_event(gobj, "EV_NEW_BUILD", {
|
|
504
|
+
running: priv.running.stamp,
|
|
505
|
+
deployed: deployed.stamp,
|
|
506
|
+
version: deployed.version
|
|
507
|
+
});
|
|
508
|
+
return 0;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/************************************************************
|
|
512
|
+
* The server did not say. Offline is a normal state, so a
|
|
513
|
+
* network failure is info; an answer that is not the app
|
|
514
|
+
* (an HTTP error, a page with no stamp) is a warning. Both
|
|
515
|
+
* are said on the transition only, not on every attempt.
|
|
516
|
+
************************************************************/
|
|
517
|
+
function ac_check_failed(gobj, event, kw, src)
|
|
518
|
+
{
|
|
519
|
+
let priv = gobj.priv;
|
|
520
|
+
|
|
521
|
+
priv.in_flight = false;
|
|
522
|
+
if(priv.failing) {
|
|
523
|
+
return 0;
|
|
524
|
+
}
|
|
525
|
+
priv.failing = true;
|
|
526
|
+
let msg = `${gobj_short_name(gobj)}: cannot read the deployed build: ${kw.reason}`;
|
|
527
|
+
if(kw.network) {
|
|
528
|
+
log_info(msg);
|
|
529
|
+
} else {
|
|
530
|
+
log_warning(msg);
|
|
531
|
+
}
|
|
532
|
+
return 0;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/************************************************************
|
|
536
|
+
* The bar's reload button. The host may veto it (unsaved
|
|
537
|
+
* edits): its answer comes back as an event.
|
|
538
|
+
************************************************************/
|
|
539
|
+
function ac_reload(gobj, event, kw, src)
|
|
540
|
+
{
|
|
541
|
+
let before_reload = gobj_read_pointer_attr(gobj, "before_reload");
|
|
542
|
+
if(typeof before_reload !== "function") {
|
|
543
|
+
gobj_send_event(gobj, "EV_RELOAD_ALLOWED", {}, gobj);
|
|
544
|
+
return 0;
|
|
545
|
+
}
|
|
546
|
+
Promise.resolve()
|
|
547
|
+
.then(() => before_reload())
|
|
548
|
+
.then(
|
|
549
|
+
(allowed) => {
|
|
550
|
+
if(gobj_is_destroying(gobj)) {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if(allowed === false) {
|
|
554
|
+
log_info(`${gobj_short_name(gobj)}: the host vetoed the reload`);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
gobj_send_event(gobj, "EV_RELOAD_ALLOWED", {}, gobj);
|
|
558
|
+
},
|
|
559
|
+
(err) => {
|
|
560
|
+
log_error(`${gobj_short_name(gobj)}: before_reload() failed: ${(err && err.message) || err}`);
|
|
561
|
+
}
|
|
562
|
+
);
|
|
563
|
+
return 0;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/************************************************************
|
|
567
|
+
* Load the deployed build. The hash stays: the reader comes
|
|
568
|
+
* back to the same route.
|
|
569
|
+
************************************************************/
|
|
570
|
+
function ac_reload_allowed(gobj, event, kw, src)
|
|
571
|
+
{
|
|
572
|
+
window.location.reload();
|
|
573
|
+
return 0;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/************************************************************
|
|
577
|
+
* The bar's ✕: hidden for this build; a later deploy shows
|
|
578
|
+
* it again.
|
|
579
|
+
************************************************************/
|
|
580
|
+
function ac_dismiss(gobj, event, kw, src)
|
|
581
|
+
{
|
|
582
|
+
let priv = gobj.priv;
|
|
583
|
+
|
|
584
|
+
priv.dismissed = priv.deployed ? priv.deployed.stamp : "";
|
|
585
|
+
remove_bar(gobj);
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/************************************************************
|
|
590
|
+
* EV_SHELL_CHANGED -- a shell was built, or is being
|
|
591
|
+
* destroyed: the bar's place may have gone with it, or be in
|
|
592
|
+
* the new one. Its language is the new shell's too.
|
|
593
|
+
************************************************************/
|
|
594
|
+
function ac_shell_changed(gobj, event, kw, src)
|
|
595
|
+
{
|
|
596
|
+
let priv = gobj.priv;
|
|
597
|
+
|
|
598
|
+
follow_language(gobj);
|
|
599
|
+
if(priv.$bar) {
|
|
600
|
+
show_bar(gobj);
|
|
601
|
+
}
|
|
602
|
+
return 0;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/************************************************************
|
|
606
|
+
* EV_LANGUAGE_CHANGED -- from the shell that holds the bar.
|
|
607
|
+
* Every text of the bar carries its key; it may live outside
|
|
608
|
+
* the shell's container, where the app's refresh does not
|
|
609
|
+
* reach.
|
|
610
|
+
************************************************************/
|
|
611
|
+
function ac_language_changed(gobj, event, kw, src)
|
|
612
|
+
{
|
|
613
|
+
let priv = gobj.priv;
|
|
614
|
+
|
|
615
|
+
if(priv.$bar) {
|
|
616
|
+
refresh_language(priv.$bar, t);
|
|
617
|
+
}
|
|
618
|
+
return 0;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
/***************************
|
|
625
|
+
* FSM
|
|
626
|
+
***************************/
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
/*---------------------------------------------*
|
|
632
|
+
* Global methods table
|
|
633
|
+
*---------------------------------------------*/
|
|
634
|
+
const gmt = {
|
|
635
|
+
mt_create: mt_create,
|
|
636
|
+
mt_start: mt_start,
|
|
637
|
+
mt_stop: mt_stop,
|
|
638
|
+
mt_destroy: mt_destroy
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
/***************************************************************
|
|
642
|
+
* Create the GClass
|
|
643
|
+
***************************************************************/
|
|
644
|
+
function create_gclass(gclass_name)
|
|
645
|
+
{
|
|
646
|
+
if(__gclass__) {
|
|
647
|
+
log_error(`GClass ALREADY created: ${gclass_name}`);
|
|
648
|
+
return -1;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/*---------------------------------------------*
|
|
652
|
+
* States
|
|
653
|
+
*---------------------------------------------*/
|
|
654
|
+
const states = [
|
|
655
|
+
["ST_CURRENT", [
|
|
656
|
+
["EV_CHECK_BUILD", ac_check_build, null],
|
|
657
|
+
["EV_TIMEOUT_PERIODIC", ac_periodic, null],
|
|
658
|
+
["EV_DEPLOYED_BUILD", ac_deployed_build, null],
|
|
659
|
+
["EV_CHECK_FAILED", ac_check_failed, null],
|
|
660
|
+
["EV_SHELL_CHANGED", ac_shell_changed, null],
|
|
661
|
+
["EV_LANGUAGE_CHANGED", ac_language_changed, null]
|
|
662
|
+
]],
|
|
663
|
+
["ST_STALE", [
|
|
664
|
+
["EV_CHECK_BUILD", ac_check_build, null],
|
|
665
|
+
["EV_TIMEOUT_PERIODIC", ac_periodic, null],
|
|
666
|
+
["EV_DEPLOYED_BUILD", ac_deployed_build, null],
|
|
667
|
+
["EV_CHECK_FAILED", ac_check_failed, null],
|
|
668
|
+
["EV_RELOAD", ac_reload, null],
|
|
669
|
+
["EV_RELOAD_ALLOWED", ac_reload_allowed, null],
|
|
670
|
+
["EV_DISMISS", ac_dismiss, null],
|
|
671
|
+
["EV_SHELL_CHANGED", ac_shell_changed, null],
|
|
672
|
+
["EV_LANGUAGE_CHANGED", ac_language_changed, null]
|
|
673
|
+
]]
|
|
674
|
+
];
|
|
675
|
+
|
|
676
|
+
/*---------------------------------------------*
|
|
677
|
+
* Events
|
|
678
|
+
*---------------------------------------------*/
|
|
679
|
+
const event_types = [
|
|
680
|
+
["EV_CHECK_BUILD", 0],
|
|
681
|
+
["EV_TIMEOUT_PERIODIC", 0],
|
|
682
|
+
["EV_DEPLOYED_BUILD", 0],
|
|
683
|
+
["EV_CHECK_FAILED", 0],
|
|
684
|
+
["EV_RELOAD", 0],
|
|
685
|
+
["EV_RELOAD_ALLOWED", 0],
|
|
686
|
+
["EV_DISMISS", 0],
|
|
687
|
+
["EV_SHELL_CHANGED", 0],
|
|
688
|
+
["EV_LANGUAGE_CHANGED", 0],
|
|
689
|
+
["EV_NEW_BUILD", event_flag_t.EVF_OUTPUT_EVENT|event_flag_t.EVF_NO_WARN_SUBS],
|
|
690
|
+
["EV_BUILD_CURRENT", event_flag_t.EVF_OUTPUT_EVENT|event_flag_t.EVF_NO_WARN_SUBS]
|
|
691
|
+
];
|
|
692
|
+
|
|
693
|
+
__gclass__ = gclass_create(
|
|
694
|
+
gclass_name,
|
|
695
|
+
event_types,
|
|
696
|
+
states,
|
|
697
|
+
gmt,
|
|
698
|
+
0, // lmt,
|
|
699
|
+
attrs_table,
|
|
700
|
+
PRIVATE_DATA,
|
|
701
|
+
0, // authz_table,
|
|
702
|
+
0, // command_table,
|
|
703
|
+
0, // s_user_trace_level
|
|
704
|
+
0 // gclass_flag
|
|
705
|
+
);
|
|
706
|
+
|
|
707
|
+
if(!__gclass__) {
|
|
708
|
+
return -1;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
return 0;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/***************************************************************
|
|
715
|
+
* Register GClass
|
|
716
|
+
***************************************************************/
|
|
717
|
+
function register_c_yui_update_watch()
|
|
718
|
+
{
|
|
719
|
+
return create_gclass(GCLASS_NAME);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
export { register_c_yui_update_watch };
|