UNPKG

bonsai-analyzer

Version:
90 lines (68 loc) 2.61 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _actions = require("./actions"); function toQueryString(obj) { return Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&'); } class UrlStateEncoder { constructor(store) { (0, _defineProperty2.default)(this, "_store", void 0); (0, _defineProperty2.default)(this, "_subscription", null); (0, _defineProperty2.default)(this, "onStoreChanged", () => { const newState = this._store.getState(); window.location.hash = this.encodeStateForHash(newState); }); this._store = store; const targetState = this.readStateFromHash(window.location.hash); if (targetState.filename !== null && targetState.filename !== undefined) { (0, _actions.fetchDataFile)(store.dispatch)(targetState.filename); if (targetState.chunk !== null && targetState.chunk !== undefined) { (0, _actions.PickedChunk)(store.dispatch)(targetState.chunk); if (targetState.rm) { const remove = (0, _actions.RemovedModule)(store.dispatch); targetState.rm.split(',').forEach(remove); } } } this._subscription = store.subscribe(this.onStoreChanged); } readStateFromHash(hash) { return hash.replace(/^#/, '').split('&').reduce((map, pair) => { const [key, val] = pair.split('='); map[key] = decodeURIComponent(val); return map; }, {}); } encodeStateForHash(state) { const vals = {}; if (state.selectedFilename !== null && state.selectedFilename !== undefined) { vals.filename = state.selectedFilename; if (state.selectedChunkId !== null && state.selectedChunkId !== undefined) { vals.chunk = state.selectedChunkId; if (state.blacklistedModuleIds && state.blacklistedModuleIds.length) { vals.rm = state.blacklistedModuleIds.join(','); } } } return toQueryString(vals); } destroy() { if (this._subscription) { this._subscription(); this._subscription = null; } } } exports.default = UrlStateEncoder; (0, _defineProperty2.default)(UrlStateEncoder, "instance", null); (0, _defineProperty2.default)(UrlStateEncoder, "factory", function (store) { if (!UrlStateEncoder.instance) { UrlStateEncoder.instance = new UrlStateEncoder(store); } return UrlStateEncoder.instance; });