chayns-api 3.6.6 → 3.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/calls/appleSafeAreaListener.js +13 -4
- package/dist/cjs/wrapper/AppWrapper.js +30 -35
- package/dist/cjs/wrapper/normalizeFunctions.js +43 -42
- package/dist/esm/calls/appleSafeAreaListener.js +12 -3
- package/dist/esm/wrapper/AppWrapper.js +30 -35
- package/dist/esm/wrapper/normalizeFunctions.js +43 -42
- package/dist/types/calls/appleSafeAreaListener.d.ts +5 -5
- package/dist/types/wrapper/AppWrapper.d.ts +1 -0
- package/package.json +1 -1
- package/tests/safe-area.test.cjs +76 -19
|
@@ -4,12 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.addAppleSafeAreaListener = void 0;
|
|
7
|
-
var _index = require("./index");
|
|
8
7
|
const addAppleSafeAreaListener = ({
|
|
9
|
-
callback
|
|
8
|
+
callback,
|
|
9
|
+
invokeCall
|
|
10
10
|
}) => {
|
|
11
|
-
void
|
|
11
|
+
void invokeCall({
|
|
12
12
|
action: 300
|
|
13
|
-
},
|
|
13
|
+
}, result => {
|
|
14
|
+
const value = result && 'retVal' in result ? result.retVal : result;
|
|
15
|
+
callback({
|
|
16
|
+
top: 0,
|
|
17
|
+
left: 0,
|
|
18
|
+
bottom: 0,
|
|
19
|
+
right: 0,
|
|
20
|
+
...value
|
|
21
|
+
});
|
|
22
|
+
});
|
|
14
23
|
};
|
|
15
24
|
exports.addAppleSafeAreaListener = addAppleSafeAreaListener;
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.AppWrapper = void 0;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash.throttle"));
|
|
8
8
|
var _getUserInfo = _interopRequireDefault(require("../calls/getUserInfo"));
|
|
9
|
+
var _appleSafeAreaListener = require("../calls/appleSafeAreaListener");
|
|
9
10
|
var _sendMessage = require("../calls/sendMessage");
|
|
10
11
|
var _constants = require("../constants");
|
|
11
12
|
var _trustedDomains = require("../constants/trustedDomains");
|
|
@@ -166,6 +167,13 @@ class AppWrapper {
|
|
|
166
167
|
console.warn(`call ${call} not implement in app`);
|
|
167
168
|
}
|
|
168
169
|
counter = 0;
|
|
170
|
+
getCallbackName() {
|
|
171
|
+
let callbackName;
|
|
172
|
+
do {
|
|
173
|
+
callbackName = `chaynsApiV5Callback_${this.counter++}`;
|
|
174
|
+
} while (callbackName in window);
|
|
175
|
+
return callbackName;
|
|
176
|
+
}
|
|
169
177
|
appCall(action, value = {}, {
|
|
170
178
|
callback,
|
|
171
179
|
awaitResult = true
|
|
@@ -178,7 +186,7 @@ class AppWrapper {
|
|
|
178
186
|
return;
|
|
179
187
|
}
|
|
180
188
|
return new Promise(resolve => {
|
|
181
|
-
const callbackName =
|
|
189
|
+
const callbackName = this.getCallbackName();
|
|
182
190
|
window[callbackName] = v => {
|
|
183
191
|
var _v$retVal2;
|
|
184
192
|
if (callback) {
|
|
@@ -297,26 +305,19 @@ class AppWrapper {
|
|
|
297
305
|
const {
|
|
298
306
|
id,
|
|
299
307
|
shouldInitialize
|
|
300
|
-
} = (0, _apiListener.addApiListener)('appleSafeAreaListener',
|
|
308
|
+
} = (0, _apiListener.addApiListener)('appleSafeAreaListener', value => {
|
|
309
|
+
this.latestAppleSafeArea = value;
|
|
310
|
+
callback(value);
|
|
311
|
+
});
|
|
301
312
|
if (this.latestAppleSafeArea) {
|
|
302
313
|
callback(this.latestAppleSafeArea);
|
|
303
314
|
}
|
|
304
315
|
if (shouldInitialize) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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);
|
|
316
|
+
(0, _appleSafeAreaListener.addAppleSafeAreaListener)({
|
|
317
|
+
invokeCall: this.functions.invokeCall,
|
|
318
|
+
callback: value => {
|
|
319
|
+
this.latestAppleSafeArea = value;
|
|
320
|
+
(0, _apiListener.dispatchApiEvent)('appleSafeAreaListener', value);
|
|
320
321
|
}
|
|
321
322
|
});
|
|
322
323
|
}
|
|
@@ -366,24 +367,19 @@ class AppWrapper {
|
|
|
366
367
|
topBarHeight: 0
|
|
367
368
|
}),
|
|
368
369
|
getAppleSafeArea: async () => {
|
|
369
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
370
370
|
if (this.latestAppleSafeArea) {
|
|
371
371
|
return this.latestAppleSafeArea;
|
|
372
372
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
hingeStatus: v.hingeStatus
|
|
384
|
-
})
|
|
385
|
-
};
|
|
386
|
-
return this.latestAppleSafeArea;
|
|
373
|
+
let resolveValue;
|
|
374
|
+
const result = new Promise(resolve => {
|
|
375
|
+
resolveValue = resolve;
|
|
376
|
+
});
|
|
377
|
+
const id = await this.functions.addAppleSafeAreaListener(resolveValue);
|
|
378
|
+
try {
|
|
379
|
+
return await result;
|
|
380
|
+
} finally {
|
|
381
|
+
await this.functions.removeAppleSafeAreaListener(id);
|
|
382
|
+
}
|
|
387
383
|
},
|
|
388
384
|
invokeCall: async (value, callback) => {
|
|
389
385
|
return this.appCall(value.action, value.value, {
|
|
@@ -396,7 +392,7 @@ class AppWrapper {
|
|
|
396
392
|
});
|
|
397
393
|
},
|
|
398
394
|
invokeDialogCall: async (value, callback) => {
|
|
399
|
-
const callbackName =
|
|
395
|
+
const callbackName = this.getCallbackName();
|
|
400
396
|
window[callbackName] = ({
|
|
401
397
|
retVal
|
|
402
398
|
}) => {
|
|
@@ -526,8 +522,7 @@ class AppWrapper {
|
|
|
526
522
|
}
|
|
527
523
|
},
|
|
528
524
|
removeAppleSafeAreaListener: async id => {
|
|
529
|
-
|
|
530
|
-
if (shouldRemove) {}
|
|
525
|
+
(0, _apiListener.removeApiListener)('appleSafeAreaListener', id);
|
|
531
526
|
},
|
|
532
527
|
selectPage: async options => {
|
|
533
528
|
var _this$values, _this$values2;
|
|
@@ -4,66 +4,67 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.normalizeFunctions = void 0;
|
|
7
|
+
var _appleSafeAreaListener = require("../calls/appleSafeAreaListener");
|
|
8
|
+
var _apiListener = require("../utils/apiListener");
|
|
7
9
|
const createAppleSafeAreaFunctions = functions => {
|
|
8
|
-
var _runtimeFunctions$add, _runtimeFunctions$rem;
|
|
9
10
|
const runtimeFunctions = functions;
|
|
10
|
-
const baseAddListener = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : callback => {
|
|
11
|
-
if (functions.invokeCall) {
|
|
12
|
-
void functions.invokeCall({
|
|
13
|
-
action: 300
|
|
14
|
-
}, callback);
|
|
15
|
-
}
|
|
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
11
|
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
12
|
const addAppleSafeAreaListener = callback => {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
13
|
+
const handleValue = value => {
|
|
14
|
+
latestValue = value;
|
|
15
|
+
callback(value);
|
|
16
|
+
};
|
|
17
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
18
|
+
return runtimeFunctions.addAppleSafeAreaListener(handleValue);
|
|
19
|
+
}
|
|
20
|
+
const {
|
|
21
|
+
id,
|
|
22
|
+
shouldInitialize
|
|
23
|
+
} = (0, _apiListener.addApiListener)('appleSafeAreaListener', handleValue);
|
|
24
|
+
if (latestValue) {
|
|
33
25
|
callback(latestValue);
|
|
34
26
|
}
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
if (shouldInitialize && runtimeFunctions.invokeCall) {
|
|
28
|
+
(0, _appleSafeAreaListener.addAppleSafeAreaListener)({
|
|
29
|
+
invokeCall: runtimeFunctions.invokeCall,
|
|
30
|
+
callback: value => {
|
|
31
|
+
latestValue = value;
|
|
32
|
+
(0, _apiListener.dispatchApiEvent)('appleSafeAreaListener', value);
|
|
33
|
+
}
|
|
38
34
|
});
|
|
39
35
|
}
|
|
40
36
|
return Promise.resolve(id);
|
|
41
37
|
};
|
|
42
38
|
const removeAppleSafeAreaListener = id => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
void
|
|
46
|
-
upstreamListenerId = null;
|
|
39
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
40
|
+
var _runtimeFunctions$rem, _runtimeFunctions$rem2;
|
|
41
|
+
return (_runtimeFunctions$rem = (_runtimeFunctions$rem2 = runtimeFunctions.removeAppleSafeAreaListener) === null || _runtimeFunctions$rem2 === void 0 ? void 0 : _runtimeFunctions$rem2.call(runtimeFunctions, id)) !== null && _runtimeFunctions$rem !== void 0 ? _runtimeFunctions$rem : Promise.resolve();
|
|
47
42
|
}
|
|
43
|
+
(0, _apiListener.removeApiListener)('appleSafeAreaListener', id);
|
|
48
44
|
return Promise.resolve();
|
|
49
45
|
};
|
|
50
|
-
const baseGetSafeArea = runtimeFunctions.getAppleSafeArea;
|
|
51
46
|
const getAppleSafeArea = async () => {
|
|
52
|
-
if (
|
|
47
|
+
if (latestValue) {
|
|
53
48
|
return latestValue;
|
|
54
49
|
}
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
if (!runtimeFunctions.addAppleSafeAreaListener && !runtimeFunctions.invokeCall) {
|
|
51
|
+
return {
|
|
52
|
+
top: 0,
|
|
53
|
+
left: 0,
|
|
54
|
+
bottom: 0,
|
|
55
|
+
right: 0
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
let resolveValue;
|
|
59
|
+
const result = new Promise(resolve => {
|
|
60
|
+
resolveValue = resolve;
|
|
61
|
+
});
|
|
62
|
+
const id = await addAppleSafeAreaListener(resolveValue);
|
|
63
|
+
try {
|
|
64
|
+
return await result;
|
|
65
|
+
} finally {
|
|
66
|
+
await removeAppleSafeAreaListener(id);
|
|
59
67
|
}
|
|
60
|
-
const fallback = {
|
|
61
|
-
top: 0,
|
|
62
|
-
left: 0,
|
|
63
|
-
bottom: 0,
|
|
64
|
-
right: 0
|
|
65
|
-
};
|
|
66
|
-
return fallback;
|
|
67
68
|
};
|
|
68
69
|
return {
|
|
69
70
|
addAppleSafeAreaListener,
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
import { invokeCall } from './index';
|
|
2
1
|
export const addAppleSafeAreaListener = ({
|
|
3
|
-
callback
|
|
2
|
+
callback,
|
|
3
|
+
invokeCall
|
|
4
4
|
}) => {
|
|
5
5
|
void invokeCall({
|
|
6
6
|
action: 300
|
|
7
|
-
},
|
|
7
|
+
}, result => {
|
|
8
|
+
const value = result && 'retVal' in result ? result.retVal : result;
|
|
9
|
+
callback({
|
|
10
|
+
top: 0,
|
|
11
|
+
left: 0,
|
|
12
|
+
bottom: 0,
|
|
13
|
+
right: 0,
|
|
14
|
+
...value
|
|
15
|
+
});
|
|
16
|
+
});
|
|
8
17
|
};
|
|
@@ -3,6 +3,7 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
|
|
|
3
3
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
4
4
|
import throttle from 'lodash.throttle';
|
|
5
5
|
import getUserInfo from '../calls/getUserInfo';
|
|
6
|
+
import { addAppleSafeAreaListener as addNativeAppleSafeAreaListener } from '../calls/appleSafeAreaListener';
|
|
6
7
|
import { sendMessageToGroup, sendMessageToPage, sendMessageToUser } from '../calls/sendMessage';
|
|
7
8
|
import { DefaultLoginDialogOptions } from '../constants';
|
|
8
9
|
import { DefaultTrustedDomains } from '../constants/trustedDomains';
|
|
@@ -260,26 +261,19 @@ export class AppWrapper {
|
|
|
260
261
|
const {
|
|
261
262
|
id,
|
|
262
263
|
shouldInitialize
|
|
263
|
-
} = addApiListener('appleSafeAreaListener',
|
|
264
|
+
} = addApiListener('appleSafeAreaListener', value => {
|
|
265
|
+
this.latestAppleSafeArea = value;
|
|
266
|
+
callback(value);
|
|
267
|
+
});
|
|
264
268
|
if (this.latestAppleSafeArea) {
|
|
265
269
|
callback(this.latestAppleSafeArea);
|
|
266
270
|
}
|
|
267
271
|
if (shouldInitialize) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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);
|
|
272
|
+
addNativeAppleSafeAreaListener({
|
|
273
|
+
invokeCall: this.functions.invokeCall,
|
|
274
|
+
callback: value => {
|
|
275
|
+
this.latestAppleSafeArea = value;
|
|
276
|
+
dispatchApiEvent('appleSafeAreaListener', value);
|
|
283
277
|
}
|
|
284
278
|
});
|
|
285
279
|
}
|
|
@@ -329,24 +323,19 @@ export class AppWrapper {
|
|
|
329
323
|
topBarHeight: 0
|
|
330
324
|
}),
|
|
331
325
|
getAppleSafeArea: async () => {
|
|
332
|
-
var _v$top, _v$left, _v$bottom, _v$right;
|
|
333
326
|
if (this.latestAppleSafeArea) {
|
|
334
327
|
return this.latestAppleSafeArea;
|
|
335
328
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
hingeStatus: v.hingeStatus
|
|
347
|
-
})
|
|
348
|
-
};
|
|
349
|
-
return this.latestAppleSafeArea;
|
|
329
|
+
let resolveValue;
|
|
330
|
+
const result = new Promise(resolve => {
|
|
331
|
+
resolveValue = resolve;
|
|
332
|
+
});
|
|
333
|
+
const id = await this.functions.addAppleSafeAreaListener(resolveValue);
|
|
334
|
+
try {
|
|
335
|
+
return await result;
|
|
336
|
+
} finally {
|
|
337
|
+
await this.functions.removeAppleSafeAreaListener(id);
|
|
338
|
+
}
|
|
350
339
|
},
|
|
351
340
|
invokeCall: async (value, callback) => {
|
|
352
341
|
return this.appCall(value.action, value.value, {
|
|
@@ -359,7 +348,7 @@ export class AppWrapper {
|
|
|
359
348
|
});
|
|
360
349
|
},
|
|
361
350
|
invokeDialogCall: async (value, callback) => {
|
|
362
|
-
const callbackName =
|
|
351
|
+
const callbackName = this.getCallbackName();
|
|
363
352
|
window[callbackName] = ({
|
|
364
353
|
retVal
|
|
365
354
|
}) => {
|
|
@@ -489,8 +478,7 @@ export class AppWrapper {
|
|
|
489
478
|
}
|
|
490
479
|
},
|
|
491
480
|
removeAppleSafeAreaListener: async id => {
|
|
492
|
-
|
|
493
|
-
if (shouldRemove) {}
|
|
481
|
+
removeApiListener('appleSafeAreaListener', id);
|
|
494
482
|
},
|
|
495
483
|
selectPage: async options => {
|
|
496
484
|
var _this$values, _this$values2;
|
|
@@ -764,6 +752,13 @@ export class AppWrapper {
|
|
|
764
752
|
notImplemented(call) {
|
|
765
753
|
console.warn(`call ${call} not implement in app`);
|
|
766
754
|
}
|
|
755
|
+
getCallbackName() {
|
|
756
|
+
let callbackName;
|
|
757
|
+
do {
|
|
758
|
+
callbackName = `chaynsApiV5Callback_${this.counter++}`;
|
|
759
|
+
} while (callbackName in window);
|
|
760
|
+
return callbackName;
|
|
761
|
+
}
|
|
767
762
|
appCall(action, value = {}, {
|
|
768
763
|
callback,
|
|
769
764
|
awaitResult = true
|
|
@@ -776,7 +771,7 @@ export class AppWrapper {
|
|
|
776
771
|
return;
|
|
777
772
|
}
|
|
778
773
|
return new Promise(resolve => {
|
|
779
|
-
const callbackName =
|
|
774
|
+
const callbackName = this.getCallbackName();
|
|
780
775
|
window[callbackName] = v => {
|
|
781
776
|
var _v$retVal2;
|
|
782
777
|
if (callback) {
|
|
@@ -1,63 +1,64 @@
|
|
|
1
|
+
import { addAppleSafeAreaListener as addNativeAppleSafeAreaListener } from '../calls/appleSafeAreaListener';
|
|
2
|
+
import { addApiListener, dispatchApiEvent, removeApiListener } from '../utils/apiListener';
|
|
1
3
|
const createAppleSafeAreaFunctions = functions => {
|
|
2
|
-
var _runtimeFunctions$add, _runtimeFunctions$rem;
|
|
3
4
|
const runtimeFunctions = functions;
|
|
4
|
-
const baseAddListener = (_runtimeFunctions$add = runtimeFunctions.addAppleSafeAreaListener) !== null && _runtimeFunctions$add !== void 0 ? _runtimeFunctions$add : callback => {
|
|
5
|
-
if (functions.invokeCall) {
|
|
6
|
-
void functions.invokeCall({
|
|
7
|
-
action: 300
|
|
8
|
-
}, callback);
|
|
9
|
-
}
|
|
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
5
|
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
6
|
const addAppleSafeAreaListener = callback => {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
const handleValue = value => {
|
|
8
|
+
latestValue = value;
|
|
9
|
+
callback(value);
|
|
10
|
+
};
|
|
11
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
12
|
+
return runtimeFunctions.addAppleSafeAreaListener(handleValue);
|
|
13
|
+
}
|
|
14
|
+
const {
|
|
15
|
+
id,
|
|
16
|
+
shouldInitialize
|
|
17
|
+
} = addApiListener('appleSafeAreaListener', handleValue);
|
|
18
|
+
if (latestValue) {
|
|
27
19
|
callback(latestValue);
|
|
28
20
|
}
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
if (shouldInitialize && runtimeFunctions.invokeCall) {
|
|
22
|
+
addNativeAppleSafeAreaListener({
|
|
23
|
+
invokeCall: runtimeFunctions.invokeCall,
|
|
24
|
+
callback: value => {
|
|
25
|
+
latestValue = value;
|
|
26
|
+
dispatchApiEvent('appleSafeAreaListener', value);
|
|
27
|
+
}
|
|
32
28
|
});
|
|
33
29
|
}
|
|
34
30
|
return Promise.resolve(id);
|
|
35
31
|
};
|
|
36
32
|
const removeAppleSafeAreaListener = id => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
void
|
|
40
|
-
upstreamListenerId = null;
|
|
33
|
+
if (runtimeFunctions.addAppleSafeAreaListener) {
|
|
34
|
+
var _runtimeFunctions$rem, _runtimeFunctions$rem2;
|
|
35
|
+
return (_runtimeFunctions$rem = (_runtimeFunctions$rem2 = runtimeFunctions.removeAppleSafeAreaListener) === null || _runtimeFunctions$rem2 === void 0 ? void 0 : _runtimeFunctions$rem2.call(runtimeFunctions, id)) !== null && _runtimeFunctions$rem !== void 0 ? _runtimeFunctions$rem : Promise.resolve();
|
|
41
36
|
}
|
|
37
|
+
removeApiListener('appleSafeAreaListener', id);
|
|
42
38
|
return Promise.resolve();
|
|
43
39
|
};
|
|
44
|
-
const baseGetSafeArea = runtimeFunctions.getAppleSafeArea;
|
|
45
40
|
const getAppleSafeArea = async () => {
|
|
46
|
-
if (
|
|
41
|
+
if (latestValue) {
|
|
47
42
|
return latestValue;
|
|
48
43
|
}
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
if (!runtimeFunctions.addAppleSafeAreaListener && !runtimeFunctions.invokeCall) {
|
|
45
|
+
return {
|
|
46
|
+
top: 0,
|
|
47
|
+
left: 0,
|
|
48
|
+
bottom: 0,
|
|
49
|
+
right: 0
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
let resolveValue;
|
|
53
|
+
const result = new Promise(resolve => {
|
|
54
|
+
resolveValue = resolve;
|
|
55
|
+
});
|
|
56
|
+
const id = await addAppleSafeAreaListener(resolveValue);
|
|
57
|
+
try {
|
|
58
|
+
return await result;
|
|
59
|
+
} finally {
|
|
60
|
+
await removeAppleSafeAreaListener(id);
|
|
53
61
|
}
|
|
54
|
-
const fallback = {
|
|
55
|
-
top: 0,
|
|
56
|
-
left: 0,
|
|
57
|
-
bottom: 0,
|
|
58
|
-
right: 0
|
|
59
|
-
};
|
|
60
|
-
return fallback;
|
|
61
62
|
};
|
|
62
63
|
return {
|
|
63
64
|
addAppleSafeAreaListener,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { AppleSafeArea } from '../types/IChaynsReact';
|
|
1
|
+
import { AppleSafeArea, ChaynsReactFunctions } from '../types/IChaynsReact';
|
|
2
2
|
export interface AddAppleSafeAreaListenerOptions {
|
|
3
3
|
callback: (safeArea: AppleSafeArea) => void;
|
|
4
|
+
invokeCall: ChaynsReactFunctions['invokeCall'];
|
|
4
5
|
}
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Registers the native safe-area callback, preserving the complete result including optional
|
|
8
|
+
* iPhone Duo hinge metadata. Callers must use the API listener registry to share this callback.
|
|
9
9
|
* @internal
|
|
10
10
|
*/
|
|
11
|
-
export declare const addAppleSafeAreaListener: ({ callback }: AddAppleSafeAreaListenerOptions) => void;
|
|
11
|
+
export declare const addAppleSafeAreaListener: ({ callback, invokeCall }: AddAppleSafeAreaListenerOptions) => void;
|
|
@@ -14,6 +14,7 @@ export declare class AppWrapper implements IChaynsReact {
|
|
|
14
14
|
chaynsApiId: string;
|
|
15
15
|
notImplemented(call: string): void;
|
|
16
16
|
counter: number;
|
|
17
|
+
private getCallbackName;
|
|
17
18
|
appCall(action: any, value?: unknown, { callback, awaitResult }?: {
|
|
18
19
|
awaitResult?: boolean | undefined;
|
|
19
20
|
}): Promise<unknown> | undefined;
|
package/package.json
CHANGED
package/tests/safe-area.test.cjs
CHANGED
|
@@ -23,7 +23,8 @@ let listenerIds;
|
|
|
23
23
|
let root;
|
|
24
24
|
let currentValue;
|
|
25
25
|
|
|
26
|
-
const emit = (value
|
|
26
|
+
const emit = (value) => {
|
|
27
|
+
const call = nativeCalls.findLast(({ action }) => action === 300);
|
|
27
28
|
const callbackName = call.value.callback.replace(/^window\./, '');
|
|
28
29
|
window[callbackName]({ retVal: value });
|
|
29
30
|
};
|
|
@@ -71,10 +72,10 @@ test('exports the three native hinge status values through the public entry poin
|
|
|
71
72
|
|
|
72
73
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
73
74
|
test(`preserves ${hingeStatus} and a zero angle in a direct safe-area request`, async () => {
|
|
74
|
-
const expected = { ...insets, hingeAngle: 0, hingeStatus };
|
|
75
|
+
const expected = { ...insets, hingeAngle: 0, hingeStatus, additionalNativeValue: 42 };
|
|
75
76
|
const pending = wrapper.functions.getAppleSafeArea();
|
|
76
77
|
assert.equal(nativeCalls[0].action, 300);
|
|
77
|
-
emit(
|
|
78
|
+
emit(expected);
|
|
78
79
|
assert.deepEqual(await pending, expected);
|
|
79
80
|
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
80
81
|
assert.equal(nativeCalls.length, 1);
|
|
@@ -102,10 +103,9 @@ test('retains the existing zero-inset fallback for an absent native response', a
|
|
|
102
103
|
test('updates the listener cache and new subscribers when only hinge metadata changes', async () => {
|
|
103
104
|
const values = [];
|
|
104
105
|
await subscribe((value) => values.push(value));
|
|
105
|
-
const stream = nativeCalls[0];
|
|
106
106
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
107
107
|
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
108
|
-
emit(expected
|
|
108
|
+
emit(expected);
|
|
109
109
|
assert.deepEqual(values.at(-1), expected);
|
|
110
110
|
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), expected);
|
|
111
111
|
}
|
|
@@ -124,38 +124,69 @@ test('stops delivering native updates to removed listeners', async () => {
|
|
|
124
124
|
assert.deepEqual(values, []);
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
-
test('
|
|
127
|
+
test('shares one native callback between compatibility wrappers and keeps remaining listeners active', async () => {
|
|
128
128
|
let dispatch;
|
|
129
|
-
|
|
129
|
+
let calls = 0;
|
|
130
|
+
const host = {
|
|
130
131
|
invokeCall: (options, callback) => {
|
|
131
132
|
assert.equal(options.action, 300);
|
|
133
|
+
calls += 1;
|
|
132
134
|
dispatch = callback;
|
|
133
135
|
return Promise.resolve();
|
|
134
136
|
},
|
|
135
|
-
}
|
|
137
|
+
};
|
|
138
|
+
const first = normalizeFunctions(host);
|
|
139
|
+
const second = normalizeFunctions(host);
|
|
140
|
+
const firstValues = [];
|
|
141
|
+
const secondValues = [];
|
|
142
|
+
const firstId = await first.addAppleSafeAreaListener((value) => firstValues.push(value));
|
|
143
|
+
const secondId = await second.addAppleSafeAreaListener((value) => secondValues.push(value));
|
|
144
|
+
try {
|
|
145
|
+
assert.equal(calls, 1);
|
|
146
|
+
const expected = { ...insets, hingeAngle: 90, hingeStatus: HingeStatus.PartiallyOpen };
|
|
147
|
+
dispatch(expected);
|
|
148
|
+
assert.deepEqual(firstValues, [expected]);
|
|
149
|
+
assert.deepEqual(secondValues, [expected]);
|
|
150
|
+
assert.deepEqual(await first.getAppleSafeArea(), expected);
|
|
151
|
+
assert.deepEqual(await second.getAppleSafeArea(), expected);
|
|
152
|
+
await first.removeAppleSafeAreaListener(firstId);
|
|
153
|
+
const opened = { ...expected, hingeAngle: 180, hingeStatus: HingeStatus.FullyOpen };
|
|
154
|
+
dispatch(opened);
|
|
155
|
+
assert.deepEqual(firstValues, [expected]);
|
|
156
|
+
assert.deepEqual(secondValues, [expected, opened]);
|
|
157
|
+
} finally {
|
|
158
|
+
await first.removeAppleSafeAreaListener(firstId);
|
|
159
|
+
await second.removeAppleSafeAreaListener(secondId);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('shares the active native callback with concurrent getters and preserves subsequent hinge updates', async () => {
|
|
136
164
|
const values = [];
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
assert.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
165
|
+
await subscribe((value) => values.push(value));
|
|
166
|
+
const first = wrapper.functions.getAppleSafeArea();
|
|
167
|
+
const second = wrapper.functions.getAppleSafeArea();
|
|
168
|
+
assert.equal(nativeCalls.length, 1);
|
|
169
|
+
const closed = { ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed };
|
|
170
|
+
emit(closed); // Only the most recently registered native callback is called.
|
|
171
|
+
assert.deepEqual(await Promise.all([first, second]), [closed, closed]);
|
|
172
|
+
const opened = { ...insets, hingeAngle: 180, hingeStatus: HingeStatus.FullyOpen };
|
|
173
|
+
emit(opened);
|
|
174
|
+
assert.deepEqual(values, [closed, opened]);
|
|
175
|
+
assert.deepEqual(await wrapper.functions.getAppleSafeArea(), opened);
|
|
176
|
+
assert.equal(nativeCalls.length, 1);
|
|
145
177
|
});
|
|
146
178
|
|
|
147
179
|
test('updates the actual hook through native folding events with identical insets', async () => {
|
|
148
180
|
await renderHook();
|
|
149
|
-
const stream = nativeCalls[0];
|
|
150
181
|
for (const hingeStatus of Object.values(HingeStatus)) {
|
|
151
182
|
const expected = { ...insets, hingeAngle: 90, hingeStatus };
|
|
152
|
-
await act(async () => emit(expected
|
|
183
|
+
await act(async () => emit(expected));
|
|
153
184
|
assert.deepEqual(currentValue, expected);
|
|
154
185
|
}
|
|
155
186
|
assert.equal(nativeCalls.length, 1);
|
|
156
187
|
const beforeCleanup = currentValue;
|
|
157
188
|
await renderHook(false);
|
|
158
|
-
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed }
|
|
189
|
+
emit({ ...insets, hingeAngle: 0, hingeStatus: HingeStatus.Closed });
|
|
159
190
|
assert.equal(currentValue, beforeCleanup);
|
|
160
191
|
});
|
|
161
192
|
|
|
@@ -174,3 +205,29 @@ test('does not register native listeners on unsupported runtimes', async () => {
|
|
|
174
205
|
assert.equal(nativeCalls.length, 0);
|
|
175
206
|
assert.deepEqual(currentValue, { top: 0, left: 0, bottom: 0, right: 0 });
|
|
176
207
|
});
|
|
208
|
+
|
|
209
|
+
for (const useDialogCall of [false, true]) {
|
|
210
|
+
test(`keeps the safe-area callback intact when another wrapper registers a ${useDialogCall ? 'dialog' : 'regular'} call`, async () => {
|
|
211
|
+
const values = [];
|
|
212
|
+
await subscribe((value) => values.push(value));
|
|
213
|
+
const safeAreaCallback = nativeCalls.at(-1).value.callback.replace(/^window\./, '');
|
|
214
|
+
const other = new AppWrapper();
|
|
215
|
+
let dialogResult;
|
|
216
|
+
const pending = useDialogCall
|
|
217
|
+
? other.functions.invokeDialogCall({ action: 999, value: {} }, (value) => { dialogResult = value; })
|
|
218
|
+
: other.functions.invokeCall({ action: 999, value: {} });
|
|
219
|
+
const otherCallback = nativeCalls.at(-1).value.callback.replace(/^window\./, '');
|
|
220
|
+
assert.notEqual(otherCallback, safeAreaCallback);
|
|
221
|
+
window[otherCallback]({ retVal: { success: true } });
|
|
222
|
+
const callResult = await pending;
|
|
223
|
+
assert.deepEqual(useDialogCall ? dialogResult : callResult, { success: true });
|
|
224
|
+
const updates = Array.from({ length: 181 }, (_, hingeAngle) => {
|
|
225
|
+
let hingeStatus = HingeStatus.PartiallyOpen;
|
|
226
|
+
if (hingeAngle === 0) hingeStatus = HingeStatus.Closed;
|
|
227
|
+
if (hingeAngle === 180) hingeStatus = HingeStatus.FullyOpen;
|
|
228
|
+
return { ...insets, hingeAngle, hingeStatus };
|
|
229
|
+
});
|
|
230
|
+
updates.forEach(emit);
|
|
231
|
+
assert.deepEqual(values, updates);
|
|
232
|
+
});
|
|
233
|
+
}
|