chayns-api 3.6.3 → 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/types/IChaynsReact.js +7 -1
- package/dist/cjs/utils/appleSafeArea.js +101 -0
- package/dist/cjs/wrapper/AppWrapper.js +11 -42
- package/dist/cjs/wrapper/normalizeFunctions.js +29 -57
- package/dist/esm/hooks/useSafeArea.js +26 -12
- package/dist/esm/types/IChaynsReact.js +6 -0
- package/dist/esm/utils/appleSafeArea.js +92 -0
- package/dist/esm/wrapper/AppWrapper.js +11 -42
- package/dist/esm/wrapper/normalizeFunctions.js +29 -57
- package/dist/types/hooks/useSafeArea.d.ts +3 -0
- package/dist/types/types/IChaynsReact.d.ts +10 -0
- package/dist/types/utils/appleSafeArea.d.ts +9 -0
- package/dist/types/wrapper/AppWrapper.d.ts +0 -1
- package/package.json +2 -1
- package/tests/safe-area.test.cjs +482 -0
- package/tests/safe-area.types.ts +12 -0
|
@@ -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
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.selectType = exports.WeekDayType = exports.ToastType = exports.TappEvent = exports.SnapshotType = exports.SharingApp = exports.ScreenSize = exports.ScanQrCodeCodeType = exports.ScanQrCodeCameraTypes = exports.RuntimeEnvironment = exports.RuntimeEnviroment = exports.MediaType = exports.LoginState = exports.Language = exports.IconType = exports.IconStyle = exports.IOSFeedbackVibration = exports.Gender = exports.Font = exports.FloatingButtonPosition = exports.FloatingButtonAnimation = exports.FileExplorerSortType = exports.FileExplorerFilterType = exports.Environment = exports.DialogType = exports.DialogSelectType = exports.DialogInputType = exports.DialogIconType = exports.DialogButtonType = exports.DialogAnimation = exports.DeviceOs = exports.DesignSettingsUpdateTappViewModes = exports.DesignSettingsUpdateSeasonTypes = exports.DesignSettingsUpdateGalleryAnimationTypes = exports.DesignSettingsUpdateCoverTypes = exports.DesignSettingsUpdateBackgroundType = exports.DateType = exports.ColorMode = exports.BrowserName = exports.AppName = exports.AppFlavor = exports.AccessMode = void 0;
|
|
6
|
+
exports.selectType = exports.WeekDayType = exports.ToastType = exports.TappEvent = exports.SnapshotType = exports.SharingApp = exports.ScreenSize = exports.ScanQrCodeCodeType = exports.ScanQrCodeCameraTypes = exports.RuntimeEnvironment = exports.RuntimeEnviroment = exports.MediaType = exports.LoginState = exports.Language = exports.IconType = exports.IconStyle = exports.IOSFeedbackVibration = exports.HingeStatus = exports.Gender = exports.Font = exports.FloatingButtonPosition = exports.FloatingButtonAnimation = exports.FileExplorerSortType = exports.FileExplorerFilterType = exports.Environment = exports.DialogType = exports.DialogSelectType = exports.DialogInputType = exports.DialogIconType = exports.DialogButtonType = exports.DialogAnimation = exports.DeviceOs = exports.DesignSettingsUpdateTappViewModes = exports.DesignSettingsUpdateSeasonTypes = exports.DesignSettingsUpdateGalleryAnimationTypes = exports.DesignSettingsUpdateCoverTypes = exports.DesignSettingsUpdateBackgroundType = exports.DateType = exports.ColorMode = exports.BrowserName = exports.AppName = exports.AppFlavor = exports.AccessMode = void 0;
|
|
7
7
|
let DateType = exports.DateType = function (DateType) {
|
|
8
8
|
DateType[DateType["DATE"] = 0] = "DATE";
|
|
9
9
|
DateType[DateType["TIME"] = 1] = "TIME";
|
|
@@ -350,4 +350,10 @@ let DialogIconType = exports.DialogIconType = function (DialogIconType) {
|
|
|
350
350
|
DialogIconType["WarningIcon"] = "%%DialogWarningIcon%%";
|
|
351
351
|
DialogIconType["ErrorIcon"] = "%%DialogErrorIcon%%";
|
|
352
352
|
return DialogIconType;
|
|
353
|
+
}({});
|
|
354
|
+
let HingeStatus = exports.HingeStatus = function (HingeStatus) {
|
|
355
|
+
HingeStatus["Closed"] = "closed";
|
|
356
|
+
HingeStatus["PartiallyOpen"] = "partiallyOpen";
|
|
357
|
+
HingeStatus["FullyOpen"] = "fullyOpen";
|
|
358
|
+
return HingeStatus;
|
|
353
359
|
}({});
|
|
@@ -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,29 +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
|
-
};
|
|
313
|
-
(0, _apiListener.dispatchApiEvent)('appleSafeAreaListener', this.latestAppleSafeArea);
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
return id;
|
|
318
|
-
},
|
|
296
|
+
addAppleSafeAreaListener: callback => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().addAppleSafeAreaListener(callback),
|
|
319
297
|
customCallbackFunction: async () => {
|
|
320
298
|
this.notImplemented('customCallbackFunction');
|
|
321
299
|
},
|
|
@@ -359,21 +337,15 @@ class AppWrapper {
|
|
|
359
337
|
pageWidth: window.innerWidth,
|
|
360
338
|
topBarHeight: 0
|
|
361
339
|
}),
|
|
362
|
-
getAppleSafeArea:
|
|
363
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
364
|
-
if (this.latestAppleSafeArea) {
|
|
365
|
-
return this.latestAppleSafeArea;
|
|
366
|
-
}
|
|
367
|
-
const v = await this.appCall(300, {});
|
|
368
|
-
this.latestAppleSafeArea = {
|
|
369
|
-
top: (_v$top = v === null || v === void 0 ? void 0 : v.top) !== null && _v$top !== void 0 ? _v$top : 0,
|
|
370
|
-
left: (_v$left = v === null || v === void 0 ? void 0 : v.left) !== null && _v$left !== void 0 ? _v$left : 0,
|
|
371
|
-
bottom: (_v$bottom = v === null || v === void 0 ? void 0 : v.bottom) !== null && _v$bottom !== void 0 ? _v$bottom : 0,
|
|
372
|
-
right: (_v$right = v === null || v === void 0 ? void 0 : v.right) !== null && _v$right !== void 0 ? _v$right : 0
|
|
373
|
-
};
|
|
374
|
-
return this.latestAppleSafeArea;
|
|
375
|
-
},
|
|
340
|
+
getAppleSafeArea: () => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().getAppleSafeArea(),
|
|
376
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
|
+
}
|
|
377
349
|
return this.appCall(value.action, value.value, {
|
|
378
350
|
callback
|
|
379
351
|
});
|
|
@@ -513,10 +485,7 @@ class AppWrapper {
|
|
|
513
485
|
this.resizeListener = null;
|
|
514
486
|
}
|
|
515
487
|
},
|
|
516
|
-
removeAppleSafeAreaListener:
|
|
517
|
-
const shouldRemove = (0, _apiListener.removeApiListener)('appleSafeAreaListener', id);
|
|
518
|
-
if (shouldRemove) {}
|
|
519
|
-
},
|
|
488
|
+
removeAppleSafeAreaListener: id => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().removeAppleSafeAreaListener(id),
|
|
520
489
|
selectPage: async options => {
|
|
521
490
|
var _this$values, _this$values2;
|
|
522
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
|
}
|
|
@@ -344,4 +344,10 @@ export let DialogIconType = function (DialogIconType) {
|
|
|
344
344
|
DialogIconType["WarningIcon"] = "%%DialogWarningIcon%%";
|
|
345
345
|
DialogIconType["ErrorIcon"] = "%%DialogErrorIcon%%";
|
|
346
346
|
return DialogIconType;
|
|
347
|
+
}({});
|
|
348
|
+
export let HingeStatus = function (HingeStatus) {
|
|
349
|
+
HingeStatus["Closed"] = "closed";
|
|
350
|
+
HingeStatus["PartiallyOpen"] = "partiallyOpen";
|
|
351
|
+
HingeStatus["FullyOpen"] = "fullyOpen";
|
|
352
|
+
return HingeStatus;
|
|
347
353
|
}({});
|
|
@@ -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,29 +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
|
-
};
|
|
276
|
-
dispatchApiEvent('appleSafeAreaListener', this.latestAppleSafeArea);
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
return id;
|
|
281
|
-
},
|
|
259
|
+
addAppleSafeAreaListener: callback => getAppAppleSafeAreaFunctions().addAppleSafeAreaListener(callback),
|
|
282
260
|
customCallbackFunction: async () => {
|
|
283
261
|
this.notImplemented('customCallbackFunction');
|
|
284
262
|
},
|
|
@@ -322,21 +300,15 @@ export class AppWrapper {
|
|
|
322
300
|
pageWidth: window.innerWidth,
|
|
323
301
|
topBarHeight: 0
|
|
324
302
|
}),
|
|
325
|
-
getAppleSafeArea:
|
|
326
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
327
|
-
if (this.latestAppleSafeArea) {
|
|
328
|
-
return this.latestAppleSafeArea;
|
|
329
|
-
}
|
|
330
|
-
const v = await this.appCall(300, {});
|
|
331
|
-
this.latestAppleSafeArea = {
|
|
332
|
-
top: (_v$top = v === null || v === void 0 ? void 0 : v.top) !== null && _v$top !== void 0 ? _v$top : 0,
|
|
333
|
-
left: (_v$left = v === null || v === void 0 ? void 0 : v.left) !== null && _v$left !== void 0 ? _v$left : 0,
|
|
334
|
-
bottom: (_v$bottom = v === null || v === void 0 ? void 0 : v.bottom) !== null && _v$bottom !== void 0 ? _v$bottom : 0,
|
|
335
|
-
right: (_v$right = v === null || v === void 0 ? void 0 : v.right) !== null && _v$right !== void 0 ? _v$right : 0
|
|
336
|
-
};
|
|
337
|
-
return this.latestAppleSafeArea;
|
|
338
|
-
},
|
|
303
|
+
getAppleSafeArea: () => getAppAppleSafeAreaFunctions().getAppleSafeArea(),
|
|
339
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
|
+
}
|
|
340
312
|
return this.appCall(value.action, value.value, {
|
|
341
313
|
callback
|
|
342
314
|
});
|
|
@@ -476,10 +448,7 @@ export class AppWrapper {
|
|
|
476
448
|
this.resizeListener = null;
|
|
477
449
|
}
|
|
478
450
|
},
|
|
479
|
-
removeAppleSafeAreaListener:
|
|
480
|
-
const shouldRemove = removeApiListener('appleSafeAreaListener', id);
|
|
481
|
-
if (shouldRemove) {}
|
|
482
|
-
},
|
|
451
|
+
removeAppleSafeAreaListener: id => getAppAppleSafeAreaFunctions().removeAppleSafeAreaListener(id),
|
|
483
452
|
selectPage: async options => {
|
|
484
453
|
var _this$values, _this$values2;
|
|
485
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;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { AppleSafeArea } from '../types/IChaynsReact';
|
|
2
2
|
/**
|
|
3
|
+
* Listens for safe-area and optional iPhone Duo hinge updates from the native app.
|
|
3
4
|
* @category Hooks
|
|
4
5
|
*/
|
|
5
6
|
export declare const useSafeAreaListener: () => (callback: (result: AppleSafeArea) => void) => () => void;
|
|
6
7
|
/**
|
|
8
|
+
* Returns safe-area insets and optional iPhone Duo hinge metadata.
|
|
9
|
+
* Hinge updates allow layouts to react when the device is opened or closed.
|
|
7
10
|
* @category Hooks
|
|
8
11
|
*/
|
|
9
12
|
export declare const useSafeArea: ({ enabled }?: {
|
|
@@ -1118,11 +1118,21 @@ export declare enum DialogIconType {
|
|
|
1118
1118
|
export type AnonymousAccountResult = {
|
|
1119
1119
|
token: string;
|
|
1120
1120
|
};
|
|
1121
|
+
/** Hinge posture reported by the native app on iPhone Duo. */
|
|
1122
|
+
export declare enum HingeStatus {
|
|
1123
|
+
Closed = "closed",
|
|
1124
|
+
PartiallyOpen = "partiallyOpen",
|
|
1125
|
+
FullyOpen = "fullyOpen"
|
|
1126
|
+
}
|
|
1121
1127
|
export interface AppleSafeArea {
|
|
1122
1128
|
top: number;
|
|
1123
1129
|
left: number;
|
|
1124
1130
|
bottom: number;
|
|
1125
1131
|
right: number;
|
|
1132
|
+
/** Only provided by the native app on iPhone Duo. */
|
|
1133
|
+
hingeAngle?: number;
|
|
1134
|
+
/** Only provided by the native app on iPhone Duo. */
|
|
1135
|
+
hingeStatus?: HingeStatus;
|
|
1126
1136
|
}
|
|
1127
1137
|
export type PageMainGroupType = {
|
|
1128
1138
|
Id: 1;
|
|
@@ -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 {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chayns-api",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.5",
|
|
4
4
|
"description": "new chayns api",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"dev": "npm link && npm run watch",
|
|
37
37
|
"format": "prettier . --write",
|
|
38
38
|
"lint": "eslint src",
|
|
39
|
+
"test:safe-area": "npm run build:cjs && npm run build:typescript && node --test tests/safe-area.test.cjs && tsc --noEmit --strict --target ES2020 --module commonjs --esModuleInterop --skipLibCheck tests/safe-area.types.ts",
|
|
39
40
|
"prepublishOnly": "npm run build:cjs && npm run build:esm && npm run build:typescript",
|
|
40
41
|
"preversion": "npm run build:typescript -- --noEmit"
|
|
41
42
|
},
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const { afterEach, beforeEach, test } = require('node:test');
|
|
3
|
+
const { JSDOM } = require('jsdom');
|
|
4
|
+
|
|
5
|
+
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'https://example.test' });
|
|
6
|
+
global.window = dom.window;
|
|
7
|
+
global.document = dom.window.document;
|
|
8
|
+
Object.defineProperty(global, 'navigator', { configurable: true, value: dom.window.navigator });
|
|
9
|
+
global.IS_REACT_ACT_ENVIRONMENT = true;
|
|
10
|
+
|
|
11
|
+
const { act, createElement, StrictMode } = require('react');
|
|
12
|
+
const { createRoot } = require('react-dom/client');
|
|
13
|
+
const { ChaynsContext } = require('../dist/cjs/components/ChaynsContext');
|
|
14
|
+
const { useSafeArea, useSafeAreaListener } = require('../dist/cjs/hooks/useSafeArea');
|
|
15
|
+
const { AppName, HingeStatus } = require('../dist/cjs/types/IChaynsReact');
|
|
16
|
+
const { AppWrapper } = require('../dist/cjs/wrapper/AppWrapper');
|
|
17
|
+
const { normalizeFunctions } = require('../dist/cjs/wrapper/normalizeFunctions');
|
|
18
|
+
|
|
19
|
+
const insets = { top: 12, left: 17, bottom: 20, right: 43 };
|
|
20
|
+
let wrapper;
|
|
21
|
+
let nativeCalls;
|
|
22
|
+
let listenerIds;
|
|
23
|
+
let root;
|
|
24
|
+
let currentValue;
|
|
25
|
+
|
|
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);
|
|
29
|
+
const callbackName = call.value.callback.replace(/^window\./, '');
|
|
30
|
+
window[callbackName]({ retVal: value });
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const subscribe = async (callback) => {
|
|
34
|
+
const id = await wrapper.functions.addAppleSafeAreaListener(callback);
|
|
35
|
+
listenerIds.push(id);
|
|
36
|
+
return id;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const Probe = ({ enabled }) => {
|
|
40
|
+
currentValue = useSafeArea({ enabled });
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const renderHook = async (enabled = true) => {
|
|
45
|
+
root ??= createRoot(document.createElement('div'));
|
|
46
|
+
await act(async () => {
|
|
47
|
+
root.render(createElement(StrictMode, null, createElement(ChaynsContext.Provider, { value: wrapper }, createElement(Probe, { enabled }))));
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
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 });
|
|
56
|
+
wrapper = new AppWrapper();
|
|
57
|
+
wrapper.values = { device: { os: 'iOS', app: { name: AppName.Sidekick, appVersion: 2120 } } };
|
|
58
|
+
nativeCalls = [];
|
|
59
|
+
listenerIds = [];
|
|
60
|
+
root = undefined;
|
|
61
|
+
currentValue = undefined;
|
|
62
|
+
window.webkit = { messageHandlers: { jsonCall: { postMessage: (json) => nativeCalls.push(JSON.parse(json)) } } };
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
afterEach(async () => {
|
|
66
|
+
if (root) await act(async () => root.unmount());
|
|
67
|
+
for (const id of listenerIds) await wrapper.functions.removeAppleSafeAreaListener(id);
|
|
68
|
+
for (const key of Object.keys(window)) {
|
|
69
|
+
if (key.startsWith('chaynsApiV5Callback_')) delete window[key];
|
|
70
|
+
}
|
|
71
|
+
window.close();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('exports the three native hinge status values through the public entry point', () => {
|
|
75
|
+
assert.deepEqual(HingeStatus, { Closed: 'closed', PartiallyOpen: 'partiallyOpen', FullyOpen: 'fullyOpen' });
|
|
76
|
+
assert.equal(require('../dist/cjs').HingeStatus, HingeStatus);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
80
|
+
test(`preserves ${hingeStatus} and a zero angle in a direct safe-area request`, async () => {
|
|
81
|
+
const expected = { ...insets, hingeAngle: 0, hingeStatus };
|
|
82
|
+
const pending = wrapper.functions.getAppleSafeArea();
|
|
83
|
+
assert.equal(nativeCalls[0].action, 300);
|
|
84
|
+
emit({ ...expected, unrelated: 'ignored' });
|
|
85
|
+
assert.deepEqual(await pending, expected);
|
|
86
|
+
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
87
|
+
assert.equal(nativeCalls.length, 1);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
test('keeps ordinary iPhones free of hinge properties in requests and listener updates', async () => {
|
|
92
|
+
const pending = wrapper.functions.getAppleSafeArea();
|
|
93
|
+
emit(insets);
|
|
94
|
+
assert.deepEqual(await pending, insets);
|
|
95
|
+
const values = [];
|
|
96
|
+
await subscribe((value) => values.push(value));
|
|
97
|
+
emit(insets);
|
|
98
|
+
assert.deepEqual(values, [insets, insets]);
|
|
99
|
+
assert.equal(Object.hasOwn(values[1], 'hingeAngle'), false);
|
|
100
|
+
assert.equal(Object.hasOwn(values[1], 'hingeStatus'), false);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('retains the existing zero-inset fallback for an absent native response', async () => {
|
|
104
|
+
const pending = wrapper.functions.getAppleSafeArea();
|
|
105
|
+
emit(undefined);
|
|
106
|
+
assert.deepEqual(await pending, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('updates the listener cache and new subscribers when only hinge metadata changes', async () => {
|
|
110
|
+
const values = [];
|
|
111
|
+
await subscribe((value) => values.push(value));
|
|
112
|
+
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
113
|
+
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
114
|
+
emit(expected);
|
|
115
|
+
assert.deepEqual(values.at(-1), expected);
|
|
116
|
+
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
117
|
+
}
|
|
118
|
+
const replay = [];
|
|
119
|
+
await subscribe((value) => replay.push(value));
|
|
120
|
+
assert.deepEqual(replay, [values.at(-1)]);
|
|
121
|
+
assert.equal(nativeCalls.length, 1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('stops delivering native updates to removed listeners', async () => {
|
|
125
|
+
const values = [];
|
|
126
|
+
const id = await subscribe((value) => values.push(value));
|
|
127
|
+
await wrapper.functions.removeAppleSafeAreaListener(id);
|
|
128
|
+
listenerIds = [];
|
|
129
|
+
emit({ ...insets, hingeAngle: 90, hingeStatus: HingeStatus.PartiallyOpen });
|
|
130
|
+
assert.deepEqual(values, []);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('passes hinge updates through the existing compatibility fallback unchanged', async () => {
|
|
134
|
+
let dispatch;
|
|
135
|
+
const functions = normalizeFunctions({
|
|
136
|
+
invokeCall: (options, callback) => {
|
|
137
|
+
assert.equal(options.action, 300);
|
|
138
|
+
dispatch = callback;
|
|
139
|
+
return Promise.resolve();
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
const values = [];
|
|
143
|
+
const id = await functions.addAppleSafeAreaListener((value) => values.push(value));
|
|
144
|
+
const expected = { ...insets, hingeAngle: 90, hingeStatus: HingeStatus.PartiallyOpen };
|
|
145
|
+
dispatch(expected);
|
|
146
|
+
assert.deepEqual(values, [expected]);
|
|
147
|
+
assert.deepEqual(await functions.getAppleSafeArea(), expected);
|
|
148
|
+
await functions.removeAppleSafeAreaListener(id);
|
|
149
|
+
dispatch({ ...expected, hingeStatus: HingeStatus.FullyOpen });
|
|
150
|
+
assert.deepEqual(values, [expected]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('updates the actual hook through native folding events with identical insets', async () => {
|
|
154
|
+
await renderHook();
|
|
155
|
+
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
156
|
+
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
157
|
+
await act(async () => emit(expected));
|
|
158
|
+
assert.deepEqual(currentValue, expected);
|
|
159
|
+
}
|
|
160
|
+
assert.equal(nativeCalls.length, 1);
|
|
161
|
+
const beforeCleanup = currentValue;
|
|
162
|
+
await renderHook(false);
|
|
163
|
+
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
164
|
+
assert.equal(currentValue, beforeCleanup);
|
|
165
|
+
await renderHook();
|
|
166
|
+
assert.deepEqual(currentValue, { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
167
|
+
assert.equal(nativeCalls.length, 1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('returns only insets on ordinary devices and retains disabled-hook behavior', async () => {
|
|
171
|
+
await renderHook(false);
|
|
172
|
+
assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
173
|
+
assert.equal(nativeCalls.length, 0);
|
|
174
|
+
await renderHook();
|
|
175
|
+
await act(async () => emit(insets));
|
|
176
|
+
assert.deepEqual(currentValue, insets);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('does not register native listeners on unsupported runtimes', async () => {
|
|
180
|
+
wrapper.values.device.os = 'Android';
|
|
181
|
+
await renderHook();
|
|
182
|
+
assert.equal(nativeCalls.length, 0);
|
|
183
|
+
assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
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
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { addAppleSafeAreaListener, AppleSafeArea, getSafeArea, HingeStatus, useSafeArea, useSafeAreaListener } from '../dist/types';
|
|
2
|
+
|
|
3
|
+
const ordinaryPhone: AppleSafeArea = { top: 12, left: 17, bottom: 20, right: 43 };
|
|
4
|
+
const duo: AppleSafeArea = { ...ordinaryPhone, hingeAngle: 0, hingeStatus: HingeStatus.Closed };
|
|
5
|
+
|
|
6
|
+
declare const hookValue: ReturnType<typeof useSafeArea>;
|
|
7
|
+
const hingeAngle: number | undefined = hookValue.hingeAngle;
|
|
8
|
+
const hingeStatus: HingeStatus | undefined = hookValue.hingeStatus;
|
|
9
|
+
|
|
10
|
+
const getInsets: () => Promise<AppleSafeArea> = getSafeArea;
|
|
11
|
+
const addListener: (callback: (value: AppleSafeArea) => void) => Promise<number> = addAppleSafeAreaListener;
|
|
12
|
+
const useListener: () => (callback: (value: AppleSafeArea) => void) => () => void = useSafeAreaListener;
|