chayns-api 3.6.5 → 3.6.7
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/cjs/calls/appleSafeAreaListener.js +13 -4
- package/dist/cjs/hooks/useSafeArea.js +11 -25
- package/dist/cjs/wrapper/AppWrapper.js +51 -13
- package/dist/cjs/wrapper/normalizeFunctions.js +61 -32
- package/dist/esm/calls/appleSafeAreaListener.js +12 -3
- package/dist/esm/hooks/useSafeArea.js +12 -26
- package/dist/esm/wrapper/AppWrapper.js +51 -13
- package/dist/esm/wrapper/normalizeFunctions.js +61 -32
- package/dist/types/calls/appleSafeAreaListener.d.ts +5 -5
- package/dist/types/wrapper/AppWrapper.d.ts +3 -1
- package/package.json +1 -1
- package/tests/safe-area.test.cjs +71 -320
- package/dist/cjs/utils/appleSafeArea.js +0 -101
- package/dist/esm/utils/appleSafeArea.js +0 -92
- package/dist/types/utils/appleSafeArea.d.ts +0 -9
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ChaynsReactFunctions, ChaynsReactValues, ChaynsStyleSettings, CleanupCallback, DataChangeCallback, IChaynsReact } from '../types/IChaynsReact';
|
|
1
|
+
import { AppleSafeArea, ChaynsReactFunctions, ChaynsReactValues, ChaynsStyleSettings, CleanupCallback, DataChangeCallback, IChaynsReact } from '../types/IChaynsReact';
|
|
2
2
|
export declare class AppWrapper implements IChaynsReact {
|
|
3
3
|
values: ChaynsReactValues;
|
|
4
4
|
history: IChaynsReact['history'];
|
|
5
5
|
accessToken: string;
|
|
6
|
+
latestAppleSafeArea: AppleSafeArea | null;
|
|
6
7
|
private trustedDomains;
|
|
7
8
|
listeners: (() => void)[];
|
|
8
9
|
customFunctions: {};
|
|
@@ -13,6 +14,7 @@ export declare class AppWrapper implements IChaynsReact {
|
|
|
13
14
|
chaynsApiId: string;
|
|
14
15
|
notImplemented(call: string): void;
|
|
15
16
|
counter: number;
|
|
17
|
+
private getCallbackName;
|
|
16
18
|
appCall(action: any, value?: unknown, { callback, awaitResult }?: {
|
|
17
19
|
awaitResult?: boolean | undefined;
|
|
18
20
|
}): Promise<unknown> | undefined;
|
package/package.json
CHANGED
package/tests/safe-area.test.cjs
CHANGED
|
@@ -11,7 +11,7 @@ global.IS_REACT_ACT_ENVIRONMENT = true;
|
|
|
11
11
|
const { act, createElement, StrictMode } = require('react');
|
|
12
12
|
const { createRoot } = require('react-dom/client');
|
|
13
13
|
const { ChaynsContext } = require('../dist/cjs/components/ChaynsContext');
|
|
14
|
-
const { useSafeArea
|
|
14
|
+
const { useSafeArea } = require('../dist/cjs/hooks/useSafeArea');
|
|
15
15
|
const { AppName, HingeStatus } = require('../dist/cjs/types/IChaynsReact');
|
|
16
16
|
const { AppWrapper } = require('../dist/cjs/wrapper/AppWrapper');
|
|
17
17
|
const { normalizeFunctions } = require('../dist/cjs/wrapper/normalizeFunctions');
|
|
@@ -23,9 +23,8 @@ let listenerIds;
|
|
|
23
23
|
let root;
|
|
24
24
|
let currentValue;
|
|
25
25
|
|
|
26
|
-
// The native host only retains the most recently registered callback for call 300.
|
|
27
26
|
const emit = (value) => {
|
|
28
|
-
const call = nativeCalls.
|
|
27
|
+
const call = nativeCalls.findLast(({ action }) => action === 300);
|
|
29
28
|
const callbackName = call.value.callback.replace(/^window\./, '');
|
|
30
29
|
window[callbackName]({ retVal: value });
|
|
31
30
|
};
|
|
@@ -49,10 +48,6 @@ const renderHook = async (enabled = true) => {
|
|
|
49
48
|
};
|
|
50
49
|
|
|
51
50
|
beforeEach(() => {
|
|
52
|
-
const testDom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'https://example.test' });
|
|
53
|
-
global.window = testDom.window;
|
|
54
|
-
global.document = testDom.window.document;
|
|
55
|
-
Object.defineProperty(global, 'navigator', { configurable: true, value: testDom.window.navigator });
|
|
56
51
|
wrapper = new AppWrapper();
|
|
57
52
|
wrapper.values = { device: { os: 'iOS', app: { name: AppName.Sidekick, appVersion: 2120 } } };
|
|
58
53
|
nativeCalls = [];
|
|
@@ -68,7 +63,6 @@ afterEach(async () => {
|
|
|
68
63
|
for (const key of Object.keys(window)) {
|
|
69
64
|
if (key.startsWith('chaynsApiV5Callback_')) delete window[key];
|
|
70
65
|
}
|
|
71
|
-
window.close();
|
|
72
66
|
});
|
|
73
67
|
|
|
74
68
|
test('exports the three native hinge status values through the public entry point', () => {
|
|
@@ -78,10 +72,10 @@ test('exports the three native hinge status values through the public entry poin
|
|
|
78
72
|
|
|
79
73
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
80
74
|
test(`preserves ${hingeStatus} and a zero angle in a direct safe-area request`, async () => {
|
|
81
|
-
const expected = { ...insets, hingeAngle: 0, hingeStatus };
|
|
75
|
+
const expected = { ...insets, hingeAngle: 0, hingeStatus, additionalNativeValue: 42 };
|
|
82
76
|
const pending = wrapper.functions.getAppleSafeArea();
|
|
83
77
|
assert.equal(nativeCalls[0].action, 300);
|
|
84
|
-
emit(
|
|
78
|
+
emit(expected);
|
|
85
79
|
assert.deepEqual(await pending, expected);
|
|
86
80
|
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
87
81
|
assert.equal(nativeCalls.length, 1);
|
|
@@ -130,24 +124,56 @@ test('stops delivering native updates to removed listeners', async () => {
|
|
|
130
124
|
assert.deepEqual(values, []);
|
|
131
125
|
});
|
|
132
126
|
|
|
133
|
-
test('
|
|
127
|
+
test('shares one native callback between compatibility wrappers and keeps remaining listeners active', async () => {
|
|
134
128
|
let dispatch;
|
|
135
|
-
|
|
129
|
+
let calls = 0;
|
|
130
|
+
const host = {
|
|
136
131
|
invokeCall: (options, callback) => {
|
|
137
132
|
assert.equal(options.action, 300);
|
|
133
|
+
calls += 1;
|
|
138
134
|
dispatch = callback;
|
|
139
135
|
return Promise.resolve();
|
|
140
136
|
},
|
|
141
|
-
}
|
|
137
|
+
};
|
|
138
|
+
const first = normalizeFunctions(host);
|
|
139
|
+
const second = normalizeFunctions(host);
|
|
140
|
+
const firstValues = [];
|
|
141
|
+
const secondValues = [];
|
|
142
|
+
const firstId = await first.addAppleSafeAreaListener((value) => firstValues.push(value));
|
|
143
|
+
const secondId = await second.addAppleSafeAreaListener((value) => secondValues.push(value));
|
|
144
|
+
try {
|
|
145
|
+
assert.equal(calls, 1);
|
|
146
|
+
const expected = { ...insets, hingeAngle: 90, hingeStatus: HingeStatus.PartiallyOpen };
|
|
147
|
+
dispatch(expected);
|
|
148
|
+
assert.deepEqual(firstValues, [expected]);
|
|
149
|
+
assert.deepEqual(secondValues, [expected]);
|
|
150
|
+
assert.deepEqual(await first.getAppleSafeArea(), expected);
|
|
151
|
+
assert.deepEqual(await second.getAppleSafeArea(), expected);
|
|
152
|
+
await first.removeAppleSafeAreaListener(firstId);
|
|
153
|
+
const opened = { ...expected, hingeAngle: 180, hingeStatus: HingeStatus.FullyOpen };
|
|
154
|
+
dispatch(opened);
|
|
155
|
+
assert.deepEqual(firstValues, [expected]);
|
|
156
|
+
assert.deepEqual(secondValues, [expected, opened]);
|
|
157
|
+
} finally {
|
|
158
|
+
await first.removeAppleSafeAreaListener(firstId);
|
|
159
|
+
await second.removeAppleSafeAreaListener(secondId);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('shares the active native callback with concurrent getters and preserves subsequent hinge updates', async () => {
|
|
142
164
|
const values = [];
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
assert.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
165
|
+
await subscribe((value) => values.push(value));
|
|
166
|
+
const first = wrapper.functions.getAppleSafeArea();
|
|
167
|
+
const second = wrapper.functions.getAppleSafeArea();
|
|
168
|
+
assert.equal(nativeCalls.length, 1);
|
|
169
|
+
const closed = { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed };
|
|
170
|
+
emit(closed); // Only the most recently registered native callback is called.
|
|
171
|
+
assert.deepEqual(await Promise.all([first, second]), [closed, closed]);
|
|
172
|
+
const opened = { ...insets, hingeAngle: 180, hingeStatus: HingeStatus.FullyOpen };
|
|
173
|
+
emit(opened);
|
|
174
|
+
assert.deepEqual(values, [closed, opened]);
|
|
175
|
+
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), opened);
|
|
176
|
+
assert.equal(nativeCalls.length, 1);
|
|
151
177
|
});
|
|
152
178
|
|
|
153
179
|
test('updates the actual hook through native folding events with identical insets', async () => {
|
|
@@ -162,9 +188,6 @@ test('updates the actual hook through native folding events with identical inset
|
|
|
162
188
|
await renderHook(false);
|
|
163
189
|
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
164
190
|
assert.equal(currentValue, beforeCleanup);
|
|
165
|
-
await renderHook();
|
|
166
|
-
assert.deepEqual(currentValue, { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
167
|
-
assert.equal(nativeCalls.length, 1);
|
|
168
191
|
});
|
|
169
192
|
|
|
170
193
|
test('returns only insets on ordinary devices and retains disabled-hook behavior', async () => {
|
|
@@ -183,300 +206,28 @@ test('does not register native listeners on unsupported runtimes', async () => {
|
|
|
183
206
|
assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
184
207
|
});
|
|
185
208
|
|
|
186
|
-
|
|
187
|
-
test(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const latest = { ...insets, right: 0, hingeStatus: HingeStatus.PartiallyOpen };
|
|
210
|
-
emit(latest);
|
|
211
|
-
const replay = [];
|
|
212
|
-
await subscribe((value) => replay.push(value));
|
|
213
|
-
assert.deepEqual(values, [insets]);
|
|
214
|
-
assert.deepEqual(replay, [latest]);
|
|
215
|
-
assert.equal(nativeCalls.length, 1);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test('shares the native safe-area channel across AppWrapper instances', async () => {
|
|
219
|
-
const firstValues = [];
|
|
220
|
-
await subscribe((value) => firstValues.push(value));
|
|
221
|
-
emit(insets);
|
|
222
|
-
const second = new AppWrapper();
|
|
223
|
-
// Avoid the separate generic callback counter collision in the old implementation.
|
|
224
|
-
second.counter = 100;
|
|
225
|
-
const secondValues = [];
|
|
226
|
-
const id = await second.functions.addAppleSafeAreaListener((value) => secondValues.push(value));
|
|
227
|
-
try {
|
|
228
|
-
const pending = second.functions.getAppleSafeArea();
|
|
229
|
-
assert.equal(nativeCalls.length, 1);
|
|
230
|
-
assert.deepEqual(await pending, insets);
|
|
231
|
-
const latest = { ...insets, hingeAngle: 90, hingeStatus: HingeStatus.PartiallyOpen };
|
|
232
|
-
emit(latest);
|
|
233
|
-
assert.equal(nativeCalls.length, 1);
|
|
234
|
-
assert.deepEqual(firstValues, [insets, latest]);
|
|
235
|
-
assert.deepEqual(secondValues, [insets, latest]);
|
|
236
|
-
} finally {
|
|
237
|
-
await second.functions.removeAppleSafeAreaListener(id);
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
test('routes direct invokeCall 300 requests through the existing native subscription', async () => {
|
|
242
|
-
const values = [];
|
|
243
|
-
await subscribe((value) => values.push(value));
|
|
244
|
-
const rawValues = [];
|
|
245
|
-
const pending = wrapper.functions.invokeCall({ action: 300 }, (value) => rawValues.push(value));
|
|
246
|
-
assert.equal(nativeCalls.length, 1);
|
|
247
|
-
emit(insets);
|
|
248
|
-
assert.deepEqual(await pending, insets);
|
|
249
|
-
const latest = { ...insets, right: 0, hingeStatus: HingeStatus.FullyOpen };
|
|
250
|
-
emit(latest);
|
|
251
|
-
assert.deepEqual(values, [insets, latest]);
|
|
252
|
-
assert.deepEqual(rawValues, [insets, latest]);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
test('uses a safe-area callback name independent of generic AppWrapper counters', async () => {
|
|
256
|
-
const values = [];
|
|
257
|
-
await subscribe((value) => values.push(value));
|
|
258
|
-
const second = new AppWrapper();
|
|
259
|
-
const otherCall = second.functions.invokeCall({ action: 999 });
|
|
260
|
-
const callbackName = nativeCalls.at(-1).value.callback.replace(/^window\./, '');
|
|
261
|
-
window[callbackName]({ retVal: 'unrelated' });
|
|
262
|
-
assert.equal(await otherCall, 'unrelated');
|
|
263
|
-
emit(insets);
|
|
264
|
-
assert.deepEqual(values, [insets]);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
test('shares compatibility subscriptions and cache across wrappers of the same host', async () => {
|
|
268
|
-
let dispatch;
|
|
269
|
-
let calls = 0;
|
|
270
|
-
const host = {
|
|
271
|
-
invokeCall: (options, callback) => {
|
|
272
|
-
calls++;
|
|
273
|
-
dispatch = callback;
|
|
274
|
-
return Promise.resolve();
|
|
275
|
-
},
|
|
276
|
-
};
|
|
277
|
-
const first = normalizeFunctions(host);
|
|
278
|
-
// Different function objects can still expose the same host bridge.
|
|
279
|
-
const second = normalizeFunctions({ ...host });
|
|
280
|
-
const firstValues = [];
|
|
281
|
-
const firstId = await first.addAppleSafeAreaListener((value) => firstValues.push(value));
|
|
282
|
-
const pending = second.getAppleSafeArea();
|
|
283
|
-
const secondValues = [];
|
|
284
|
-
const secondId = await second.addAppleSafeAreaListener((value) => secondValues.push(value));
|
|
285
|
-
assert.equal(calls, 1);
|
|
286
|
-
dispatch(insets);
|
|
287
|
-
assert.deepEqual(await pending, insets);
|
|
288
|
-
await first.removeAppleSafeAreaListener(firstId);
|
|
289
|
-
await second.removeAppleSafeAreaListener(secondId);
|
|
290
|
-
const latest = { ...insets, right: 0, hingeStatus: HingeStatus.PartiallyOpen };
|
|
291
|
-
dispatch(latest);
|
|
292
|
-
const replay = [];
|
|
293
|
-
const replayId = await first.addAppleSafeAreaListener((value) => replay.push(value));
|
|
294
|
-
assert.equal(calls, 1);
|
|
295
|
-
assert.deepEqual(replay, [latest]);
|
|
296
|
-
assert.deepEqual(firstValues, [insets]);
|
|
297
|
-
assert.deepEqual(secondValues, [insets]);
|
|
298
|
-
await first.removeAppleSafeAreaListener(replayId);
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
test('does not mix compatibility subscriptions belonging to different hosts', async () => {
|
|
302
|
-
const callbacks = [];
|
|
303
|
-
const createHost = () => normalizeFunctions({ invokeCall: (options, callback) => { callbacks.push(callback); return Promise.resolve(); } });
|
|
304
|
-
const first = createHost();
|
|
305
|
-
const second = createHost();
|
|
306
|
-
const firstValue = first.getAppleSafeArea();
|
|
307
|
-
const secondValue = second.getAppleSafeArea();
|
|
308
|
-
assert.equal(callbacks.length, 2);
|
|
309
|
-
callbacks[0](insets);
|
|
310
|
-
const other = { ...insets, left: 0 };
|
|
311
|
-
callbacks[1](other);
|
|
312
|
-
assert.deepEqual(await Promise.all([firstValue, secondValue]), [insets, other]);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
test('a listener hook cleanup removes its own registration when used more than once', async () => {
|
|
316
|
-
let addListener;
|
|
317
|
-
const add = wrapper.functions.addAppleSafeAreaListener;
|
|
318
|
-
wrapper.functions.addAppleSafeAreaListener = async (callback) => {
|
|
319
|
-
const id = await add(callback);
|
|
320
|
-
listenerIds.push(id);
|
|
321
|
-
return id;
|
|
322
|
-
};
|
|
323
|
-
const ListenerProbe = () => { addListener = useSafeAreaListener(); return null; };
|
|
324
|
-
root = createRoot(document.createElement('div'));
|
|
325
|
-
await act(async () => root.render(createElement(ChaynsContext.Provider, { value: wrapper }, createElement(ListenerProbe))));
|
|
326
|
-
const firstValues = [];
|
|
327
|
-
const secondValues = [];
|
|
328
|
-
const removeFirst = addListener((value) => firstValues.push(value));
|
|
329
|
-
const removeSecond = addListener((value) => secondValues.push(value));
|
|
330
|
-
await act(async () => removeFirst());
|
|
331
|
-
emit(insets);
|
|
332
|
-
assert.deepEqual(firstValues, []);
|
|
333
|
-
assert.deepEqual(secondValues, [insets]);
|
|
334
|
-
removeSecond();
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
test('adding a listener while the first getter is pending does not register call 300 again', async () => {
|
|
339
|
-
const pending = wrapper.functions.getAppleSafeArea();
|
|
340
|
-
const values = [];
|
|
341
|
-
await subscribe((value) => values.push(value));
|
|
342
|
-
assert.equal(nativeCalls.length, 1);
|
|
343
|
-
emit(insets);
|
|
344
|
-
assert.deepEqual(await pending, insets);
|
|
345
|
-
emit({ ...insets, right: 0 });
|
|
346
|
-
assert.equal(values.length, 2);
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
test('compatibility invokeCall 300 shares the listener and preserves unrelated raw calls', async () => {
|
|
350
|
-
let dispatch;
|
|
351
|
-
const actions = [];
|
|
352
|
-
const functions = normalizeFunctions({
|
|
353
|
-
invokeCall: (options, callback) => {
|
|
354
|
-
actions.push(options.action);
|
|
355
|
-
if (options.action === 300) dispatch = callback;
|
|
356
|
-
return Promise.resolve('unrelated');
|
|
357
|
-
},
|
|
358
|
-
});
|
|
359
|
-
const values = [];
|
|
360
|
-
const id = await functions.addAppleSafeAreaListener((value) => values.push(value));
|
|
361
|
-
const pending = functions.invokeCall({ action: 300 });
|
|
362
|
-
const rawValues = [];
|
|
363
|
-
const withCallback = functions.invokeCall({ action: 300 }, (value) => rawValues.push(value));
|
|
364
|
-
dispatch(insets);
|
|
365
|
-
assert.deepEqual(await pending, insets);
|
|
366
|
-
assert.deepEqual(await withCallback, insets);
|
|
367
|
-
dispatch({ ...insets, right: 0 });
|
|
368
|
-
assert.deepEqual(rawValues, values);
|
|
369
|
-
assert.equal(await functions.invokeCall({ action: 999 }), 'unrelated');
|
|
370
|
-
assert.deepEqual(actions, [300, 999]);
|
|
371
|
-
await functions.removeAppleSafeAreaListener(id);
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
test('a partial host with an existing listener never calls a competing getter', async () => {
|
|
375
|
-
let dispatch;
|
|
376
|
-
let calls = 0;
|
|
377
|
-
const host = {
|
|
378
|
-
addAppleSafeAreaListener: (callback) => { calls++; dispatch = callback; return Promise.resolve(17); },
|
|
379
|
-
getAppleSafeArea: () => { throw new Error('competing getter must not be used'); },
|
|
380
|
-
};
|
|
381
|
-
const first = normalizeFunctions(host);
|
|
382
|
-
const second = normalizeFunctions({ ...host });
|
|
383
|
-
const pending = first.getAppleSafeArea();
|
|
384
|
-
const values = [];
|
|
385
|
-
const id = await second.addAppleSafeAreaListener((value) => values.push(value));
|
|
386
|
-
dispatch(insets);
|
|
387
|
-
assert.deepEqual(await pending, insets);
|
|
388
|
-
await second.removeAppleSafeAreaListener(id);
|
|
389
|
-
const latest = { ...insets, hingeStatus: HingeStatus.PartiallyOpen };
|
|
390
|
-
dispatch(latest);
|
|
391
|
-
assert.deepEqual(await first.getAppleSafeArea(), latest);
|
|
392
|
-
assert.deepEqual(values, [insets]);
|
|
393
|
-
assert.equal(calls, 1);
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
test('preserves complete host implementations and the unsupported-host zero fallback', async () => {
|
|
397
|
-
const host = { addAppleSafeAreaListener() {}, removeAppleSafeAreaListener() {}, getAppleSafeArea() {}, isTrustedUrl() {} };
|
|
398
|
-
assert.equal(normalizeFunctions(host), host);
|
|
399
|
-
assert.deepEqual(await normalizeFunctions({}).getAppleSafeArea(), { top: 0, left: 0, bottom: 0, right: 0 });
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
test('getter-only compatibility hosts do not share cached state across server requests', async () => {
|
|
403
|
-
let calls = 0;
|
|
404
|
-
const getAppleSafeArea = () => Promise.resolve({ ...insets, top: ++calls });
|
|
405
|
-
const first = normalizeFunctions({ getAppleSafeArea });
|
|
406
|
-
const second = normalizeFunctions({ getAppleSafeArea });
|
|
407
|
-
assert.equal((await first.getAppleSafeArea()).top, 1);
|
|
408
|
-
assert.equal((await second.getAppleSafeArea()).top, 2);
|
|
409
|
-
assert.equal((await first.getAppleSafeArea()).top, 1);
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
test('logs a failed native registration, rejects the getter and allows a later retry', async (t) => {
|
|
413
|
-
const errors = t.mock.method(console, 'error', () => {});
|
|
414
|
-
const postMessage = window.webkit.messageHandlers.jsonCall.postMessage;
|
|
415
|
-
window.webkit.messageHandlers.jsonCall.postMessage = () => { throw new Error('native bridge unavailable'); };
|
|
416
|
-
await assert.rejects(wrapper.functions.getAppleSafeArea(), /native bridge unavailable/);
|
|
417
|
-
assert.equal(errors.mock.calls.length, 1);
|
|
418
|
-
assert.match(errors.mock.calls[0].arguments[0], /Failed to initialize Apple safe-area updates/);
|
|
419
|
-
window.webkit.messageHandlers.jsonCall.postMessage = postMessage;
|
|
420
|
-
const pending = wrapper.functions.getAppleSafeArea();
|
|
421
|
-
emit(insets);
|
|
422
|
-
assert.deepEqual(await pending, insets);
|
|
423
|
-
assert.equal(nativeCalls.length, 1);
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
test('retries an asynchronously rejected compatibility subscription without losing local listeners', async (t) => {
|
|
427
|
-
const errors = t.mock.method(console, 'error', () => {});
|
|
428
|
-
let attempts = 0;
|
|
429
|
-
let dispatch;
|
|
430
|
-
const functions = normalizeFunctions({
|
|
431
|
-
invokeCall: (options, callback) => {
|
|
432
|
-
attempts++;
|
|
433
|
-
if (attempts === 1) return Promise.reject(new Error('host unavailable'));
|
|
434
|
-
dispatch = callback;
|
|
435
|
-
return Promise.resolve();
|
|
436
|
-
},
|
|
209
|
+
for (const useDialogCall of [false, true]) {
|
|
210
|
+
test(`keeps the safe-area callback intact when another wrapper registers a ${useDialogCall ? 'dialog' : 'regular'} call`, async () => {
|
|
211
|
+
const values = [];
|
|
212
|
+
await subscribe((value) => values.push(value));
|
|
213
|
+
const safeAreaCallback = nativeCalls.at(-1).value.callback.replace(/^window\./, '');
|
|
214
|
+
const other = new AppWrapper();
|
|
215
|
+
let dialogResult;
|
|
216
|
+
const pending = useDialogCall
|
|
217
|
+
? other.functions.invokeDialogCall({ action: 999, value: {} }, (value) => { dialogResult = value; })
|
|
218
|
+
: other.functions.invokeCall({ action: 999, value: {} });
|
|
219
|
+
const otherCallback = nativeCalls.at(-1).value.callback.replace(/^window\./, '');
|
|
220
|
+
assert.notEqual(otherCallback, safeAreaCallback);
|
|
221
|
+
window[otherCallback]({ retVal: { success: true } });
|
|
222
|
+
const callResult = await pending;
|
|
223
|
+
assert.deepEqual(useDialogCall ? dialogResult : callResult, { success: true });
|
|
224
|
+
const updates = Array.from({ length: 181 }, (_, hingeAngle) => {
|
|
225
|
+
let hingeStatus = HingeStatus.PartiallyOpen;
|
|
226
|
+
if (hingeAngle === 0) hingeStatus = HingeStatus.Closed;
|
|
227
|
+
if (hingeAngle === 180) hingeStatus = HingeStatus.FullyOpen;
|
|
228
|
+
return { ...insets, hingeAngle, hingeStatus };
|
|
229
|
+
});
|
|
230
|
+
updates.forEach(emit);
|
|
231
|
+
assert.deepEqual(values, updates);
|
|
437
232
|
});
|
|
438
|
-
|
|
439
|
-
await assert.rejects(first, /host unavailable/);
|
|
440
|
-
const values = [];
|
|
441
|
-
const id = await functions.addAppleSafeAreaListener((value) => values.push(value));
|
|
442
|
-
const second = functions.getAppleSafeArea();
|
|
443
|
-
dispatch(insets);
|
|
444
|
-
assert.deepEqual(await second, insets);
|
|
445
|
-
assert.deepEqual(values, [insets]);
|
|
446
|
-
assert.equal(attempts, 2);
|
|
447
|
-
assert.equal(errors.mock.calls.length, 1);
|
|
448
|
-
await functions.removeAppleSafeAreaListener(id);
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
test('the hook cleans up the old host and listens to updated provider functions', async () => {
|
|
453
|
-
const values = wrapper.values;
|
|
454
|
-
const createHost = () => {
|
|
455
|
-
let counter = 0;
|
|
456
|
-
const callbacks = new Map();
|
|
457
|
-
return {
|
|
458
|
-
values,
|
|
459
|
-
subscribe: () => () => {},
|
|
460
|
-
callbacks,
|
|
461
|
-
functions: {
|
|
462
|
-
addAppleSafeAreaListener: (callback) => { const id = ++counter; callbacks.set(id, callback); return Promise.resolve(id); },
|
|
463
|
-
removeAppleSafeAreaListener: (id) => { callbacks.delete(id); return Promise.resolve(); },
|
|
464
|
-
},
|
|
465
|
-
};
|
|
466
|
-
};
|
|
467
|
-
const oldHost = createHost();
|
|
468
|
-
wrapper = oldHost;
|
|
469
|
-
await renderHook();
|
|
470
|
-
await act(async () => oldHost.callbacks.forEach((callback) => callback(insets)));
|
|
471
|
-
assert.deepEqual(currentValue, insets);
|
|
472
|
-
const newHost = createHost();
|
|
473
|
-
wrapper = newHost;
|
|
474
|
-
await renderHook();
|
|
475
|
-
assert.equal(oldHost.callbacks.size, 0);
|
|
476
|
-
assert.equal(newHost.callbacks.size, 1);
|
|
477
|
-
const latest = { ...insets, right: 0, hingeStatus: HingeStatus.PartiallyOpen };
|
|
478
|
-
await act(async () => newHost.callbacks.forEach((callback) => callback(latest)));
|
|
479
|
-
assert.deepEqual(currentValue, latest);
|
|
480
|
-
await renderHook(false);
|
|
481
|
-
assert.equal(newHost.callbacks.size, 0);
|
|
482
|
-
});
|
|
233
|
+
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.getAppleSafeAreaFunctions = exports.getAppAppleSafeAreaFunctions = void 0;
|
|
7
|
-
var _appCall = _interopRequireDefault(require("./appCall"));
|
|
8
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
-
const channels = new WeakMap();
|
|
10
|
-
const getAppleSafeAreaFunctions = (host, subscribe) => {
|
|
11
|
-
const existing = channels.get(host);
|
|
12
|
-
if (existing) {
|
|
13
|
-
return existing;
|
|
14
|
-
}
|
|
15
|
-
let latestValue;
|
|
16
|
-
let initialValue;
|
|
17
|
-
let counter = 0;
|
|
18
|
-
const listeners = new Map();
|
|
19
|
-
const ensureStarted = () => {
|
|
20
|
-
if (initialValue) {
|
|
21
|
-
return initialValue;
|
|
22
|
-
}
|
|
23
|
-
let resolveInitial;
|
|
24
|
-
let rejectInitial;
|
|
25
|
-
const pending = new Promise((resolve, reject) => {
|
|
26
|
-
resolveInitial = resolve;
|
|
27
|
-
rejectInitial = reject;
|
|
28
|
-
});
|
|
29
|
-
initialValue = pending;
|
|
30
|
-
void pending.catch(error => {
|
|
31
|
-
console.error('[chayns-api] Failed to initialize Apple safe-area updates', error);
|
|
32
|
-
});
|
|
33
|
-
const handleError = error => {
|
|
34
|
-
initialValue = undefined;
|
|
35
|
-
rejectInitial(error);
|
|
36
|
-
};
|
|
37
|
-
try {
|
|
38
|
-
const registration = subscribe(value => {
|
|
39
|
-
latestValue = value;
|
|
40
|
-
resolveInitial(value);
|
|
41
|
-
Array.from(listeners.values()).forEach(listener => listener(value));
|
|
42
|
-
});
|
|
43
|
-
void Promise.resolve(registration).catch(handleError);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
handleError(error);
|
|
46
|
-
}
|
|
47
|
-
return pending;
|
|
48
|
-
};
|
|
49
|
-
const functions = {
|
|
50
|
-
addAppleSafeAreaListener: callback => {
|
|
51
|
-
const id = ++counter;
|
|
52
|
-
listeners.set(id, callback);
|
|
53
|
-
if (latestValue) {
|
|
54
|
-
callback(latestValue);
|
|
55
|
-
}
|
|
56
|
-
void ensureStarted();
|
|
57
|
-
return Promise.resolve(id);
|
|
58
|
-
},
|
|
59
|
-
removeAppleSafeAreaListener: id => {
|
|
60
|
-
listeners.delete(id);
|
|
61
|
-
return Promise.resolve();
|
|
62
|
-
},
|
|
63
|
-
getAppleSafeArea: () => {
|
|
64
|
-
if (latestValue) {
|
|
65
|
-
return Promise.resolve(latestValue);
|
|
66
|
-
}
|
|
67
|
-
return ensureStarted();
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
channels.set(host, functions);
|
|
71
|
-
return functions;
|
|
72
|
-
};
|
|
73
|
-
exports.getAppleSafeAreaFunctions = getAppleSafeAreaFunctions;
|
|
74
|
-
const getAppAppleSafeAreaFunctions = () => {
|
|
75
|
-
const host = window;
|
|
76
|
-
return getAppleSafeAreaFunctions(host, callback => {
|
|
77
|
-
host.chaynsApiAppleSafeAreaCallback = response => {
|
|
78
|
-
var _response$retVal, _value$top, _value$left, _value$bottom, _value$right;
|
|
79
|
-
const value = (_response$retVal = response === null || response === void 0 ? void 0 : response.retVal) !== null && _response$retVal !== void 0 ? _response$retVal : response;
|
|
80
|
-
callback({
|
|
81
|
-
top: (_value$top = value === null || value === void 0 ? void 0 : value.top) !== null && _value$top !== void 0 ? _value$top : 0,
|
|
82
|
-
left: (_value$left = value === null || value === void 0 ? void 0 : value.left) !== null && _value$left !== void 0 ? _value$left : 0,
|
|
83
|
-
bottom: (_value$bottom = value === null || value === void 0 ? void 0 : value.bottom) !== null && _value$bottom !== void 0 ? _value$bottom : 0,
|
|
84
|
-
right: (_value$right = value === null || value === void 0 ? void 0 : value.right) !== null && _value$right !== void 0 ? _value$right : 0,
|
|
85
|
-
...((value === null || value === void 0 ? void 0 : value.hingeAngle) === undefined ? {} : {
|
|
86
|
-
hingeAngle: value.hingeAngle
|
|
87
|
-
}),
|
|
88
|
-
...((value === null || value === void 0 ? void 0 : value.hingeStatus) === undefined ? {} : {
|
|
89
|
-
hingeStatus: value.hingeStatus
|
|
90
|
-
})
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
(0, _appCall.default)({
|
|
94
|
-
action: 300,
|
|
95
|
-
value: {
|
|
96
|
-
callback: 'window.chaynsApiAppleSafeAreaCallback'
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
};
|
|
101
|
-
exports.getAppAppleSafeAreaFunctions = getAppAppleSafeAreaFunctions;
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import invokeAppCall from './appCall';
|
|
2
|
-
const channels = new WeakMap();
|
|
3
|
-
export const getAppleSafeAreaFunctions = (host, subscribe) => {
|
|
4
|
-
const existing = channels.get(host);
|
|
5
|
-
if (existing) {
|
|
6
|
-
return existing;
|
|
7
|
-
}
|
|
8
|
-
let latestValue;
|
|
9
|
-
let initialValue;
|
|
10
|
-
let counter = 0;
|
|
11
|
-
const listeners = new Map();
|
|
12
|
-
const ensureStarted = () => {
|
|
13
|
-
if (initialValue) {
|
|
14
|
-
return initialValue;
|
|
15
|
-
}
|
|
16
|
-
let resolveInitial;
|
|
17
|
-
let rejectInitial;
|
|
18
|
-
const pending = new Promise((resolve, reject) => {
|
|
19
|
-
resolveInitial = resolve;
|
|
20
|
-
rejectInitial = reject;
|
|
21
|
-
});
|
|
22
|
-
initialValue = pending;
|
|
23
|
-
void pending.catch(error => {
|
|
24
|
-
console.error('[chayns-api] Failed to initialize Apple safe-area updates', error);
|
|
25
|
-
});
|
|
26
|
-
const handleError = error => {
|
|
27
|
-
initialValue = undefined;
|
|
28
|
-
rejectInitial(error);
|
|
29
|
-
};
|
|
30
|
-
try {
|
|
31
|
-
const registration = subscribe(value => {
|
|
32
|
-
latestValue = value;
|
|
33
|
-
resolveInitial(value);
|
|
34
|
-
Array.from(listeners.values()).forEach(listener => listener(value));
|
|
35
|
-
});
|
|
36
|
-
void Promise.resolve(registration).catch(handleError);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
handleError(error);
|
|
39
|
-
}
|
|
40
|
-
return pending;
|
|
41
|
-
};
|
|
42
|
-
const functions = {
|
|
43
|
-
addAppleSafeAreaListener: callback => {
|
|
44
|
-
const id = ++counter;
|
|
45
|
-
listeners.set(id, callback);
|
|
46
|
-
if (latestValue) {
|
|
47
|
-
callback(latestValue);
|
|
48
|
-
}
|
|
49
|
-
void ensureStarted();
|
|
50
|
-
return Promise.resolve(id);
|
|
51
|
-
},
|
|
52
|
-
removeAppleSafeAreaListener: id => {
|
|
53
|
-
listeners.delete(id);
|
|
54
|
-
return Promise.resolve();
|
|
55
|
-
},
|
|
56
|
-
getAppleSafeArea: () => {
|
|
57
|
-
if (latestValue) {
|
|
58
|
-
return Promise.resolve(latestValue);
|
|
59
|
-
}
|
|
60
|
-
return ensureStarted();
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
channels.set(host, functions);
|
|
64
|
-
return functions;
|
|
65
|
-
};
|
|
66
|
-
export const getAppAppleSafeAreaFunctions = () => {
|
|
67
|
-
const host = window;
|
|
68
|
-
return getAppleSafeAreaFunctions(host, callback => {
|
|
69
|
-
host.chaynsApiAppleSafeAreaCallback = response => {
|
|
70
|
-
var _response$retVal, _value$top, _value$left, _value$bottom, _value$right;
|
|
71
|
-
const value = (_response$retVal = response === null || response === void 0 ? void 0 : response.retVal) !== null && _response$retVal !== void 0 ? _response$retVal : response;
|
|
72
|
-
callback({
|
|
73
|
-
top: (_value$top = value === null || value === void 0 ? void 0 : value.top) !== null && _value$top !== void 0 ? _value$top : 0,
|
|
74
|
-
left: (_value$left = value === null || value === void 0 ? void 0 : value.left) !== null && _value$left !== void 0 ? _value$left : 0,
|
|
75
|
-
bottom: (_value$bottom = value === null || value === void 0 ? void 0 : value.bottom) !== null && _value$bottom !== void 0 ? _value$bottom : 0,
|
|
76
|
-
right: (_value$right = value === null || value === void 0 ? void 0 : value.right) !== null && _value$right !== void 0 ? _value$right : 0,
|
|
77
|
-
...((value === null || value === void 0 ? void 0 : value.hingeAngle) === undefined ? {} : {
|
|
78
|
-
hingeAngle: value.hingeAngle
|
|
79
|
-
}),
|
|
80
|
-
...((value === null || value === void 0 ? void 0 : value.hingeStatus) === undefined ? {} : {
|
|
81
|
-
hingeStatus: value.hingeStatus
|
|
82
|
-
})
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
invokeAppCall({
|
|
86
|
-
action: 300,
|
|
87
|
-
value: {
|
|
88
|
-
callback: 'window.chaynsApiAppleSafeAreaCallback'
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
};
|