navigation-stack 0.3.1 → 0.5.0
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/CHANGELOG.md +12 -0
- package/README.md +611 -163
- package/data-storage/package.json +6 -0
- package/karma.conf.cjs +21 -4
- package/lib/cjs/NavigationStack.js +88 -0
- package/lib/cjs/data-storage/DataStorage.js +71 -0
- package/lib/cjs/data-storage/LocationDataStorage.js +29 -0
- package/lib/cjs/data-storage/index.js +9 -0
- package/lib/cjs/debug.js +12 -0
- package/lib/cjs/environment/InMemoryEnvironment.js +15 -0
- package/lib/cjs/environment/WebBrowserEnvironment.js +15 -0
- package/lib/cjs/environment/data-storage/InMemoryDataStorage.js +27 -0
- package/lib/cjs/environment/data-storage/WebBrowserDataStorage.js +21 -0
- package/lib/cjs/environment/scroll-position/InMemoryScrollPosition.js +44 -0
- package/lib/cjs/environment/scroll-position/WebBrowserScrollPosition.js +60 -0
- package/lib/cjs/getLocationFromInternalLocation.js +14 -0
- package/lib/cjs/index.js +20 -16
- package/lib/cjs/navigationBlockers.js +28 -23
- package/lib/cjs/{normalizeInputLocation.js → parseInputLocation.js} +25 -9
- package/lib/cjs/{ActionTypes.js → redux/ActionTypes.js} +1 -1
- package/lib/cjs/redux/ActionTypesInternal.js +8 -0
- package/lib/cjs/{Actions.js → redux/Actions.js} +5 -4
- package/lib/cjs/redux/createMiddlewares.js +60 -0
- package/lib/cjs/redux/index.js +13 -0
- package/lib/cjs/redux/internalLocationReducer.js +14 -0
- package/lib/cjs/redux/middleware/createAddInputLocationBasePathMiddleware.js +32 -0
- package/lib/cjs/redux/middleware/createNonProgrammaticNavigationBlockerMiddleware.js +113 -0
- package/lib/cjs/redux/middleware/createProgrammaticNavigationBlockerMiddleware.js +94 -0
- package/lib/cjs/redux/middleware/createRemoveOutputLocationBasePathMiddleware.js +30 -0
- package/lib/cjs/redux/middleware/createUpdateInternalLocationMiddleware.js +73 -0
- package/lib/cjs/{middleware/navigationActionMiddleware.js → redux/middleware/navigationOperationMiddleware.js} +11 -8
- package/lib/cjs/{middleware/normalizeInputLocationMiddleware.js → redux/middleware/parseInputLocationMiddleware.js} +6 -4
- package/lib/cjs/redux/middleware/updateLocationMiddleware.js +34 -0
- package/lib/cjs/scroll-position/PageScrollPositionSetter.js +97 -0
- package/lib/cjs/scroll-position/ScrollPositionAutoSaver.js +141 -0
- package/lib/cjs/scroll-position/ScrollPositionRestoration.js +407 -0
- package/lib/cjs/scroll-position/ScrollPositionSaver.js +87 -0
- package/lib/cjs/scroll-position/ScrollPositionSetter.js +16 -0
- package/lib/cjs/scroll-position/constants.js +5 -0
- package/lib/cjs/scroll-position/index.js +7 -0
- package/lib/cjs/scroll-position/scheduleNextTick.js +11 -0
- package/lib/cjs/session/InMemorySession.js +22 -0
- package/lib/cjs/session/ServerSideRenderSession.js +17 -0
- package/lib/cjs/session/Session.js +202 -0
- package/lib/cjs/session/WebBrowserSession.js +20 -0
- package/lib/cjs/session/key/createSessionKey.js +23 -0
- package/lib/cjs/session/lifecycle/InMemorySessionLifecycle.js +19 -0
- package/lib/cjs/session/lifecycle/WebBrowserSessionLifecycle.js +128 -0
- package/lib/cjs/session/lifecycle/page-lifecycle/PageLifecycle.js +269 -0
- package/lib/cjs/session/lifecycle/page-lifecycle/PageLifecycleInstance.js +8 -0
- package/lib/cjs/session/lifecycle/page-lifecycle/supportsConstructableEventTarget.js +33 -0
- package/lib/cjs/session/navigation/InMemoryNavigation.js +104 -0
- package/lib/cjs/session/navigation/ServerSideNavigation.js +61 -0
- package/lib/cjs/session/navigation/WebBrowserNavigation.js +221 -0
- package/lib/cjs/session/navigation/error/NavigationOutOfBoundsError.js +12 -0
- package/lib/cjs/session/navigation/error/ServerSideNavigationError.js +21 -0
- package/lib/cjs/session/navigation/operation/operations.js +11 -0
- package/lib/cjs/session/subscription/Subscription.js +81 -0
- package/lib/data-storage/index.d.ts +35 -0
- package/lib/esm/NavigationStack.js +81 -0
- package/lib/esm/data-storage/DataStorage.js +65 -0
- package/lib/esm/data-storage/LocationDataStorage.js +22 -0
- package/lib/esm/data-storage/index.js +2 -0
- package/lib/esm/debug.js +7 -0
- package/lib/esm/environment/InMemoryEnvironment.js +8 -0
- package/lib/esm/environment/WebBrowserEnvironment.js +8 -0
- package/lib/esm/environment/data-storage/InMemoryDataStorage.js +21 -0
- package/lib/esm/environment/data-storage/WebBrowserDataStorage.js +15 -0
- package/lib/esm/environment/scroll-position/InMemoryScrollPosition.js +38 -0
- package/lib/esm/environment/scroll-position/WebBrowserScrollPosition.js +54 -0
- package/lib/esm/getLocationFromInternalLocation.js +9 -0
- package/lib/esm/index.js +10 -8
- package/lib/esm/navigationBlockers.js +28 -23
- package/lib/esm/{normalizeInputLocation.js → parseInputLocation.js} +24 -8
- package/lib/esm/{ActionTypes.js → redux/ActionTypes.js} +1 -1
- package/lib/esm/redux/ActionTypesInternal.js +3 -0
- package/lib/esm/{Actions.js → redux/Actions.js} +5 -4
- package/lib/esm/redux/createMiddlewares.js +54 -0
- package/lib/esm/redux/index.js +4 -0
- package/lib/esm/redux/internalLocationReducer.js +8 -0
- package/lib/esm/redux/middleware/createAddInputLocationBasePathMiddleware.js +27 -0
- package/lib/esm/redux/middleware/createNonProgrammaticNavigationBlockerMiddleware.js +108 -0
- package/lib/esm/redux/middleware/createProgrammaticNavigationBlockerMiddleware.js +88 -0
- package/lib/esm/redux/middleware/createRemoveOutputLocationBasePathMiddleware.js +25 -0
- package/lib/esm/redux/middleware/createUpdateInternalLocationMiddleware.js +68 -0
- package/lib/esm/{middleware/navigationActionMiddleware.js → redux/middleware/navigationOperationMiddleware.js} +10 -7
- package/lib/esm/{middleware/normalizeInputLocationMiddleware.js → redux/middleware/parseInputLocationMiddleware.js} +5 -3
- package/lib/esm/redux/middleware/updateLocationMiddleware.js +28 -0
- package/lib/esm/scroll-position/PageScrollPositionSetter.js +91 -0
- package/lib/esm/scroll-position/ScrollPositionAutoSaver.js +134 -0
- package/lib/esm/scroll-position/ScrollPositionRestoration.js +400 -0
- package/lib/esm/scroll-position/ScrollPositionSaver.js +80 -0
- package/lib/esm/scroll-position/ScrollPositionSetter.js +10 -0
- package/lib/esm/scroll-position/constants.js +1 -0
- package/lib/esm/scroll-position/index.js +1 -0
- package/lib/esm/scroll-position/scheduleNextTick.js +6 -0
- package/lib/esm/session/InMemorySession.js +15 -0
- package/lib/esm/session/ServerSideRenderSession.js +11 -0
- package/lib/esm/session/Session.js +195 -0
- package/lib/esm/session/WebBrowserSession.js +13 -0
- package/lib/esm/session/key/createSessionKey.js +18 -0
- package/lib/esm/session/lifecycle/InMemorySessionLifecycle.js +13 -0
- package/lib/esm/session/lifecycle/WebBrowserSessionLifecycle.js +120 -0
- package/lib/esm/session/lifecycle/page-lifecycle/PageLifecycle.js +263 -0
- package/lib/esm/session/lifecycle/page-lifecycle/PageLifecycleInstance.js +2 -0
- package/lib/esm/session/lifecycle/page-lifecycle/supportsConstructableEventTarget.js +30 -0
- package/lib/esm/session/navigation/InMemoryNavigation.js +97 -0
- package/lib/esm/session/navigation/ServerSideNavigation.js +54 -0
- package/lib/esm/session/navigation/WebBrowserNavigation.js +213 -0
- package/lib/esm/session/navigation/error/NavigationOutOfBoundsError.js +6 -0
- package/lib/esm/session/navigation/error/ServerSideNavigationError.js +14 -0
- package/lib/esm/session/navigation/operation/operations.js +6 -0
- package/lib/esm/session/subscription/Subscription.js +75 -0
- package/lib/index.d.ts +179 -157
- package/lib/redux/index.d.ts +90 -0
- package/lib/scroll-position/index.d.ts +107 -0
- package/package.json +9 -5
- package/redux/package.json +6 -0
- package/scroll-position/package.json +6 -0
- package/src/NavigationStack.js +100 -0
- package/src/data-storage/DataStorage.js +69 -0
- package/src/data-storage/LocationDataStorage.js +23 -0
- package/src/data-storage/index.js +2 -0
- package/src/debug.js +8 -0
- package/src/environment/InMemoryEnvironment.js +9 -0
- package/src/environment/WebBrowserEnvironment.js +9 -0
- package/src/environment/data-storage/InMemoryDataStorage.js +23 -0
- package/src/environment/data-storage/WebBrowserDataStorage.js +17 -0
- package/src/environment/scroll-position/InMemoryScrollPosition.js +45 -0
- package/src/environment/scroll-position/WebBrowserScrollPosition.js +72 -0
- package/src/getLocationFromInternalLocation.js +7 -0
- package/src/index.js +10 -8
- package/src/navigationBlockers.js +31 -27
- package/src/{normalizeInputLocation.js → parseInputLocation.js} +23 -8
- package/src/{ActionTypes.js → redux/ActionTypes.js} +1 -1
- package/src/redux/ActionTypesInternal.js +3 -0
- package/src/{Actions.js → redux/Actions.js} +4 -3
- package/src/redux/createMiddlewares.js +65 -0
- package/src/redux/index.js +4 -0
- package/src/redux/internalLocationReducer.js +9 -0
- package/src/redux/middleware/createAddInputLocationBasePathMiddleware.js +27 -0
- package/src/redux/middleware/createNonProgrammaticNavigationBlockerMiddleware.js +119 -0
- package/src/redux/middleware/createProgrammaticNavigationBlockerMiddleware.js +94 -0
- package/src/redux/middleware/createRemoveOutputLocationBasePathMiddleware.js +26 -0
- package/src/redux/middleware/createUpdateInternalLocationMiddleware.js +72 -0
- package/src/{middleware/navigationActionMiddleware.js → redux/middleware/navigationOperationMiddleware.js} +10 -3
- package/src/{middleware/normalizeInputLocationMiddleware.js → redux/middleware/parseInputLocationMiddleware.js} +5 -3
- package/src/redux/middleware/updateLocationMiddleware.js +28 -0
- package/src/scroll-position/PageScrollPositionSetter.js +110 -0
- package/src/scroll-position/ScrollPositionAutoSaver.js +168 -0
- package/src/scroll-position/ScrollPositionRestoration.js +551 -0
- package/src/scroll-position/ScrollPositionSaver.js +120 -0
- package/src/scroll-position/ScrollPositionSetter.js +16 -0
- package/src/scroll-position/constants.js +1 -0
- package/src/scroll-position/index.js +1 -0
- package/src/scroll-position/scheduleNextTick.js +6 -0
- package/src/session/InMemorySession.js +13 -0
- package/src/session/ServerSideRenderSession.js +9 -0
- package/src/session/Session.js +238 -0
- package/src/session/WebBrowserSession.js +13 -0
- package/src/session/key/createSessionKey.js +18 -0
- package/src/session/lifecycle/InMemorySessionLifecycle.js +13 -0
- package/src/session/lifecycle/WebBrowserSessionLifecycle.js +126 -0
- package/src/session/lifecycle/page-lifecycle/PageLifecycle.js +291 -0
- package/src/session/lifecycle/page-lifecycle/PageLifecycleInstance.js +3 -0
- package/src/session/lifecycle/page-lifecycle/supportsConstructableEventTarget.js +32 -0
- package/src/session/navigation/InMemoryNavigation.js +78 -0
- package/src/session/navigation/ServerSideNavigation.js +43 -0
- package/src/session/navigation/WebBrowserNavigation.js +224 -0
- package/src/session/navigation/error/NavigationOutOfBoundsError.js +7 -0
- package/src/session/navigation/error/ServerSideNavigationError.js +18 -0
- package/src/session/navigation/operation/operations.js +6 -0
- package/src/session/subscription/Subscription.js +76 -0
- package/test/NavigationStack.test.js +296 -0
- package/test/{LocationDataStorage.test.js → data-storage/LocationDataStorage.test.js} +3 -3
- package/test/data-storage/index.test.js +8 -0
- package/test/index.js +12 -0
- package/test/index.test.js +8 -7
- package/test/{helpers.js → middlewareTestUtil.js} +9 -12
- package/test/{normalizeInputLocation.test.js → parseInputLocationMiddleware.test.js} +9 -9
- package/test/{Action.test.js → redux/Action.test.js} +7 -6
- package/test/{ActionTypes.test.js → redux/ActionTypes.test.js} +2 -2
- package/test/redux/createMiddlewares.test.js +96 -0
- package/test/redux/index.test.js +10 -0
- package/test/{locationReducer.test.js → redux/locationReducer.test.js} +4 -7
- package/test/redux/middleware/createAddInputLocationBasePathMiddleware.test.js +40 -0
- package/test/redux/middleware/createNonProgrammaticNavigationBlockerMiddleware.test.js +264 -0
- package/test/redux/middleware/createProgrammaticNavigationBlockerMiddleware.test.js +312 -0
- package/test/redux/middleware/createRemoveOutputLocationBasePathMiddleware.test.js +51 -0
- package/test/{middleware/navigationActionMiddleware.test.js → redux/middleware/navigationOperationMiddleware.test.js} +16 -12
- package/test/{middleware/normalizeInputLocationMiddleware.test.js → redux/middleware/parseInputLocationMiddleware.test.js} +4 -4
- package/test/scroll-position/ScrollPositionRestoration.test.js +435 -0
- package/test/scroll-position/addScrollableContainer.js +39 -0
- package/test/scroll-position/addScrollableContainerWithAnchors.js +56 -0
- package/test/scroll-position/createApp.js +132 -0
- package/test/scroll-position/delay.js +9 -0
- package/test/scroll-position/mockPageLifecycle.js +17 -0
- package/test/scroll-position/runApp.js +24 -0
- package/test/scroll-position/withScrollableContainerAtIndexPageWithDisabledAutomaticScrollPositionRestoration.js +72 -0
- package/test/session/InMemorySession.test.js +348 -0
- package/test/session/ServerSession.test.js +17 -9
- package/test/session/WebBrowserSession.test.js +265 -0
- package/test/testUtil.js +3 -0
- package/types/data-storage/index.d.ts +35 -0
- package/types/index.d.ts +179 -157
- package/types/redux/index.d.ts +90 -0
- package/types/scroll-position/index.d.ts +107 -0
- package/types/tsconfig.json +1 -1
- package/lib/cjs/LocationDataStorage.js +0 -61
- package/lib/cjs/addBeforeLocationChangeListener.js +0 -7
- package/lib/cjs/beforeLocationChangeListeners.js +0 -51
- package/lib/cjs/createMiddlewares.js +0 -47
- package/lib/cjs/middleware/createBasePathMiddleware.js +0 -24
- package/lib/cjs/middleware/createBeforeLocationChangeListenerMiddleware.js +0 -39
- package/lib/cjs/middleware/createLocationMiddleware.js +0 -56
- package/lib/cjs/middleware/createNavigationBlockerMiddleware.js +0 -161
- package/lib/cjs/middleware/createTransformLocationMiddleware.js +0 -38
- package/lib/cjs/onlyAllowedOnClientSide.js +0 -10
- package/lib/cjs/session/BrowserSession.js +0 -235
- package/lib/cjs/session/MemorySession.js +0 -223
- package/lib/cjs/session/ServerSession.js +0 -65
- package/lib/esm/LocationDataStorage.js +0 -54
- package/lib/esm/addBeforeLocationChangeListener.js +0 -2
- package/lib/esm/beforeLocationChangeListeners.js +0 -44
- package/lib/esm/createMiddlewares.js +0 -41
- package/lib/esm/middleware/createBasePathMiddleware.js +0 -19
- package/lib/esm/middleware/createBeforeLocationChangeListenerMiddleware.js +0 -34
- package/lib/esm/middleware/createLocationMiddleware.js +0 -50
- package/lib/esm/middleware/createNavigationBlockerMiddleware.js +0 -156
- package/lib/esm/middleware/createTransformLocationMiddleware.js +0 -33
- package/lib/esm/onlyAllowedOnClientSide.js +0 -5
- package/lib/esm/session/BrowserSession.js +0 -229
- package/lib/esm/session/MemorySession.js +0 -217
- package/lib/esm/session/ServerSession.js +0 -58
- package/src/LocationDataStorage.js +0 -60
- package/src/addBeforeLocationChangeListener.js +0 -2
- package/src/beforeLocationChangeListeners.js +0 -54
- package/src/createMiddlewares.js +0 -45
- package/src/middleware/createBasePathMiddleware.js +0 -20
- package/src/middleware/createBeforeLocationChangeListenerMiddleware.js +0 -40
- package/src/middleware/createLocationMiddleware.js +0 -55
- package/src/middleware/createNavigationBlockerMiddleware.js +0 -168
- package/src/middleware/createTransformLocationMiddleware.js +0 -29
- package/src/onlyAllowedOnClientSide.js +0 -5
- package/src/session/BrowserSession.js +0 -235
- package/src/session/MemorySession.js +0 -219
- package/src/session/ServerSession.js +0 -67
- package/test/createMiddlewares.test.js +0 -62
- package/test/middleware/createBasePathMiddleware.test.js +0 -67
- package/test/middleware/createBeforeLocationChangeListenerMiddleware.test.js +0 -141
- package/test/middleware/createNavigationBlockerMiddleware.test.js +0 -471
- package/test/middleware/createTransformLocationMiddleware.test.js +0 -44
- package/test/session/BrowserSession.test.js +0 -182
- package/test/session/MemorySession.test.js +0 -244
- /package/lib/cjs/{locationReducer.js → redux/locationReducer.js} +0 -0
- /package/lib/esm/{locationReducer.js → redux/locationReducer.js} +0 -0
- /package/src/{locationReducer.js → redux/locationReducer.js} +0 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
class NavigationOutOfBoundsError extends Error {
|
|
6
|
+
constructor(index) {
|
|
7
|
+
super(`Location index ${index} is out of navigation history bounds`);
|
|
8
|
+
this.index = index;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.default = NavigationOutOfBoundsError;
|
|
12
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _getLocationUrl = _interopRequireDefault(require("../../../getLocationUrl"));
|
|
6
|
+
const _excluded = ["operation"];
|
|
7
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
9
|
+
class ServerSideNavigationError extends Error {
|
|
10
|
+
constructor(location) {
|
|
11
|
+
super(location ? `Navigate to ${(0, _getLocationUrl.default)(location)}` : 'Navigate to previous or next location');
|
|
12
|
+
if (location) {
|
|
13
|
+
// Remove `operation` property from `location`.
|
|
14
|
+
// eslint-disable-next-line no-unused-vars
|
|
15
|
+
const locationBase = _objectWithoutPropertiesLoose(location, _excluded);
|
|
16
|
+
this.location = locationBase;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.default = ServerSideNavigationError;
|
|
21
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
class Subscription {
|
|
6
|
+
constructor({
|
|
7
|
+
activateSubscription
|
|
8
|
+
} = {}) {
|
|
9
|
+
this.notifySubscribers = argument => {
|
|
10
|
+
// `._latest` is only used in tests.
|
|
11
|
+
this._latest = argument;
|
|
12
|
+
for (const {
|
|
13
|
+
listener
|
|
14
|
+
} of this._listeners) {
|
|
15
|
+
listener(argument);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
this._activateSubscription = activateSubscription;
|
|
19
|
+
|
|
20
|
+
// This property is accessed in tests.
|
|
21
|
+
this._listeners = [];
|
|
22
|
+
}
|
|
23
|
+
subscribe(listener) {
|
|
24
|
+
// If subscriptions are stopped, i.e. no new subscriptions are to be added,
|
|
25
|
+
// then don't add any listeners and return a "do nothing" function.
|
|
26
|
+
if (this._stopped) {
|
|
27
|
+
return () => {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Creating a `listenerEntry` object ensures that the `.filter()` function
|
|
31
|
+
// during "unsubscribe" step doesn't accidentally remove another listeners
|
|
32
|
+
// having the same `listener` function.
|
|
33
|
+
// I.e. it's not illegal to call `.subscribe(listener)` multiple times
|
|
34
|
+
// with the same argument, and those would be considered different subscriptions.
|
|
35
|
+
const listenerEntry = {
|
|
36
|
+
listener
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// If it's the first listener, activate subscription.
|
|
40
|
+
if (this._listeners.length === 0) {
|
|
41
|
+
this._deactivateSubscription = this._activateSubscription(this.notifySubscribers);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Add the `listener` to the list.
|
|
45
|
+
this._listeners.push(listenerEntry);
|
|
46
|
+
|
|
47
|
+
// The returned `unsubscribe()` function is "idempotent", i.e. it can be called multiple times.
|
|
48
|
+
return () => {
|
|
49
|
+
// Remove the listener, if not already removed.
|
|
50
|
+
this._removeListener(listenerEntry);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
stop() {
|
|
54
|
+
if (this._stopped) {
|
|
55
|
+
throw new Error('Already stopped');
|
|
56
|
+
}
|
|
57
|
+
this._stopped = true;
|
|
58
|
+
|
|
59
|
+
// Clear any remaining listeners.
|
|
60
|
+
for (const listener of this._listeners.slice()) {
|
|
61
|
+
this._removeListener(listener);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
_removeListener(listenerEntry) {
|
|
65
|
+
// If no listeners are left, no need to do anything.
|
|
66
|
+
if (this._listeners.length === 0) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Remove the `listener` from the list.
|
|
71
|
+
this._listeners = this._listeners.filter(_ => _ !== listenerEntry);
|
|
72
|
+
|
|
73
|
+
// If it was the last listener, deactivate subscription.
|
|
74
|
+
if (this._listeners.length === 0) {
|
|
75
|
+
this._deactivateSubscription();
|
|
76
|
+
this._deactivateSubscription = undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.default = Subscription;
|
|
81
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// TypeScript Version: 3.0
|
|
2
|
+
|
|
3
|
+
import { Session } from '../index.d.js';
|
|
4
|
+
|
|
5
|
+
export {};
|
|
6
|
+
|
|
7
|
+
export type GenericValue =
|
|
8
|
+
| string
|
|
9
|
+
| number
|
|
10
|
+
| boolean
|
|
11
|
+
| Record<string, unknown>
|
|
12
|
+
| null
|
|
13
|
+
| undefined;
|
|
14
|
+
|
|
15
|
+
export class DataStorage<
|
|
16
|
+
Key extends string = string,
|
|
17
|
+
Value extends GenericValue = GenericValue,
|
|
18
|
+
> {
|
|
19
|
+
constructor(session: Session, options: { namespace: string });
|
|
20
|
+
|
|
21
|
+
get(key: Key): Value;
|
|
22
|
+
|
|
23
|
+
set(key: Key, value: Value): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class LocationDataStorage<
|
|
27
|
+
Key extends string = string,
|
|
28
|
+
Value extends GenericValue = GenericValue,
|
|
29
|
+
> {
|
|
30
|
+
constructor(session: Session, options: { namespace: string });
|
|
31
|
+
|
|
32
|
+
get(location: Location, key: Key): Value;
|
|
33
|
+
|
|
34
|
+
set(location: Location, key: Key, value: Value): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const _excluded = ["maintainScrollPosition"];
|
|
2
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
3
|
+
import { applyMiddleware, createStore } from 'redux';
|
|
4
|
+
import Actions from './redux/Actions';
|
|
5
|
+
import createMiddlewares from './redux/createMiddlewares';
|
|
6
|
+
import locationReducer from './redux/locationReducer';
|
|
7
|
+
import ScrollPositionRestoration from './scroll-position/ScrollPositionRestoration';
|
|
8
|
+
function getCreateMiddlewaresOptions(navigationStackOptions) {
|
|
9
|
+
if (!navigationStackOptions) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
// eslint-disable-next-line no-unused-vars
|
|
13
|
+
const restOptions = _objectWithoutPropertiesLoose(navigationStackOptions, _excluded);
|
|
14
|
+
return restOptions;
|
|
15
|
+
}
|
|
16
|
+
export default class NavigationStack {
|
|
17
|
+
constructor(session, options) {
|
|
18
|
+
this._session = session;
|
|
19
|
+
|
|
20
|
+
// Create a Redux store.
|
|
21
|
+
this._store = createStore(locationReducer, applyMiddleware(...createMiddlewares(session, getCreateMiddlewaresOptions(options))));
|
|
22
|
+
|
|
23
|
+
// Create `ScrollPositionRestoration`.
|
|
24
|
+
if (options && options.maintainScrollPosition) {
|
|
25
|
+
this._scrollPositionRestoration = new ScrollPositionRestoration(session);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
addScrollableContainer(scrollableContainerKey, scrollableContainer) {
|
|
29
|
+
if (!this._scrollPositionRestoration) {
|
|
30
|
+
throw new Error('`maintainScrollPosition: true` option not passed');
|
|
31
|
+
}
|
|
32
|
+
return this._scrollPositionRestoration.addScrollableContainer(scrollableContainerKey, scrollableContainer);
|
|
33
|
+
}
|
|
34
|
+
subscribe(listener) {
|
|
35
|
+
// Subscribe to any potential Redux state changes.
|
|
36
|
+
return this._store.subscribe(() => {
|
|
37
|
+
// Initially, calls the listener when setting the initial location.
|
|
38
|
+
// After that, calls it on any location change.
|
|
39
|
+
const location = this.current();
|
|
40
|
+
if (!this._latestLocation || location !== this._latestLocation) {
|
|
41
|
+
this._latestLocation = location;
|
|
42
|
+
listener(location);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
init(initialLocation) {
|
|
47
|
+
if (this._latestLocation) {
|
|
48
|
+
throw new Error('Already initialized');
|
|
49
|
+
}
|
|
50
|
+
this._store.dispatch(Actions.init(initialLocation));
|
|
51
|
+
this._latestLocation = this.current();
|
|
52
|
+
}
|
|
53
|
+
current() {
|
|
54
|
+
return this._store.getState();
|
|
55
|
+
}
|
|
56
|
+
push(location) {
|
|
57
|
+
this._store.dispatch(Actions.push(location));
|
|
58
|
+
}
|
|
59
|
+
replace(location) {
|
|
60
|
+
this._store.dispatch(Actions.replace(location));
|
|
61
|
+
}
|
|
62
|
+
shift(delta) {
|
|
63
|
+
this._store.dispatch(Actions.shift(delta));
|
|
64
|
+
}
|
|
65
|
+
stop() {
|
|
66
|
+
if (this._scrollPositionRestoration) {
|
|
67
|
+
this._scrollPositionRestoration.stop();
|
|
68
|
+
}
|
|
69
|
+
this._store.dispatch(Actions.stop());
|
|
70
|
+
}
|
|
71
|
+
locationRendered() {
|
|
72
|
+
if (this._scrollPositionRestoration) {
|
|
73
|
+
const location = this.current();
|
|
74
|
+
if (!location) {
|
|
75
|
+
throw new Error('Not initialized');
|
|
76
|
+
}
|
|
77
|
+
return this._scrollPositionRestoration.locationRendered(location);
|
|
78
|
+
}
|
|
79
|
+
return Promise.resolve();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export default class DataStorage {
|
|
2
|
+
constructor(session, {
|
|
3
|
+
namespace
|
|
4
|
+
}) {
|
|
5
|
+
if (!session.key) {
|
|
6
|
+
throw new Error('`DataStorage` requires a `session.key`');
|
|
7
|
+
}
|
|
8
|
+
this._sessionKey = session.key;
|
|
9
|
+
this._dataStorage = session.environment.dataStorage;
|
|
10
|
+
this._namespace = namespace;
|
|
11
|
+
}
|
|
12
|
+
get(key) {
|
|
13
|
+
const storageKey = this._getStorageKey(key);
|
|
14
|
+
try {
|
|
15
|
+
const value = this._dataStorage.get(storageKey);
|
|
16
|
+
// === null is probably sufficient.
|
|
17
|
+
if (value === null) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// We want to catch JSON parse errors in case someone separately threw
|
|
22
|
+
// junk into sessionStorage under our namespace.
|
|
23
|
+
return JSON.parse(value);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
// eslint-disable-next-line no-console
|
|
26
|
+
console.error('[navigation-stack] Could not read data from storage');
|
|
27
|
+
|
|
28
|
+
// Pretend that the entry doesn't exist.
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
set(key, value) {
|
|
33
|
+
const storageKey = this._getStorageKey(key);
|
|
34
|
+
if (value === undefined) {
|
|
35
|
+
try {
|
|
36
|
+
this._dataStorage.remove(storageKey);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// No need to handle errors here.
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.error('[navigation-stack] Could not delete data from storage');
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Unlike with read, we want to fail on invalid values here, since the
|
|
46
|
+
// value here is provided by the caller of this method.
|
|
47
|
+
const valueString = JSON.stringify(value);
|
|
48
|
+
try {
|
|
49
|
+
this._dataStorage.set(storageKey, valueString);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
// No need to handle errors here either. If it didn't work, it didn't
|
|
52
|
+
// work. We make no guarantees about actually saving the value.
|
|
53
|
+
// eslint-disable-next-line no-console
|
|
54
|
+
console.error('[navigation-stack] Could not save data in storage');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// It could also implement a method that would clean up any data written so far,
|
|
59
|
+
// but so far it doesn't seem to be required by any real-world use case.
|
|
60
|
+
// cleanUp() {}
|
|
61
|
+
|
|
62
|
+
_getStorageKey(key) {
|
|
63
|
+
return `${this._sessionKey}|${this.namespace}|${key}`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import DataStorage from './DataStorage';
|
|
2
|
+
import getLocationUrl from '../getLocationUrl';
|
|
3
|
+
export default class LocationDataStorage {
|
|
4
|
+
constructor(session, {
|
|
5
|
+
namespace
|
|
6
|
+
}) {
|
|
7
|
+
this._storage = new DataStorage(session, {
|
|
8
|
+
namespace
|
|
9
|
+
});
|
|
10
|
+
this._getFallbackLocationKey = getLocationUrl;
|
|
11
|
+
}
|
|
12
|
+
get(location, key) {
|
|
13
|
+
return this._storage.get(this._getKey(location, key));
|
|
14
|
+
}
|
|
15
|
+
set(location, key, value) {
|
|
16
|
+
this._storage.set(this._getKey(location, key), value);
|
|
17
|
+
}
|
|
18
|
+
_getKey(location, key) {
|
|
19
|
+
const locationKey = location.key || this._getFallbackLocationKey(location);
|
|
20
|
+
return `${locationKey}|${key}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/lib/esm/debug.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import InMemoryDataStorage from './data-storage/InMemoryDataStorage';
|
|
2
|
+
import InMemoryScrollPosition from './scroll-position/InMemoryScrollPosition';
|
|
3
|
+
export default class InMemoryEnvironment {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.dataStorage = new InMemoryDataStorage();
|
|
6
|
+
this.scrollPosition = new InMemoryScrollPosition();
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import WebBrowserDataStorage from './data-storage/WebBrowserDataStorage';
|
|
2
|
+
import WebBrowserScrollPosition from './scroll-position/WebBrowserScrollPosition';
|
|
3
|
+
export default class WebBrowserEnvironment {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.dataStorage = new WebBrowserDataStorage();
|
|
6
|
+
this.scrollPosition = new WebBrowserScrollPosition();
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default class InMemoryDataStorage {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._state = {};
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Returns either a `string` value or `null` if the key doesn't exist.
|
|
7
|
+
get(key) {
|
|
8
|
+
if (key in this._state) {
|
|
9
|
+
return this._state[key];
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
remove(key) {
|
|
14
|
+
if (key in this._state) {
|
|
15
|
+
delete this._state[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
set(key, value) {
|
|
19
|
+
this._state[key] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class WebBrowserDataStorage {
|
|
2
|
+
// Returns either a `string` value or `null` if the key doesn't exist.
|
|
3
|
+
get(key) {
|
|
4
|
+
// `sessionStorage` persists across page reloads, and so does web browser navigation history.
|
|
5
|
+
return window.sessionStorage.getItem(key);
|
|
6
|
+
}
|
|
7
|
+
remove(key) {
|
|
8
|
+
// `sessionStorage` persists across page reloads, and so does web browser navigation history.
|
|
9
|
+
window.sessionStorage.removeItem(key);
|
|
10
|
+
}
|
|
11
|
+
set(key, value) {
|
|
12
|
+
// `sessionStorage` persists across page reloads, and so does web browser navigation history.
|
|
13
|
+
window.sessionStorage.setItem(key, value);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default class InMemoryScrollPosition {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.init();
|
|
4
|
+
}
|
|
5
|
+
getPageScrollPosition() {
|
|
6
|
+
return this._pageScrollPosition || [0, 0];
|
|
7
|
+
}
|
|
8
|
+
setPageScrollPosition(scrollPosition) {
|
|
9
|
+
this._pageScrollPosition = scrollPosition;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// eslint-disable-next-line no-unused-vars
|
|
13
|
+
setPageScrollPositionAtAnchor(anchor) {
|
|
14
|
+
this.setPageScrollPosition([0, 0]);
|
|
15
|
+
}
|
|
16
|
+
getScrollableContainerScrollPosition(key) {
|
|
17
|
+
return this._scrollableContainerScrollPositions[key] || [0, 0];
|
|
18
|
+
}
|
|
19
|
+
setScrollableContainerScrollPosition(key, scrollPosition) {
|
|
20
|
+
this._scrollableContainerScrollPositions[key] = scrollPosition;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// eslint-disable-next-line no-unused-vars
|
|
24
|
+
addPageScrollListener(listener) {
|
|
25
|
+
return () => {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// eslint-disable-next-line no-unused-vars
|
|
29
|
+
addScrollableContainerScrollListener(scrollableContainerElement, listener) {
|
|
30
|
+
return () => {};
|
|
31
|
+
}
|
|
32
|
+
enableAutomaticScrollRestoration() {}
|
|
33
|
+
disableAutomaticScrollRestoration() {}
|
|
34
|
+
init() {
|
|
35
|
+
this._pageScrollPosition = undefined;
|
|
36
|
+
this._scrollableContainerScrollPositions = {};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export default class WebBrowserScrollPosition {
|
|
2
|
+
getPageScrollPosition() {
|
|
3
|
+
return [window.pageXOffset, window.pageYOffset];
|
|
4
|
+
}
|
|
5
|
+
setPageScrollPosition(scrollPosition) {
|
|
6
|
+
const [scrollX, scrollY] = scrollPosition;
|
|
7
|
+
window.scrollTo(scrollX, scrollY);
|
|
8
|
+
}
|
|
9
|
+
setPageScrollPositionAtAnchor(anchor) {
|
|
10
|
+
const anchorElement = document.getElementById(anchor) || document.getElementsByName(anchor)[0];
|
|
11
|
+
if (anchorElement) {
|
|
12
|
+
// By default it scrolls the element into view
|
|
13
|
+
// so that it's visible at the top of the window.
|
|
14
|
+
anchorElement.scrollIntoView();
|
|
15
|
+
} else {
|
|
16
|
+
this.setPageScrollPosition([0, 0]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
getScrollableContainerScrollPosition(scrollableContainerElement) {
|
|
20
|
+
return [scrollableContainerElement.scrollLeft, scrollableContainerElement.scrollTop];
|
|
21
|
+
}
|
|
22
|
+
setScrollableContainerScrollPosition(scrollableContainerElement, scrollPosition) {
|
|
23
|
+
const [scrollX, scrollY] = scrollPosition;
|
|
24
|
+
scrollableContainerElement.scrollLeft = scrollX;
|
|
25
|
+
scrollableContainerElement.scrollTop = scrollY;
|
|
26
|
+
}
|
|
27
|
+
addPageScrollListener(listener) {
|
|
28
|
+
// eslint-disable-next-line no-unused-vars
|
|
29
|
+
const scrollListener = event => {
|
|
30
|
+
listener();
|
|
31
|
+
};
|
|
32
|
+
window.addEventListener('scroll', scrollListener);
|
|
33
|
+
return () => {
|
|
34
|
+
window.removeEventListener('scroll', scrollListener);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
addScrollableContainerScrollListener(scrollableContainerElement, listener) {
|
|
38
|
+
// eslint-disable-next-line no-unused-vars
|
|
39
|
+
const scrollListener = event => {
|
|
40
|
+
listener();
|
|
41
|
+
};
|
|
42
|
+
scrollableContainerElement.addEventListener('scroll', scrollListener);
|
|
43
|
+
return () => {
|
|
44
|
+
scrollableContainerElement.removeEventListener('scroll', scrollListener);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
enableAutomaticScrollRestoration() {
|
|
48
|
+
window.history.scrollRestoration = 'auto';
|
|
49
|
+
}
|
|
50
|
+
disableAutomaticScrollRestoration() {
|
|
51
|
+
window.history.scrollRestoration = 'manual';
|
|
52
|
+
}
|
|
53
|
+
init() {}
|
|
54
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const _excluded = ["operation", "delta"];
|
|
2
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
3
|
+
// Converts `LocationInternal` object to a publicly-visible `Location` object.
|
|
4
|
+
// It hides non-essential properties of location such as `operation` and `delta`.
|
|
5
|
+
export default function getLocationFromInternalLocation(internalLocation) {
|
|
6
|
+
// eslint-disable-next-line no-unused-vars
|
|
7
|
+
const location = _objectWithoutPropertiesLoose(internalLocation, _excluded);
|
|
8
|
+
return location;
|
|
9
|
+
}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
export { default as Actions } from './Actions';
|
|
2
|
-
export { default as ActionTypes } from './ActionTypes';
|
|
3
1
|
export { addBasePath, removeBasePath } from './basePath';
|
|
4
2
|
export { default as addNavigationBlocker } from './addNavigationBlocker';
|
|
5
3
|
export { default as getLocationUrl } from './getLocationUrl';
|
|
6
4
|
export { default as parseLocationUrl } from './parseLocationUrl';
|
|
7
|
-
export { default as
|
|
8
|
-
export { default as
|
|
9
|
-
export { default as
|
|
10
|
-
export { default as
|
|
11
|
-
export { default as
|
|
12
|
-
export { default as
|
|
5
|
+
export { default as parseInputLocation } from './parseInputLocation';
|
|
6
|
+
export { default as NavigationStack } from './NavigationStack';
|
|
7
|
+
export { default as DataStorage } from './data-storage/DataStorage';
|
|
8
|
+
export { default as LocationDataStorage } from './data-storage/LocationDataStorage';
|
|
9
|
+
export { default as Session } from './session/Session';
|
|
10
|
+
export { default as InMemorySession } from './session/InMemorySession';
|
|
11
|
+
export { default as WebBrowserSession } from './session/WebBrowserSession';
|
|
12
|
+
export { default as ServerSideRenderSession } from './session/ServerSideRenderSession';
|
|
13
|
+
export { default as ServerSideNavigationError } from './session/navigation/error/ServerSideNavigationError';
|
|
14
|
+
export { default as NavigationOutOfBoundsError } from './session/navigation/error/NavigationOutOfBoundsError';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-underscore-dangle */
|
|
2
2
|
|
|
3
|
+
import debug from './debug';
|
|
3
4
|
import isPromise from './isPromise';
|
|
4
5
|
export function getNavigationBlockers(session) {
|
|
5
6
|
return session._navigationBlockersList || [];
|
|
@@ -16,12 +17,12 @@ function removeNavigationBlockerFromTheList(blocker, session) {
|
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
export function removeAllNavigationBlockers(session) {
|
|
19
|
-
if (getNavigationBlockers(session).some(blocker => blocker.
|
|
20
|
-
if (!session.
|
|
21
|
-
throw new Error('`
|
|
20
|
+
if (getNavigationBlockers(session).some(blocker => blocker.beforeTermination)) {
|
|
21
|
+
if (!session._removeTerminationBlocker) {
|
|
22
|
+
throw new Error('`_removeTerminationBlocker` property not found in the `session`');
|
|
22
23
|
}
|
|
23
|
-
session.
|
|
24
|
-
session.
|
|
24
|
+
session._removeTerminationBlocker();
|
|
25
|
+
session._removeTerminationBlocker = undefined;
|
|
25
26
|
}
|
|
26
27
|
session._navigationBlockersList = [];
|
|
27
28
|
}
|
|
@@ -72,45 +73,47 @@ export function runNavigationBlockers(navigationBlockers, toLocation) {
|
|
|
72
73
|
if (isPromise(result)) {
|
|
73
74
|
return result.then(resultValue => {
|
|
74
75
|
if (resultValue) {
|
|
76
|
+
debug('Navigation blocked', toLocation.pathname);
|
|
75
77
|
return resultValue;
|
|
76
78
|
}
|
|
77
79
|
return next();
|
|
78
80
|
});
|
|
79
81
|
}
|
|
80
82
|
if (result) {
|
|
83
|
+
debug('Navigation blocked', toLocation.pathname);
|
|
81
84
|
return result;
|
|
82
85
|
}
|
|
83
86
|
return next();
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
/* istanbul ignore next: not testable with Karma */
|
|
87
|
-
function
|
|
90
|
+
function terminationBlocker(session) {
|
|
88
91
|
const result = runNavigationBlockers(getNavigationBlockers(session), null);
|
|
89
92
|
|
|
90
|
-
// If no blocker returned anything, don't prevent the
|
|
93
|
+
// If no blocker returned anything, so don't prevent the navigation.
|
|
91
94
|
if (!result) {
|
|
92
95
|
return undefined;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
// Web browsers don't allow displaying a custom modal in "beforeunload" phase.
|
|
96
99
|
// They only allow displaying a standard one, with the default text.
|
|
97
|
-
// Hence, "asynchronous" blockers should be ignored.
|
|
100
|
+
// Hence, "asynchronous" blockers should be ignored because web browsers won't wait for those to finish anyway.
|
|
98
101
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
|
|
99
102
|
if (isPromise(result)) {
|
|
100
103
|
return undefined;
|
|
101
104
|
}
|
|
102
105
|
|
|
103
|
-
//
|
|
106
|
+
// Block the navigation.
|
|
104
107
|
return true;
|
|
105
108
|
}
|
|
106
109
|
export function addNavigationBlocker(session, blocker) {
|
|
107
|
-
// All navigation blockers also run on `
|
|
110
|
+
// All navigation blockers also run on `beforeTermination` event.
|
|
108
111
|
// If required, this could be a parameter of this function.
|
|
109
112
|
// The rationale could be that adding a `beforeunload` listener
|
|
110
113
|
// disables web page caching in some browsers like Firefox.
|
|
111
|
-
const
|
|
114
|
+
const beforeTermination = true;
|
|
112
115
|
|
|
113
|
-
// If it's the first "
|
|
116
|
+
// If it's the first "beforeTermination" blocker, add a `terminationBlocker`.
|
|
114
117
|
//
|
|
115
118
|
// Sidenote: Add the "beforeunload" event listener only as needed, as its presence
|
|
116
119
|
// prevents the page from being added to the page navigation cache:
|
|
@@ -120,27 +123,29 @@ export function addNavigationBlocker(session, blocker) {
|
|
|
120
123
|
// and this is bad for performance."
|
|
121
124
|
//
|
|
122
125
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
|
|
123
|
-
if (
|
|
124
|
-
if (session.
|
|
125
|
-
throw new Error('Unexpected `
|
|
126
|
+
if (beforeTermination && !getNavigationBlockers(session).some(navigationBlocker => navigationBlocker.beforeTermination)) {
|
|
127
|
+
if (session._removeTerminationBlocker) {
|
|
128
|
+
throw new Error('Unexpected `_removeTerminationBlocker` property found in the `session`');
|
|
126
129
|
}
|
|
127
|
-
session.
|
|
130
|
+
session._removeTerminationBlocker = session.lifecycle.addTerminationBlocker(() => {
|
|
131
|
+
return terminationBlocker(session);
|
|
132
|
+
});
|
|
128
133
|
}
|
|
129
134
|
const newNavigationBlocker = {
|
|
130
135
|
blocker,
|
|
131
|
-
|
|
136
|
+
beforeTermination
|
|
132
137
|
};
|
|
133
138
|
addNavigationBlockerToTheList(newNavigationBlocker, session);
|
|
134
139
|
return () => {
|
|
135
140
|
removeNavigationBlockerFromTheList(newNavigationBlocker, session);
|
|
136
141
|
|
|
137
|
-
// If it was the last "
|
|
138
|
-
if (
|
|
139
|
-
if (!session.
|
|
140
|
-
throw new Error('`
|
|
142
|
+
// If it was the last "beforeTermination" blocker, remove navigation blocker.
|
|
143
|
+
if (beforeTermination && !getNavigationBlockers(session).some(navigationBlocker => navigationBlocker.beforeTermination)) {
|
|
144
|
+
if (!session._removeTerminationBlocker) {
|
|
145
|
+
throw new Error('`_removeTerminationBlocker` property not found in the `session`');
|
|
141
146
|
}
|
|
142
|
-
session.
|
|
143
|
-
session.
|
|
147
|
+
session._removeTerminationBlocker();
|
|
148
|
+
session._removeTerminationBlocker = undefined;
|
|
144
149
|
}
|
|
145
150
|
};
|
|
146
151
|
}
|