@glassly/miniapp 0.4.1 → 0.4.2-dev.1176

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 (78) hide show
  1. package/README.md +1 -0
  2. package/dist/display/index.d.ts +14 -4
  3. package/dist/display/index.d.ts.map +1 -1
  4. package/dist/display/index.js +11 -4
  5. package/dist/display/index.js.map +1 -1
  6. package/dist/display/profiles/index.d.ts +1 -1
  7. package/dist/display/profiles/index.d.ts.map +1 -1
  8. package/dist/display/profiles/index.js +1 -1
  9. package/dist/display/profiles/index.js.map +1 -1
  10. package/dist/display/profiles/nimo.d.ts +6 -0
  11. package/dist/display/profiles/nimo.d.ts.map +1 -1
  12. package/dist/display/profiles/nimo.js +17 -0
  13. package/dist/display/profiles/nimo.js.map +1 -1
  14. package/dist/display/scene/anchor.d.ts +16 -0
  15. package/dist/display/scene/anchor.d.ts.map +1 -0
  16. package/dist/display/scene/anchor.js +32 -0
  17. package/dist/display/scene/anchor.js.map +1 -0
  18. package/dist/display/scene/degrade.d.ts +25 -0
  19. package/dist/display/scene/degrade.d.ts.map +1 -0
  20. package/dist/display/scene/degrade.js +67 -0
  21. package/dist/display/scene/degrade.js.map +1 -0
  22. package/dist/display/scene/differ.d.ts +50 -0
  23. package/dist/display/scene/differ.d.ts.map +1 -0
  24. package/dist/display/scene/differ.js +129 -0
  25. package/dist/display/scene/differ.js.map +1 -0
  26. package/dist/display/scene/process.d.ts +41 -0
  27. package/dist/display/scene/process.d.ts.map +1 -0
  28. package/dist/display/scene/process.js +408 -0
  29. package/dist/display/scene/process.js.map +1 -0
  30. package/dist/display/scene/store.d.ts +48 -0
  31. package/dist/display/scene/store.d.ts.map +1 -0
  32. package/dist/display/scene/store.js +0 -0
  33. package/dist/display/scene/store.js.map +1 -0
  34. package/dist/display/scene/svgPath.d.ts +9 -0
  35. package/dist/display/scene/svgPath.d.ts.map +1 -0
  36. package/dist/display/scene/svgPath.js +167 -0
  37. package/dist/display/scene/svgPath.js.map +1 -0
  38. package/dist/host/index.d.ts +7 -0
  39. package/dist/host/index.d.ts.map +1 -0
  40. package/dist/host/index.js +7 -0
  41. package/dist/host/index.js.map +1 -0
  42. package/dist/host/uiShim.d.ts +38 -0
  43. package/dist/host/uiShim.d.ts.map +1 -0
  44. package/dist/host/uiShim.js +383 -0
  45. package/dist/host/uiShim.js.map +1 -0
  46. package/dist/host/webviewGlobals.d.ts +60 -0
  47. package/dist/host/webviewGlobals.d.ts.map +1 -0
  48. package/dist/host/webviewGlobals.js +211 -0
  49. package/dist/host/webviewGlobals.js.map +1 -0
  50. package/dist/mirror/canvas.d.ts +13 -2
  51. package/dist/mirror/canvas.d.ts.map +1 -1
  52. package/dist/mirror/canvas.js +42 -21
  53. package/dist/mirror/canvas.js.map +1 -1
  54. package/dist/mirror/index.d.ts +1 -1
  55. package/dist/mirror/index.d.ts.map +1 -1
  56. package/dist/mirror/index.js.map +1 -1
  57. package/dist/modules/display.d.ts +10 -0
  58. package/dist/modules/display.d.ts.map +1 -1
  59. package/dist/modules/display.js.map +1 -1
  60. package/dist/ui/index.d.ts +1 -1
  61. package/dist/ui/index.js +1 -1
  62. package/package.json +6 -2
  63. package/src/display/index.ts +15 -3
  64. package/src/display/profiles/index.ts +1 -1
  65. package/src/display/profiles/nimo.ts +18 -0
  66. package/src/display/scene/anchor.ts +38 -0
  67. package/src/display/scene/degrade.ts +83 -0
  68. package/src/display/scene/differ.ts +152 -0
  69. package/src/display/scene/process.ts +443 -0
  70. package/src/display/scene/store.ts +0 -0
  71. package/src/display/scene/svgPath.ts +163 -0
  72. package/src/host/index.ts +14 -0
  73. package/src/host/uiShim.ts +389 -0
  74. package/src/host/webviewGlobals.ts +251 -0
  75. package/src/mirror/canvas.ts +86 -43
  76. package/src/mirror/index.ts +1 -1
  77. package/src/modules/display.ts +4 -0
  78. package/src/ui/index.ts +1 -1
