chayns-api 3.6.4 → 3.6.5
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/hooks/useSafeArea.js +25 -11
- package/dist/cjs/utils/appleSafeArea.js +101 -0
- package/dist/cjs/wrapper/AppWrapper.js +11 -54
- package/dist/cjs/wrapper/normalizeFunctions.js +29 -57
- package/dist/esm/hooks/useSafeArea.js +26 -12
- package/dist/esm/utils/appleSafeArea.js +92 -0
- package/dist/esm/wrapper/AppWrapper.js +11 -54
- package/dist/esm/wrapper/normalizeFunctions.js +29 -57
- package/dist/types/utils/appleSafeArea.d.ts +9 -0
- package/dist/types/wrapper/AppWrapper.d.ts +1 -2
- package/package.json +1 -1
- package/tests/safe-area.test.cjs +313 -7
|
@@ -30,16 +30,24 @@ const isSafeAreaSupported = (os, app) => {
|
|
|
30
30
|
return app.appVersion >= minVersion;
|
|
31
31
|
};
|
|
32
32
|
const useSafeAreaListener = () => {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
const $ = (0, _reactCompilerRuntime.c)(3);
|
|
34
|
+
const addListener = (0, _context.useFunctionsSelector)(_temp);
|
|
35
|
+
const removeListener = (0, _context.useFunctionsSelector)(_temp2);
|
|
36
|
+
let t0;
|
|
37
|
+
if ($[0] !== addListener || $[1] !== removeListener) {
|
|
38
|
+
t0 = callback => {
|
|
39
|
+
const listenerId = addListener(callback);
|
|
40
|
+
return () => {
|
|
41
|
+
listenerId.then(removeListener);
|
|
42
|
+
};
|
|
41
43
|
};
|
|
42
|
-
|
|
44
|
+
$[0] = addListener;
|
|
45
|
+
$[1] = removeListener;
|
|
46
|
+
$[2] = t0;
|
|
47
|
+
} else {
|
|
48
|
+
t0 = $[2];
|
|
49
|
+
}
|
|
50
|
+
return t0;
|
|
43
51
|
};
|
|
44
52
|
exports.useSafeAreaListener = useSafeAreaListener;
|
|
45
53
|
const useSafeArea = t0 => {
|
|
@@ -70,7 +78,7 @@ const useSafeArea = t0 => {
|
|
|
70
78
|
}
|
|
71
79
|
const [value, setValue] = (0, _react.useState)(t3);
|
|
72
80
|
const addListener = useSafeAreaListener();
|
|
73
|
-
const device = (0, _context.useValuesSelector)(
|
|
81
|
+
const device = (0, _context.useValuesSelector)(_temp3);
|
|
74
82
|
let t4;
|
|
75
83
|
if ($[3] !== device.app || $[4] !== device.os) {
|
|
76
84
|
t4 = isSafeAreaSupported(device.os, device.app);
|
|
@@ -105,6 +113,12 @@ const useSafeArea = t0 => {
|
|
|
105
113
|
return value;
|
|
106
114
|
};
|
|
107
115
|
exports.useSafeArea = useSafeArea;
|
|
108
|
-
function _temp(
|
|
116
|
+
function _temp(f) {
|
|
117
|
+
return f.addAppleSafeAreaListener;
|
|
118
|
+
}
|
|
119
|
+
function _temp2(f_0) {
|
|
120
|
+
return f_0.removeAppleSafeAreaListener;
|
|
121
|
+
}
|
|
122
|
+
function _temp3(v) {
|
|
109
123
|
return v.device;
|
|
110
124
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
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;
|
|
@@ -15,6 +15,7 @@ var _DialogHandler = _interopRequireDefault(require("../handler/DialogHandler"))
|
|
|
15
15
|
var _apiListener = require("../utils/apiListener");
|
|
16
16
|
var _IChaynsReact = require("../types/IChaynsReact");
|
|
17
17
|
var _appCall = _interopRequireDefault(require("../utils/appCall"));
|
|
18
|
+
var _appleSafeArea = require("../utils/appleSafeArea");
|
|
18
19
|
var _appStorage = require("../utils/appStorage");
|
|
19
20
|
var _deviceHelper = _interopRequireWildcard(require("../utils/deviceHelper"));
|
|
20
21
|
var _is = require("../utils/is");
|
|
@@ -24,7 +25,6 @@ class AppWrapper {
|
|
|
24
25
|
values = null;
|
|
25
26
|
history = null;
|
|
26
27
|
accessToken = '';
|
|
27
|
-
latestAppleSafeArea = null;
|
|
28
28
|
trustedDomains = _trustedDomains.DefaultTrustedDomains;
|
|
29
29
|
listeners = [];
|
|
30
30
|
customFunctions = {};
|
|
@@ -293,35 +293,7 @@ class AppWrapper {
|
|
|
293
293
|
}
|
|
294
294
|
return id;
|
|
295
295
|
},
|
|
296
|
-
addAppleSafeAreaListener:
|
|
297
|
-
const {
|
|
298
|
-
id,
|
|
299
|
-
shouldInitialize
|
|
300
|
-
} = (0, _apiListener.addApiListener)('appleSafeAreaListener', callback);
|
|
301
|
-
if (this.latestAppleSafeArea) {
|
|
302
|
-
callback(this.latestAppleSafeArea);
|
|
303
|
-
}
|
|
304
|
-
if (shouldInitialize) {
|
|
305
|
-
void this.appCall(300, {}, {
|
|
306
|
-
callback: v => {
|
|
307
|
-
this.latestAppleSafeArea = {
|
|
308
|
-
top: v.top,
|
|
309
|
-
left: v.left,
|
|
310
|
-
bottom: v.bottom,
|
|
311
|
-
right: v.right,
|
|
312
|
-
...(v.hingeAngle === undefined ? {} : {
|
|
313
|
-
hingeAngle: v.hingeAngle
|
|
314
|
-
}),
|
|
315
|
-
...(v.hingeStatus === undefined ? {} : {
|
|
316
|
-
hingeStatus: v.hingeStatus
|
|
317
|
-
})
|
|
318
|
-
};
|
|
319
|
-
(0, _apiListener.dispatchApiEvent)('appleSafeAreaListener', this.latestAppleSafeArea);
|
|
320
|
-
}
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
return id;
|
|
324
|
-
},
|
|
296
|
+
addAppleSafeAreaListener: callback => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().addAppleSafeAreaListener(callback),
|
|
325
297
|
customCallbackFunction: async () => {
|
|
326
298
|
this.notImplemented('customCallbackFunction');
|
|
327
299
|
},
|
|
@@ -365,27 +337,15 @@ class AppWrapper {
|
|
|
365
337
|
pageWidth: window.innerWidth,
|
|
366
338
|
topBarHeight: 0
|
|
367
339
|
}),
|
|
368
|
-
getAppleSafeArea:
|
|
369
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
370
|
-
if (this.latestAppleSafeArea) {
|
|
371
|
-
return this.latestAppleSafeArea;
|
|
372
|
-
}
|
|
373
|
-
const v = await this.appCall(300, {});
|
|
374
|
-
this.latestAppleSafeArea = {
|
|
375
|
-
top: (_v$top = v === null || v === void 0 ? void 0 : v.top) !== null && _v$top !== void 0 ? _v$top : 0,
|
|
376
|
-
left: (_v$left = v === null || v === void 0 ? void 0 : v.left) !== null && _v$left !== void 0 ? _v$left : 0,
|
|
377
|
-
bottom: (_v$bottom = v === null || v === void 0 ? void 0 : v.bottom) !== null && _v$bottom !== void 0 ? _v$bottom : 0,
|
|
378
|
-
right: (_v$right = v === null || v === void 0 ? void 0 : v.right) !== null && _v$right !== void 0 ? _v$right : 0,
|
|
379
|
-
...((v === null || v === void 0 ? void 0 : v.hingeAngle) === undefined ? {} : {
|
|
380
|
-
hingeAngle: v.hingeAngle
|
|
381
|
-
}),
|
|
382
|
-
...((v === null || v === void 0 ? void 0 : v.hingeStatus) === undefined ? {} : {
|
|
383
|
-
hingeStatus: v.hingeStatus
|
|
384
|
-
})
|
|
385
|
-
};
|
|
386
|
-
return this.latestAppleSafeArea;
|
|
387
|
-
},
|
|
340
|
+
getAppleSafeArea: () => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().getAppleSafeArea(),
|
|
388
341
|
invokeCall: async (value, callback) => {
|
|
342
|
+
if (value.action === 300) {
|
|
343
|
+
const functions = (0, _appleSafeArea.getAppAppleSafeAreaFunctions)();
|
|
344
|
+
if (callback) {
|
|
345
|
+
await functions.addAppleSafeAreaListener(callback);
|
|
346
|
+
}
|
|
347
|
+
return functions.getAppleSafeArea();
|
|
348
|
+
}
|
|
389
349
|
return this.appCall(value.action, value.value, {
|
|
390
350
|
callback
|
|
391
351
|
});
|
|
@@ -525,10 +485,7 @@ class AppWrapper {
|
|
|
525
485
|
this.resizeListener = null;
|
|
526
486
|
}
|
|
527
487
|
},
|
|
528
|
-
removeAppleSafeAreaListener:
|
|
529
|
-
const shouldRemove = (0, _apiListener.removeApiListener)('appleSafeAreaListener', id);
|
|
530
|
-
if (shouldRemove) {}
|
|
531
|
-
},
|
|
488
|
+
removeAppleSafeAreaListener: id => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().removeAppleSafeAreaListener(id),
|
|
532
489
|
selectPage: async options => {
|
|
533
490
|
var _this$values, _this$values2;
|
|
534
491
|
if ((_this$values = this.values) !== null && _this$values !== void 0 && (_this$values = _this$values.site) !== null && _this$values !== void 0 && _this$values.id && options.siteId && options.siteId !== ((_this$values2 = this.values) === null || _this$values2 === void 0 || (_this$values2 = _this$values2.site) === null || _this$values2 === void 0 ? void 0 : _this$values2.id)) {
|
|
@@ -4,72 +4,31 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.normalizeFunctions = void 0;
|
|
7
|
+
var _appleSafeArea = require("../utils/appleSafeArea");
|
|
7
8
|
const createAppleSafeAreaFunctions = functions => {
|
|
8
|
-
var
|
|
9
|
+
var _ref, _runtimeFunctions$add;
|
|
9
10
|
const runtimeFunctions = functions;
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const host = (_ref = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : runtimeFunctions.invokeCall) !== null && _ref !== void 0 ? _ref : functions;
|
|
12
|
+
return (0, _appleSafeArea.getAppleSafeAreaFunctions)(host, callback => {
|
|
13
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
14
|
+
return runtimeFunctions.addAppleSafeAreaListener(callback);
|
|
15
|
+
}
|
|
16
|
+
if (runtimeFunctions.invokeCall) {
|
|
17
|
+
return runtimeFunctions.invokeCall({
|
|
13
18
|
action: 300
|
|
14
19
|
}, callback);
|
|
15
20
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const baseRemoveListener = (_runtimeFunctions$rem = runtimeFunctions.removeAppleSafeAreaListener) !== null && _runtimeFunctions$rem !== void 0 ? _runtimeFunctions$rem : () => Promise.resolve();
|
|
19
|
-
let upstreamListenerId = null;
|
|
20
|
-
let hasLatestValue = false;
|
|
21
|
-
let latestValue = null;
|
|
22
|
-
let counter = 0;
|
|
23
|
-
const listeners = {};
|
|
24
|
-
const dispatch = value => {
|
|
25
|
-
latestValue = value;
|
|
26
|
-
hasLatestValue = true;
|
|
27
|
-
Object.values(listeners).forEach(listener => listener(value));
|
|
28
|
-
};
|
|
29
|
-
const addAppleSafeAreaListener = callback => {
|
|
30
|
-
const id = ++counter;
|
|
31
|
-
listeners[id] = callback;
|
|
32
|
-
if (hasLatestValue && latestValue) {
|
|
33
|
-
callback(latestValue);
|
|
34
|
-
}
|
|
35
|
-
if (!upstreamListenerId) {
|
|
36
|
-
upstreamListenerId = baseAddListener(result => {
|
|
37
|
-
dispatch(result);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
return Promise.resolve(id);
|
|
41
|
-
};
|
|
42
|
-
const removeAppleSafeAreaListener = id => {
|
|
43
|
-
delete listeners[id];
|
|
44
|
-
if (Object.keys(listeners).length === 0 && upstreamListenerId) {
|
|
45
|
-
void upstreamListenerId.then(listenerId => baseRemoveListener(listenerId));
|
|
46
|
-
upstreamListenerId = null;
|
|
47
|
-
}
|
|
48
|
-
return Promise.resolve();
|
|
49
|
-
};
|
|
50
|
-
const baseGetSafeArea = runtimeFunctions.getAppleSafeArea;
|
|
51
|
-
const getAppleSafeArea = async () => {
|
|
52
|
-
if (hasLatestValue && latestValue) {
|
|
53
|
-
return latestValue;
|
|
54
|
-
}
|
|
55
|
-
if (baseGetSafeArea) {
|
|
56
|
-
const result = await baseGetSafeArea();
|
|
57
|
-
dispatch(result);
|
|
58
|
-
return result;
|
|
21
|
+
if (runtimeFunctions.getAppleSafeArea) {
|
|
22
|
+
return runtimeFunctions.getAppleSafeArea().then(callback);
|
|
59
23
|
}
|
|
60
|
-
|
|
24
|
+
callback({
|
|
61
25
|
top: 0,
|
|
62
26
|
left: 0,
|
|
63
27
|
bottom: 0,
|
|
64
28
|
right: 0
|
|
65
|
-
};
|
|
66
|
-
return
|
|
67
|
-
};
|
|
68
|
-
return {
|
|
69
|
-
addAppleSafeAreaListener,
|
|
70
|
-
removeAppleSafeAreaListener,
|
|
71
|
-
getAppleSafeArea
|
|
72
|
-
};
|
|
29
|
+
});
|
|
30
|
+
return undefined;
|
|
31
|
+
});
|
|
73
32
|
};
|
|
74
33
|
const normalizeFunctions = functions => {
|
|
75
34
|
var _runtimeFunctions$isT;
|
|
@@ -82,7 +41,20 @@ const normalizeFunctions = functions => {
|
|
|
82
41
|
...functions
|
|
83
42
|
};
|
|
84
43
|
if (!hasSafeAreaFunctions) {
|
|
85
|
-
|
|
44
|
+
const safeAreaFunctions = createAppleSafeAreaFunctions(functions);
|
|
45
|
+
Object.assign(normalizedFunctions, safeAreaFunctions);
|
|
46
|
+
if (runtimeFunctions.invokeCall) {
|
|
47
|
+
const invokeCall = runtimeFunctions.invokeCall;
|
|
48
|
+
normalizedFunctions.invokeCall = async (value, callback) => {
|
|
49
|
+
if (value.action === 300) {
|
|
50
|
+
if (callback) {
|
|
51
|
+
await safeAreaFunctions.addAppleSafeAreaListener(callback);
|
|
52
|
+
}
|
|
53
|
+
return safeAreaFunctions.getAppleSafeArea();
|
|
54
|
+
}
|
|
55
|
+
return invokeCall(value, callback);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
86
58
|
}
|
|
87
59
|
normalizedFunctions.isTrustedUrl = (_runtimeFunctions$isT = runtimeFunctions.isTrustedUrl) !== null && _runtimeFunctions$isT !== void 0 ? _runtimeFunctions$isT : () => Promise.resolve(true);
|
|
88
60
|
return normalizedFunctions;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { c as _c } from "react-compiler-runtime";
|
|
2
|
-
import { useCallback, useEffect,
|
|
2
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
3
3
|
import { AppName } from '../types/IChaynsReact';
|
|
4
4
|
import { useFunctionsSelector, useValuesSelector } from './context';
|
|
5
5
|
const SAFE_AREA_SUPPORT_CONFIG = {
|
|
@@ -24,16 +24,24 @@ const isSafeAreaSupported = (os, app) => {
|
|
|
24
24
|
return app.appVersion >= minVersion;
|
|
25
25
|
};
|
|
26
26
|
export const useSafeAreaListener = () => {
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
const $ = _c(3);
|
|
28
|
+
const addListener = useFunctionsSelector(_temp);
|
|
29
|
+
const removeListener = useFunctionsSelector(_temp2);
|
|
30
|
+
let t0;
|
|
31
|
+
if ($[0] !== addListener || $[1] !== removeListener) {
|
|
32
|
+
t0 = callback => {
|
|
33
|
+
const listenerId = addListener(callback);
|
|
34
|
+
return () => {
|
|
35
|
+
listenerId.then(removeListener);
|
|
36
|
+
};
|
|
35
37
|
};
|
|
36
|
-
|
|
38
|
+
$[0] = addListener;
|
|
39
|
+
$[1] = removeListener;
|
|
40
|
+
$[2] = t0;
|
|
41
|
+
} else {
|
|
42
|
+
t0 = $[2];
|
|
43
|
+
}
|
|
44
|
+
return t0;
|
|
37
45
|
};
|
|
38
46
|
export const useSafeArea = t0 => {
|
|
39
47
|
const $ = _c(11);
|
|
@@ -63,7 +71,7 @@ export const useSafeArea = t0 => {
|
|
|
63
71
|
}
|
|
64
72
|
const [value, setValue] = useState(t3);
|
|
65
73
|
const addListener = useSafeAreaListener();
|
|
66
|
-
const device = useValuesSelector(
|
|
74
|
+
const device = useValuesSelector(_temp3);
|
|
67
75
|
let t4;
|
|
68
76
|
if ($[3] !== device.app || $[4] !== device.os) {
|
|
69
77
|
t4 = isSafeAreaSupported(device.os, device.app);
|
|
@@ -97,6 +105,12 @@ export const useSafeArea = t0 => {
|
|
|
97
105
|
useEffect(t5, t6);
|
|
98
106
|
return value;
|
|
99
107
|
};
|
|
100
|
-
function _temp(
|
|
108
|
+
function _temp(f) {
|
|
109
|
+
return f.addAppleSafeAreaListener;
|
|
110
|
+
}
|
|
111
|
+
function _temp2(f_0) {
|
|
112
|
+
return f_0.removeAppleSafeAreaListener;
|
|
113
|
+
}
|
|
114
|
+
function _temp3(v) {
|
|
101
115
|
return v.device;
|
|
102
116
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
};
|
|
@@ -12,6 +12,7 @@ import DialogHandler from '../handler/DialogHandler';
|
|
|
12
12
|
import { addApiListener, dispatchApiEvent, removeApiListener } from '../utils/apiListener';
|
|
13
13
|
import { AppName, DialogButtonType, DialogType, Environment, Font, Gender, IconType, LoginState, RuntimeEnvironment, TappEvent } from '../types/IChaynsReact';
|
|
14
14
|
import invokeAppCall from '../utils/appCall';
|
|
15
|
+
import { getAppAppleSafeAreaFunctions } from '../utils/appleSafeArea';
|
|
15
16
|
import { addAppStorageListener, clearAppStorage, isAppStorageAvailable, setAppStorageItem } from '../utils/appStorage';
|
|
16
17
|
import getDeviceInfo, { getScreenSize } from '../utils/deviceHelper';
|
|
17
18
|
import { isAppCallSupported } from '../utils/is';
|
|
@@ -152,7 +153,6 @@ export class AppWrapper {
|
|
|
152
153
|
_defineProperty(this, "values", null);
|
|
153
154
|
_defineProperty(this, "history", null);
|
|
154
155
|
_defineProperty(this, "accessToken", '');
|
|
155
|
-
_defineProperty(this, "latestAppleSafeArea", null);
|
|
156
156
|
_defineProperty(this, "trustedDomains", DefaultTrustedDomains);
|
|
157
157
|
_defineProperty(this, "listeners", []);
|
|
158
158
|
_defineProperty(this, "customFunctions", {});
|
|
@@ -256,35 +256,7 @@ export class AppWrapper {
|
|
|
256
256
|
}
|
|
257
257
|
return id;
|
|
258
258
|
},
|
|
259
|
-
addAppleSafeAreaListener:
|
|
260
|
-
const {
|
|
261
|
-
id,
|
|
262
|
-
shouldInitialize
|
|
263
|
-
} = addApiListener('appleSafeAreaListener', callback);
|
|
264
|
-
if (this.latestAppleSafeArea) {
|
|
265
|
-
callback(this.latestAppleSafeArea);
|
|
266
|
-
}
|
|
267
|
-
if (shouldInitialize) {
|
|
268
|
-
void this.appCall(300, {}, {
|
|
269
|
-
callback: v => {
|
|
270
|
-
this.latestAppleSafeArea = {
|
|
271
|
-
top: v.top,
|
|
272
|
-
left: v.left,
|
|
273
|
-
bottom: v.bottom,
|
|
274
|
-
right: v.right,
|
|
275
|
-
...(v.hingeAngle === undefined ? {} : {
|
|
276
|
-
hingeAngle: v.hingeAngle
|
|
277
|
-
}),
|
|
278
|
-
...(v.hingeStatus === undefined ? {} : {
|
|
279
|
-
hingeStatus: v.hingeStatus
|
|
280
|
-
})
|
|
281
|
-
};
|
|
282
|
-
dispatchApiEvent('appleSafeAreaListener', this.latestAppleSafeArea);
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
return id;
|
|
287
|
-
},
|
|
259
|
+
addAppleSafeAreaListener: callback => getAppAppleSafeAreaFunctions().addAppleSafeAreaListener(callback),
|
|
288
260
|
customCallbackFunction: async () => {
|
|
289
261
|
this.notImplemented('customCallbackFunction');
|
|
290
262
|
},
|
|
@@ -328,27 +300,15 @@ export class AppWrapper {
|
|
|
328
300
|
pageWidth: window.innerWidth,
|
|
329
301
|
topBarHeight: 0
|
|
330
302
|
}),
|
|
331
|
-
getAppleSafeArea:
|
|
332
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
333
|
-
if (this.latestAppleSafeArea) {
|
|
334
|
-
return this.latestAppleSafeArea;
|
|
335
|
-
}
|
|
336
|
-
const v = await this.appCall(300, {});
|
|
337
|
-
this.latestAppleSafeArea = {
|
|
338
|
-
top: (_v$top = v === null || v === void 0 ? void 0 : v.top) !== null && _v$top !== void 0 ? _v$top : 0,
|
|
339
|
-
left: (_v$left = v === null || v === void 0 ? void 0 : v.left) !== null && _v$left !== void 0 ? _v$left : 0,
|
|
340
|
-
bottom: (_v$bottom = v === null || v === void 0 ? void 0 : v.bottom) !== null && _v$bottom !== void 0 ? _v$bottom : 0,
|
|
341
|
-
right: (_v$right = v === null || v === void 0 ? void 0 : v.right) !== null && _v$right !== void 0 ? _v$right : 0,
|
|
342
|
-
...((v === null || v === void 0 ? void 0 : v.hingeAngle) === undefined ? {} : {
|
|
343
|
-
hingeAngle: v.hingeAngle
|
|
344
|
-
}),
|
|
345
|
-
...((v === null || v === void 0 ? void 0 : v.hingeStatus) === undefined ? {} : {
|
|
346
|
-
hingeStatus: v.hingeStatus
|
|
347
|
-
})
|
|
348
|
-
};
|
|
349
|
-
return this.latestAppleSafeArea;
|
|
350
|
-
},
|
|
303
|
+
getAppleSafeArea: () => getAppAppleSafeAreaFunctions().getAppleSafeArea(),
|
|
351
304
|
invokeCall: async (value, callback) => {
|
|
305
|
+
if (value.action === 300) {
|
|
306
|
+
const functions = getAppAppleSafeAreaFunctions();
|
|
307
|
+
if (callback) {
|
|
308
|
+
await functions.addAppleSafeAreaListener(callback);
|
|
309
|
+
}
|
|
310
|
+
return functions.getAppleSafeArea();
|
|
311
|
+
}
|
|
352
312
|
return this.appCall(value.action, value.value, {
|
|
353
313
|
callback
|
|
354
314
|
});
|
|
@@ -488,10 +448,7 @@ export class AppWrapper {
|
|
|
488
448
|
this.resizeListener = null;
|
|
489
449
|
}
|
|
490
450
|
},
|
|
491
|
-
removeAppleSafeAreaListener:
|
|
492
|
-
const shouldRemove = removeApiListener('appleSafeAreaListener', id);
|
|
493
|
-
if (shouldRemove) {}
|
|
494
|
-
},
|
|
451
|
+
removeAppleSafeAreaListener: id => getAppAppleSafeAreaFunctions().removeAppleSafeAreaListener(id),
|
|
495
452
|
selectPage: async options => {
|
|
496
453
|
var _this$values, _this$values2;
|
|
497
454
|
if ((_this$values = this.values) !== null && _this$values !== void 0 && (_this$values = _this$values.site) !== null && _this$values !== void 0 && _this$values.id && options.siteId && options.siteId !== ((_this$values2 = this.values) === null || _this$values2 === void 0 || (_this$values2 = _this$values2.site) === null || _this$values2 === void 0 ? void 0 : _this$values2.id)) {
|
|
@@ -1,69 +1,28 @@
|
|
|
1
|
+
import { getAppleSafeAreaFunctions } from '../utils/appleSafeArea';
|
|
1
2
|
const createAppleSafeAreaFunctions = functions => {
|
|
2
|
-
var
|
|
3
|
+
var _ref, _runtimeFunctions$add;
|
|
3
4
|
const runtimeFunctions = functions;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const host = (_ref = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : runtimeFunctions.invokeCall) !== null && _ref !== void 0 ? _ref : functions;
|
|
6
|
+
return getAppleSafeAreaFunctions(host, callback => {
|
|
7
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
8
|
+
return runtimeFunctions.addAppleSafeAreaListener(callback);
|
|
9
|
+
}
|
|
10
|
+
if (runtimeFunctions.invokeCall) {
|
|
11
|
+
return runtimeFunctions.invokeCall({
|
|
7
12
|
action: 300
|
|
8
13
|
}, callback);
|
|
9
14
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const baseRemoveListener = (_runtimeFunctions$rem = runtimeFunctions.removeAppleSafeAreaListener) !== null && _runtimeFunctions$rem !== void 0 ? _runtimeFunctions$rem : () => Promise.resolve();
|
|
13
|
-
let upstreamListenerId = null;
|
|
14
|
-
let hasLatestValue = false;
|
|
15
|
-
let latestValue = null;
|
|
16
|
-
let counter = 0;
|
|
17
|
-
const listeners = {};
|
|
18
|
-
const dispatch = value => {
|
|
19
|
-
latestValue = value;
|
|
20
|
-
hasLatestValue = true;
|
|
21
|
-
Object.values(listeners).forEach(listener => listener(value));
|
|
22
|
-
};
|
|
23
|
-
const addAppleSafeAreaListener = callback => {
|
|
24
|
-
const id = ++counter;
|
|
25
|
-
listeners[id] = callback;
|
|
26
|
-
if (hasLatestValue && latestValue) {
|
|
27
|
-
callback(latestValue);
|
|
28
|
-
}
|
|
29
|
-
if (!upstreamListenerId) {
|
|
30
|
-
upstreamListenerId = baseAddListener(result => {
|
|
31
|
-
dispatch(result);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
return Promise.resolve(id);
|
|
35
|
-
};
|
|
36
|
-
const removeAppleSafeAreaListener = id => {
|
|
37
|
-
delete listeners[id];
|
|
38
|
-
if (Object.keys(listeners).length === 0 && upstreamListenerId) {
|
|
39
|
-
void upstreamListenerId.then(listenerId => baseRemoveListener(listenerId));
|
|
40
|
-
upstreamListenerId = null;
|
|
41
|
-
}
|
|
42
|
-
return Promise.resolve();
|
|
43
|
-
};
|
|
44
|
-
const baseGetSafeArea = runtimeFunctions.getAppleSafeArea;
|
|
45
|
-
const getAppleSafeArea = async () => {
|
|
46
|
-
if (hasLatestValue && latestValue) {
|
|
47
|
-
return latestValue;
|
|
48
|
-
}
|
|
49
|
-
if (baseGetSafeArea) {
|
|
50
|
-
const result = await baseGetSafeArea();
|
|
51
|
-
dispatch(result);
|
|
52
|
-
return result;
|
|
15
|
+
if (runtimeFunctions.getAppleSafeArea) {
|
|
16
|
+
return runtimeFunctions.getAppleSafeArea().then(callback);
|
|
53
17
|
}
|
|
54
|
-
|
|
18
|
+
callback({
|
|
55
19
|
top: 0,
|
|
56
20
|
left: 0,
|
|
57
21
|
bottom: 0,
|
|
58
22
|
right: 0
|
|
59
|
-
};
|
|
60
|
-
return
|
|
61
|
-
};
|
|
62
|
-
return {
|
|
63
|
-
addAppleSafeAreaListener,
|
|
64
|
-
removeAppleSafeAreaListener,
|
|
65
|
-
getAppleSafeArea
|
|
66
|
-
};
|
|
23
|
+
});
|
|
24
|
+
return undefined;
|
|
25
|
+
});
|
|
67
26
|
};
|
|
68
27
|
export const normalizeFunctions = functions => {
|
|
69
28
|
var _runtimeFunctions$isT;
|
|
@@ -76,7 +35,20 @@ export const normalizeFunctions = functions => {
|
|
|
76
35
|
...functions
|
|
77
36
|
};
|
|
78
37
|
if (!hasSafeAreaFunctions) {
|
|
79
|
-
|
|
38
|
+
const safeAreaFunctions = createAppleSafeAreaFunctions(functions);
|
|
39
|
+
Object.assign(normalizedFunctions, safeAreaFunctions);
|
|
40
|
+
if (runtimeFunctions.invokeCall) {
|
|
41
|
+
const invokeCall = runtimeFunctions.invokeCall;
|
|
42
|
+
normalizedFunctions.invokeCall = async (value, callback) => {
|
|
43
|
+
if (value.action === 300) {
|
|
44
|
+
if (callback) {
|
|
45
|
+
await safeAreaFunctions.addAppleSafeAreaListener(callback);
|
|
46
|
+
}
|
|
47
|
+
return safeAreaFunctions.getAppleSafeArea();
|
|
48
|
+
}
|
|
49
|
+
return invokeCall(value, callback);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
80
52
|
}
|
|
81
53
|
normalizedFunctions.isTrustedUrl = (_runtimeFunctions$isT = runtimeFunctions.isTrustedUrl) !== null && _runtimeFunctions$isT !== void 0 ? _runtimeFunctions$isT : () => Promise.resolve(true);
|
|
82
54
|
return normalizedFunctions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AppleSafeArea, ChaynsReactFunctions } from '../types/IChaynsReact';
|
|
2
|
+
type AppleSafeAreaFunctions = Pick<ChaynsReactFunctions, 'addAppleSafeAreaListener' | 'removeAppleSafeAreaListener' | 'getAppleSafeArea'>;
|
|
3
|
+
type SafeAreaCallback = (value: AppleSafeArea) => void;
|
|
4
|
+
type Subscribe = (callback: SafeAreaCallback) => unknown;
|
|
5
|
+
/** Shares one upstream subscription and the latest value for each native host. */
|
|
6
|
+
export declare const getAppleSafeAreaFunctions: (host: object, subscribe: Subscribe) => AppleSafeAreaFunctions;
|
|
7
|
+
/** Uses a dedicated callback name so other AppWrapper instances cannot overwrite it. */
|
|
8
|
+
export declare const getAppAppleSafeAreaFunctions: () => AppleSafeAreaFunctions;
|
|
9
|
+
export {};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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;
|
|
7
6
|
private trustedDomains;
|
|
8
7
|
listeners: (() => void)[];
|
|
9
8
|
customFunctions: {};
|
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 } = require('../dist/cjs/hooks/useSafeArea');
|
|
14
|
+
const { useSafeArea, useSafeAreaListener } = 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,7 +23,9 @@ let listenerIds;
|
|
|
23
23
|
let root;
|
|
24
24
|
let currentValue;
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
// The native host only retains the most recently registered callback for call 300.
|
|
27
|
+
const emit = (value) => {
|
|
28
|
+
const call = nativeCalls.filter((entry) => entry.action === 300).at(-1);
|
|
27
29
|
const callbackName = call.value.callback.replace(/^window\./, '');
|
|
28
30
|
window[callbackName]({ retVal: value });
|
|
29
31
|
};
|
|
@@ -47,6 +49,10 @@ const renderHook = async (enabled = true) => {
|
|
|
47
49
|
};
|
|
48
50
|
|
|
49
51
|
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 });
|
|
50
56
|
wrapper = new AppWrapper();
|
|
51
57
|
wrapper.values = { device: { os: 'iOS', app: { name: AppName.Sidekick, appVersion: 2120 } } };
|
|
52
58
|
nativeCalls = [];
|
|
@@ -62,6 +68,7 @@ afterEach(async () => {
|
|
|
62
68
|
for (const key of Object.keys(window)) {
|
|
63
69
|
if (key.startsWith('chaynsApiV5Callback_')) delete window[key];
|
|
64
70
|
}
|
|
71
|
+
window.close();
|
|
65
72
|
});
|
|
66
73
|
|
|
67
74
|
test('exports the three native hinge status values through the public entry point', () => {
|
|
@@ -102,10 +109,9 @@ test('retains the existing zero-inset fallback for an absent native response', a
|
|
|
102
109
|
test('updates the listener cache and new subscribers when only hinge metadata changes', async () => {
|
|
103
110
|
const values = [];
|
|
104
111
|
await subscribe((value) => values.push(value));
|
|
105
|
-
const stream = nativeCalls[0];
|
|
106
112
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
107
113
|
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
108
|
-
emit(expected
|
|
114
|
+
emit(expected);
|
|
109
115
|
assert.deepEqual(values.at(-1), expected);
|
|
110
116
|
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
111
117
|
}
|
|
@@ -146,17 +152,19 @@ test('passes hinge updates through the existing compatibility fallback unchanged
|
|
|
146
152
|
|
|
147
153
|
test('updates the actual hook through native folding events with identical insets', async () => {
|
|
148
154
|
await renderHook();
|
|
149
|
-
const stream = nativeCalls[0];
|
|
150
155
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
151
156
|
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
152
|
-
await act(async () => emit(expected
|
|
157
|
+
await act(async () => emit(expected));
|
|
153
158
|
assert.deepEqual(currentValue, expected);
|
|
154
159
|
}
|
|
155
160
|
assert.equal(nativeCalls.length, 1);
|
|
156
161
|
const beforeCleanup = currentValue;
|
|
157
162
|
await renderHook(false);
|
|
158
|
-
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed }
|
|
163
|
+
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
159
164
|
assert.equal(currentValue, beforeCleanup);
|
|
165
|
+
await renderHook();
|
|
166
|
+
assert.deepEqual(currentValue, { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
167
|
+
assert.equal(nativeCalls.length, 1);
|
|
160
168
|
});
|
|
161
169
|
|
|
162
170
|
test('returns only insets on ordinary devices and retains disabled-hook behavior', async () => {
|
|
@@ -174,3 +182,301 @@ test('does not register native listeners on unsupported runtimes', async () => {
|
|
|
174
182
|
assert.equal(nativeCalls.length, 0);
|
|
175
183
|
assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
176
184
|
});
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
test('shares one persistent callback between a listener and concurrent initial getters', async () => {
|
|
188
|
+
const values = [];
|
|
189
|
+
await subscribe((value) => values.push(value));
|
|
190
|
+
const first = wrapper.functions.getAppleSafeArea();
|
|
191
|
+
const second = wrapper.functions.getAppleSafeArea();
|
|
192
|
+
assert.equal(nativeCalls.length, 1);
|
|
193
|
+
const closed = { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed };
|
|
194
|
+
emit(closed);
|
|
195
|
+
assert.deepEqual(await Promise.all([first, second]), [closed, closed]);
|
|
196
|
+
const open = { ...insets, right: 0, hingeAngle: 180, hingeStatus: HingeStatus.FullyOpen };
|
|
197
|
+
emit(open);
|
|
198
|
+
assert.deepEqual(values, [closed, open]);
|
|
199
|
+
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), open);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('keeps the stream alive after a getter initializes it and after all listeners leave', async () => {
|
|
203
|
+
const pending = wrapper.functions.getAppleSafeArea();
|
|
204
|
+
emit(insets);
|
|
205
|
+
await pending;
|
|
206
|
+
const values = [];
|
|
207
|
+
const id = await subscribe((value) => values.push(value));
|
|
208
|
+
await wrapper.functions.removeAppleSafeAreaListener(id);
|
|
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
|
+
},
|
|
437
|
+
});
|
|
438
|
+
const first = functions.getAppleSafeArea();
|
|
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
|
+
});
|