Repository URL to install this package:
|
Version:
1.2.10 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.syncHistoryWithStore = void 0;
var _mobx = require("xmobx/mobx");
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const syncHistoryWithStore = (history, store) => {
// Initialise store
store.history = history; // Handle update from history object
const handleLocationChange = location => {
store._updateLocation(location);
};
const unsubscribeFromHistory = history.listen(handleLocationChange);
handleLocationChange(history.location);
const subscribe = listener => {
const onStoreChange = change => {
const rawLocation = _objectSpread({}, store.location);
listener(rawLocation, history.action);
}; // Listen for changes to location state in store
const unsubscribeFromStore = (0, _mobx.observe)(store, 'location', onStoreChange);
listener(store.location, history.action);
return () => {
unsubscribeFromStore();
};
};
const unsubscribe = () => unsubscribeFromHistory();
history.subscribe = subscribe;
history.unsubscribe = unsubscribe;
return history;
};
exports.syncHistoryWithStore = syncHistoryWithStore;