@yuneta/gobj-ui 7.27.28 → 7.27.30

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