diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-22 01:44:28 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-22 01:44:28 +0800 |
commit | ed91c6c874282ab069a72784b8e563b634a236bf (patch) | |
tree | e471f5006f7a7476a7c7166a040dd024146da4d2 /packages/instant/src | |
parent | b494a4a4dba6c781667b690f38387f03a22fe141 (diff) | |
download | dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar.gz dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar.bz2 dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar.lz dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar.xz dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.tar.zst dexon-sol-tools-ed91c6c874282ab069a72784b8e563b634a236bf.zip |
fix: multiple render and closes edge case for onpopstate render
Diffstat (limited to 'packages/instant/src')
-rw-r--r-- | packages/instant/src/index.umd.ts | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index 15ca59323..3a8694d6a 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -81,30 +81,32 @@ export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_Z if (!isInstantRendered()) { renderInstant(config, selector); } - } else { - // Before we render, push to history saying that instant is showing for this part of the history. - window.history.pushState({ zeroExInstantShowing: true }, '0x Instant'); - let removeInstant = renderInstant(config, selector); - - let prevOnPopState = util.boundNoop; - if (window.onpopstate) { - prevOnPopState = window.onpopstate.bind(window); - } - window.onpopstate = (e: PopStateEvent) => { - // Don't override integrators handler. - // prevOnPopState(e); - const newState = e.state; - if (newState && newState.zeroExInstantShowing) { - // We have returned to a history state that expects instant to be rendered. - if (!isInstantRendered()) { - removeInstant = renderInstant(config, selector); - } - } else { - // History has changed to a different state. - if (isInstantRendered()) { - removeInstant(); - } - } - }; + return; + } + // Before we render, push to history saying that instant is showing for this part of the history. + window.history.pushState({ zeroExInstantShowing: true }, '0x Instant'); + let removeInstant = renderInstant(config, selector); + // If the integrator defined a popstate handler, save it to __zeroExInstantIntegratorsPopStateHandler + // unless we have already done so on a previous render. + const anyWindow = window as any; + if (window.onpopstate && !anyWindow.__zeroExInstantIntegratorsPopStateHandler) { + anyWindow.__zeroExInstantIntegratorsPopStateHandler = window.onpopstate.bind(window); } + const integratorsOnPopStateHandler = anyWindow.__zeroExInstantIntegratorsPopStateHandler || util.boundNoop; + const onPopStateHandler = (e: PopStateEvent) => { + integratorsOnPopStateHandler(e); + const newState = e.state; + if (newState && newState.zeroExInstantShowing) { + // We have returned to a history state that expects instant to be rendered. + if (!isInstantRendered()) { + removeInstant = renderInstant(config, selector); + } + } else { + // History has changed to a different state. + if (isInstantRendered()) { + removeInstant(); + } + } + }; + window.onpopstate = onPopStateHandler; }; |