@lango-project/lango-client 0.4.1 → 0.4.2

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.
Files changed (65) hide show
  1. package/README.md +26 -0
  2. package/cli/.lango/runtime.mjs +16 -0
  3. package/dist/.lango/runtime.mjs +16 -0
  4. package/dist/accordion.mjs +9 -8
  5. package/dist/accordion.mjs.map +1 -1
  6. package/dist/activities.mjs +8 -7
  7. package/dist/activities.mjs.map +1 -1
  8. package/dist/app.mjs +465 -50
  9. package/dist/app.mjs.map +1 -1
  10. package/dist/boot.mjs +3 -2
  11. package/dist/boot.mjs.map +1 -1
  12. package/dist/button.mjs +8 -7
  13. package/dist/button.mjs.map +1 -1
  14. package/dist/chip.mjs +5 -4
  15. package/dist/chip.mjs.map +1 -1
  16. package/dist/combo_box.mjs +15 -14
  17. package/dist/combo_box.mjs.map +1 -1
  18. package/dist/content.mjs +9 -8
  19. package/dist/content.mjs.map +1 -1
  20. package/dist/date_input.mjs +3 -2
  21. package/dist/date_input.mjs.map +1 -1
  22. package/dist/error_reporter.mjs +6 -5
  23. package/dist/error_reporter.mjs.map +1 -1
  24. package/dist/fields.mjs +9 -8
  25. package/dist/fields.mjs.map +1 -1
  26. package/dist/file_input.mjs +19 -18
  27. package/dist/file_input.mjs.map +1 -1
  28. package/dist/form.mjs +14 -2
  29. package/dist/form.mjs.map +1 -1
  30. package/dist/heartbeat.mjs +12 -11
  31. package/dist/heartbeat.mjs.map +1 -1
  32. package/dist/input_changes.mjs +5 -4
  33. package/dist/input_changes.mjs.map +1 -1
  34. package/dist/longpoll.mjs +12 -11
  35. package/dist/longpoll.mjs.map +1 -1
  36. package/dist/main.mjs +35 -18
  37. package/dist/main.mjs.map +1 -1
  38. package/dist/menus.mjs +19 -18
  39. package/dist/menus.mjs.map +1 -1
  40. package/dist/navigation.mjs +7 -6
  41. package/dist/navigation.mjs.map +1 -1
  42. package/dist/numeric_input.mjs +9 -8
  43. package/dist/numeric_input.mjs.map +1 -1
  44. package/dist/otp_input.mjs +5 -4
  45. package/dist/otp_input.mjs.map +1 -1
  46. package/dist/presentations.mjs +17 -16
  47. package/dist/presentations.mjs.map +1 -1
  48. package/dist/renderer.mjs +74 -0
  49. package/dist/renderer.mjs.map +1 -1
  50. package/dist/table_scope.mjs +3 -2
  51. package/dist/table_scope.mjs.map +1 -1
  52. package/dist/tables.mjs +11 -10
  53. package/dist/tables.mjs.map +1 -1
  54. package/dist/tabs.mjs +7 -6
  55. package/dist/tabs.mjs.map +1 -1
  56. package/dist/vendor/dompurify.mjs +267 -181
  57. package/dist/vendor/purify.es.mjs.map +1 -1
  58. package/dist/widgets.mjs +5 -4
  59. package/dist/widgets.mjs.map +1 -1
  60. package/package.json +4 -3
  61. package/src/app.lango +325 -11
  62. package/src/form.lango +12 -2
  63. package/src/heartbeat.lango +2 -2
  64. package/src/main.lango +15 -0
  65. package/src/renderer.lango +61 -0
package/dist/app.mjs CHANGED
@@ -3,6 +3,7 @@ import { Heartbeat } from "./heartbeat.mjs";
3
3
  import { ErrorReporter } from "./error_reporter.mjs";