@@ -0,0 +1,167 @@
1
+ /** SVG path compiler shared by scene validation and native wire encoding.
2
+ * Ported from g2flash revision 21; no DOM, Node or mobile-platform dependencies. */
3
+ const u16 = (v) => [v & 255, (v >>> 8) & 255];
4
+ function integer(v, min, max, name) {
5
+ if (!Number.isInteger(v) || v < min || v > max)
6
+ throw new Error(`${name} outside ${min}..${max}: ${v}`);
7
+ return v;
8
+ }
9
+ /** SVG d subset: absolute/relative M L H V Q C S T Z; implicit repetitions.
10
+ * Unsupported syntax is rejected. Each filled contour must explicitly close. */
11
+ export function svgPath(d) {
12
+ const tokens = d.match(/[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?/g) ?? [];
13
+ const residue = d.replace(/[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?/g, "");
14
+ if (/[^\s,]/.test(residue))
15
+ throw new Error("Invalid SVG path syntax");
16
+ let i = 0, cmd = "", x = 0, y = 0, sx = 0, sy = 0, open = false, previous = "";
17
+ let cx = 0, cy = 0;
18
+ const out = [];
19
+ const emit = (op, ...coords) => {
20
+ out.push(op, ...coords.flatMap((v) => u16(integer(Math.round(v * 16), -32768, 32767, "Q4 coordinate"))));
21
+ };
22
+ const number = () => {
23
+ if (i >= tokens.length || /^[a-z]$/i.test(tokens[i]))
24
+ throw new Error("Missing SVG coordinate");
25
+ const v = Number(tokens[i++]);
26
+ if (!Number.isFinite(v))
27
+ throw new Error("Invalid number");
28
+ return v;
29
+ };
30
+ while (i < tokens.length) {
31
+ if (/^[a-z]$/i.test(tokens[i]))
32
+ cmd = tokens[i++];
33
+ if (!cmd || !/^[MLHVQCSTZ]$/i.test(cmd))
34
+ throw new Error(`Unsupported SVG command: ${cmd}`);
35
+ const op = cmd.toUpperCase(), rel = cmd !== op;
36
+ const pair = () => [number() + (rel ? x : 0), number() + (rel ? y : 0)];
37
+ if (op === "Z") {
38
+ if (!open)
39
+ throw new Error("Z without an open contour");
40
+ emit(4);
41
+ x = sx;
42
+ y = sy;
43
+ open = false;
44
+ previous = "Z";
45
+ cmd = "";
46
+ continue;
47
+ }
48
+ if (op !== "M" && !open)
49
+ throw new Error("Path must start with M");
50
+ if (op === "M") {
51
+ if (open)
52
+ throw new Error("Filled contours must close with Z before another M");
53
+ [x, y] = pair();
54
+ sx = x;
55
+ sy = y;
56
+ open = true;
57
+ emit(0, x, y);
58
+ cmd = rel ? "l" : "L";
59
+ }
60
+ else if (op === "L" || op === "H" || op === "V") {
61
+ if (op === "L")
62
+ [x, y] = pair();
63
+ else if (op === "H")
64
+ x = number() + (rel ? x : 0);
65
+ else
66
+ y = number() + (rel ? y : 0);
67
+ emit(1, x, y);
68
+ }
69
+ else if (op === "Q" || op === "T") {
70
+ const c = op === "Q" ? pair() : previous === "Q" || previous === "T" ? [2 * x - cx, 2 * y - cy] : [x, y];
71
+ const end = pair();
72
+ emit(2, ...c, ...end);
73
+ [cx, cy] = c;
74
+ [x, y] = end;
75
+ }
76
+ else {
77
+ const a = op === "C" ? pair() : previous === "C" || previous === "S" ? [2 * x - cx, 2 * y - cy] : [x, y];
78
+ const b = pair(), end = pair();
79
+ emit(3, ...a, ...b, ...end);
80
+ [cx, cy] = b;
81
+ [x, y] = end;
82
+ }
83
+ previous = op;
84
+ }
85
+ if (open || !out.length)
86
+ throw new Error("Expected explicitly closed filled path");
87
+ return out;
88
+ }
89
+ /** Check the same fixed-point subdivision and budgets as revision 21 firmware. */
90
+ export function compileSvgPath(d) {
91
+ if (typeof d !== "string" || d.length > 65536)
92
+ throw new Error("Invalid SVG path data");
93
+ const commands = svgPath(d);
94
+ if (commands.length > 8192)
95
+ throw new Error("SVG path exceeds 8192 command bytes");
96
+ let edges = 0, count = 0, x = 0, y = 0, sx = 0, sy = 0;
97
+ const edge = (ax, ay, bx, by) => {
98
+ if ((ax !== bx || ay !== by) && ++edges > 512)
99
+ throw new Error("SVG path exceeds 512 compiled edges");
100
+ };
101
+ const curve = (p, depth) => {
102
+ const u = [
103
+ 3 * p[2] - 2 * p[0] - p[6],
104
+ 3 * p[3] - 2 * p[1] - p[7],
105
+ 3 * p[4] - 2 * p[6] - p[0],
106
+ 3 * p[5] - 2 * p[7] - p[1],
107
+ ];
108
+ if (u.every((v) => Math.abs(v) <= 16)) {
109
+ edge(p[0], p[1], p[6], p[7]);
110
+ return;
111
+ }
112
+ if (depth === 8)
113
+ throw new Error("SVG curve exceeds subdivision limit");
114
+ const a = new Array(8), b = new Array(8);
115
+ for (let k = 0; k < 2; k++) {
116
+ const ab = Math.trunc((p[k] + p[k + 2]) / 2), bc = Math.trunc((p[k + 2] + p[k + 4]) / 2), cd = Math.trunc((p[k + 4] + p[k + 6]) / 2);
117
+ const abc = Math.trunc((ab + bc) / 2), bcd = Math.trunc((bc + cd) / 2), mid = Math.trunc((abc + bcd) / 2);
118
+ a[k] = p[k];
119
+ a[k + 2] = ab;
120
+ a[k + 4] = abc;
121
+ a[k + 6] = mid;
122
+ b[k] = mid;
123
+ b[k + 2] = bcd;
124
+ b[k + 4] = cd;
125
+ b[k + 6] = p[k + 6];
126
+ }
127
+ curve(a, depth + 1);
128
+ curve(b, depth + 1);
129
+ };
130
+ for (let i = 0; i < commands.length;) {
131
+ const op = commands[i++], pairs = op === 4 ? 0 : op === 3 ? 3 : op === 2 ? 2 : 1;
132
+ if (++count > 1024)
133
+ throw new Error("SVG path exceeds 1024 commands");
134
+ const p = [x, y];
135
+ for (let k = 0; k < pairs * 2; k++, i += 2)
136
+ p.push(((commands[i] | (commands[i + 1] << 8)) << 16) >> 16);
137
+ if (op === 0) {
138
+ x = sx = p[2];
139
+ y = sy = p[3];
140
+ }
141
+ else if (op === 4) {
142
+ edge(x, y, sx, sy);
143
+ x = sx;
144
+ y = sy;
145
+ }
146
+ else if (op === 1) {
147
+ edge(x, y, p[2], p[3]);
148
+ x = p[2];
149
+ y = p[3];
150
+ }
151
+ else {
152
+ if (op === 2) {
153
+ p[6] = p[4];
154
+ p[7] = p[5];
155
+ for (let k = 0; k < 2; k++) {
156
+ p[k + 4] = Math.trunc((p[k + 6] + 2 * p[k + 2]) / 3);
157
+ p[k + 2] = Math.trunc((p[k] + 2 * p[k + 2]) / 3);
158
+ }
159
+ }
160
+ curve(p, 0);
161
+ x = p[6];
162
+ y = p[7];
163
+ }
164
+ }
165
+ return { commands, edges };
166
+ }
167
+ //# sourceMappingURL=svgPath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"svgPath.js","sourceRoot":"","sources":["../../../src/display/scene/svgPath.ts"],"names":[],"mappings":"AAAA;oFACoF;AACpF,MAAM,GAAG,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA;AACrD,SAAS,OAAO,CAAC,CAAS,EAAE,GAAW,EAAE,GAAW,EAAE,IAAY;IAChE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,YAAY,GAAG,KAAK,GAAG,KAAK,CAAC,EAAE,CAAC,CAAA;IACvG,OAAO,CAAC,CAAA;AACV,CAAC;AACD;gFACgF;AAChF,MAAM,UAAU,OAAO,CAAC,CAAS;IAC/B,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,wDAAwD,CAAC,IAAI,EAAE,CAAA;IACtF,MAAM,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC,wDAAwD,EAAE,EAAE,CAAC,CAAA;IACvF,IAAI,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;IACtE,IAAI,CAAC,GAAG,CAAC,EACP,GAAG,GAAG,EAAE,EACR,CAAC,GAAG,CAAC,EACL,CAAC,GAAG,CAAC,EACL,EAAE,GAAG,CAAC,EACN,EAAE,GAAG,CAAC,EACN,IAAI,GAAG,KAAK,EACZ,QAAQ,GAAG,EAAE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E,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;YACtB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;YACR,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;QACV,CAAC;aAAM,CAAC;YACN,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;gBACb,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;gBACX,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;gBACX,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;oBAC3B,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;oBACpD,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;gBAClD,CAAC;YACH,CAAC;YACD,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;YACX,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;YACR,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;QACV,CAAC;IACH,CAAC;IACD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,CAAA;AAC1B,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * What a host injects into a miniapp's UI WebView. Used by the Glassly App's
3
+ * engine and the desktop simulator; miniapps never import this.
4
+ */
5
+ export { buildUiShim, type UiShimOptions } from "./uiShim";
6
+ export { buildWebviewGlobalsScript, buildSafeAreaUpdateScript, type BuildWebviewGlobalsOptions, type CapsuleMenuRect, type MiniappColorScheme, type MiniappSafeArea, } from "./webviewGlobals";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,WAAW,EAAE,KAAK,aAAa,EAAC,MAAM,UAAU,CAAA;AACxD,OAAO,EACL,yBAAyB,EACzB,yBAAyB,EACzB,KAAK,0BAA0B,EAC/B,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,eAAe,GACrB,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * What a host injects into a miniapp's UI WebView. Used by the Glassly App's
3
+ * engine and the desktop simulator; miniapps never import this.
4
+ */
5
+ export { buildUiShim } from "./uiShim";
6
+ export { buildWebviewGlobalsScript, buildSafeAreaUpdateScript, } from "./webviewGlobals";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,WAAW,EAAqB,MAAM,UAAU,CAAA;AACxD,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAK1B,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * window.glassly — WebView-side bridge to the per-miniapp background JSContext.
3
+ *
4
+ * Injected into every UI WebView via `injectedJavaScriptBeforeContentLoaded`
5
+ * (iOS) or `injectedJavaScriptObject` + a bundler-emitted inline `<script>`
6
+ * (Android, where the `BeforeContentLoaded` prop is documented unreliable
7
+ * per react-native-webview issues #1099 / #1609).
8
+ *
9
+ * The shim:
10
+ * - Buffers `glassly.send(channel, payload)` calls until `glassly.ready()`
11
+ * fires + the host acks the channel is open. WebView-side BUFFERS (the
12
+ * WebView is the short-lived side and shouldn't drop user input).
13
+ * - Installs `window.__host.recv(envelope)` for the host to push
14
+ * background → WebView messages via `webview.injectJavaScript(...)`.
15
+ * - Sends `__heartbeat__` every 5s; host considers the WebView gone
16
+ * after 15s of silence.
17
+ * - Maintains monotonic seq numbers; the host's per-WebView dedup window
18
+ * drops replays during reconnect.
19
+ *
20
+ * This file exports a `buildUiShim()` function returning the JS
21
+ * source as a string. The host's UIRouter wraps it inside the
22
+ * existing WebView setup so a single bundler-time string emission covers
23
+ * both platforms.
24
+ *
25
+ * IMPORTANT: the shim runs in the WebView's bare ECMAScript scope before
26
+ * the miniapp's bundle. It must not import anything; the source is one
27
+ * IIFE. The only injected dependencies are
28
+ * `window.ReactNativeWebView.postMessage` (provided by react-native-webview)
29
+ * and (on Android) `window.ReactNativeWebView.injectedObjectJson()`
30
+ * (provided when the host sets `injectedJavaScriptObject`).
31
+ */
32
+ export interface UiShimOptions {
33
+ packageName: string;
34
+ /** @deprecated No longer used — heartbeat removed since WebViews are foreground-only. */
35
+ heartbeatIntervalMs?: number;
36
+ }
37
+ export declare function buildUiShim(options: UiShimOptions): string;
38
+ //# sourceMappingURL=uiShim.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uiShim.d.ts","sourceRoot":"","sources":["../../src/host/uiShim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAA;IACnB,yFAAyF;IACzF,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CA8V1D"}
@@ -0,0 +1,383 @@
1
+ /**
2
+ * window.glassly — WebView-side bridge to the per-miniapp background JSContext.
3
+ *
4
+ * Injected into every UI WebView via `injectedJavaScriptBeforeContentLoaded`
5
+ * (iOS) or `injectedJavaScriptObject` + a bundler-emitted inline `<script>`
6
+ * (Android, where the `BeforeContentLoaded` prop is documented unreliable
7
+ * per react-native-webview issues #1099 / #1609).
8
+ *
9
+ * The shim:
10
+ * - Buffers `glassly.send(channel, payload)` calls until `glassly.ready()`
11
+ * fires + the host acks the channel is open. WebView-side BUFFERS (the
12
+ * WebView is the short-lived side and shouldn't drop user input).
13
+ * - Installs `window.__host.recv(envelope)` for the host to push
14
+ * background → WebView messages via `webview.injectJavaScript(...)`.
15
+ * - Sends `__heartbeat__` every 5s; host considers the WebView gone
16
+ * after 15s of silence.
17
+ * - Maintains monotonic seq numbers; the host's per-WebView dedup window
18
+ * drops replays during reconnect.
19
+ *
20
+ * This file exports a `buildUiShim()` function returning the JS
21
+ * source as a string. The host's UIRouter wraps it inside the
22
+ * existing WebView setup so a single bundler-time string emission covers
23
+ * both platforms.
24
+ *
25
+ * IMPORTANT: the shim runs in the WebView's bare ECMAScript scope before
26
+ * the miniapp's bundle. It must not import anything; the source is one
27
+ * IIFE. The only injected dependencies are
28
+ * `window.ReactNativeWebView.postMessage` (provided by react-native-webview)
29
+ * and (on Android) `window.ReactNativeWebView.injectedObjectJson()`
30
+ * (provided when the host sets `injectedJavaScriptObject`).
31
+ */
32
+ export function buildUiShim(options) {
33
+ const packageNameJson = JSON.stringify(options.packageName);
34
+ // The IIFE assembles a typed `glassly` global plus an internal
35
+ // `__host` for host inbound calls. Quoted strings are JSON-safe so
36
+ // direct injection inside another JS literal also works.
37
+ return `
38
+ (function () {
39
+ if (typeof window === 'undefined') return;
40
+ if (window.glassly && window.__host) return;
41
+ var packageName = ${packageNameJson};
42
+ // Resolve window.ReactNativeWebView.postMessage LAZILY, not once at shim-eval.
43
+ // On Android the shim is injected as a WebViewCompat document-start script
44
+ // (so it beats the page bundle and the ready handshake can't be raced — see
45
+ // the react-native-webview patch). But the order in which that document-start
46
+ // script runs vs. the WebMessageListener that installs
47
+ // window.ReactNativeWebView is not contractually fixed on Android the way it
48
+ // is on iOS (postMessageScript is registered before atStartScript there). If
49
+ // we captured rnPost once and the bridge object wasn't installed yet, every
50
+ // postMessage — including {type:'ready'} — would be silently dropped and the
51
+ // splash would hang forever. Resolving per-call means we always pick up the
52
+ // bridge once it exists, which it reliably does by the time the bundle mounts
53
+ // and calls glassly.ready().
54
+ function getRnPost() {
55
+ try {
56
+ return window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage === 'function'
57
+ ? window.ReactNativeWebView.postMessage.bind(window.ReactNativeWebView)
58
+ : null;
59
+ } catch (e) { return null; }
60
+ }
61
+
62
+ // Outbound seq number — purely a log-correlation aid (the host's
63
+ // router doesn't dedup anymore since reconnect can't happen: one
64
+ // WebView, foreground-only, destroyed on close → fresh shim on
65
+ // next open).
66
+ var outboundSeq = 1;
67
+ var ready = false;
68
+ var channelHandlers = Object.create(null);
69
+ var openHandlers = [];
70
+ var closeHandlers = [];
71
+ // Queued glassly.send calls before glassly.ready() acks. FIFO drained
72
+ // on ack. Buffered so user input (button taps, etc.) isn't dropped
73
+ // when the WebView mounts faster than the host expects.
74
+ var outboundQueue = [];
75
+ // Inbound payloads that arrived before any glassly.on subscriber was
76
+ // registered for the channel. The background side fires
77
+ // session.ui.onOpen handlers as soon as the WebView posts {type:'ready'},
78
+ // and on a fast bridge the resulting ui.send round-trips back before
79
+ // React useEffects have had a chance to attach listeners. Without
80
+ // buffering, the very first snapshot (the one a controller pushes from
81
+ // its ui.onOpen handler to hydrate the fresh UI) is silently dropped.
82
+ //
83
+ // Drain policy: the queue for a channel is flushed into the FIRST
84
+ // glassly.on(channel, ...) subscriber, then discarded. Subsequent
85
+ // sends with no listener fall back to the old drop-on-floor behaviour
86
+ // — we don't want a re-subscribe to replay stale state.
87
+ //
88
+ // Cap: 32 entries per channel, oldest dropped on overflow. Bounds the
89
+ // memory cost when a controller pushes to a channel that no UI page
90
+ // ever subscribes to.
91
+ var pendingByChannel = Object.create(null);
92
+ var PENDING_CAP_PER_CHANNEL = 32;
93
+
94
+ function postEnvelope(envelope) {
95
+ var rnPost = getRnPost();
96
+ if (!rnPost) return;
97
+ try { rnPost(JSON.stringify(envelope)); } catch (_) {}
98
+ }
99
+
100
+ function send(channel, payload) {
101
+ var seq = outboundSeq++;
102
+ var envelope = { type: 'msg', seq: seq, channel: String(channel), payload: payload };
103
+ if (!ready) {
104
+ outboundQueue.push(envelope);
105
+ return;
106
+ }
107
+ postEnvelope(envelope);
108
+ }
109
+
110
+ function flushQueue() {
111
+ while (outboundQueue.length > 0) {
112
+ postEnvelope(outboundQueue.shift());
113
+ }
114
+ }
115
+
116
+ // NOTE: console.* tap lives in webviewGlobals.ts (injected alongside
117
+ // this shim for every UI WebView, two-layer or single-bundle). That
118
+ // shim posts {payload:{type:"dev_log", ...}} envelopes which
119
+ // LocalMiniappRuntime forwards to the dev sidecar tagged source:"ui".
120
+ // We don't re-wrap here to avoid double-delivery.
121
+
122
+ function on(channel, cb) {
123
+ if (typeof cb !== 'function') return function () {};
124
+ var list = channelHandlers[channel];
125
+ var firstSubscriber = !list || list.length === 0;
126
+ if (!list) { list = []; channelHandlers[channel] = list; }
127
+ list.push(cb);
128
+ // Drain any payloads buffered before this channel had a listener.
129
+ // Only fired on the first subscriber for the channel — a later
130
+ // re-subscribe (after an unsubscribe) does NOT replay history.
131
+ if (firstSubscriber) {
132
+ var pending = pendingByChannel[channel];
133
+ if (pending) {
134
+ delete pendingByChannel[channel];
135
+ for (var i = 0; i < pending.length; i++) {
136
+ try { cb(pending[i]); } catch (e) {}
137
+ }
138
+ }
139
+ }
140
+ return function () {
141
+ var idx = list.indexOf(cb);
142
+ if (idx >= 0) list.splice(idx, 1);
143
+ };
144
+ }
145
+
146
+ // ── RPC ─────────────────────────────────────────────────────────────
147
+ // Outbound RPC: every glassly.request() generates a request id, sends a
148
+ // 'msg' envelope tagged with requestId, and stashes a one-shot resolver
149
+ // keyed on the id. The background-side reply is a recv('msg') with the
150
+ // same channel + requestId; we route it directly to the resolver.
151
+ //
152
+ // Cancellation: caller's AbortSignal triggers a 'cancel' envelope to
153
+ // background and rejects the local promise with a DOM-standard
154
+ // AbortError. The background side's UI_CANCEL handler aborts the
155
+ // handler's ctx.signal and drops the reply.
156
+ var rpcCounter = 0;
157
+ var rpcInflight = Object.create(null);
158
+
159
+ function makeRequestId() {
160
+ rpcCounter += 1;
161
+ return 'r' + rpcCounter + '-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8);
162
+ }
163
+
164
+ function RpcError(message, code, stage, transport) {
165
+ var e = new Error(message);
166
+ e.name = 'RpcError';
167
+ if (code) {
168
+ try { e.cause = {code: code}; } catch (_) { /* old runtimes */ }
169
+ e.code = code;
170
+ }
171
+ // Diagnostic fields ride as own props so a miniapp can show exactly which
172
+ // pipeline stage and transport failed, not just a message string.
173
+ if (stage) e.stage = stage;
174
+ if (transport) e.transport = transport;
175
+ return e;
176
+ }
177
+
178
+ function RpcTimeoutError(message) {
179
+ var e = new Error(message || 'RPC timed out');
180
+ e.name = 'RpcTimeoutError';
181
+ return e;
182
+ }
183
+
184
+ function makeAbortError() {
185
+ var e;
186
+ try { e = new DOMException('aborted', 'AbortError'); } catch (_) {
187
+ e = new Error('aborted');
188
+ e.name = 'AbortError';
189
+ }
190
+ return e;
191
+ }
192
+
193
+ function request(channel, payload, options) {
194
+ var opts = options || {};
195
+ var signal = opts.signal;
196
+ var timeoutMs = typeof opts.timeout === 'number' ? opts.timeout : null;
197
+ var id = makeRequestId();
198
+
199
+ return new Promise(function (resolve, reject) {
200
+ var done = false;
201
+ var timeoutHandle = null;
202
+ var abortListener = null;
203
+
204
+ function cleanup() {
205
+ if (done) return;
206
+ done = true;
207
+ delete rpcInflight[id];
208
+ if (timeoutHandle != null) { clearTimeout(timeoutHandle); timeoutHandle = null; }
209
+ if (signal && abortListener) {
210
+ try { signal.removeEventListener('abort', abortListener); } catch (_) {}
211
+ }
212
+ }
213
+
214
+ function settle(envelope) {
215
+ if (done) return;
216
+ cleanup();
217
+ if (envelope && envelope.ok === true) {
218
+ resolve(envelope.result);
219
+ } else if (envelope && envelope.ok === false) {
220
+ reject(RpcError(envelope.error && envelope.error.message || 'RPC failed', envelope.error && envelope.error.code, envelope.error && envelope.error.stage, envelope.error && envelope.error.transport));
221
+ } else {
222
+ reject(RpcError('malformed RPC reply'));
223
+ }
224
+ }
225
+
226
+ rpcInflight[id] = settle;
227
+
228
+ if (signal) {
229
+ if (signal.aborted) {
230
+ cleanup();
231
+ reject(makeAbortError());
232
+ return;
233
+ }
234
+ abortListener = function () {
235
+ if (done) return;
236
+ cleanup();
237
+ postEnvelope({type: 'cancel', requestId: id});
238
+ reject(makeAbortError());
239
+ };
240
+ try { signal.addEventListener('abort', abortListener); } catch (_) {}
241
+ }
242
+
243
+ if (timeoutMs != null) {
244
+ timeoutHandle = setTimeout(function () {
245
+ if (done) return;
246
+ cleanup();
247
+ postEnvelope({type: 'cancel', requestId: id});
248
+ reject(RpcTimeoutError());
249
+ }, timeoutMs);
250
+ }
251
+
252
+ var envelope = {type: 'msg', seq: outboundSeq++, channel: String(channel), payload: payload, requestId: id};
253
+ if (!ready) {
254
+ outboundQueue.push(envelope);
255
+ return;
256
+ }
257
+ postEnvelope(envelope);
258
+ });
259
+ }
260
+
261
+ function onOpen(cb) {
262
+ if (typeof cb !== 'function') return function () {};
263
+ openHandlers.push(cb);
264
+ if (ready) {
265
+ try { cb(); } catch (e) {}
266
+ }
267
+ return function () {
268
+ var idx = openHandlers.indexOf(cb);
269
+ if (idx >= 0) openHandlers.splice(idx, 1);
270
+ };
271
+ }
272
+
273
+ function onClose(cb) {
274
+ if (typeof cb !== 'function') return function () {};
275
+ closeHandlers.push(cb);
276
+ return function () {
277
+ var idx = closeHandlers.indexOf(cb);
278
+ if (idx >= 0) closeHandlers.splice(idx, 1);
279
+ };
280
+ }
281
+
282
+ function readyFn() {
283
+ if (ready) return;
284
+ ready = true;
285
+ postEnvelope({ type: 'ready' });
286
+ flushQueue();
287
+ for (var i = 0; i < openHandlers.length; i++) {
288
+ try { openHandlers[i](); } catch (e) {}
289
+ }
290
+ }
291
+
292
+ function setBackgroundColorFn(color) {
293
+ // Tells the host what to paint the native WebView layer (the surface
294
+ // exposed during keyboard push-up / resize animations, which page CSS
295
+ // cannot reach). Host validates and falls back to its theme background.
296
+ if (typeof color !== 'string' || !color) return;
297
+ postEnvelope({ type: 'set_background', color: color });
298
+ }
299
+
300
+ var backHandler = null;
301
+
302
+ function setBackActionFn(handler) {
303
+ backHandler = typeof handler === 'function' ? handler : null;
304
+ postEnvelope({ type: 'set_back_action', shown: backHandler !== null });
305
+ }
306
+
307
+ function fireChannel(channel, payload) {
308
+ var list = channelHandlers[channel];
309
+ if (!list || list.length === 0) {
310
+ // No subscriber yet — stash for the first glassly.on(channel, ...).
311
+ // Bounded FIFO: drop the oldest when capped so a stuck channel
312
+ // can't grow unbounded.
313
+ var q = pendingByChannel[channel];
314
+ if (!q) { q = []; pendingByChannel[channel] = q; }
315
+ q.push(payload);
316
+ if (q.length > PENDING_CAP_PER_CHANNEL) q.shift();
317
+ return;
318
+ }
319
+ for (var i = 0; i < list.length; i++) {
320
+ try { list[i](payload); } catch (e) {}
321
+ }
322
+ }
323
+
324
+ function recv(envelope) {
325
+ if (!envelope || typeof envelope !== 'object') return;
326
+ var type = envelope.type;
327
+ if (type === 'msg' && typeof envelope.channel === 'string') {
328
+ // RPC frames carry a requestId; broadcast frames don't. Treat them
329
+ // as disjoint — never fall through from RPC to broadcast. An orphan
330
+ // RPC reply (caller already aborted/timed out) is silently dropped.
331
+ if (typeof envelope.requestId === 'string') {
332
+ var settle = rpcInflight[envelope.requestId];
333
+ if (settle) settle(envelope.payload);
334
+ return;
335
+ }
336
+ fireChannel(envelope.channel, envelope.payload);
337
+ return;
338
+ }
339
+ if (type === 'open') {
340
+ for (var j = 0; j < openHandlers.length; j++) {
341
+ try { openHandlers[j](); } catch (e) {}
342
+ }
343
+ return;
344
+ }
345
+ if (type === 'close') {
346
+ for (var k = 0; k < closeHandlers.length; k++) {
347
+ try { closeHandlers[k](); } catch (e) {}
348
+ }
349
+ return;
350
+ }
351
+ if (type === 'back') {
352
+ if (backHandler) {
353
+ try { backHandler(); } catch (e) {}
354
+ }
355
+ return;
356
+ }
357
+ if (type === 'cancel' && typeof envelope.requestId === 'string') {
358
+ // Background-side cancel (reserved; currently unused). If we had a
359
+ // future bidirectional RPC this would abort an in-flight UI handler.
360
+ return;
361
+ }
362
+ if (type === 'ack') {
363
+ return;
364
+ }
365
+ }
366
+
367
+ window.glassly = {
368
+ send: send,
369
+ on: on,
370
+ request: request,
371
+ onOpen: onOpen,
372
+ onClose: onClose,
373
+ ready: readyFn,
374
+ setBackgroundColor: setBackgroundColorFn,
375
+ setBackAction: setBackActionFn,
376
+ /** @internal — packageName for diagnostic logs only. */
377
+ _packageName: packageName,
378
+ };
379
+ window.__host = { recv: recv };
380
+ })();
381
+ `.trim();
382
+ }
383
+ //# sourceMappingURL=uiShim.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uiShim.js","sourceRoot":"","sources":["../../src/host/uiShim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAQH,MAAM,UAAU,WAAW,CAAC,OAAsB;IAChD,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;IAC3D,+DAA+D;IAC/D,mEAAmE;IACnE,yDAAyD;IACzD,OAAO;;;;sBAIa,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoVpC,CAAC,IAAI,EAAE,CAAA;AACR,CAAC"}