chayns-api 3.6.5 → 3.6.6

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.
@@ -30,24 +30,16 @@ const isSafeAreaSupported = (os, app) => {
30
30
  return app.appVersion >= minVersion;
31
31
  };
32
32
  const useSafeAreaListener = () => {
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
- };
33
+ const addListener = (0, _context.useFunctionsSelector)(f => f.addAppleSafeAreaListener);
34
+ const removeListener = (0, _context.useFunctionsSelector)(f => f.removeAppleSafeAreaListener);
35
+ const promiseRef = (0, _react.useRef)();
36
+ return (0, _react.useCallback)(callback => {
37
+ promiseRef.current = addListener(callback);
38
+ return () => {
39
+ var _promiseRef$current;
40
+ void ((_promiseRef$current = promiseRef.current) === null || _promiseRef$current === void 0 ? void 0 : _promiseRef$current.then(removeListener));
43
41
  };
44
- $[0] = addListener;
45
- $[1] = removeListener;
46
- $[2] = t0;
47
- } else {
48
- t0 = $[2];
49
- }
50
- return t0;
42
+ }, []);
51
43
  };
52
44
  exports.useSafeAreaListener = useSafeAreaListener;
53
45
  const useSafeArea = t0 => {
@@ -78,7 +70,7 @@ const useSafeArea = t0 => {
78
70
  }
79
71
  const [value, setValue] = (0, _react.useState)(t3);
80
72
  const addListener = useSafeAreaListener();
81
- const device = (0, _context.useValuesSelector)(_temp3);
73
+ const device = (0, _context.useValuesSelector)(_temp);
82
74
  let t4;
83
75
  if ($[3] !== device.app || $[4] !== device.os) {
84
76
  t4 = isSafeAreaSupported(device.os, device.app);
@@ -113,12 +105,6 @@ const useSafeArea = t0 => {
113
105
  return value;
114
106
  };
115
107
  exports.useSafeArea = useSafeArea;
116
- function _temp(f) {
117
- return f.addAppleSafeAreaListener;
118
- }
119
- function _temp2(f_0) {
120
- return f_0.removeAppleSafeAreaListener;
121
- }
122
- function _temp3(v) {
108
+ function _temp(v) {
123
109
  return v.device;
124
110
  }
@@ -15,7 +15,6 @@ 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");
19
18
  var _appStorage = require("../utils/appStorage");
20
19
  var _deviceHelper = _interopRequireWildcard(require("../utils/deviceHelper"));
21
20
  var _is = require("../utils/is");
@@ -25,6 +24,7 @@ class AppWrapper {
25
24
  values = null;
26
25
  history = null;
27
26
  accessToken = '';
27
+ latestAppleSafeArea = null;
28
28
  trustedDomains = _trustedDomains.DefaultTrustedDomains;
29
29
  listeners = [];
30
30
  customFunctions = {};
@@ -293,7 +293,35 @@ class AppWrapper {
293
293
  }
294
294
  return id;
295
295
  },
296
- addAppleSafeAreaListener: callback => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().addAppleSafeAreaListener(callback),
296
+ addAppleSafeAreaListener: async callback => {
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
+ },
297
325
  customCallbackFunction: async () => {
298
326
  this.notImplemented('customCallbackFunction');
299
327
  },
@@ -337,15 +365,27 @@ class AppWrapper {
337
365
  pageWidth: window.innerWidth,
338
366
  topBarHeight: 0
339
367
  }),
340
- getAppleSafeArea: () => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().getAppleSafeArea(),
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();
368
+ getAppleSafeArea: async () => {
369
+ var _v$top, _v$left, _v$bottom, _v$right;
370
+ if (this.latestAppleSafeArea) {
371
+ return this.latestAppleSafeArea;
348
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
+ },
388
+ invokeCall: async (value, callback) => {
349
389
  return this.appCall(value.action, value.value, {
350
390
  callback
351
391
  });
@@ -485,7 +525,10 @@ class AppWrapper {
485
525
  this.resizeListener = null;
486
526
  }
487
527
  },
488
- removeAppleSafeAreaListener: id => (0, _appleSafeArea.getAppAppleSafeAreaFunctions)().removeAppleSafeAreaListener(id),
528
+ removeAppleSafeAreaListener: async id => {
529
+ const shouldRemove = (0, _apiListener.removeApiListener)('appleSafeAreaListener', id);
530
+ if (shouldRemove) {}
531
+ },
489
532
  selectPage: async options => {
490
533
  var _this$values, _this$values2;
491
534
  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,31 +4,72 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.normalizeFunctions = void 0;
7
- var _appleSafeArea = require("../utils/appleSafeArea");
8
7
  const createAppleSafeAreaFunctions = functions => {
9
- var _ref, _runtimeFunctions$add;
8
+ var _runtimeFunctions$add, _runtimeFunctions$rem;
10
9
  const runtimeFunctions = functions;
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({
10
+ const baseAddListener = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : callback => {
11
+ if (functions.invokeCall) {
12
+ void functions.invokeCall({
18
13
  action: 300
19
14
  }, callback);
20
15
  }
21
- if (runtimeFunctions.getAppleSafeArea) {
22
- return runtimeFunctions.getAppleSafeArea().then(callback);
16
+ return Promise.resolve(-1);
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;
23
59
  }
24
- callback({
60
+ const fallback = {
25
61
  top: 0,
26
62
  left: 0,
27
63
  bottom: 0,
28
64
  right: 0
29
- });
30
- return undefined;
31
- });
65
+ };
66
+ return fallback;
67
+ };
68
+ return {
69
+ addAppleSafeAreaListener,
70
+ removeAppleSafeAreaListener,
71
+ getAppleSafeArea
72
+ };
32
73
  };
33
74
  const normalizeFunctions = functions => {
34
75
  var _runtimeFunctions$isT;
@@ -41,20 +82,7 @@ const normalizeFunctions = functions => {
41
82
  ...functions
42
83
  };
43
84
  if (!hasSafeAreaFunctions) {
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
- }
85
+ Object.assign(normalizedFunctions, createAppleSafeAreaFunctions(functions));
58
86
  }
59
87
  normalizedFunctions.isTrustedUrl = (_runtimeFunctions$isT = runtimeFunctions.isTrustedUrl) !== null && _runtimeFunctions$isT !== void 0 ? _runtimeFunctions$isT : () => Promise.resolve(true);
60
88
  return normalizedFunctions;
@@ -1,5 +1,5 @@
1
1
  import { c as _c } from "react-compiler-runtime";
2
- import { useCallback, useEffect, useState } from 'react';
2
+ import { useCallback, useEffect, useRef, 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,24 +24,16 @@ const isSafeAreaSupported = (os, app) => {
24
24
  return app.appVersion >= minVersion;
25
25
  };
26
26
  export const useSafeAreaListener = () => {
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
- };
27
+ const addListener = useFunctionsSelector(f => f.addAppleSafeAreaListener);
28
+ const removeListener = useFunctionsSelector(f => f.removeAppleSafeAreaListener);
29
+ const promiseRef = useRef();
30
+ return useCallback(callback => {
31
+ promiseRef.current = addListener(callback);
32
+ return () => {
33
+ var _promiseRef$current;
34
+ void ((_promiseRef$current = promiseRef.current) === null || _promiseRef$current === void 0 ? void 0 : _promiseRef$current.then(removeListener));
37
35
  };
38
- $[0] = addListener;
39
- $[1] = removeListener;
40
- $[2] = t0;
41
- } else {
42
- t0 = $[2];
43
- }
44
- return t0;
36
+ }, []);
45
37
  };
46
38
  export const useSafeArea = t0 => {
47
39
  const $ = _c(11);
@@ -71,7 +63,7 @@ export const useSafeArea = t0 => {
71
63
  }
72
64
  const [value, setValue] = useState(t3);
73
65
  const addListener = useSafeAreaListener();
74
- const device = useValuesSelector(_temp3);
66
+ const device = useValuesSelector(_temp);
75
67
  let t4;
76
68
  if ($[3] !== device.app || $[4] !== device.os) {
77
69
  t4 = isSafeAreaSupported(device.os, device.app);
@@ -105,12 +97,6 @@ export const useSafeArea = t0 => {
105
97
  useEffect(t5, t6);
106
98
  return value;
107
99
  };
108
- function _temp(f) {
109
- return f.addAppleSafeAreaListener;
110
- }
111
- function _temp2(f_0) {
112
- return f_0.removeAppleSafeAreaListener;
113
- }
114
- function _temp3(v) {
100
+ function _temp(v) {
115
101
  return v.device;
116
102
  }
@@ -12,7 +12,6 @@ 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';
16
15
  import { addAppStorageListener, clearAppStorage, isAppStorageAvailable, setAppStorageItem } from '../utils/appStorage';
17
16
  import getDeviceInfo, { getScreenSize } from '../utils/deviceHelper';
18
17
  import { isAppCallSupported } from '../utils/is';
@@ -153,6 +152,7 @@ export class AppWrapper {
153
152
  _defineProperty(this, "values", null);
154
153
  _defineProperty(this, "history", null);
155
154
  _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,7 +256,35 @@ export class AppWrapper {
256
256
  }
257
257
  return id;
258
258
  },
259
- addAppleSafeAreaListener: callback => getAppAppleSafeAreaFunctions().addAppleSafeAreaListener(callback),
259
+ addAppleSafeAreaListener: async callback => {
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
+ },
260
288
  customCallbackFunction: async () => {
261
289
  this.notImplemented('customCallbackFunction');
262
290
  },
@@ -300,15 +328,27 @@ export class AppWrapper {
300
328
  pageWidth: window.innerWidth,
301
329
  topBarHeight: 0
302
330
  }),
303
- getAppleSafeArea: () => getAppAppleSafeAreaFunctions().getAppleSafeArea(),
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();
331
+ getAppleSafeArea: async () => {
332
+ var _v$top, _v$left, _v$bottom, _v$right;
333
+ if (this.latestAppleSafeArea) {
334
+ return this.latestAppleSafeArea;
311
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
+ },
351
+ invokeCall: async (value, callback) => {
312
352
  return this.appCall(value.action, value.value, {
313
353
  callback
314
354
  });
@@ -448,7 +488,10 @@ export class AppWrapper {
448
488
  this.resizeListener = null;
449
489
  }
450
490
  },
451
- removeAppleSafeAreaListener: id => getAppAppleSafeAreaFunctions().removeAppleSafeAreaListener(id),
491
+ removeAppleSafeAreaListener: async id => {
492
+ const shouldRemove = removeApiListener('appleSafeAreaListener', id);
493
+ if (shouldRemove) {}
494
+ },
452
495
  selectPage: async options => {
453
496
  var _this$values, _this$values2;
454
497
  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,28 +1,69 @@
1
- import { getAppleSafeAreaFunctions } from '../utils/appleSafeArea';
2
1
  const createAppleSafeAreaFunctions = functions => {
3
- var _ref, _runtimeFunctions$add;
2
+ var _runtimeFunctions$add, _runtimeFunctions$rem;
4
3
  const runtimeFunctions = functions;
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({
4
+ const baseAddListener = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : callback => {
5
+ if (functions.invokeCall) {
6
+ void functions.invokeCall({
12
7
  action: 300
13
8
  }, callback);
14
9
  }
15
- if (runtimeFunctions.getAppleSafeArea) {
16
- return runtimeFunctions.getAppleSafeArea().then(callback);
10
+ return Promise.resolve(-1);
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;
17
53
  }
18
- callback({
54
+ const fallback = {
19
55
  top: 0,
20
56
  left: 0,
21
57
  bottom: 0,
22
58
  right: 0
23
- });
24
- return undefined;
25
- });
59
+ };
60
+ return fallback;
61
+ };
62
+ return {
63
+ addAppleSafeAreaListener,
64
+ removeAppleSafeAreaListener,
65
+ getAppleSafeArea
66
+ };
26
67
  };
27
68
  export const normalizeFunctions = functions => {
28
69
  var _runtimeFunctions$isT;
@@ -35,20 +76,7 @@ export const normalizeFunctions = functions => {
35
76
  ...functions
36
77
  };
37
78
  if (!hasSafeAreaFunctions) {
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
- }
79
+ Object.assign(normalizedFunctions, createAppleSafeAreaFunctions(functions));
52
80
  }
53
81
  normalizedFunctions.isTrustedUrl = (_runtimeFunctions$isT = runtimeFunctions.isTrustedUrl) !== null && _runtimeFunctions$isT !== void 0 ? _runtimeFunctions$isT : () => Promise.resolve(true);
54
82
  return normalizedFunctions;
@@ -1,8 +1,9 @@
1
- import { ChaynsReactFunctions, ChaynsReactValues, ChaynsStyleSettings, CleanupCallback, DataChangeCallback, IChaynsReact } from '../types/IChaynsReact';
1
+ import { AppleSafeArea, ChaynsReactFunctions, ChaynsReactValues, ChaynsStyleSettings, CleanupCallback, DataChangeCallback, IChaynsReact } from '../types/IChaynsReact';
2
2
  export declare class AppWrapper implements IChaynsReact {
3
3
  values: ChaynsReactValues;
4
4
  history: IChaynsReact['history'];
5
5
  accessToken: string;
6
+ latestAppleSafeArea: AppleSafeArea | null;
6
7
  private trustedDomains;
7
8
  listeners: (() => void)[];
8
9
  customFunctions: {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chayns-api",
3
- "version": "3.6.5",
3
+ "version": "3.6.6",
4
4
  "description": "new chayns api",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -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, useSafeAreaListener } = require('../dist/cjs/hooks/useSafeArea');
14
+ const { useSafeArea } = require('../dist/cjs/hooks/useSafeArea');
15
15
  const { AppName, HingeStatus } = require('../dist/cjs/types/IChaynsReact');
16
16
  const { AppWrapper } = require('../dist/cjs/wrapper/AppWrapper');
17
17
  const { normalizeFunctions } = require('../dist/cjs/wrapper/normalizeFunctions');
@@ -23,9 +23,7 @@ let listenerIds;
23
23
  let root;
24
24
  let currentValue;
25
25
 
26
- // The native host only retains the most recently registered callback for call 300.
27
- const emit = (value) => {
28
- const call = nativeCalls.filter((entry) => entry.action === 300).at(-1);
26
+ const emit = (value, call = nativeCalls.at(-1)) => {
29
27
  const callbackName = call.value.callback.replace(/^window\./, '');
30
28
  window[callbackName]({ retVal: value });
31
29
  };
@@ -49,10 +47,6 @@ const renderHook = async (enabled = true) => {
49
47
  };
50
48
 
51
49
  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
50
  wrapper = new AppWrapper();
57
51
  wrapper.values = { device: { os: 'iOS', app: { name: AppName.Sidekick, appVersion: 2120 } } };
58
52
  nativeCalls = [];
@@ -68,7 +62,6 @@ afterEach(async () => {
68
62
  for (const key of Object.keys(window)) {
69
63
  if (key.startsWith('chaynsApiV5Callback_')) delete window[key];
70
64
  }
71
- window.close();
72
65
  });
73
66
 
74
67
  test('exports the three native hinge status values through the public entry point', () => {
@@ -109,9 +102,10 @@ test('retains the existing zero-inset fallback for an absent native response', a
109
102
  test('updates the listener cache and new subscribers when only hinge metadata changes', async () => {
110
103
  const values = [];
111
104
  await subscribe((value) => values.push(value));
105
+ const stream = nativeCalls[0];
112
106
  for (const hingeStatus of Object.values(HingeStatus)) {
113
107
  const expected = { ...insets, hingeAngle: 90, hingeStatus };
114
- emit(expected);
108
+ emit(expected, stream);
115
109
  assert.deepEqual(values.at(-1), expected);
116
110
  assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
117
111
  }
@@ -152,19 +146,17 @@ test('passes hinge updates through the existing compatibility fallback unchanged
152
146
 
153
147
  test('updates the actual hook through native folding events with identical insets', async () => {
154
148
  await renderHook();
149
+ const stream = nativeCalls[0];
155
150
  for (const hingeStatus of Object.values(HingeStatus)) {
156
151
  const expected = { ...insets, hingeAngle: 90, hingeStatus };
157
- await act(async () => emit(expected));
152
+ await act(async () => emit(expected, stream));
158
153
  assert.deepEqual(currentValue, expected);
159
154
  }
160
155
  assert.equal(nativeCalls.length, 1);
161
156
  const beforeCleanup = currentValue;
162
157
  await renderHook(false);
163
- emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
158
+ emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed }, stream);
164
159
  assert.equal(currentValue, beforeCleanup);
165
- await renderHook();
166
- assert.deepEqual(currentValue, { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
167
- assert.equal(nativeCalls.length, 1);
168
160
  });
169
161
 
170
162
  test('returns only insets on ordinary devices and retains disabled-hook behavior', async () => {
@@ -182,301 +174,3 @@ test('does not register native listeners on unsupported runtimes', async () => {
182
174
  assert.equal(nativeCalls.length, 0);
183
175
  assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
184
176
  });
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
- });
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getAppleSafeAreaFunctions = exports.getAppAppleSafeAreaFunctions = void 0;
7
- var _appCall = _interopRequireDefault(require("./appCall"));
8
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
- const channels = new WeakMap();
10
- const getAppleSafeAreaFunctions = (host, subscribe) => {
11
- const existing = channels.get(host);
12
- if (existing) {
13
- return existing;
14
- }
15
- let latestValue;
16
- let initialValue;
17
- let counter = 0;
18
- const listeners = new Map();
19
- const ensureStarted = () => {
20
- if (initialValue) {
21
- return initialValue;
22
- }
23
- let resolveInitial;
24
- let rejectInitial;
25
- const pending = new Promise((resolve, reject) => {
26
- resolveInitial = resolve;
27
- rejectInitial = reject;
28
- });
29
- initialValue = pending;
30
- void pending.catch(error => {
31
- console.error('[chayns-api] Failed to initialize Apple safe-area updates', error);
32
- });
33
- const handleError = error => {
34
- initialValue = undefined;
35
- rejectInitial(error);
36
- };
37
- try {
38
- const registration = subscribe(value => {
39
- latestValue = value;
40
- resolveInitial(value);
41
- Array.from(listeners.values()).forEach(listener => listener(value));
42
- });
43
- void Promise.resolve(registration).catch(handleError);
44
- } catch (error) {
45
- handleError(error);
46
- }
47
- return pending;
48
- };
49
- const functions = {
50
- addAppleSafeAreaListener: callback => {
51
- const id = ++counter;
52
- listeners.set(id, callback);
53
- if (latestValue) {
54
- callback(latestValue);
55
- }
56
- void ensureStarted();
57
- return Promise.resolve(id);
58
- },
59
- removeAppleSafeAreaListener: id => {
60
- listeners.delete(id);
61
- return Promise.resolve();
62
- },
63
- getAppleSafeArea: () => {
64
- if (latestValue) {
65
- return Promise.resolve(latestValue);
66
- }
67
- return ensureStarted();
68
- }
69
- };
70
- channels.set(host, functions);
71
- return functions;
72
- };
73
- exports.getAppleSafeAreaFunctions = getAppleSafeAreaFunctions;
74
- const getAppAppleSafeAreaFunctions = () => {
75
- const host = window;
76
- return getAppleSafeAreaFunctions(host, callback => {
77
- host.chaynsApiAppleSafeAreaCallback = response => {
78
- var _response$retVal, _value$top, _value$left, _value$bottom, _value$right;
79
- const value = (_response$retVal = response === null || response === void 0 ? void 0 : response.retVal) !== null && _response$retVal !== void 0 ? _response$retVal : response;
80
- callback({
81
- top: (_value$top = value === null || value === void 0 ? void 0 : value.top) !== null && _value$top !== void 0 ? _value$top : 0,
82
- left: (_value$left = value === null || value === void 0 ? void 0 : value.left) !== null && _value$left !== void 0 ? _value$left : 0,
83
- bottom: (_value$bottom = value === null || value === void 0 ? void 0 : value.bottom) !== null && _value$bottom !== void 0 ? _value$bottom : 0,
84
- right: (_value$right = value === null || value === void 0 ? void 0 : value.right) !== null && _value$right !== void 0 ? _value$right : 0,
85
- ...((value === null || value === void 0 ? void 0 : value.hingeAngle) === undefined ? {} : {
86
- hingeAngle: value.hingeAngle
87
- }),
88
- ...((value === null || value === void 0 ? void 0 : value.hingeStatus) === undefined ? {} : {
89
- hingeStatus: value.hingeStatus
90
- })
91
- });
92
- };
93
- (0, _appCall.default)({
94
- action: 300,
95
- value: {
96
- callback: 'window.chaynsApiAppleSafeAreaCallback'
97
- }
98
- });
99
- });
100
- };
101
- exports.getAppAppleSafeAreaFunctions = getAppAppleSafeAreaFunctions;
@@ -1,92 +0,0 @@
1
- import invokeAppCall from './appCall';
2
- const channels = new WeakMap();
3
- export const getAppleSafeAreaFunctions = (host, subscribe) => {
4
- const existing = channels.get(host);
5
- if (existing) {
6
- return existing;
7
- }
8
- let latestValue;
9
- let initialValue;
10
- let counter = 0;
11
- const listeners = new Map();
12
- const ensureStarted = () => {
13
- if (initialValue) {
14
- return initialValue;
15
- }
16
- let resolveInitial;
17
- let rejectInitial;
18
- const pending = new Promise((resolve, reject) => {
19
- resolveInitial = resolve;
20
- rejectInitial = reject;
21
- });
22
- initialValue = pending;
23
- void pending.catch(error => {
24
- console.error('[chayns-api] Failed to initialize Apple safe-area updates', error);
25
- });
26
- const handleError = error => {
27
- initialValue = undefined;
28
- rejectInitial(error);
29
- };
30
- try {
31
- const registration = subscribe(value => {
32
- latestValue = value;
33
- resolveInitial(value);
34
- Array.from(listeners.values()).forEach(listener => listener(value));
35
- });
36
- void Promise.resolve(registration).catch(handleError);
37
- } catch (error) {
38
- handleError(error);
39
- }
40
- return pending;
41
- };
42
- const functions = {
43
- addAppleSafeAreaListener: callback => {
44
- const id = ++counter;
45
- listeners.set(id, callback);
46
- if (latestValue) {
47
- callback(latestValue);
48
- }
49
- void ensureStarted();
50
- return Promise.resolve(id);
51
- },
52
- removeAppleSafeAreaListener: id => {
53
- listeners.delete(id);
54
- return Promise.resolve();
55
- },
56
- getAppleSafeArea: () => {
57
- if (latestValue) {
58
- return Promise.resolve(latestValue);
59
- }
60
- return ensureStarted();
61
- }
62
- };
63
- channels.set(host, functions);
64
- return functions;
65
- };
66
- export const getAppAppleSafeAreaFunctions = () => {
67
- const host = window;
68
- return getAppleSafeAreaFunctions(host, callback => {
69
- host.chaynsApiAppleSafeAreaCallback = response => {
70
- var _response$retVal, _value$top, _value$left, _value$bottom, _value$right;
71
- const value = (_response$retVal = response === null || response === void 0 ? void 0 : response.retVal) !== null && _response$retVal !== void 0 ? _response$retVal : response;
72
- callback({
73
- top: (_value$top = value === null || value === void 0 ? void 0 : value.top) !== null && _value$top !== void 0 ? _value$top : 0,
74
- left: (_value$left = value === null || value === void 0 ? void 0 : value.left) !== null && _value$left !== void 0 ? _value$left : 0,
75
- bottom: (_value$bottom = value === null || value === void 0 ? void 0 : value.bottom) !== null && _value$bottom !== void 0 ? _value$bottom : 0,
76
- right: (_value$right = value === null || value === void 0 ? void 0 : value.right) !== null && _value$right !== void 0 ? _value$right : 0,
77
- ...((value === null || value === void 0 ? void 0 : value.hingeAngle) === undefined ? {} : {
78
- hingeAngle: value.hingeAngle
79
- }),
80
- ...((value === null || value === void 0 ? void 0 : value.hingeStatus) === undefined ? {} : {
81
- hingeStatus: value.hingeStatus
82
- })
83
- });
84
- };
85
- invokeAppCall({
86
- action: 300,
87
- value: {
88
- callback: 'window.chaynsApiAppleSafeAreaCallback'
89
- }
90
- });
91
- });
92
- };
@@ -1,9 +0,0 @@
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 {};