@gramlot/gramlot 0.3.16 → 0.3.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/editors/__init__.py +1 -1
- package/dist/components/editors/assets/collection.js +16162 -11689
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/editors/grammar.json +3 -0
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/collection.js +134 -179
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/inputs/grammar.json +19 -0
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +4643 -3481
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/layout/grammar.json +509 -1
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +306 -94
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/components/tree/grammar.json +16 -0
- package/dist/components/typst/__init__.py +1 -1
- package/dist/components/typst/assets/collection.js +2 -2
- package/dist/components/typst/assets/index.js +1 -1
- package/dist/gramlot.js +2124 -506
- package/dist/gramlot.min.js +95 -40
- package/dist/runtime-notices.json +4 -0
- package/package.json +12 -8
- package/src/binding/device-resolver.js +27 -0
- package/src/binding/error-notifications.js +84 -0
- package/src/binding/remote-resolver.d.ts +18 -0
- package/src/binding/remote.js +11 -4
- package/src/binding/runtime.js +6 -2
- package/src/bootstrap.js +4 -4
- package/src/components/buttons.js +10 -3
- package/src/components/code-mirror-engine.js +14 -4
- package/src/components/code-mirror.js +2 -3
- package/src/components/container.js +1 -1
- package/src/components/date-calendar.js +1 -1
- package/src/components/formlet-labels.js +5 -4
- package/src/components/html-document.js +4 -2
- package/src/components/html-editor.js +2 -2
- package/src/components/jodit-engine.js +72 -11
- package/src/components/labelbox.js +19 -63
- package/src/components/layout.js +31 -19
- package/src/components/markdown-document.js +1 -1
- package/src/components/markdown-editor.js +2 -2
- package/src/components/menus.js +7 -6
- package/src/components/prose-mirror-engine.js +14 -1
- package/src/components/registry.js +2 -0
- package/src/components/selected-container.js +107 -14
- package/src/components/toolbar-overflow.js +9 -8
- package/src/components/transform-pane.js +140 -0
- package/src/components/tray-geometry.js +87 -0
- package/src/components/tray-pane.js +435 -0
- package/src/components/tray-settings.js +62 -0
- package/src/components/tree.js +115 -21
- package/src/components/typed-editor.js +1 -1
- package/src/components/typst-client.js +14 -13
- package/src/components/validation.js +1 -1
- package/src/components/window-surface.js +281 -0
- package/src/components/windows.js +32 -261
- package/src/device-frames.js +195 -0
- package/src/filesystem.d.ts +12 -5
- package/src/filesystem.js +8 -8
- package/src/gramlot.d.ts +3 -1
- package/src/gramlot.js +11 -4
- package/src/handlers/device-handler.d.ts +75 -0
- package/src/handlers/device-handler.js +1037 -0
- package/src/handlers/dom-handler.d.ts +2 -0
- package/src/handlers/dom-handler.js +9 -0
- package/src/handlers/rpc-handler.d.ts +2 -2
- package/src/http-transport.d.ts +3 -3
- package/src/http-transport.js +17 -11
- package/src/ide-documents.js +6 -4
- package/src/ide-runtime.d.ts +14 -3
- package/src/ide-runtime.js +123 -94
- package/src/index-page-runtime.d.ts +2 -2
- package/src/index-page-runtime.js +91 -43
- package/src/index-page-styles.js +1 -1
- package/src/index.d.ts +7 -2
- package/src/index.js +5 -1
- package/src/inout.d.ts +1 -1
- package/src/inout.js +5 -1
- package/src/mobile-environment.d.ts +6 -0
- package/src/mobile-environment.js +52 -0
- package/src/navigation-resolver.d.ts +14 -2
- package/src/renderer/attributes.js +5 -2
- package/src/renderer/drag-drop.js +5 -4
- package/src/renderer/gramlot-renderer.d.ts +13 -2
- package/src/renderer/gramlot-renderer.js +49 -13
- package/src/server/index-page.js +4 -4
- package/src/view/controls.js +48 -23
- package/src/view/html.js +85 -4
- package/src/view/labelbox.js +95 -0
- package/themes/gramlot-base/theme.css +4 -0
- package/themes/mela/theme.css +62 -15
- package/src/date-parser/civil.js +0 -61
- package/src/date-parser/index.d.ts +0 -18
- package/src/date-parser/index.js +0 -196
- package/src/date-parser/locales.js +0 -257
|
@@ -0,0 +1,1037 @@
|
|
|
1
|
+
/* @ts-self-types="./device-handler.d.ts" */
|
|
2
|
+
import {Bag} from '@genrojs/bag';
|
|
3
|
+
import {Handler} from './handler.js';
|
|
4
|
+
import {DeviceResolver} from '../binding/device-resolver.js';
|
|
5
|
+
|
|
6
|
+
const NAMES = ['appinfo', 'location'];
|
|
7
|
+
const ERROR_STATUS = {UNAVAILABLE: 'unavailable', PERMISSION_DENIED: 'denied',
|
|
8
|
+
TIMEOUT: 'error', FAILED: 'error', CANCELLED: 'cancelled'};
|
|
9
|
+
|
|
10
|
+
function failureAttributes(error) {
|
|
11
|
+
const code = Object.hasOwn(ERROR_STATUS, error?.code) ? error.code : 'FAILED';
|
|
12
|
+
return {status: ERROR_STATUS[code],
|
|
13
|
+
error: code === 'CANCELLED' ? null : {code, message: String(error?.message ?? 'Device operation failed')}};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Device values, desired updates and effective state in one page's Data Bag. */
|
|
17
|
+
export class DeviceHandler extends Handler {
|
|
18
|
+
#database = null;
|
|
19
|
+
#clipboardRequest = null;
|
|
20
|
+
#captureRequest = null;
|
|
21
|
+
#audio = null;
|
|
22
|
+
#barcodeRequest = null;
|
|
23
|
+
#hapticRequest = null;
|
|
24
|
+
#stateEvents = new Map();
|
|
25
|
+
#sensors = new Map();
|
|
26
|
+
#biometricsRequest = null;
|
|
27
|
+
#notificationRequest = null;
|
|
28
|
+
#notificationEvents = null;
|
|
29
|
+
#pushRequest = null;
|
|
30
|
+
#pushEvents = null;
|
|
31
|
+
#shortcutEvents = null;
|
|
32
|
+
#linkEvents = null;
|
|
33
|
+
|
|
34
|
+
constructor(gramlot) {
|
|
35
|
+
super(gramlot);
|
|
36
|
+
this.provider = null;
|
|
37
|
+
this.disposed = false;
|
|
38
|
+
this.monitor = null;
|
|
39
|
+
this.retiring = null;
|
|
40
|
+
this.resolvers = new Map();
|
|
41
|
+
this.cleanup = Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
installData() {
|
|
45
|
+
const {gramlot} = this;
|
|
46
|
+
gramlot.data.setItem('gramlot.device.database', null, {status: 'idle', error: null});
|
|
47
|
+
gramlot.data.setItem('gramlot.device.orientation', null, {status: 'idle', error: null, listening: false});
|
|
48
|
+
for (const name of ['gyroscope', 'accelerometer']) {
|
|
49
|
+
gramlot.data.setItem(`gramlot.device.${name}`, null,
|
|
50
|
+
{enabled: false, watching: false, status: 'idle', error: null});
|
|
51
|
+
gramlot.data.getNode(`gramlot.device.${name}`).setAttr({error: null}, false, true, false);
|
|
52
|
+
this.#sensors.set(name, {node: gramlot.data.getNode(`gramlot.device.${name}`),
|
|
53
|
+
stream: null, cleanup: Promise.resolve()});
|
|
54
|
+
}
|
|
55
|
+
for (const name of NAMES) {
|
|
56
|
+
const path = `gramlot.device.${name}`;
|
|
57
|
+
const attrs = {status: 'idle', error: null, cacheTime: name === 'appinfo' ? -1 : 5};
|
|
58
|
+
if (name === 'location') Object.assign(attrs, {enabled: false, watching: false});
|
|
59
|
+
gramlot.data.setItem(path, null);
|
|
60
|
+
const node = gramlot.data.getNode(path);
|
|
61
|
+
node.setAttr(attrs, false, true, false);
|
|
62
|
+
const resolver = new DeviceResolver(this, name, attrs.cacheTime);
|
|
63
|
+
node.resolver = resolver;
|
|
64
|
+
this.resolvers.set(name, resolver);
|
|
65
|
+
}
|
|
66
|
+
gramlot.data.setItem('gramlot.device.clipboard', null,
|
|
67
|
+
{status: 'idle', error: null, text: '', copy: null, paste: null, operation: null});
|
|
68
|
+
const clipboard = gramlot.data.getNode('gramlot.device.clipboard');
|
|
69
|
+
gramlot.data.setItem('gramlot.device.camera', null, {status: 'idle', error: null, capture: null, choose: null});
|
|
70
|
+
const camera = gramlot.data.getNode('gramlot.device.camera');
|
|
71
|
+
gramlot.data.setItem('gramlot.device.barcode', null, {status: 'idle', error: null, scan: null});
|
|
72
|
+
const barcode = gramlot.data.getNode('gramlot.device.barcode');
|
|
73
|
+
gramlot.data.setItem('gramlot.device.biometrics', null, {status: 'idle', error: null, verify: null});
|
|
74
|
+
const biometrics = gramlot.data.getNode('gramlot.device.biometrics');
|
|
75
|
+
const notifications = new Bag();
|
|
76
|
+
notifications.setItem('request', new Bag());
|
|
77
|
+
for (const name of ['scheduled', 'received', 'action']) notifications.setItem(name, null);
|
|
78
|
+
gramlot.data.setItem('gramlot.device.notifications', notifications,
|
|
79
|
+
{status: 'idle', error: null, schedule: null, listening: false, listenerError: null});
|
|
80
|
+
const notificationNode = gramlot.data.getNode('gramlot.device.notifications');
|
|
81
|
+
const push = new Bag();
|
|
82
|
+
for (const name of ['registration', 'received', 'action']) push.setItem(name, null);
|
|
83
|
+
gramlot.data.setItem('gramlot.device.push', push,
|
|
84
|
+
{status: 'idle', error: null, register: null, listening: false, listenerError: null});
|
|
85
|
+
const pushNode = gramlot.data.getNode('gramlot.device.push');
|
|
86
|
+
const links = new Bag();
|
|
87
|
+
links.setItem('request', null);
|
|
88
|
+
gramlot.data.setItem('gramlot.device.links', links, {listening: false, listenerError: null});
|
|
89
|
+
const shortcuts = new Bag();
|
|
90
|
+
shortcuts.setItem('request', null);
|
|
91
|
+
gramlot.data.setItem('gramlot.device.shortcuts', shortcuts, {listening: false, listenerError: null});
|
|
92
|
+
gramlot.data.setItem('gramlot.device.network', null, {status: 'idle', error: null, listening: false});
|
|
93
|
+
const lifecycle = new Bag();
|
|
94
|
+
lifecycle.setItem('active', null);
|
|
95
|
+
lifecycle.setItem('event', null);
|
|
96
|
+
lifecycle.setItem('sequence', 0);
|
|
97
|
+
gramlot.data.setItem('gramlot.device.lifecycle', lifecycle, {status: 'idle', error: null, listening: false});
|
|
98
|
+
gramlot.data.setItem('gramlot.device.haptics', null,
|
|
99
|
+
{status: 'idle', error: null, impact: null, style: 'medium', vibrate: null, duration: 600});
|
|
100
|
+
const haptics = gramlot.data.getNode('gramlot.device.haptics');
|
|
101
|
+
gramlot.data.setItem('gramlot.device.audio', null,
|
|
102
|
+
{status: 'idle', error: null, recording: false, start: null, stop: null, cancel: null,
|
|
103
|
+
share: null, shareStatus: 'idle', shareError: null, shareActivity: null});
|
|
104
|
+
const audio = gramlot.data.getNode('gramlot.device.audio');
|
|
105
|
+
gramlot.data.subscribe('gramlot.device', {update: event => {
|
|
106
|
+
if (this.disposed) return;
|
|
107
|
+
for (const [name, sensor] of this.#sensors) {
|
|
108
|
+
if (event.node === sensor.node && event.attrs_diff?.enabled) {
|
|
109
|
+
if (typeof sensor.node.attr.enabled !== 'boolean') throw new TypeError('Device enabled must be boolean');
|
|
110
|
+
this.#reconcileSensor(name, sensor);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (event.node === clipboard && event.fired) {
|
|
114
|
+
if (event.attrs_diff?.copy?.new === true) this.#clipboardCommand(clipboard, 'copy');
|
|
115
|
+
if (event.attrs_diff?.paste?.new === true) this.#clipboardCommand(clipboard, 'paste');
|
|
116
|
+
}
|
|
117
|
+
if (event.node === audio && event.fired) {
|
|
118
|
+
if (event.attrs_diff?.start?.new === true) this.#startAudio(audio);
|
|
119
|
+
if (event.attrs_diff?.stop?.new === true) this.#stopAudio(audio);
|
|
120
|
+
if (event.attrs_diff?.cancel?.new === true) this.#cancelAudio(audio);
|
|
121
|
+
if (event.attrs_diff?.share?.new === true) this.#shareAudio(audio);
|
|
122
|
+
}
|
|
123
|
+
if (event.node === haptics && event.fired && event.attrs_diff?.impact?.new === true) {
|
|
124
|
+
this.#impact(haptics);
|
|
125
|
+
}
|
|
126
|
+
if (event.node === haptics && event.fired && event.attrs_diff?.vibrate?.new === true) {
|
|
127
|
+
this.#vibrate(haptics);
|
|
128
|
+
}
|
|
129
|
+
if (event.node === barcode && event.fired && event.attrs_diff?.scan?.new === true) {
|
|
130
|
+
this.#scanBarcode(barcode);
|
|
131
|
+
}
|
|
132
|
+
if (event.node === camera && event.fired && event.attrs_diff?.choose?.new === true) {
|
|
133
|
+
this.#photo(camera, 'choose');
|
|
134
|
+
}
|
|
135
|
+
if (event.node === camera && event.fired && event.attrs_diff?.capture?.new === true) {
|
|
136
|
+
this.#photo(camera, 'capture');
|
|
137
|
+
}
|
|
138
|
+
if (event.node === biometrics && event.fired && event.attrs_diff?.verify?.new === true) {
|
|
139
|
+
this.#verifyBiometrics(biometrics);
|
|
140
|
+
}
|
|
141
|
+
if (event.node === notificationNode && event.fired && event.attrs_diff?.schedule?.new === true) {
|
|
142
|
+
this.#scheduleNotification(notificationNode);
|
|
143
|
+
}
|
|
144
|
+
if (event.node === pushNode && event.fired && event.attrs_diff?.register?.new === true) {
|
|
145
|
+
this.#registerPush(pushNode);
|
|
146
|
+
}
|
|
147
|
+
for (const resolver of this.resolvers.values()) {
|
|
148
|
+
if (event.node !== resolver._node) continue;
|
|
149
|
+
const {cacheTime} = event.node.attr;
|
|
150
|
+
if (typeof cacheTime !== 'number' || !Number.isFinite(cacheTime) || (cacheTime < 0 && cacheTime !== -1)) {
|
|
151
|
+
throw new TypeError('Device cacheTime must be -1 or a nonnegative number of seconds');
|
|
152
|
+
}
|
|
153
|
+
resolver.cacheTime = cacheTime;
|
|
154
|
+
if (resolver.name === 'location') {
|
|
155
|
+
if (typeof event.node.attr.enabled !== 'boolean') throw new TypeError('Device enabled must be boolean');
|
|
156
|
+
this.reconcile();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Attach the explicitly selected provider once, before the page reads device values. */
|
|
163
|
+
attach(provider, {dispose = null} = {}) {
|
|
164
|
+
this.assertLive();
|
|
165
|
+
if (this.provider) throw new Error('A device provider is already attached');
|
|
166
|
+
if (!Array.isArray(provider.capabilities) || provider.capabilities.some(name => ![...NAMES, 'camera', 'biometrics', 'notifications', 'push', 'shortcuts', 'barcode', 'network', 'lifecycle', 'haptics', 'audio', 'clipboard', 'links', 'orientation', 'gyroscope', 'accelerometer', 'database'].includes(name))) {
|
|
167
|
+
throw new TypeError('Unknown device provider capability');
|
|
168
|
+
}
|
|
169
|
+
if(dispose !== null && typeof dispose !== 'function')throw new TypeError('Device attachment dispose must be a function');
|
|
170
|
+
this.releaseProvider = dispose;
|
|
171
|
+
this.provider = provider;
|
|
172
|
+
this.installData();
|
|
173
|
+
this.reconcile();
|
|
174
|
+
this.#startStateEvents('orientation', (callback, signal) => provider.startOrientationEvents(callback, signal), (node, state) => {
|
|
175
|
+
if (!['portrait', 'landscape'].includes(state?.type) || !['primary', 'secondary'].includes(state.direction) ||
|
|
176
|
+
!Number.isFinite(state.timestamp)) throw new TypeError('Invalid screen orientation from device provider');
|
|
177
|
+
const value = new Bag();
|
|
178
|
+
for (const key of ['type', 'direction', 'timestamp']) value.setItem(key, state[key]);
|
|
179
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
180
|
+
});
|
|
181
|
+
this.#startStateEvents('network', (callback, signal) => provider.startNetworkEvents(callback, signal), (node, state) => {
|
|
182
|
+
if (typeof state?.connected !== 'boolean' || !['none', 'unknown', 'wifi', 'cellular'].includes(state.type)) {
|
|
183
|
+
throw new TypeError('Invalid network state from device provider');
|
|
184
|
+
}
|
|
185
|
+
const value = new Bag();
|
|
186
|
+
value.setItem('connected', state.connected);
|
|
187
|
+
value.setItem('type', state.type);
|
|
188
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
189
|
+
});
|
|
190
|
+
this.#startStateEvents('lifecycle', (callback, signal) => provider.startLifecycleEvents(callback, signal), (node, event) => {
|
|
191
|
+
if (event?.kind === 'state' && typeof event.active === 'boolean') {
|
|
192
|
+
node.staticValue.setItem('active', event.active);
|
|
193
|
+
} else if (event?.kind === 'pause' || event?.kind === 'resume') {
|
|
194
|
+
node.staticValue.setItem('event', event.kind);
|
|
195
|
+
node.staticValue.setItem('sequence', node.staticValue.getItem('sequence') + 1);
|
|
196
|
+
} else {
|
|
197
|
+
throw new TypeError('Invalid lifecycle event from device provider');
|
|
198
|
+
}
|
|
199
|
+
node.setAttr({status: 'success', error: null}, true, true, false);
|
|
200
|
+
});
|
|
201
|
+
if (provider.capabilities.includes('links')) {
|
|
202
|
+
this.#startLinkEvents(this.gramlot.data.getNode('gramlot.device.links'));
|
|
203
|
+
}
|
|
204
|
+
if (provider.capabilities.includes('shortcuts')) {
|
|
205
|
+
this.#startShortcutEvents(this.gramlot.data.getNode('gramlot.device.shortcuts'));
|
|
206
|
+
}
|
|
207
|
+
if (provider.capabilities.includes('push')) {
|
|
208
|
+
this.#startPushEvents(this.gramlot.data.getNode('gramlot.device.push'));
|
|
209
|
+
}
|
|
210
|
+
if (provider.capabilities.includes('notifications')) {
|
|
211
|
+
this.#startNotificationEvents(this.gramlot.data.getNode('gramlot.device.notifications'));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
assertLive() {
|
|
216
|
+
if (this.disposed) throw new Error('DeviceHandler is disposed');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
resolver(name) {
|
|
220
|
+
this.assertLive();
|
|
221
|
+
if (!this.resolvers.has(name)) throw new TypeError(`Unknown device value: ${name}`);
|
|
222
|
+
return this.resolvers.get(name);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
invalidate(name) { this.resolver(name).reset(); }
|
|
226
|
+
|
|
227
|
+
/** Command only. Success/failure is delivered through Data, never a public Promise. */
|
|
228
|
+
refresh(name) {
|
|
229
|
+
const resolver = this.resolver(name);
|
|
230
|
+
if (resolver.request) return;
|
|
231
|
+
const request = {revision: resolver.revision, received: resolver.received};
|
|
232
|
+
resolver.request = request;
|
|
233
|
+
this.attributes(resolver, {status: 'pending', error: null});
|
|
234
|
+
Promise.resolve().then(() => {
|
|
235
|
+
if (this.disposed || resolver.request !== request) return;
|
|
236
|
+
this.requireCapability(name);
|
|
237
|
+
return name === 'appinfo' ? this.provider.readAppInfo() : this.provider.readLocation();
|
|
238
|
+
}).then(value => {
|
|
239
|
+
if (this.disposed || resolver.request !== request) return;
|
|
240
|
+
this.accept(resolver, value);
|
|
241
|
+
// Invalidation while pending retains the arriving result, but leaves it stale.
|
|
242
|
+
if (resolver.revision !== request.revision) resolver._lastUpdate = null;
|
|
243
|
+
}).catch(error => {
|
|
244
|
+
if (!this.disposed && resolver.request === request && resolver.received === request.received) {
|
|
245
|
+
this.failure(resolver, error);
|
|
246
|
+
}
|
|
247
|
+
}).finally(() => {
|
|
248
|
+
if (resolver.request === request) resolver.request = null;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Page-owned state streams share lifetime/error handling, never public dispatch.
|
|
253
|
+
#startStateEvents(name, start, accept) {
|
|
254
|
+
const node = this.gramlot.data.getNode(`gramlot.device.${name}`);
|
|
255
|
+
if (!this.provider.capabilities.includes(name)) {
|
|
256
|
+
node.setAttr(failureAttributes({code: 'UNAVAILABLE', message: `${name} is unavailable`}), true, true, false);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const events = {controller: new AbortController()};
|
|
260
|
+
this.#stateEvents.set(name, events);
|
|
261
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
262
|
+
events.start = Promise.resolve().then(() => {
|
|
263
|
+
if (events.controller.signal.aborted) return async () => {};
|
|
264
|
+
return start(value => {
|
|
265
|
+
if (events.controller.signal.aborted) return;
|
|
266
|
+
try { accept(node, value); }
|
|
267
|
+
catch (error) { node.setAttr(failureAttributes(error), true, true, false); }
|
|
268
|
+
}, events.controller.signal);
|
|
269
|
+
}).then(stop => {
|
|
270
|
+
if (!events.controller.signal.aborted) node.setAttr({listening: true}, true, true, false);
|
|
271
|
+
return stop;
|
|
272
|
+
});
|
|
273
|
+
events.start.catch(error => {
|
|
274
|
+
const aborted = events.controller.signal.aborted;
|
|
275
|
+
events.controller.abort();
|
|
276
|
+
if (!aborted) node.setAttr({...failureAttributes(error), listening: false}, true, true, false);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#reconcileSensor(name, sensor) {
|
|
281
|
+
const {node} = sensor;
|
|
282
|
+
if (!node.attr.enabled) {
|
|
283
|
+
const stream = sensor.stream;
|
|
284
|
+
if (!stream) return;
|
|
285
|
+
sensor.stream = null;
|
|
286
|
+
stream.controller.abort();
|
|
287
|
+
sensor.cleanup = stream.start.then(stop => stop(), () => undefined).then(() => {
|
|
288
|
+
if (!this.disposed && !sensor.stream) node.setAttr({watching: false, status: 'idle', error: null}, true, true, false);
|
|
289
|
+
});
|
|
290
|
+
sensor.cleanup.catch(error => {
|
|
291
|
+
if (!this.disposed) node.setAttr(failureAttributes(error), true, true, false);
|
|
292
|
+
});
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (sensor.stream) return;
|
|
296
|
+
const stream = {controller: new AbortController()};
|
|
297
|
+
sensor.stream = stream;
|
|
298
|
+
stream.start = sensor.cleanup.then(() => {
|
|
299
|
+
if (stream.controller.signal.aborted) return async () => {};
|
|
300
|
+
this.requireCapability(name);
|
|
301
|
+
const accept = event => {
|
|
302
|
+
if (this.disposed || stream.controller.signal.aborted) return;
|
|
303
|
+
try {
|
|
304
|
+
if (event?.kind === 'state' && typeof event.watching === 'boolean') {
|
|
305
|
+
node.setAttr({watching: event.watching, status: event.watching ? 'pending' : 'idle', error: null}, true, true, false);
|
|
306
|
+
} else if (event?.kind === 'sample') {
|
|
307
|
+
for (const field of ['x', 'y', 'z', 'timestamp']) {
|
|
308
|
+
if (!Number.isFinite(event[field])) throw new TypeError(`Invalid ${name}.${field}`);
|
|
309
|
+
}
|
|
310
|
+
if (!node.attr.watching) return;
|
|
311
|
+
if (node.staticValue && event.timestamp < node.staticValue.getItem('timestamp')) return;
|
|
312
|
+
const value = new Bag();
|
|
313
|
+
for (const field of ['x', 'y', 'z', 'timestamp']) value.setItem(field, event[field]);
|
|
314
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
315
|
+
} else if (event?.kind === 'error' && event.error instanceof Error) {
|
|
316
|
+
node.setAttr({...failureAttributes(event.error), watching: false}, true, true, false);
|
|
317
|
+
} else throw new TypeError(`Invalid ${name} event`);
|
|
318
|
+
} catch (error) { node.setAttr(failureAttributes(error), true, true, false); }
|
|
319
|
+
};
|
|
320
|
+
return name === 'gyroscope' ? this.provider.startGyroscopeUpdates(accept, stream.controller.signal)
|
|
321
|
+
: this.provider.startAccelerometerUpdates(accept, stream.controller.signal);
|
|
322
|
+
});
|
|
323
|
+
stream.start.catch(error => {
|
|
324
|
+
if (!this.disposed && !stream.controller.signal.aborted) {
|
|
325
|
+
node.setAttr({...failureAttributes(error), watching: false}, true, true, false);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#startAudio(node) {
|
|
332
|
+
const previous = this.#audio;
|
|
333
|
+
if (previous) {
|
|
334
|
+
if (previous.phase !== 'complete') return;
|
|
335
|
+
previous.phase = 'replacing';
|
|
336
|
+
node.setValue(null, true, {status: 'pending', error: null, recording: false,
|
|
337
|
+
shareStatus: 'idle', shareError: null, shareActivity: null}, true, false);
|
|
338
|
+
previous.close = previous.start.then(session => session.dispose()).then(() => {
|
|
339
|
+
if (this.disposed || previous.controller.signal.aborted || this.#audio !== previous) return;
|
|
340
|
+
this.#audio = null;
|
|
341
|
+
this.#startAudio(node);
|
|
342
|
+
}).catch(error => {
|
|
343
|
+
if (!this.disposed && this.#audio === previous) {
|
|
344
|
+
previous.phase = 'failed';
|
|
345
|
+
node.setAttr(failureAttributes(error), true, true, false);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const audio = {controller: new AbortController(), phase: 'starting'};
|
|
351
|
+
this.#audio = audio;
|
|
352
|
+
node.setValue(null, true, {status: 'pending', error: null, recording: false,
|
|
353
|
+
shareStatus: 'idle', shareError: null, shareActivity: null}, true, false);
|
|
354
|
+
audio.start = Promise.resolve().then(async () => {
|
|
355
|
+
if (audio.controller.signal.aborted) return null;
|
|
356
|
+
this.requireCapability('audio');
|
|
357
|
+
return this.provider.startAudioRecording(error => {
|
|
358
|
+
if (this.#audio === audio && !audio.controller.signal.aborted) this.#cancelAudio(node, error);
|
|
359
|
+
}, audio.controller.signal);
|
|
360
|
+
});
|
|
361
|
+
audio.start.then(session => {
|
|
362
|
+
if (!session || audio.controller.signal.aborted || this.disposed) return;
|
|
363
|
+
audio.phase = 'recording';
|
|
364
|
+
node.setAttr({status: 'recording', error: null, recording: true}, true, true, false);
|
|
365
|
+
}).catch(error => {
|
|
366
|
+
if (audio.controller.signal.aborted || this.disposed) return;
|
|
367
|
+
if (this.#audio === audio) this.#audio = null;
|
|
368
|
+
node.setAttr({...failureAttributes(error), recording: false}, true, true, false);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
#stopAudio(node) {
|
|
373
|
+
const audio = this.#audio;
|
|
374
|
+
if (!audio || audio.phase !== 'recording') return;
|
|
375
|
+
audio.phase = 'stopping';
|
|
376
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
377
|
+
audio.stop = audio.start.then(session => session.stop()).then(result => {
|
|
378
|
+
if (audio.controller.signal.aborted || this.disposed) return;
|
|
379
|
+
if (typeof result?.url !== 'string' || !result.url || typeof result.mimeType !== 'string' ||
|
|
380
|
+
!result.mimeType.startsWith('audio/') || !Number.isFinite(result.duration) || result.duration < 0) {
|
|
381
|
+
throw new TypeError('Invalid audio result from device provider');
|
|
382
|
+
}
|
|
383
|
+
const value = new Bag();
|
|
384
|
+
for (const key of ['url', 'mimeType', 'duration']) value.setItem(key, result[key]);
|
|
385
|
+
audio.phase = 'complete';
|
|
386
|
+
node.setValue(value, true, {status: 'success', error: null, recording: false}, true, false);
|
|
387
|
+
}).catch(error => {
|
|
388
|
+
if (!audio.controller.signal.aborted && !this.disposed) this.#cancelAudio(node, error);
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
#shareAudio(node) {
|
|
393
|
+
const audio = this.#audio;
|
|
394
|
+
if (audio?.phase === 'sharing') return;
|
|
395
|
+
if (!audio || audio.phase !== 'complete') {
|
|
396
|
+
const failure = failureAttributes(new Error('Record and stop audio before sharing'));
|
|
397
|
+
node.setAttr({shareStatus: failure.status, shareError: failure.error, shareActivity: null}, true, true, false);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
audio.phase = 'sharing';
|
|
401
|
+
node.setAttr({shareStatus: 'pending', shareError: null, shareActivity: null}, true, true, false);
|
|
402
|
+
audio.share = audio.start.then(session => session.share(audio.controller.signal)).then(result => {
|
|
403
|
+
if (audio.controller.signal.aborted || this.disposed) return;
|
|
404
|
+
if (typeof result?.activity !== 'string') throw new TypeError('Invalid audio share result from device provider');
|
|
405
|
+
node.setAttr({shareStatus: 'success', shareError: null, shareActivity: result.activity}, true, true, false);
|
|
406
|
+
}).catch(error => {
|
|
407
|
+
if (audio.controller.signal.aborted || this.disposed) return;
|
|
408
|
+
const failure = failureAttributes(error);
|
|
409
|
+
node.setAttr({shareStatus: failure.status, shareError: failure.error, shareActivity: null}, true, true, false);
|
|
410
|
+
}).finally(() => {
|
|
411
|
+
if (this.#audio === audio && !audio.controller.signal.aborted) audio.phase = 'complete';
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
#cancelAudio(node, error = {code: 'CANCELLED', message: 'Audio recording cancelled'}) {
|
|
416
|
+
const audio = this.#audio;
|
|
417
|
+
if (!audio || audio.phase === 'closing' || audio.phase === 'sharing') return;
|
|
418
|
+
audio.phase = 'closing';
|
|
419
|
+
audio.controller.abort();
|
|
420
|
+
node.setValue(null, true, {status: 'pending', error: null,
|
|
421
|
+
shareStatus: 'idle', shareError: null, shareActivity: null}, true, false);
|
|
422
|
+
audio.close = audio.start.then(session => session === null ? undefined : session.dispose(), error => { if (error?.code !== 'CANCELLED') throw error; }).then(() => {
|
|
423
|
+
if (this.disposed || this.#audio !== audio) return;
|
|
424
|
+
this.#audio = null;
|
|
425
|
+
node.setAttr({...failureAttributes(error), recording: false}, true, true, false);
|
|
426
|
+
}).catch(cleanupError => {
|
|
427
|
+
if (!this.disposed && this.#audio === audio) {
|
|
428
|
+
// Keep ownership when cleanup failed; the page may retry cancellation.
|
|
429
|
+
audio.phase = 'failed';
|
|
430
|
+
node.setAttr(failureAttributes(cleanupError), true, true, false);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#clipboardCommand(node, operation) {
|
|
436
|
+
if (this.#clipboardRequest) return;
|
|
437
|
+
const request = new AbortController();
|
|
438
|
+
this.#clipboardRequest = request;
|
|
439
|
+
const text = node.attr.text;
|
|
440
|
+
node.setValue(null, true, {status: 'pending', error: null, operation}, true, false);
|
|
441
|
+
Promise.resolve().then(async () => {
|
|
442
|
+
if (request.signal.aborted) return;
|
|
443
|
+
this.requireCapability('clipboard');
|
|
444
|
+
let result;
|
|
445
|
+
if (operation === 'copy') {
|
|
446
|
+
if (typeof text !== 'string') throw new TypeError('Clipboard text must be a string');
|
|
447
|
+
await this.provider.copyText(text, request.signal);
|
|
448
|
+
result = {text};
|
|
449
|
+
} else {
|
|
450
|
+
result = await this.provider.pasteText(request.signal);
|
|
451
|
+
}
|
|
452
|
+
if (request.signal.aborted) return;
|
|
453
|
+
if (typeof result?.text !== 'string') throw new TypeError('Invalid clipboard text from device provider');
|
|
454
|
+
const value = new Bag();
|
|
455
|
+
value.setItem('text', result.text);
|
|
456
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
457
|
+
}).catch(error => {
|
|
458
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
459
|
+
}).finally(() => {
|
|
460
|
+
if (this.#clipboardRequest === request) this.#clipboardRequest = null;
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
#vibrate(node) {
|
|
465
|
+
if (this.#hapticRequest) return;
|
|
466
|
+
const request = new AbortController();
|
|
467
|
+
this.#hapticRequest = request;
|
|
468
|
+
const {duration} = node.attr;
|
|
469
|
+
node.setValue(null, true, {status: 'pending', error: null}, true, false);
|
|
470
|
+
Promise.resolve().then(async () => {
|
|
471
|
+
if (request.signal.aborted) return;
|
|
472
|
+
this.requireCapability('haptics');
|
|
473
|
+
if (!Number.isInteger(duration) || duration < 1 || duration > 5000) {
|
|
474
|
+
throw new TypeError('Vibration duration must be an integer from 1 to 5000 milliseconds');
|
|
475
|
+
}
|
|
476
|
+
await this.provider.vibrate(duration, request.signal);
|
|
477
|
+
if (request.signal.aborted) return;
|
|
478
|
+
const value = new Bag();
|
|
479
|
+
value.setItem('duration', duration);
|
|
480
|
+
value.setItem('acknowledged', true);
|
|
481
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
482
|
+
}).catch(error => {
|
|
483
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
484
|
+
}).finally(() => {
|
|
485
|
+
if (this.#hapticRequest === request) this.#hapticRequest = null;
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
#impact(node) {
|
|
490
|
+
if (this.#hapticRequest) return;
|
|
491
|
+
const request = new AbortController();
|
|
492
|
+
this.#hapticRequest = request;
|
|
493
|
+
const {style} = node.attr;
|
|
494
|
+
node.setValue(null, true, {status: 'pending', error: null}, true, false);
|
|
495
|
+
Promise.resolve().then(async () => {
|
|
496
|
+
if (request.signal.aborted) return;
|
|
497
|
+
this.requireCapability('haptics');
|
|
498
|
+
if (!['light', 'medium', 'heavy'].includes(style)) throw new TypeError('Invalid haptic style');
|
|
499
|
+
await this.provider.impact(style, request.signal);
|
|
500
|
+
if (request.signal.aborted) return;
|
|
501
|
+
const value = new Bag();
|
|
502
|
+
value.setItem('style', style);
|
|
503
|
+
value.setItem('acknowledged', true);
|
|
504
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
505
|
+
}).catch(error => {
|
|
506
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
507
|
+
}).finally(() => {
|
|
508
|
+
if (this.#hapticRequest === request) this.#hapticRequest = null;
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
#scanBarcode(node) {
|
|
513
|
+
if (this.#barcodeRequest) return;
|
|
514
|
+
const request = new AbortController();
|
|
515
|
+
this.#barcodeRequest = request;
|
|
516
|
+
node.setValue(null, true, {status: 'pending', error: null}, true, false);
|
|
517
|
+
Promise.resolve().then(() => {
|
|
518
|
+
if (request.signal.aborted) return;
|
|
519
|
+
this.requireCapability('barcode');
|
|
520
|
+
return this.provider.scanBarcode(request.signal);
|
|
521
|
+
}).then(result => {
|
|
522
|
+
if (request.signal.aborted) return;
|
|
523
|
+
if (typeof result?.text !== 'string' || !result.text || typeof result.format !== 'string' || !result.format) {
|
|
524
|
+
throw new TypeError('Invalid barcode result from device provider');
|
|
525
|
+
}
|
|
526
|
+
const value = new Bag();
|
|
527
|
+
value.setItem('text', result.text);
|
|
528
|
+
value.setItem('format', result.format);
|
|
529
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
530
|
+
}).catch(error => {
|
|
531
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
532
|
+
}).finally(() => {
|
|
533
|
+
if (this.#barcodeRequest === request) this.#barcodeRequest = null;
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// Camera is an explicit Data event, never a resolver read with a UI side effect.
|
|
538
|
+
#photo(node, operation) {
|
|
539
|
+
if (this.#captureRequest) return;
|
|
540
|
+
const request = new AbortController();
|
|
541
|
+
this.#captureRequest = request;
|
|
542
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
543
|
+
Promise.resolve().then(() => {
|
|
544
|
+
if (request.signal.aborted) return;
|
|
545
|
+
this.requireCapability('camera');
|
|
546
|
+
return operation === 'choose'
|
|
547
|
+
? this.provider.choosePhoto(request.signal)
|
|
548
|
+
: this.provider.takePhoto(request.signal);
|
|
549
|
+
}).then(photo => {
|
|
550
|
+
if (request.signal.aborted) return;
|
|
551
|
+
if (typeof photo?.url !== 'string' || !photo.url || typeof photo.format !== 'string' || !photo.format) {
|
|
552
|
+
throw new TypeError('Invalid camera photo from device provider');
|
|
553
|
+
}
|
|
554
|
+
const value = new Bag();
|
|
555
|
+
value.setItem('url', photo.url);
|
|
556
|
+
value.setItem('format', photo.format);
|
|
557
|
+
node.setValue(value, true, {status: 'success', error: null}, true, false);
|
|
558
|
+
}).catch(error => {
|
|
559
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
560
|
+
}).finally(() => {
|
|
561
|
+
if (this.#captureRequest === request) this.#captureRequest = null;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// A fresh attempt clears the previous local verification, including on cancellation.
|
|
566
|
+
#verifyBiometrics(node) {
|
|
567
|
+
if (this.#biometricsRequest) return;
|
|
568
|
+
const request = new AbortController();
|
|
569
|
+
this.#biometricsRequest = request;
|
|
570
|
+
node.setValue(null, true, {status: 'pending', error: null}, true, false);
|
|
571
|
+
Promise.resolve().then(() => {
|
|
572
|
+
if (request.signal.aborted) return;
|
|
573
|
+
this.requireCapability('biometrics');
|
|
574
|
+
return this.provider.verifyBiometrics(request.signal);
|
|
575
|
+
}).then(() => {
|
|
576
|
+
if (request.signal.aborted) return;
|
|
577
|
+
const result = new Bag();
|
|
578
|
+
result.setItem('verified', true);
|
|
579
|
+
node.setValue(result, true, {status: 'success', error: null}, true, false);
|
|
580
|
+
}).catch(error => {
|
|
581
|
+
if (!request.signal.aborted) node.setValue(null, true, failureAttributes(error), true, false);
|
|
582
|
+
}).finally(() => {
|
|
583
|
+
if (this.#biometricsRequest === request) this.#biometricsRequest = null;
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
#startNotificationEvents(node) {
|
|
588
|
+
if (this.#notificationEvents) return this.#notificationEvents.start;
|
|
589
|
+
const events = {controller: new AbortController()};
|
|
590
|
+
this.#notificationEvents = events;
|
|
591
|
+
events.start = Promise.resolve().then(() => {
|
|
592
|
+
if (events.controller.signal.aborted) return async () => {};
|
|
593
|
+
return this.provider.startNotificationEvents(event => {
|
|
594
|
+
if (events.controller.signal.aborted) return;
|
|
595
|
+
try {
|
|
596
|
+
if (!['received', 'action'].includes(event.kind) || !Number.isInteger(event.id)
|
|
597
|
+
|| typeof event.title !== 'string' || typeof event.body !== 'string'
|
|
598
|
+
|| (event.kind === 'action' && typeof event.actionId !== 'string')) {
|
|
599
|
+
throw new TypeError('Invalid notification event from device provider');
|
|
600
|
+
}
|
|
601
|
+
const value = new Bag();
|
|
602
|
+
for (const key of ['id', 'title', 'body']) value.setItem(key, event[key]);
|
|
603
|
+
if (event.kind === 'action') value.setItem('actionId', event.actionId);
|
|
604
|
+
node.staticValue.setItem(event.kind, value);
|
|
605
|
+
} catch (error) {
|
|
606
|
+
node.setAttr({listenerError: failureAttributes(error).error}, true, true, false);
|
|
607
|
+
}
|
|
608
|
+
}, events.controller.signal);
|
|
609
|
+
}).then(stop => {
|
|
610
|
+
if (!events.controller.signal.aborted) node.setAttr({listening: true}, true, true, false);
|
|
611
|
+
return stop;
|
|
612
|
+
});
|
|
613
|
+
events.start.catch(error => {
|
|
614
|
+
const aborted = events.controller.signal.aborted;
|
|
615
|
+
events.controller.abort();
|
|
616
|
+
if (this.#notificationEvents === events) this.#notificationEvents = null;
|
|
617
|
+
if (!aborted) {
|
|
618
|
+
node.setAttr({listening: false, listenerError: failureAttributes(error).error}, true, true, false);
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
node.setAttr({listenerError: null}, true, true, false);
|
|
622
|
+
return events.start;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
#scheduleNotification(node) {
|
|
626
|
+
if (this.#notificationRequest) return;
|
|
627
|
+
const request = new AbortController();
|
|
628
|
+
this.#notificationRequest = request;
|
|
629
|
+
const value = node.staticValue.getItem('request');
|
|
630
|
+
const input = value instanceof Bag
|
|
631
|
+
? Object.fromEntries(['id', 'title', 'body', 'delay'].map(key => [key, value.getItem(key)])) : null;
|
|
632
|
+
node.staticValue.setItem('scheduled', null);
|
|
633
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
634
|
+
Promise.resolve().then(async () => {
|
|
635
|
+
if (request.signal.aborted) return;
|
|
636
|
+
this.requireCapability('notifications');
|
|
637
|
+
if (!input || !Number.isInteger(input.id) || input.id < 1 || input.id > 2147483647
|
|
638
|
+
|| typeof input.title !== 'string' || !input.title.trim()
|
|
639
|
+
|| typeof input.body !== 'string' || !input.body.trim()
|
|
640
|
+
|| typeof input.delay !== 'number' || !Number.isFinite(input.delay) || input.delay <= 0) {
|
|
641
|
+
throw new TypeError('Notification requires a positive 32-bit id, title, body and positive delay in seconds');
|
|
642
|
+
}
|
|
643
|
+
await this.#startNotificationEvents(node);
|
|
644
|
+
if (request.signal.aborted) return;
|
|
645
|
+
return this.provider.scheduleNotification(input, request.signal);
|
|
646
|
+
}).then(result => {
|
|
647
|
+
if (request.signal.aborted) return;
|
|
648
|
+
if (result?.id !== input.id || typeof result.at !== 'number' || !Number.isFinite(result.at)) {
|
|
649
|
+
throw new TypeError('Invalid notification scheduling acknowledgement');
|
|
650
|
+
}
|
|
651
|
+
const scheduled = new Bag();
|
|
652
|
+
scheduled.setItem('id', result.id);
|
|
653
|
+
scheduled.setItem('at', result.at);
|
|
654
|
+
node.staticValue.setItem('scheduled', scheduled);
|
|
655
|
+
node.setAttr({status: 'success', error: null}, true, true, false);
|
|
656
|
+
}).catch(error => {
|
|
657
|
+
if (!request.signal.aborted) node.setAttr(failureAttributes(error), true, true, false);
|
|
658
|
+
}).finally(() => {
|
|
659
|
+
if (this.#notificationRequest === request) this.#notificationRequest = null;
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
#finishPushRequest() {
|
|
664
|
+
const request = this.#pushRequest;
|
|
665
|
+
this.#pushRequest = null;
|
|
666
|
+
if (request) clearTimeout(request.timer);
|
|
667
|
+
return request;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
#acceptPushEvent(node, event) {
|
|
671
|
+
if (event.kind === 'registration') {
|
|
672
|
+
if (typeof event.token !== 'string' || !event.token) throw new TypeError('Invalid push registration token');
|
|
673
|
+
this.#finishPushRequest();
|
|
674
|
+
const value = new Bag();
|
|
675
|
+
value.setItem('token', event.token);
|
|
676
|
+
node.staticValue.setItem('registration', value);
|
|
677
|
+
node.setAttr({status: 'success', error: null}, true, true, false);
|
|
678
|
+
} else if (event.kind === 'registrationError') {
|
|
679
|
+
this.#finishPushRequest();
|
|
680
|
+
node.staticValue.setItem('registration', null);
|
|
681
|
+
node.setAttr(failureAttributes(event.error), true, true, false);
|
|
682
|
+
} else {
|
|
683
|
+
if (!['received', 'action'].includes(event.kind)) throw new TypeError('Invalid push event kind');
|
|
684
|
+
const fields = ['id', 'title', 'body', 'messageId', 'destination'];
|
|
685
|
+
if (event.kind === 'action') fields.push('actionId');
|
|
686
|
+
const value = new Bag();
|
|
687
|
+
for (const key of fields) {
|
|
688
|
+
if (typeof event[key] !== 'string') throw new TypeError(`Invalid push event ${key}`);
|
|
689
|
+
value.setItem(key, event[key]);
|
|
690
|
+
}
|
|
691
|
+
node.staticValue.setItem(event.kind, value);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
#startPushEvents(node) {
|
|
696
|
+
if (this.#pushEvents) return this.#pushEvents.start;
|
|
697
|
+
const events = {controller: new AbortController()};
|
|
698
|
+
this.#pushEvents = events;
|
|
699
|
+
events.start = Promise.resolve().then(() => {
|
|
700
|
+
if (events.controller.signal.aborted) return async () => {};
|
|
701
|
+
return this.provider.startPushEvents(event => {
|
|
702
|
+
if (events.controller.signal.aborted) return;
|
|
703
|
+
try { this.#acceptPushEvent(node, event); }
|
|
704
|
+
catch (error) { node.setAttr({listenerError: failureAttributes(error).error}, true, true, false); }
|
|
705
|
+
}, events.controller.signal);
|
|
706
|
+
}).then(stop => {
|
|
707
|
+
if (!events.controller.signal.aborted) node.setAttr({listening: true}, true, true, false);
|
|
708
|
+
return stop;
|
|
709
|
+
});
|
|
710
|
+
events.start.catch(error => {
|
|
711
|
+
const aborted = events.controller.signal.aborted;
|
|
712
|
+
events.controller.abort();
|
|
713
|
+
if (this.#pushEvents === events) this.#pushEvents = null;
|
|
714
|
+
if (!aborted) node.setAttr({listening: false, listenerError: failureAttributes(error).error}, true, true, false);
|
|
715
|
+
});
|
|
716
|
+
node.setAttr({listenerError: null}, true, true, false);
|
|
717
|
+
return events.start;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
#registerPush(node) {
|
|
721
|
+
if (this.#pushRequest) return;
|
|
722
|
+
const request = {controller: new AbortController(), timer: null};
|
|
723
|
+
this.#pushRequest = request;
|
|
724
|
+
request.timer = setTimeout(() => {
|
|
725
|
+
if (this.disposed || this.#pushRequest !== request) return;
|
|
726
|
+
this.#finishPushRequest();
|
|
727
|
+
request.controller.abort();
|
|
728
|
+
node.setAttr(failureAttributes({code: 'TIMEOUT', message: 'Push registration has not returned a token'}), true, true, false);
|
|
729
|
+
}, 30000);
|
|
730
|
+
node.staticValue.setItem('registration', null);
|
|
731
|
+
node.setAttr({status: 'pending', error: null}, true, true, false);
|
|
732
|
+
Promise.resolve().then(async () => {
|
|
733
|
+
if (request.controller.signal.aborted) return;
|
|
734
|
+
this.requireCapability('push');
|
|
735
|
+
await this.#startPushEvents(node);
|
|
736
|
+
if (request.controller.signal.aborted || this.#pushRequest !== request) return;
|
|
737
|
+
await this.provider.registerPush(request.controller.signal);
|
|
738
|
+
// SDK register() only initiates registration. The token event completes it.
|
|
739
|
+
}).catch(error => {
|
|
740
|
+
if (this.disposed || this.#pushRequest !== request) return;
|
|
741
|
+
this.#finishPushRequest();
|
|
742
|
+
request.controller.abort();
|
|
743
|
+
node.setAttr(failureAttributes(error), true, true, false);
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
#startShortcutEvents(node) {
|
|
748
|
+
const events = {controller: new AbortController()};
|
|
749
|
+
this.#shortcutEvents = events;
|
|
750
|
+
events.start = Promise.resolve().then(() => {
|
|
751
|
+
if (events.controller.signal.aborted) return async () => {};
|
|
752
|
+
return this.provider.startShortcutEvents((request, error = null) => {
|
|
753
|
+
if (events.controller.signal.aborted) return;
|
|
754
|
+
try {
|
|
755
|
+
if (error) throw error;
|
|
756
|
+
for (const key of ['id', 'destination']) {
|
|
757
|
+
if (typeof request?.[key] !== 'string' || !request[key]) {
|
|
758
|
+
throw new TypeError(`Invalid shortcut request ${key}`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (node.staticValue.getItem('request')?.getItem('id') === request.id) return;
|
|
762
|
+
const value = new Bag();
|
|
763
|
+
value.setItem('id', request.id);
|
|
764
|
+
value.setItem('destination', request.destination);
|
|
765
|
+
node.setAttr({listenerError: null}, true, true, false);
|
|
766
|
+
node.staticValue.setItem('request', value);
|
|
767
|
+
} catch (error) {
|
|
768
|
+
node.setAttr({listenerError: failureAttributes(error).error}, true, true, false);
|
|
769
|
+
}
|
|
770
|
+
}, events.controller.signal);
|
|
771
|
+
}).then(stop => {
|
|
772
|
+
if (!events.controller.signal.aborted) node.setAttr({listening: true}, true, true, false);
|
|
773
|
+
return stop;
|
|
774
|
+
});
|
|
775
|
+
events.start.catch(error => {
|
|
776
|
+
const aborted = events.controller.signal.aborted;
|
|
777
|
+
events.controller.abort();
|
|
778
|
+
if (!aborted) node.setAttr({listening: false, listenerError: failureAttributes(error).error}, true, true, false);
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
#startLinkEvents(node) {
|
|
783
|
+
const events = {controller: new AbortController()};
|
|
784
|
+
this.#linkEvents = events;
|
|
785
|
+
events.start = Promise.resolve().then(() => {
|
|
786
|
+
if (events.controller.signal.aborted) return async () => {};
|
|
787
|
+
return this.provider.startLinkEvents((request, error = null) => {
|
|
788
|
+
if (events.controller.signal.aborted) return;
|
|
789
|
+
try {
|
|
790
|
+
if (error) throw error;
|
|
791
|
+
for (const key of ['id', 'url']) {
|
|
792
|
+
if (typeof request?.[key] !== 'string' || !request[key]) {
|
|
793
|
+
throw new TypeError(`Invalid link request ${key}`);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
if (node.staticValue.getItem('request')?.getItem('id') === request.id) return;
|
|
797
|
+
const value = new Bag();
|
|
798
|
+
value.setItem('id', request.id);
|
|
799
|
+
value.setItem('url', request.url);
|
|
800
|
+
node.setAttr({listenerError: null}, true, true, false);
|
|
801
|
+
node.staticValue.setItem('request', value);
|
|
802
|
+
} catch (error) {
|
|
803
|
+
node.setAttr({listenerError: failureAttributes(error).error}, true, true, false);
|
|
804
|
+
}
|
|
805
|
+
}, events.controller.signal);
|
|
806
|
+
}).then(stop => {
|
|
807
|
+
if (!events.controller.signal.aborted) node.setAttr({listening: true}, true, true, false);
|
|
808
|
+
return stop;
|
|
809
|
+
});
|
|
810
|
+
events.start.catch(error => {
|
|
811
|
+
const aborted = events.controller.signal.aborted;
|
|
812
|
+
events.controller.abort();
|
|
813
|
+
if (!aborted) node.setAttr({listening: false, listenerError: failureAttributes(error).error}, true, true, false);
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
requireCapability(name) {
|
|
818
|
+
if (!this.provider || !this.provider.capabilities.includes(name)) {
|
|
819
|
+
throw Object.assign(new Error(`${name} is unavailable in this environment`), {code: 'UNAVAILABLE'});
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
attributes(resolver, attrs) {
|
|
824
|
+
const writing = resolver.writing;
|
|
825
|
+
resolver.writing = true;
|
|
826
|
+
try { resolver._node.setAttr(attrs, true, true, false); }
|
|
827
|
+
finally { resolver.writing = writing; }
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
accept(resolver, value) {
|
|
831
|
+
const fields = resolver.name === 'appinfo' ? ['id', 'name', 'version', 'build']
|
|
832
|
+
: ['latitude', 'longitude', 'accuracy', 'timestamp'];
|
|
833
|
+
const type = resolver.name === 'appinfo' ? 'string' : 'number';
|
|
834
|
+
for (const key of fields) {
|
|
835
|
+
if (typeof value[key] !== type || (type === 'number' && !Number.isFinite(value[key]))) {
|
|
836
|
+
throw new TypeError(`Invalid ${resolver.name}.${key} from device provider`);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
const current = resolver.cachedValue;
|
|
840
|
+
// A slower point lookup must not overwrite a newer native measurement.
|
|
841
|
+
if (resolver.name === 'location' && current && value.timestamp < current.getItem('timestamp')) {
|
|
842
|
+
this.attributes(resolver, {status: 'success', error: null});
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
const bag = new Bag();
|
|
846
|
+
for (const key of fields) bag.setItem(key, value[key]);
|
|
847
|
+
resolver._lastUpdate = Date.now();
|
|
848
|
+
resolver.failed = false;
|
|
849
|
+
resolver.received++;
|
|
850
|
+
const writing = resolver.writing;
|
|
851
|
+
resolver.writing = true;
|
|
852
|
+
try { resolver._node.setValue(bag, true, {status: 'success', error: null}, true, false); }
|
|
853
|
+
finally { resolver.writing = writing; }
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
failure(resolver, error) {
|
|
857
|
+
resolver.failed = true;
|
|
858
|
+
this.attributes(resolver, failureAttributes(error));
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
reconcile() {
|
|
862
|
+
const resolver = this.resolvers.get('location');
|
|
863
|
+
if (resolver._node.attr.enabled) {
|
|
864
|
+
if (this.monitor) return;
|
|
865
|
+
const monitor = {stop: null, controller: new AbortController()};
|
|
866
|
+
this.monitor = monitor;
|
|
867
|
+
monitor.start = Promise.resolve(this.cleanup).then(() => {
|
|
868
|
+
if (this.disposed || this.monitor !== monitor) return null;
|
|
869
|
+
this.requireCapability('location');
|
|
870
|
+
return this.provider.startLocationUpdates((value, error) => {
|
|
871
|
+
if (this.disposed || this.monitor !== monitor) return;
|
|
872
|
+
if (error) this.failure(resolver, error);
|
|
873
|
+
else {
|
|
874
|
+
try { this.accept(resolver, value); }
|
|
875
|
+
catch (failure) { this.failure(resolver, failure); }
|
|
876
|
+
}
|
|
877
|
+
}, monitor.controller.signal);
|
|
878
|
+
});
|
|
879
|
+
monitor.start.then(stop => {
|
|
880
|
+
if (stop === null || this.disposed || this.monitor !== monitor) return;
|
|
881
|
+
monitor.stop = stop;
|
|
882
|
+
this.attributes(resolver, {watching: true});
|
|
883
|
+
}).catch(error => {
|
|
884
|
+
if (!this.disposed && this.monitor === monitor) {
|
|
885
|
+
// Keep the failed intent until enabled changes; Data notifications cannot retry it.
|
|
886
|
+
this.attributes(resolver, {watching: false});
|
|
887
|
+
this.failure(resolver, error);
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
this.attributes(resolver, {status: 'pending', error: null});
|
|
891
|
+
} else if (this.monitor) {
|
|
892
|
+
const monitor = this.monitor;
|
|
893
|
+
this.monitor = null;
|
|
894
|
+
monitor.controller.abort();
|
|
895
|
+
this.retiring = monitor;
|
|
896
|
+
// Ignore callbacks now; report effective stopping only after native cleanup succeeds.
|
|
897
|
+
this.cleanup = monitor.start.then(stop => stop === null ? undefined : stop(), () => undefined).then(() => {
|
|
898
|
+
if (this.retiring === monitor) this.retiring = null;
|
|
899
|
+
if (!this.disposed) {
|
|
900
|
+
const attrs = {watching: false};
|
|
901
|
+
if (!this.monitor && !resolver.request && resolver._node.attr.status === 'pending') {
|
|
902
|
+
Object.assign(attrs, {status: 'cancelled', error: null});
|
|
903
|
+
}
|
|
904
|
+
this.attributes(resolver, attrs);
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
this.cleanup.catch(error => { if (!this.disposed) this.failure(resolver, error); });
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/** A page-owned database connection; database technology remains provider-owned. */
|
|
912
|
+
async openDatabase(name) {
|
|
913
|
+
this.assertLive();
|
|
914
|
+
if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(name) || typeof name !== 'string') throw new TypeError('Invalid database name');
|
|
915
|
+
if (this.#database) throw new Error('This page already owns a database connection');
|
|
916
|
+
const node = this.gramlot.data.getNode('gramlot.device.database');
|
|
917
|
+
const state = (status, error = null) => {
|
|
918
|
+
if (!this.disposed) node.setAttr({status, error}, true, true, false);
|
|
919
|
+
};
|
|
920
|
+
if (!this.provider.capabilities.includes('database')) {
|
|
921
|
+
const error = Object.assign(new Error('Native database unavailable'), {code: 'UNAVAILABLE'});
|
|
922
|
+
state('unavailable', {code: error.code, message: error.message}); throw error;
|
|
923
|
+
}
|
|
924
|
+
const slot = {controller: new AbortController(), close: null, start: null};
|
|
925
|
+
this.#database = slot; state('pending');
|
|
926
|
+
slot.start = this.provider.openDatabase(name, slot.controller.signal);
|
|
927
|
+
try {
|
|
928
|
+
const connection = await slot.start;
|
|
929
|
+
let closed = false, closing = null;
|
|
930
|
+
const close = () => {
|
|
931
|
+
if (closing) return closing;
|
|
932
|
+
closed = true;
|
|
933
|
+
closing = connection.close().then(() => { state('closed'); }, error => { state('error', {code:'FAILED', message:error.message}); throw error; })
|
|
934
|
+
.finally(() => { if (this.#database === slot) this.#database = null; });
|
|
935
|
+
return closing;
|
|
936
|
+
};
|
|
937
|
+
slot.close = close;
|
|
938
|
+
if (this.disposed || slot.controller.signal.aborted) { await close(); throw Object.assign(new Error('Page closed'), {code:'CANCELLED'}); }
|
|
939
|
+
const invoke = async (method, sql, params) => {
|
|
940
|
+
this.assertLive();
|
|
941
|
+
if (closed) throw new Error('Database connection is closed');
|
|
942
|
+
state('pending');
|
|
943
|
+
try { const result = await connection[method](sql, params); state('success'); return result; }
|
|
944
|
+
catch (error) { state('error', {code:'FAILED', message:error.message}); throw error; }
|
|
945
|
+
};
|
|
946
|
+
state('success');
|
|
947
|
+
return {query:(sql, params = []) => invoke('query', sql, params),
|
|
948
|
+
execute:(sql, params = []) => invoke('execute', sql, params), close};
|
|
949
|
+
} catch (error) {
|
|
950
|
+
if (this.#database === slot) this.#database = null;
|
|
951
|
+
if (!this.disposed) node.setAttr(failureAttributes(error), true, true, false);
|
|
952
|
+
throw error;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
dispose() {
|
|
957
|
+
if (this.disposed) return;
|
|
958
|
+
this.disposed = true;
|
|
959
|
+
const database = this.#database;
|
|
960
|
+
database?.controller.abort();
|
|
961
|
+
if (database) this.cleanup = Promise.all([this.cleanup,
|
|
962
|
+
database.start.then(() => database.close?.(), () => undefined)]).then(() => undefined);
|
|
963
|
+
const sensorCleanup = [...this.#sensors.values()].map(sensor => {
|
|
964
|
+
const stream = sensor.stream;
|
|
965
|
+
sensor.stream = null;
|
|
966
|
+
stream?.controller.abort();
|
|
967
|
+
return stream ? stream.start.then(stop => stop(), () => undefined) : sensor.cleanup;
|
|
968
|
+
});
|
|
969
|
+
this.#clipboardRequest?.abort();
|
|
970
|
+
this.#clipboardRequest = null;
|
|
971
|
+
const audio = this.#audio;
|
|
972
|
+
this.#audio = null;
|
|
973
|
+
audio?.controller.abort();
|
|
974
|
+
this.#hapticRequest?.abort();
|
|
975
|
+
this.#hapticRequest = null;
|
|
976
|
+
const stateEvents = [...this.#stateEvents.values()];
|
|
977
|
+
this.#stateEvents.clear();
|
|
978
|
+
for (const events of stateEvents) events.controller.abort();
|
|
979
|
+
const linkEvents = this.#linkEvents;
|
|
980
|
+
this.#linkEvents = null;
|
|
981
|
+
linkEvents?.controller.abort();
|
|
982
|
+
const shortcutEvents = this.#shortcutEvents;
|
|
983
|
+
this.#shortcutEvents = null;
|
|
984
|
+
shortcutEvents?.controller.abort();
|
|
985
|
+
this.#finishPushRequest()?.controller.abort();
|
|
986
|
+
const pushEvents = this.#pushEvents;
|
|
987
|
+
this.#pushEvents = null;
|
|
988
|
+
pushEvents?.controller.abort();
|
|
989
|
+
this.#barcodeRequest?.abort();
|
|
990
|
+
this.#barcodeRequest = null;
|
|
991
|
+
this.#captureRequest?.abort();
|
|
992
|
+
this.#captureRequest = null;
|
|
993
|
+
this.#biometricsRequest?.abort();
|
|
994
|
+
this.#biometricsRequest = null;
|
|
995
|
+
this.#notificationRequest?.abort();
|
|
996
|
+
this.#notificationRequest = null;
|
|
997
|
+
const notificationEvents = this.#notificationEvents;
|
|
998
|
+
this.#notificationEvents = null;
|
|
999
|
+
notificationEvents?.controller.abort();
|
|
1000
|
+
this.gramlot.data.unsubscribe('gramlot.device', {any: true});
|
|
1001
|
+
const monitor = this.monitor;
|
|
1002
|
+
this.monitor = null;
|
|
1003
|
+
monitor?.controller.abort();
|
|
1004
|
+
if (monitor) {
|
|
1005
|
+
this.cleanup = monitor.start.then(stop => stop === null ? undefined : stop(), () => undefined);
|
|
1006
|
+
} else if (this.retiring?.stop) {
|
|
1007
|
+
const retiring = this.retiring;
|
|
1008
|
+
this.cleanup = this.cleanup.catch(() => retiring.stop());
|
|
1009
|
+
}
|
|
1010
|
+
if (notificationEvents) {
|
|
1011
|
+
this.cleanup = Promise.all([this.cleanup,
|
|
1012
|
+
notificationEvents.start.then(stop => stop(), () => undefined)]).then(() => undefined);
|
|
1013
|
+
}
|
|
1014
|
+
if (pushEvents) {
|
|
1015
|
+
this.cleanup = Promise.all([this.cleanup,
|
|
1016
|
+
pushEvents.start.then(stop => stop(), () => undefined)]).then(() => undefined);
|
|
1017
|
+
}
|
|
1018
|
+
if (linkEvents) {
|
|
1019
|
+
this.cleanup = Promise.all([this.cleanup,
|
|
1020
|
+
linkEvents.start.then(stop => stop(), () => undefined)]).then(() => undefined);
|
|
1021
|
+
}
|
|
1022
|
+
if (shortcutEvents) {
|
|
1023
|
+
this.cleanup = Promise.all([this.cleanup,
|
|
1024
|
+
shortcutEvents.start.then(stop => stop(), () => undefined)]).then(() => undefined);
|
|
1025
|
+
}
|
|
1026
|
+
this.cleanup = Promise.all([this.cleanup, ...sensorCleanup, ...stateEvents.map(events =>
|
|
1027
|
+
events.start.then(stop => stop(), () => undefined))]).then(() => undefined);
|
|
1028
|
+
if (audio) {
|
|
1029
|
+
this.cleanup = Promise.all([this.cleanup,
|
|
1030
|
+
audio.start.then(session => session === null ? undefined : session.dispose(), error => { if (error?.code !== 'CANCELLED') throw error; })]).then(() => undefined);
|
|
1031
|
+
}
|
|
1032
|
+
// Cleanup rejection is observed; disposed pages must not receive late Data writes.
|
|
1033
|
+
this.cleanup = this.cleanup.finally(() => this.releaseProvider?.());
|
|
1034
|
+
this.cleanup.catch(error => { this.cleanupError = error; });
|
|
1035
|
+
for (const resolver of this.resolvers.values()) resolver.request = null;
|
|
1036
|
+
}
|
|
1037
|
+
}
|