chayns-api 3.1.0-beta.0 → 3.1.0-beta.2
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.
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.BlockRegistry = void 0;
|
|
7
|
+
var _calls = require("../../calls");
|
|
7
8
|
const BLOCK_TIMEOUT_MS = 30000;
|
|
8
9
|
let _nextId = 1;
|
|
9
10
|
class BlockRegistry {
|
|
@@ -13,6 +14,7 @@ class BlockRegistry {
|
|
|
13
14
|
e.preventDefault();
|
|
14
15
|
e.returnValue = '';
|
|
15
16
|
};
|
|
17
|
+
totalBlockCount = 0;
|
|
16
18
|
add(layer, callback, opts = {}) {
|
|
17
19
|
var _opts$scope, _opts$isBeforeUnload;
|
|
18
20
|
const entry = {
|
|
@@ -32,22 +34,25 @@ class BlockRegistry {
|
|
|
32
34
|
if (entry.opts.isBeforeUnload) {
|
|
33
35
|
this.incrementBeforeUnload();
|
|
34
36
|
}
|
|
37
|
+
this.incrementTotalBlocks();
|
|
35
38
|
return () => this.remove(layer.id, entry);
|
|
36
39
|
}
|
|
37
40
|
remove(layerId, entry) {
|
|
38
41
|
const set = this.layerBlocks.get(layerId);
|
|
39
42
|
if (!set) return;
|
|
40
|
-
set.delete(entry);
|
|
43
|
+
if (!set.delete(entry)) return;
|
|
41
44
|
if (set.size === 0) this.layerBlocks.delete(layerId);
|
|
42
45
|
if (entry.opts.isBeforeUnload) {
|
|
43
46
|
this.decrementBeforeUnload();
|
|
44
47
|
}
|
|
48
|
+
this.decrementTotalBlocks();
|
|
45
49
|
}
|
|
46
50
|
removeAllForLayer(layerId) {
|
|
47
51
|
const set = this.layerBlocks.get(layerId);
|
|
48
52
|
if (!set) return;
|
|
49
53
|
for (const entry of set) {
|
|
50
54
|
if (entry.opts.isBeforeUnload) this.decrementBeforeUnload();
|
|
55
|
+
this.decrementTotalBlocks();
|
|
51
56
|
}
|
|
52
57
|
this.layerBlocks.delete(layerId);
|
|
53
58
|
}
|
|
@@ -106,5 +111,29 @@ class BlockRegistry {
|
|
|
106
111
|
window.removeEventListener('beforeunload', this.beforeUnloadHandler);
|
|
107
112
|
}
|
|
108
113
|
}
|
|
114
|
+
incrementTotalBlocks() {
|
|
115
|
+
this.totalBlockCount++;
|
|
116
|
+
if (this.totalBlockCount > 0) {
|
|
117
|
+
this.setNativeNavigationEnabled(true);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
decrementTotalBlocks() {
|
|
121
|
+
if (this.totalBlockCount === 0) return;
|
|
122
|
+
this.totalBlockCount--;
|
|
123
|
+
if (this.totalBlockCount === 0) {
|
|
124
|
+
this.setNativeNavigationEnabled(false);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
setNativeNavigationEnabled(enabled) {
|
|
128
|
+
try {
|
|
129
|
+
void (0, _calls.invokeCall)({
|
|
130
|
+
action: 249,
|
|
131
|
+
value: {
|
|
132
|
+
enabled
|
|
133
|
+
}
|
|
134
|
+
}, this.nativeNavigationCallback);
|
|
135
|
+
} catch {}
|
|
136
|
+
}
|
|
137
|
+
nativeNavigationCallback = () => {};
|
|
109
138
|
}
|
|
110
139
|
exports.BlockRegistry = BlockRegistry;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
2
2
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
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
|
+
import { invokeCall } from '../../calls';
|
|
4
5
|
const BLOCK_TIMEOUT_MS = 30000;
|
|
5
6
|
let _nextId = 1;
|
|
6
7
|
export class BlockRegistry {
|
|
@@ -11,6 +12,8 @@ export class BlockRegistry {
|
|
|
11
12
|
e.preventDefault();
|
|
12
13
|
e.returnValue = '';
|
|
13
14
|
});
|
|
15
|
+
_defineProperty(this, "totalBlockCount", 0);
|
|
16
|
+
_defineProperty(this, "nativeNavigationCallback", () => {});
|
|
14
17
|
}
|
|
15
18
|
add(layer, callback, opts = {}) {
|
|
16
19
|
var _opts$scope, _opts$isBeforeUnload;
|
|
@@ -31,22 +34,25 @@ export class BlockRegistry {
|
|
|
31
34
|
if (entry.opts.isBeforeUnload) {
|
|
32
35
|
this.incrementBeforeUnload();
|
|
33
36
|
}
|
|
37
|
+
this.incrementTotalBlocks();
|
|
34
38
|
return () => this.remove(layer.id, entry);
|
|
35
39
|
}
|
|
36
40
|
remove(layerId, entry) {
|
|
37
41
|
const set = this.layerBlocks.get(layerId);
|
|
38
42
|
if (!set) return;
|
|
39
|
-
set.delete(entry);
|
|
43
|
+
if (!set.delete(entry)) return;
|
|
40
44
|
if (set.size === 0) this.layerBlocks.delete(layerId);
|
|
41
45
|
if (entry.opts.isBeforeUnload) {
|
|
42
46
|
this.decrementBeforeUnload();
|
|
43
47
|
}
|
|
48
|
+
this.decrementTotalBlocks();
|
|
44
49
|
}
|
|
45
50
|
removeAllForLayer(layerId) {
|
|
46
51
|
const set = this.layerBlocks.get(layerId);
|
|
47
52
|
if (!set) return;
|
|
48
53
|
for (const entry of set) {
|
|
49
54
|
if (entry.opts.isBeforeUnload) this.decrementBeforeUnload();
|
|
55
|
+
this.decrementTotalBlocks();
|
|
50
56
|
}
|
|
51
57
|
this.layerBlocks.delete(layerId);
|
|
52
58
|
}
|
|
@@ -105,4 +111,27 @@ export class BlockRegistry {
|
|
|
105
111
|
window.removeEventListener('beforeunload', this.beforeUnloadHandler);
|
|
106
112
|
}
|
|
107
113
|
}
|
|
114
|
+
incrementTotalBlocks() {
|
|
115
|
+
this.totalBlockCount++;
|
|
116
|
+
if (this.totalBlockCount > 0) {
|
|
117
|
+
this.setNativeNavigationEnabled(true);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
decrementTotalBlocks() {
|
|
121
|
+
if (this.totalBlockCount === 0) return;
|
|
122
|
+
this.totalBlockCount--;
|
|
123
|
+
if (this.totalBlockCount === 0) {
|
|
124
|
+
this.setNativeNavigationEnabled(false);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
setNativeNavigationEnabled(enabled) {
|
|
128
|
+
try {
|
|
129
|
+
void invokeCall({
|
|
130
|
+
action: 249,
|
|
131
|
+
value: {
|
|
132
|
+
enabled
|
|
133
|
+
}
|
|
134
|
+
}, this.nativeNavigationCallback);
|
|
135
|
+
} catch {}
|
|
136
|
+
}
|
|
108
137
|
}
|
|
@@ -11,6 +11,9 @@ export declare class BlockRegistry {
|
|
|
11
11
|
/** Number of blocks with `isBeforeUnload: true`. When > 0, listener is attached. */
|
|
12
12
|
private beforeUnloadCount;
|
|
13
13
|
private readonly beforeUnloadHandler;
|
|
14
|
+
/** Total number of currently registered blocks across all layers.
|
|
15
|
+
* Used to toggle the native navigation handling (chayns app, action 249). */
|
|
16
|
+
private totalBlockCount;
|
|
14
17
|
add(layer: ChaynsHistoryLayer, callback: () => Promise<boolean>, opts?: ChaynsHistoryBlockOptions): () => void;
|
|
15
18
|
remove(layerId: string, entry: BlockEntry): void;
|
|
16
19
|
/** Remove all blocks registered for a layer (called on destroy). */
|
|
@@ -34,5 +37,29 @@ export declare class BlockRegistry {
|
|
|
34
37
|
private runBlock;
|
|
35
38
|
private incrementBeforeUnload;
|
|
36
39
|
private decrementBeforeUnload;
|
|
40
|
+
private incrementTotalBlocks;
|
|
41
|
+
private decrementTotalBlocks;
|
|
42
|
+
/**
|
|
43
|
+
* Toggles the host app's native navigation handling so that a registered
|
|
44
|
+
* block actually prevents the user from navigating natively (e.g. via the
|
|
45
|
+
* Android back button or the swipe gesture in the chayns app).
|
|
46
|
+
*
|
|
47
|
+
* Maps to chayns invokeCall action `249`:
|
|
48
|
+
* ```
|
|
49
|
+
* chayns.invokeCall({ action: 249, value: { enabled, callback } });
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* `enabled: true` here means: "a block is active, take over navigation" —
|
|
53
|
+
* the host disables its own native back handling and instead forwards the
|
|
54
|
+
* event to us via the provided callback, where we can run our block logic.
|
|
55
|
+
*/
|
|
56
|
+
private setNativeNavigationEnabled;
|
|
57
|
+
/**
|
|
58
|
+
* Invoked by the host app when the user attempts a native navigation
|
|
59
|
+
* gesture while blocks are active. We run the block callbacks against the
|
|
60
|
+
* current root layer; if any of them denies, navigation stays cancelled.
|
|
61
|
+
* When all blocks allow the navigation we trigger a normal back.
|
|
62
|
+
*/
|
|
63
|
+
private readonly nativeNavigationCallback;
|
|
37
64
|
}
|
|
38
65
|
export {};
|