4
4
  import {
5
5
  hostGlobal as __hostGlobal,
6
+ bindContexts as __bindContexts,
6
7
  fromJson as __fromJson,
7
8
  toJson as __toJson,
8
9
  getClass as __getClass,
@@ -13,8 +14,10 @@ import {
13
14
  isObject as __isObject,
14
15
  isArray as __isArray,
15
16
  add as __add,
17
+ clear as __clear,
16
18
  requireBoolean as __requireBoolean,
17
19
  normalize as __normalize,
20
+ numericAdd as __numericAdd,
18
21
  call as __call,
19
22
  boundary as __boundary,
20
23
  requireArity as __requireArity,
@@ -35,6 +38,12 @@ export class App {
35
38
  #events;
36
39
  #template;
37
40
  #pingJob;
41
+ #login;
42
+ #committingAuthentication;
43
+ #authVersion;
44
+ #authChanges;
45
+ #externalStale;
46
+ #storageChange;
38
47
  constructor(host, renderer) {
39
48
  __requireArity(arguments.length, [0, 1], "constructor");
40
49
  host = __boundary(host);
@@ -52,6 +61,20 @@ export class App {
52
61
  this.#pingJob = null;
53
62
  this.resources = [];
54
63
  this.reporter = null;
64
+ this.#login = null;
65
+ this.#committingAuthentication = false;
66
+ this.#authVersion = 0;
67
+ this.#authChanges = [];
68
+ this.#externalStale = false;
69
+ this.#storageChange = null;
70
+ try {
71
+ this.#storageChange = __normalize(this.#host.storage.getItem("lango.authChange"));
72
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(this.#storageChange)))) || __requireBoolean(!__requireBoolean(__normalize(this.#storageChange.startsWith("v1:")))))) {
73
+ this.#storageChange = null;
74
+ }
75
+ } catch (problem) {
76
+ this.#storageChange = null;
77
+ }
55
78
  }
56
79
  now() {
57
80
  if (__requireBoolean(__normalize(this.#host.now) !== null)) {
@@ -113,8 +136,22 @@ export class App {
113
136
  saveToken(token) {
114
137
  __requireArity(arguments.length, [0], "saveToken");
115
138
  token = __boundary(token);
139
+ if (__requireBoolean(__normalize(this.#externalStale))) {
140
+ return false;
141
+ }
116
142
  try {
117
- __normalize(this.#host.storage.setItem("lango.token", token));
143
+ let storage = __normalize(this.#host.storage);
144
+ if (__requireBoolean(__normalize(storage.getItem) !== null)) {
145
+ let current = __normalize(storage.getItem("lango.authChange"));
146
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(this.#committingAuthentication))) && __requireBoolean(__isString(current))) && __requireBoolean(__normalize(current.startsWith("v1:")))) && __requireBoolean(current !== __normalize(this.#storageChange)))) {
147
+ this.externalAuthentication(current);
148
+ return false;
149
+ }
150
+ }
151
+ if (__requireBoolean(__normalize(this.#storageChange) !== null)) {
152
+ __normalize(storage.setItem(`lango.token:${__normalize(this.#storageChange)}`, token));
153
+ }
154
+ __normalize(storage.setItem("lango.token", token));
118
155
  } catch (error) {
119
156
  return null;
120
157
  }
@@ -124,6 +161,9 @@ export class App {
124
161
  clearSavedToken() {
125
162
  try {
126
163
  __normalize(this.#host.storage.removeItem("lango.token"));
164
+ if (__requireBoolean(__normalize(this.#storageChange) !== null)) {
165
+ __normalize(this.#host.storage.removeItem(`lango.token:${__normalize(this.#storageChange)}`));
166
+ }
127
167
  return true;
128
168
  } catch (error) {
129
169
  return false;
@@ -147,7 +187,7 @@ export class App {
147
187
  return this.request(url, input, token, signal, true);
148
188
  return null;
149
189
  }
150
- request(url, input, token, signal = null, channel = false, opening = false) {
190
+ request(url, input, token, signal = null, channel = false, opening = false, recover = false, expected = null) {
151
191
  __requireArity(arguments.length, [0, 1, 2], "request");
152
192
  url = __boundary(url);
153
193
  input = __boundary(input);
@@ -155,6 +195,8 @@ export class App {
155
195
  signal = __boundary(signal);
156
196
  channel = __boundary(channel);
157
197
  opening = __boundary(opening);
198
+ recover = __boundary(recover);
199
+ expected = __boundary(expected);
158
200
  let target = this.checkUrl(url);
159
201
  let headers = { "Content-Type": "application/json" };
160
202
  headers["X-Lango-Screen"] = "wide";
@@ -164,13 +206,26 @@ export class App {
164
206
  if (__requireBoolean(token !== null)) {
165
207
  headers.Authorization = `Bearer ${token}`;
166
208
  }
209
+ if (__requireBoolean(expected !== null)) {
210
+ headers["X-Lango-Expected-User"] = __toJson(expected);
211
+ }
167
212
  let options = { method: "POST", headers: headers, body: __toJson(input) };
168
213
  if (__requireBoolean(signal !== null)) {
169
214
  options.signal = signal;
170
215
  }
171
- return __normalize(this.#host.fetch(`${__normalize(target.pathname)}${__normalize(target.search)}`, options)).then(__withArity((res) => {
216
+ return __normalize(this.#host.fetch(`${__normalize(target.pathname)}${__normalize(target.search)}`, options)).then(__withArity(__bindContexts((res) => {
172
217
  res = __boundary(res);
173
218
  if (__requireBoolean(!__requireBoolean(__normalize(res.ok)))) {
219
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(recover) && __requireBoolean(__requireBoolean(__normalize(res.status) === 401) || __requireBoolean(__normalize(res.status) === 403))) && __requireBoolean(__normalize(res.headers) !== null)) && __requireBoolean(__normalize(res.headers.get("x-lango-need-login")) === "1"))) {
220
+ return __normalize(res.json()).then(__withArity(__bindContexts((body) => {
221
+ body = __boundary(body);
222
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(body))) || __requireBoolean(!__requireBoolean(__isObject(__normalize(body.needLogin))))) || __requireBoolean(__requireBoolean(__normalize(body.error) !== "LoginRequired") && __requireBoolean(__normalize(body.error) !== "ReauthenticationRequired"))) || __requireBoolean(body.needLogin.reason !== __normalize(body.error)))) {
223
+ throw "Invalid login challenge";
224
+ }
225
+ return { needLogin: __normalize(body.needLogin) };
226
+ return null;
227
+ }), [0], "closure"));
228
+ }
174
229
  if (__requireBoolean(__requireBoolean(__requireBoolean(opening) && __requireBoolean(__normalize(res.status) === 401)) && __requireBoolean(token !== null))) {
175
230
  if (__requireBoolean(this.clearSavedToken())) {
176
231
  throw new (__hostGlobal(["Error"]))("Startup HTTP 401: saved token was rejected and cleared. Reload the page to start a new session.");
@@ -184,7 +239,7 @@ export class App {
184
239
  return { status: __normalize(res.status), retryAfter: __normalize(res.headers.get("retry-after")) };
185
240
  }
186
241
  }
187
- return __normalize(res.json()).then(__withArity((body) => {
242
+ return __normalize(res.json()).then(__withArity(__bindContexts((body) => {
188
243
  body = __boundary(body);
189
244
  if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(body))) || __requireBoolean(__isArray(body)))) {
190
245
  if (__requireBoolean(channel)) {
@@ -196,49 +251,74 @@ export class App {
196
251
  if (__requireBoolean(status === null)) {
197
252
  status = 200;
198
253
  }
199
- return { status: status, token: __normalize(res.headers.get("x-lango-token")), body: body };
254
+ return { authChange: __normalize(res.headers.get("x-lango-auth-change")), status: status, token: __normalize(res.headers.get("x-lango-token")), body: body };
200
255
  return null;
201
- }, [0], "closure")).catch(__withArity((error) => {
256
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
202
257
  error = __boundary(error);
203
258
  if (__requireBoolean(channel)) {
204
259
  throw { code: "InvalidPollResponse" };
205
260
  }
206
261
  throw error;
207
262
  return null;
208
- }, [0], "closure"));
263
+ }), [0], "closure"));
209
264
  return null;
210
- }, [0], "closure"));
265
+ }), [0], "closure"));
211
266
  return null;
212
267
  }
213
268
  start() {
214
269
  let token = null;
215
270
  try {
216
- token = __normalize(this.#host.storage.getItem("lango.token"));
271
+ this.#storageChange = __normalize(this.#host.storage.getItem("lango.authChange"));
272
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(this.#storageChange)))) || __requireBoolean(!__requireBoolean(__normalize(this.#storageChange.startsWith("v1:")))))) {
273
+ this.#storageChange = null;
274
+ }
275
+ token = null;
276
+ if (__requireBoolean(__normalize(this.#storageChange) !== null)) {
277
+ token = __normalize(this.#host.storage.getItem(`lango.token:${__normalize(this.#storageChange)}`));
278
+ }
279
+ if (__requireBoolean(token === null)) {
280
+ token = __normalize(this.#host.storage.getItem("lango.token"));
281
+ }
217
282
  } catch (error) {
218
283
  token = null;
219
284
  }
220
- return this.request("/", { }, token, null, false, true).then(__withArity((packet) => {
285
+ return this.request("/", { }, token, null, false, true).then(__withArity(__bindContexts((packet) => {
221
286
  packet = __boundary(packet);
222
- this.#events = __normalize(packet.body);
287
+ this.acceptEvents(__normalize(packet.body));
223
288
  if (__requireBoolean(__normalize(this.#events.clientErrorUrl) !== null)) {
224
289
  this.reporter = new ErrorReporter(this, __normalize(this.#events.clientErrorUrl));
225
290
  }
226
291
  if (__requireBoolean(__normalize(this.#events.pingUrl) !== null)) {
227
292
  this.heartbeat = new Heartbeat(this, __normalize(this.#host), __normalize(this.#events.pingUrl));
228
293
  }
294
+ if (__requireBoolean(__normalize(this.#externalStale))) {
295
+ if (__requireBoolean(__normalize(this.root) === null)) {
296
+ this.root = new Form(this);
297
+ this.root.element = __normalize(this.#host.mount);
298
+ }
299
+ this.invalidateForm(__normalize(this.root));
300
+ return null;
301
+ }
229
302
  this.root = new Form(this, null, __normalize(packet.token));
230
303
  this.root.element = __normalize(this.#host.mount);
231
304
  __normalize(this.root.setToken(__normalize(packet.token)));
232
- return this.navigate(__normalize(this.#host.location.pathname), false).then(__withArity((ignored) => {
305
+ return this.navigate(__normalize(this.#host.location.pathname), false).then(__withArity(__bindContexts((ignored) => {
233
306
  ignored = __boundary(ignored);
234
307
  if (__requireBoolean(__normalize(this.heartbeat) !== null)) {
235
308
  __normalize(this.heartbeat.start());
236
309
  }
237
310
  return null;
238
311
  return null;
239
- }, [0], "closure"));
312
+ }), [0], "closure"));
313
+ return null;
314
+ }), [0], "closure")).catch(__withArity(__bindContexts((problem) => {
315
+ problem = __boundary(problem);
316
+ if (__requireBoolean(__normalize(this.#externalStale))) {
317
+ return null;
318
+ }
319
+ throw problem;
240
320
  return null;
241
- }, [0], "closure"));
321
+ }), [0], "closure"));
242
322
  return null;
243
323
  }
244
324
  send(form, url, owner = null) {
@@ -249,13 +329,16 @@ export class App {
249
329
  if (__requireBoolean(__requireBoolean(__normalize(this.#disposed)) || __requireBoolean(__normalize(form.disposed)))) {
250
330
  return __normalize(__hostGlobal(["Promise"]).reject("Form is disposed"));
251
331
  }
252
- let pending = new (__hostGlobal(["Promise"]))(__withArity((resolve, reject) => {
332
+ if (__requireBoolean(__requireBoolean(__normalize(form.stale)) || __requireBoolean(__normalize(this.#externalStale)))) {
333
+ return __normalize(__hostGlobal(["Promise"]).reject("SessionChanged: reload the page"));
334
+ }
335
+ let pending = new (__hostGlobal(["Promise"]))(__withArity(__bindContexts((resolve, reject) => {
253
336
  resolve = __boundary(resolve);
254
337
  reject = __boundary(reject);
255
- __add(this.#queue, { kind: "send", form: form, url: url, owner: owner, resolve: resolve, reject: reject });
338
+ __add(this.#queue, { authVersion: __normalize(this.#authVersion), kind: "send", form: form, url: url, owner: owner, resolve: resolve, reject: reject });
256
339
  this.drain();
257
340
  return null;
258
- }, [0, 1], "closure"));
341
+ }), [0, 1], "closure"));
259
342
  return pending;
260
343
  return null;
261
344
  }
@@ -269,7 +352,7 @@ export class App {
269
352
  return __normalize(__hostGlobal(["Promise"]).resolve(null));
270
353
  }
271
354
  let deferred = __normalize(__hostGlobal(["Promise"]).withResolvers());
272
- __add(this.#queue, { kind: "delivery", form: form, action: action, owner: owner, generation: generation, resolve: __normalize(deferred.resolve), reject: __normalize(deferred.reject) });
355
+ __add(this.#queue, { authVersion: __normalize(this.#authVersion), kind: "delivery", form: form, action: action, owner: owner, generation: generation, resolve: __normalize(deferred.resolve), reject: __normalize(deferred.reject) });
273
356
  this.drain();
274
357
  return __normalize(deferred.promise);
275
358
  return null;
@@ -284,7 +367,7 @@ export class App {
284
367
  return __normalize(__hostGlobal(["Promise"]).resolve(null));
285
368
  }
286
369
  let deferred = __normalize(__hostGlobal(["Promise"]).withResolvers());
287
- __add(this.#queue, { kind: "report", form: form, url: url, input: input, owner: owner, resolve: __normalize(deferred.resolve), reject: __normalize(deferred.reject) });
370
+ __add(this.#queue, { authVersion: __normalize(this.#authVersion), kind: "report", form: form, url: url, input: input, owner: owner, resolve: __normalize(deferred.resolve), reject: __normalize(deferred.reject) });
288
371
  this.drain();
289
372
  return __normalize(deferred.promise);
290
373
  return null;
@@ -292,7 +375,7 @@ export class App {
292
375
  canPoll(form) {
293
376
  __requireArity(arguments.length, [0], "canPoll");
294
377
  form = __boundary(form);
295
- if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#disposed)) || __requireBoolean(__normalize(form.disposed))) || __requireBoolean(__normalize(this.#pingJob) !== null))) {
378
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#disposed)) || __requireBoolean(__normalize(form.disposed))) || __requireBoolean(__normalize(form.stale))) || __requireBoolean(__normalize(this.#externalStale))) || __requireBoolean(__normalize(this.#pingJob) !== null)) || __requireBoolean(__normalize(this.#login) !== null))) {
296
379
  return false;
297
380
  }
298
381
  for (const job of __normalize(this.#active)) {
@@ -326,7 +409,10 @@ export class App {
326
409
  return null;
327
410
  }
328
411
  drain() {
329
- if (__requireBoolean(__normalize(this.#pingJob) !== null)) {
412
+ if (__requireBoolean(__normalize(this.#disposed))) {
413
+ return null;
414
+ }
415
+ if (__requireBoolean(__requireBoolean(__normalize(this.#pingJob) !== null) && __requireBoolean(__normalize(this.#login) === null))) {
330
416
  if (__requireBoolean(__requireBoolean(__normalize(this.#active.length) === 0) && __requireBoolean(!__requireBoolean(__normalize(this.#pingJob.running))))) {
331
417
  this.#pingJob.running = true;
332
418
  this.executePing(__normalize(this.#pingJob));
@@ -334,6 +420,14 @@ export class App {
334
420
  return null;
335
421
  }
336
422
  for (const job of __normalize(this.#queue.slice())) {
423
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(job.authVersion) !== __normalize(this.#authVersion)) || __requireBoolean(job.form.stale)) || __requireBoolean(__normalize(this.#externalStale)))) {
424
+ __normalize(this.#queue.splice(__normalize(this.#queue.indexOf(job)), 1));
425
+ __normalize(job.reject("SessionChanged: queued request cancelled"));
426
+ continue;
427
+ }
428
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) !== null) && __requireBoolean(!__requireBoolean(__normalize(this.#login.form.related(__normalize(job.form))))))) {
429
+ continue;
430
+ }
337
431
  let blocked = false;
338
432
  for (const running of __normalize(this.#active)) {
339
433
  if (__requireBoolean(__normalize(job.form.related(__normalize(running.form))))) {
@@ -366,7 +460,7 @@ export class App {
366
460
  executeDelivery(job) {
367
461
  __requireArity(arguments.length, [0], "executeDelivery");
368
462
  job = __boundary(job);
369
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity((ignored) => {
463
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
370
464
  ignored = __boundary(ignored);
371
465
  if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(job.form.disposed)) && __requireBoolean(job.owner.active)) && __requireBoolean(job.owner.generation === __normalize(job.generation)))) {
372
466
  let action = __normalize(job.action);
@@ -374,15 +468,18 @@ export class App {
374
468
  }
375
469
  __normalize(job.resolve(null));
376
470
  return null;
377
- }, [0], "closure")).catch(__withArity((error) => {
471
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
378
472
  error = __boundary(error);
379
473
  __normalize(job.reject(error));
380
474
  return null;
381
- }, [0], "closure")).finally(() => {
382
- __normalize(this.#active.splice(__normalize(this.#active.indexOf(job)), 1));
475
+ }), [0], "closure")).finally(__bindContexts(() => {
476
+ let index = __normalize(this.#active.indexOf(job));
477
+ if (__requireBoolean(index >= 0)) {
478
+ __normalize(this.#active.splice(index, 1));
479
+ }
383
480
  this.drain();
384
481
  return null;
385
- });
482
+ }));
386
483
  return null;
387
484
  }
388
485
  ping() {
@@ -402,34 +499,37 @@ export class App {
402
499
  executePing(job) {
403
500
  __requireArity(arguments.length, [0], "executePing");
404
501
  job = __boundary(job);
405
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity((ignored) => {
502
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
406
503
  ignored = __boundary(ignored);
407
504
  return __normalize(this.heartbeat.exchange());
408
505
  return null;
409
- }, [0], "closure")).then(__withArity((ignored) => {
506
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
410
507
  ignored = __boundary(ignored);
411
508
  __normalize(job.resolve(null));
412
509
  return null;
413
- }, [0], "closure")).catch(__withArity((error) => {
510
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
414
511
  error = __boundary(error);
415
512
  __normalize(job.reject(error));
416
513
  return null;
417
- }, [0], "closure")).finally(() => {
514
+ }), [0], "closure")).finally(__bindContexts(() => {
418
515
  this.#pingJob = null;
419
516
  this.drain();
420
517
  return null;
421
- });
518
+ }));
422
519
  return null;
423
520
  }
424
521
  execute(job) {
425
522
  __requireArity(arguments.length, [0], "execute");
426
523
  job = __boundary(job);
427
524
  let form = __normalize(job.form);
428
- return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity((ignored) => {
525
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
429
526
  ignored = __boundary(ignored);
430
527
  if (__requireBoolean(__normalize(form.disposed))) {
431
528
  throw "Form is disposed";
432
529
  }
530
+ if (__requireBoolean(__requireBoolean(__normalize(form.stale)) || __requireBoolean(__normalize(this.#externalStale)))) {
531
+ throw "SessionChanged: reload the page";
532
+ }
433
533
  if (__requireBoolean(__requireBoolean(__normalize(job.owner) !== null) && __requireBoolean(!__requireBoolean(job.owner.active)))) {
434
534
  return null;
435
535
  }
@@ -440,9 +540,9 @@ export class App {
440
540
  __normalize(this.renderer.waiting(form, true));
441
541
  let ready = __normalize(form.prepareValue());
442
542
  if (__requireBoolean(ready === null)) {
443
- return this.post(__normalize(job.url), __normalize(form.getValue()), __normalize(form.token));
543
+ return this.sendInput(job, __fromJson(__toJson(__normalize(form.getValue()))));
444
544
  }
445
- return ready.then(__withArity((ignored) => {
545
+ return ready.then(__withArity(__bindContexts((ignored) => {
446
546
  ignored = __boundary(ignored);
447
547
  if (__requireBoolean(__normalize(form.disposed))) {
448
548
  throw "Form is disposed";
@@ -450,36 +550,45 @@ export class App {
450
550
  if (__requireBoolean(__requireBoolean(__normalize(job.owner) !== null) && __requireBoolean(!__requireBoolean(job.owner.active)))) {
451
551
  return null;
452
552
  }
453
- return this.post(__normalize(job.url), __normalize(form.getValue()), __normalize(form.token));
553
+ return this.sendInput(job, __fromJson(__toJson(__normalize(form.getValue()))));
454
554
  return null;
455
- }, [0], "closure"));
555
+ }), [0], "closure"));
456
556
  return null;
457
- }, [0], "closure")).then(__withArity((packet) => {
557
+ }), [0], "closure")).then(__withArity(__bindContexts((packet) => {
458
558
  packet = __boundary(packet);
559
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(job.authVersion) !== __normalize(this.#authVersion)) || __requireBoolean(__normalize(form.stale))) || __requireBoolean(__normalize(this.#externalStale)))) {
560
+ throw "SessionChanged: response discarded";
561
+ }
459
562
  if (__requireBoolean(__requireBoolean(__requireBoolean(packet !== null) && __requireBoolean(!__requireBoolean(__normalize(form.disposed)))) && __requireBoolean(__requireBoolean(__normalize(job.owner) === null) || __requireBoolean(job.owner.active)))) {
460
563
  if (__requireBoolean(__normalize(job.kind) === "report")) {
461
564
  __normalize(form.setToken(__normalize(packet.token)));
462
565
  } else {
463
566
  __normalize(form.handle(packet));
464
567
  }
568
+ if (__requireBoolean(__requireBoolean(__normalize(form.stale)) || __requireBoolean(__normalize(this.#externalStale)))) {
569
+ throw "SessionChanged: response discarded";
570
+ }
465
571
  }
466
572
  return null;
467
- }, [0], "closure")).then(__withArity((ignored) => {
573
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
468
574
  ignored = __boundary(ignored);
469
575
  __normalize(job.resolve(null));
470
576
  return null;
471
- }, [0], "closure")).catch(__withArity((error) => {
577
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
472
578
  error = __boundary(error);
473
579
  __normalize(job.reject(error));
474
580
  return null;
475
- }, [0], "closure")).finally(() => {
581
+ }), [0], "closure")).finally(__bindContexts(() => {
476
582
  if (__requireBoolean(__normalize(job.kind) === "send")) {
477
583
  __normalize(this.renderer.waiting(form, false));
478
584
  }
479
- __normalize(this.#active.splice(__normalize(this.#active.indexOf(job)), 1));
585
+ let index = __normalize(this.#active.indexOf(job));
586
+ if (__requireBoolean(index >= 0)) {
587
+ __normalize(this.#active.splice(index, 1));
588
+ }
480
589
  this.drain();
481
590
  return null;
482
- });
591
+ }));
483
592
  return null;
484
593
  }
485
594
  navigate(url, push = true) {
@@ -503,7 +612,7 @@ export class App {
503
612
  this.#template = null;
504
613
  }
505
614
  }
506
- return ready.then(__withArity((ignored) => {
615
+ return ready.then(__withArity(__bindContexts((ignored) => {
507
616
  ignored = __boundary(ignored);
508
617
  this.#template = template;
509
618
  let form = __normalize(this.root);
@@ -515,7 +624,7 @@ export class App {
515
624
  }
516
625
  return __normalize(form.send(path));
517
626
  return null;
518
- }, [0], "closure")).then(__withArity((ignored) => {
627
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
519
628
  ignored = __boundary(ignored);
520
629
  if (__requireBoolean(push)) {
521
630
  __normalize(this.#host.history.pushState(null, "", `${__normalize(target.pathname)}${__normalize(target.search)}${__normalize(target.hash)}`));
@@ -525,7 +634,7 @@ export class App {
525
634
  }
526
635
  return null;
527
636
  return null;
528
- }, [0], "closure"));
637
+ }), [0], "closure"));
529
638
  return null;
530
639
  }
531
640
  selectTemplate(path) {
@@ -564,7 +673,14 @@ export class App {
564
673
  __requireArity(arguments.length, [0, 1], "command");
565
674
  form = __boundary(form);
566
675
  cmd = __boundary(cmd);
567
- if (__normalize(cmd.type) === "values") {
676
+ if (__normalize(cmd.type) === "loginSatisfied") {
677
+ this.finishLogin(form, __normalize(cmd.userId));
678
+ } else if (__normalize(cmd.type) === "cancelLogin") {
679
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) === null) || __requireBoolean(!__requireBoolean(__normalize(this.#login.form.related(form)))))) {
680
+ throw "cancelLogin requires the active login form";
681
+ }
682
+ this.#login.cancelled = true;
683
+ } else if (__normalize(cmd.type) === "values") {
568
684
  __normalize(form.setValue(__normalize(cmd.values)));
569
685
  } else if (__normalize(cmd.type) === "addRow") {
570
686
  __normalize(__normalize(form.find(__normalize(cmd.name))).addRow(__normalize(cmd.value)));
@@ -596,14 +712,14 @@ export class App {
596
712
  __normalize(this.renderer.locationChanged(__normalize(target.href)));
597
713
  }
598
714
  } else if (__normalize(cmd.type) === "redirect") {
599
- let action = () => {
600
- this.navigate(__normalize(cmd.url)).catch(__withArity((problem) => {
715
+ let action = __bindContexts(() => {
716
+ this.navigate(__normalize(cmd.url)).catch(__withArity(__bindContexts((problem) => {
601
717
  problem = __boundary(problem);
602
718
  this.error(form, problem);
603
719
  return null;
604
- }, [0], "closure"));
720
+ }), [0], "closure"));
605
721
  return null;
606
- };
722
+ });
607
723
  __normalize(this.#host.defer(action));
608
724
  } else if (__normalize(cmd.type) === "disable") {
609
725
  __normalize(__normalize(form.find(__normalize(cmd.name))).setDisabled(true));
@@ -647,6 +763,7 @@ export class App {
647
763
  }
648
764
  dispose() {
649
765
  this.#disposed = true;
766
+ this.cancelLogin();
650
767
  if (__requireBoolean(__normalize(this.heartbeat) !== null)) {
651
768
  __normalize(this.heartbeat.dispose());
652
769
  }
@@ -667,5 +784,303 @@ export class App {
667
784
  return null;
668
785
  return null;
669
786
  }
787
+ sendInput(job, input) {
788
+ __requireArity(arguments.length, [0, 1], "sendInput");
789
+ job = __boundary(job);
790
+ input = __boundary(input);
791
+ let form = __normalize(job.form);
792
+ let recover = __requireBoolean(__normalize(this.#login) === null) || __requireBoolean(!__requireBoolean(__normalize(this.#login.form.related(form))));
793
+ return this.request(__normalize(job.url), input, __normalize(form.token), null, false, false, recover).then(__withArity(__bindContexts((packet) => {
794
+ packet = __boundary(packet);
795
+ if (__requireBoolean(__normalize(packet.needLogin) === null)) {
796
+ return packet;
797
+ }
798
+ return this.resumeInput(job, input, __normalize(packet.needLogin));
799
+ return null;
800
+ }), [0], "closure"));
801
+ return null;
802
+ }
803
+ resumeInput(job, input, challenge) {
804
+ __requireArity(arguments.length, [0, 1, 2], "resumeInput");
805
+ job = __boundary(job);
806
+ input = __boundary(input);
807
+ challenge = __boundary(challenge);
808
+ return this.needLogin(job, challenge).then(__withArity(__bindContexts((identity) => {
809
+ identity = __boundary(identity);
810
+ let form = __normalize(job.form);
811
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(form.disposed)) || __requireBoolean(__normalize(form.stale))) || __requireBoolean(__normalize(this.#externalStale))) || __requireBoolean(__requireBoolean(__normalize(job.owner) !== null) && __requireBoolean(!__requireBoolean(job.owner.active))))) {
812
+ throw "Pending login request is no longer active";
813
+ }
814
+ job.authVersion = __normalize(this.#authVersion);
815
+ return this.request(__normalize(job.url), input, __normalize(identity.token), null, false, false, false, __normalize(identity.expected));
816
+ return null;
817
+ }), [0], "closure"));
818
+ return null;
819
+ }
820
+ needLogin(job, challenge) {
821
+ __requireArity(arguments.length, [0, 1], "needLogin");
822
+ job = __boundary(job);
823
+ challenge = __boundary(challenge);
824
+ if (__requireBoolean(__normalize(this.#events.needLoginUrl) === null)) {
825
+ throw "Login is required but no needLogin event is configured";
826
+ }
827
+ let session = __normalize(this.#login);
828
+ if (__requireBoolean(__requireBoolean(session !== null) && __requireBoolean(__normalize(session.originalToken) !== job.form.token))) {
829
+ this.invalidateForm(__normalize(job.form));
830
+ throw "SessionChanged: another form is awaiting login";
831
+ }
832
+ if (__requireBoolean(session === null)) {
833
+ let deferred = __normalize(__hostGlobal(["Promise"]).withResolvers());
834
+ let scope = new Form(this, null, job.form.token);
835
+ session = { form: scope, source: __normalize(job.form), promise: __normalize(deferred.promise), resolve: __normalize(deferred.resolve), reject: __normalize(deferred.reject), release: null, ready: null, cancelled: false, change: null, originalToken: job.form.token };
836
+ __normalize(session.promise).catch(__withArity(__bindContexts((problem) => {
837
+ problem = __boundary(problem);
838
+ return null;
839
+ return null;
840
+ }), [0], "closure"));
841
+ this.#login = session;
842
+ this.pausePolls();
843
+ try {
844
+ let cancel = __bindContexts(() => {
845
+ this.cancelLogin();
846
+ return null;
847
+ });
848
+ session.release = __normalize(this.renderer.openLogin(scope, cancel));
849
+ this.post(__normalize(this.#events.needLoginUrl), challenge, __normalize(scope.token)).then(__withArity(__bindContexts((packet) => {
850
+ packet = __boundary(packet);
851
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) === session) && __requireBoolean(!__requireBoolean(session.form.disposed)))) {
852
+ __normalize(session.form.handle(packet));
853
+ }
854
+ return null;
855
+ }), [0], "closure")).catch(__withArity(__bindContexts((problem) => {
856
+ problem = __boundary(problem);
857
+ if (__requireBoolean(__normalize(this.#login) === session)) {
858
+ this.cancelLogin(problem);
859
+ }
860
+ return null;
861
+ }), [0], "closure"));
862
+ } catch (problem) {
863
+ this.cancelLogin(problem);
864
+ }
865
+ }
866
+ return __normalize(session.promise).then(__withArity(__bindContexts((identity) => {
867
+ identity = __boundary(identity);
868
+ if (__requireBoolean(__requireBoolean(__normalize(challenge.userId) !== null) && __requireBoolean(__normalize(identity.userId) !== __normalize(challenge.userId)))) {
869
+ for (const form of __normalize(this.forms.slice())) {
870
+ this.invalidateForm(form);
871
+ }
872
+ throw "IdentityChanged";
873
+ }
874
+ return { token: __normalize(identity.token), expected: __normalize(challenge.userId) };
875
+ return null;
876
+ }), [0], "closure"));
877
+ return null;
878
+ }
879
+ finishLogin(form, userId) {
880
+ __requireArity(arguments.length, [0, 1], "finishLogin");
881
+ form = __boundary(form);
882
+ userId = __boundary(userId);
883
+ let session = __normalize(this.#login);
884
+ if (__requireBoolean(__requireBoolean(__requireBoolean(session === null) || __requireBoolean(!__requireBoolean(__normalize(session.form.related(form))))) || __requireBoolean(__normalize(form.token) === null))) {
885
+ throw "loginSatisfied requires an active login form and token";
886
+ }
887
+ if (__requireBoolean(__normalize(this.#active.length) === 0)) {
888
+ throw "No request is waiting for login";
889
+ }
890
+ session.ready = { token: __normalize(form.token), userId: userId };
891
+ return null;
892
+ return null;
893
+ }
894
+ completeLogin(form) {
895
+ __requireArity(arguments.length, [0], "completeLogin");
896
+ form = __boundary(form);
897
+ let session = __normalize(this.#login);
898
+ if (__requireBoolean(__requireBoolean(session === null) || __requireBoolean(!__requireBoolean(__normalize(session.form.related(form)))))) {
899
+ return null;
900
+ }
901
+ if (__requireBoolean(__normalize(session.cancelled))) {
902
+ return this.cancelLogin();
903
+ }
904
+ if (__requireBoolean(__normalize(session.ready) === null)) {
905
+ return null;
906
+ }
907
+ this.#login = null;
908
+ let identity = __normalize(session.ready);
909
+ let change = __normalize(session.change);
910
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(change))) || __requireBoolean(!__requireBoolean(__normalize(change.startsWith("v1:")))))) {
911
+ change = `v1:${__normalize(__hostGlobal(["crypto"]).randomUUID())}`;
912
+ }
913
+ this.commitAuthentication(__normalize(session.source), __normalize(session.originalToken), __normalize(identity.token), change);
914
+ __normalize(session.resolve(identity));
915
+ if (__requireBoolean(__normalize(session.release) !== null)) {
916
+ let release = __normalize(session.release);
917
+ __call(release, );
918
+ }
919
+ __normalize(session.form.dispose());
920
+ this.drain();
921
+ return null;
922
+ return null;
923
+ }
924
+ cancelLogin(problem = "LoginCancelled") {
925
+ problem = __boundary(problem);
926
+ let session = __normalize(this.#login);
927
+ if (__requireBoolean(session === null)) {
928
+ return null;
929
+ }
930
+ this.#login = null;
931
+ __normalize(session.reject(problem));
932
+ if (__requireBoolean(__normalize(session.release) !== null)) {
933
+ let release = __normalize(session.release);
934
+ __call(release, );
935
+ }
936
+ __normalize(session.form.dispose());
937
+ this.drain();
938
+ return null;
939
+ return null;
940
+ }
941
+ loginScopeDisposed(form) {
942
+ __requireArity(arguments.length, [0], "loginScopeDisposed");
943
+ form = __boundary(form);
944
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) !== null) && __requireBoolean(__requireBoolean(__normalize(this.#login.form) === form) || __requireBoolean(__normalize(this.#login.source) === form)))) {
945
+ this.cancelLogin();
946
+ }
947
+ return null;
948
+ return null;
949
+ }
950
+ acceptEvents(events) {
951
+ __requireArity(arguments.length, [0], "acceptEvents");
952
+ events = __boundary(events);
953
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(events))) || __requireBoolean(__isArray(events)))) {
954
+ throw "Startup events must be a map";
955
+ }
956
+ if (__requireBoolean(__normalize(events.needLoginUrl) !== null)) {
957
+ this.checkUrl(__normalize(events.needLoginUrl));
958
+ }
959
+ this.#events = events;
960
+ return null;
961
+ return null;
962
+ }
963
+ beginLoginResponse(form, packet) {
964
+ __requireArity(arguments.length, [0, 1], "beginLoginResponse");
965
+ form = __boundary(form);
966
+ packet = __boundary(packet);
967
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) !== null) && __requireBoolean(__normalize(this.#login.form.related(form))))) {
968
+ this.#login.change = __normalize(packet.authChange);
969
+ this.#login.ready = null;
970
+ this.#login.cancelled = false;
971
+ }
972
+ return null;
973
+ return null;
974
+ }
975
+ observeAuthentication(form, previous, packet) {
976
+ __requireArity(arguments.length, [0, 1, 2], "observeAuthentication");
977
+ form = __boundary(form);
978
+ previous = __boundary(previous);
979
+ packet = __boundary(packet);
980
+ let change = __normalize(packet.authChange);
981
+ if (__requireBoolean(__requireBoolean(__normalize(this.#login) !== null) && __requireBoolean(__normalize(this.#login.form.related(form))))) {
982
+ return null;
983
+ }
984
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__isString(change)) && __requireBoolean(__normalize(change.startsWith("v1:")))) && __requireBoolean(!__requireBoolean(__normalize(this.#authChanges.includes(change)))))) {
985
+ this.commitAuthentication(form, previous, __normalize(packet.token), change);
986
+ }
987
+ return null;
988
+ return null;
989
+ }
990
+ commitAuthentication(source, previous, token, change) {
991
+ __requireArity(arguments.length, [0, 1, 2, 3], "commitAuthentication");
992
+ source = __boundary(source);
993
+ previous = __boundary(previous);
994
+ token = __boundary(token);
995
+ change = __boundary(change);
996
+ if (__requireBoolean(__requireBoolean(__normalize(this.#disposed)) || __requireBoolean(__normalize(this.#authChanges.includes(change))))) {
997
+ return null;
998
+ }
999
+ let previousChange = __normalize(this.#storageChange);
1000
+ this.#storageChange = change;
1001
+ __add(this.#authChanges, change);
1002
+ if (__requireBoolean(__normalize(this.#authChanges.length) > 64)) {
1003
+ __normalize(this.#authChanges.shift());
1004
+ }
1005
+ this.#authVersion = __numericAdd(__normalize(this.#authVersion), 1);
1006
+ this.#committingAuthentication = true;
1007
+ for (const form of __normalize(this.forms.slice())) {
1008
+ if (__requireBoolean(__requireBoolean(__normalize(form.disposed)) || __requireBoolean(__normalize(form.stale)))) {
1009
+ continue;
1010
+ }
1011
+ if (__requireBoolean(__requireBoolean(__requireBoolean(form === source) || __requireBoolean(__normalize(form.token) === previous)) || __requireBoolean(__normalize(form.token) === token))) {
1012
+ __normalize(form.setToken(token));
1013
+ } else {
1014
+ this.invalidateForm(form);
1015
+ }
1016
+ }
1017
+ this.#committingAuthentication = false;
1018
+ try {
1019
+ __normalize(this.#host.storage.setItem("lango.authChange", change));
1020
+ if (__requireBoolean(__requireBoolean(previousChange !== null) && __requireBoolean(__normalize(this.#host.storage.removeItem) !== null))) {
1021
+ __normalize(this.#host.storage.removeItem(`lango.token:${previousChange}`));
1022
+ }
1023
+ } catch (problem) {
1024
+ return null;
1025
+ }
1026
+ return null;
1027
+ return null;
1028
+ }
1029
+ externalAuthentication(change) {
1030
+ __requireArity(arguments.length, [0], "externalAuthentication");
1031
+ change = __boundary(change);
1032
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#disposed)) || __requireBoolean(!__requireBoolean(__isString(change)))) || __requireBoolean(!__requireBoolean(__normalize(change.startsWith("v1:"))))) || __requireBoolean(__normalize(this.#authChanges.includes(change))))) {
1033
+ return null;
1034
+ }
1035
+ __add(this.#authChanges, change);
1036
+ if (__requireBoolean(__normalize(this.#authChanges.length) > 64)) {
1037
+ __normalize(this.#authChanges.shift());
1038
+ }
1039
+ this.#storageChange = change;
1040
+ this.#externalStale = true;
1041
+ this.#authVersion = __numericAdd(__normalize(this.#authVersion), 1);
1042
+ this.cancelLogin("SessionChanged: authentication changed in another tab");
1043
+ if (__requireBoolean(__normalize(this.heartbeat) !== null)) {
1044
+ __normalize(this.heartbeat.dispose());
1045
+ }
1046
+ if (__requireBoolean(__requireBoolean(__normalize(this.root) === null) && __requireBoolean(__normalize(this.#host.mount) !== null))) {
1047
+ this.root = new Form(this);
1048
+ this.root.element = __normalize(this.#host.mount);
1049
+ }
1050
+ for (const form of __normalize(this.forms.slice())) {
1051
+ this.invalidateForm(form);
1052
+ }
1053
+ return null;
1054
+ return null;
1055
+ }
1056
+ invalidateForm(form) {
1057
+ __requireArity(arguments.length, [0], "invalidateForm");
1058
+ form = __boundary(form);
1059
+ if (__requireBoolean(__requireBoolean(__normalize(form.disposed)) || __requireBoolean(__normalize(form.stale)))) {
1060
+ return null;
1061
+ }
1062
+ form.stale = true;
1063
+ __clear(form.activity);
1064
+ let parent = __normalize(form.parent);
1065
+ __loop1: while (__requireBoolean(parent !== null)) {
1066
+ if (__requireBoolean(__normalize(parent.stale))) {
1067
+ return null;
1068
+ }
1069
+ parent = __normalize(parent.parent);
1070
+ }
1071
+ if (__requireBoolean(__normalize(this.renderer.sessionChanged) !== null)) {
1072
+ __normalize(this.renderer.sessionChanged(form));
1073
+ }
1074
+ return null;
1075
+ return null;
1076
+ }
1077
+ reloadSession() {
1078
+ if (__requireBoolean(__normalize(this.#host.reload) === null)) {
1079
+ throw "Reload the page to continue";
1080
+ }
1081
+ __normalize(this.#host.reload());
1082
+ return null;
1083
+ return null;
1084
+ }
670
1085
  }
671
1086
  //# sourceMappingURL=app.mjs.map