[Canvas] Tear down history instances (#68277)

* [Canvas] Tear down history instances

* Always create history on Canvas startup

Co-authored-by: Poff Poffenberger <poffdeluxe@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Corey Robertson 2020-06-08 14:11:51 -04:00 committed by GitHub
parent 0189ae5c3f
commit 16fbbf4345
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -33,7 +33,7 @@ import { CapabilitiesStrings } from '../i18n';
import { startServices, services } from './services';
// @ts-ignore Untyped local
import { destroyHistory } from './lib/history_provider';
import { createHistory, destroyHistory } from './lib/history_provider';
// @ts-ignore Untyped local
import { stopRouter } from './lib/router_provider';
import { initFunctions } from './functions';
@ -97,6 +97,9 @@ export const initializeCanvas = async (
services.expressions.getService().registerFunction(fn);
}
// Re-initialize our history
createHistory();
// Create Store
const canvasStore = await createStore(coreSetup, setupPlugins);

View file

@ -134,7 +134,7 @@ function wrapHistoryInstance(history) {
return wrappedHistory;
}
let instances = new WeakMap();
const instances = new WeakMap();
const getHistoryInstance = (win) => {
// if no window object, use memory module
@ -144,13 +144,7 @@ const getHistoryInstance = (win) => {
return createHashStateHistory();
};
export const historyProvider = (win = getWindow()) => {
// return cached instance if one exists
const instance = instances.get(win);
if (instance) {
return instance;
}
export const createHistory = (win = getWindow()) => {
// create and cache wrapped history instance
const historyInstance = getHistoryInstance(win);
const wrappedInstance = wrapHistoryInstance(historyInstance);
@ -159,6 +153,20 @@ export const historyProvider = (win = getWindow()) => {
return wrappedInstance;
};
export const destroyHistory = () => {
instances = new WeakMap();
export const historyProvider = (win = getWindow()) => {
// return cached instance if one exists
const instance = instances.get(win);
if (instance) {
return instance;
}
return createHistory(win);
};
export const destroyHistory = (win = getWindow()) => {
const instance = instances.get(win);
if (instance) {
instance.resetOnChange();
}
};