open-consul/ui/packages/consul-ui/app/locations/fsm.js
John Cowen 7083c39b96
ui: Support Route optional parameters/segments (#10212)
Moves our URLs with 'optional namespace segment' into a separately abstracted 'optional URL segment' feature
2021-05-26 17:43:46 +01:00

47 lines
1.2 KiB
JavaScript

// a simple state machine that the History API happens to more or less implement
// it should really be an EventTarget but what we need here is simple enough
export class FSM {
// extends EventTarget/EventSource
state = {};
constructor(location, listener = () => {}) {
this.listener = listener;
this.location = location;
}
/**
* @param state The infinite/extended state or context
* @param _ `_` was meant to be title but was never used, don't use this
* argument for anything unless browsers change, see:
* https://github.com/whatwg/html/issues/2174
* @param path The state/event
*/
pushState(state, _, path) {
this.state = state;
this.location.pathname = path;
this.listener({ state: this.state });
}
replaceState() {
return this.pushState(...arguments);
}
}
export class Location {
pathname = '';
search = '';
hash = '';
}
export default class FSMLocation {
implementation = 'fsm';
static create() {
return new this(...arguments);
}
constructor(owner) {
this.container = Object.entries(owner)[0][1];
}
visit() {
return this.transitionTo(...arguments);
}
hrefTo() {}
transitionTo() {}
}