History
History is Synapse's abstraction over a stack of navigation states; Router delegates all state bookkeeping to it, and App exposes it as the history property. BrowserHistory is the implementation that syncs with the real browser address bar and window.history.
History
An in-memory stack, used by default (an App/Router created without an explicit history gets a plain new History()).
import { History } from '@chialab/synapse';
const history = new History();
history.start(); // resets internal state and marks it activeKey members:
start()/stop()— activate/deactivate the instance;activereflects the current state.start()callsreset(), clearing all entries.pushState(state)/replaceState(state)— record aState({ url, path, title, data, request, response }, see Request & Response) as a new entry or in place of the current one; both emit a'pushstate'/'replacestate'event with{ state, previous }and return the storedHistoryState.go(shift)/back()/forward()— move the internal index byshift(or ±1) and emit'popstate'with{ state, previous }; out-of-range shifts are ignored.state— the currentState,states— the full list,index— the current position,length— the number of entries.compareStates(state1, state2)— returnsNavigationDirection.backifstate2sits beforestate1in the stack,NavigationDirection.forwardotherwise. This is whatApp#onPopStateuses to computenavigationDirection.
History extends the internal Emitter, so you can also do history.on('pushstate' | 'replacestate' | 'popstate', listener) directly.
BrowserHistory
Syncs the same API with window.history and the browser's popstate event, so back/forward buttons and the address bar work as expected.
import { render } from '@chialab/dna';
import { BrowserHistory } from '@chialab/synapse';
const app = render(<DemoApp base="/" history={new BrowserHistory()} />, document.body);
app.start();- Only one active
BrowserHistoryis allowed at a time; callingstart()on a second instance while another is active throwsYou cannot initialize more than one "BrowserHistory".. pushState/replaceStatecall through toHistoryand then towindow.history.pushState/replaceState, serializing theHistoryState(viaJSON.parse(JSON.stringify(...))) so it's safe to store natively.go(shift)delegates towindow.history.go()and resolves once the corresponding nativepopstatefires.- Native
popstateevents are translated back into Synapse's'popstate'event: if the event carries a recognizableHistoryStatefrom this session, the internal index is updated (or the stack is reset, if the state belongs to a differentHistoryinstance — e.g. after a full reload); otherwise (e.g. a hash-only navigation typed by hand) it's re-resolved fromwindow.location.href. listen()/unlisten()are deprecated aliases forstart()/stop().
In a demo-app element, this is set declaratively, as in the demo/navigation example:
import { html, render } from '@chialab/dna';
import { BrowserHistory } from '@chialab/synapse';
const app = render(html`<demo-app base=${`${location.pathname}#!/`} history=${new BrowserHistory()} />`, document.body);
app.start();Navigation direction
compareStates() returns one of the two 'back' / 'forward' string values (internally a NavigationDirection enum). App exposes the result as its navigationDirection state property, reflected in the :navigation attribute of the app element — useful for CSS-driven transition direction (see Page & Transition).