open-consul/ui/packages/consul-ui/app/services/state.js
John Cowen ef01ea18f1
ui: Run Ember native class code mod (#9093)
* ui: Apply native class codemod to all services

* ui: Apply native class codemod to routes

* ui: Apply native class codemod to controllers

* Fix up ember proxy `content` issue

* Add a CreateTime on policy creation

* Minor formatting

* Convert child based saving to use ec instead of custom approach

* Remove custom event source repo wrapping initializer

* Repos here are no longer proxy objects revert to using them normally

* Remove areas of code that were used to set up source backed repos
2020-11-09 09:25:35 +00:00

86 lines
2.2 KiB
JavaScript

import Service, { inject as service } from '@ember/service';
import { set } from '@ember/object';
import flat from 'flat';
import { createMachine, interpret } from '@xstate/fsm';
export default class StateService extends Service {
@service('logger')
logger;
// @xstate/fsm
log(chart, state) {
// this.logger.execute(`${chart.id} > ${state.value}`);
}
addGuards(chart, options) {
this.guards(chart).forEach(function([path, name]) {
// xstate/fsm has no guard lookup
set(chart, path, function() {
return !!options.onGuard(...[name, ...arguments]);
});
});
return [chart, options];
}
machine(chart, options = {}) {
return createMachine(...this.addGuards(chart, options));
}
prepareChart(chart) {
// xstate/fsm has no guard lookup so we clone the chart here
// for when we replace the string based guards with functions
// further down
chart = JSON.parse(JSON.stringify(chart));
// xstate/fsm doesn't seem to interpret toplevel/global events
// artificially add them here instead
if (typeof chart.on !== 'undefined') {
Object.values(chart.states).forEach(function(state) {
if (typeof state.on === 'undefined') {
state.on = chart.on;
} else {
Object.keys(chart.on).forEach(function(key) {
if (typeof state.on[key] === 'undefined') {
state.on[key] = chart.on[key];
}
});
}
});
}
return chart;
}
// abstract
matches(state, matches) {
if (typeof state === 'undefined') {
return false;
}
const values = Array.isArray(matches) ? matches : [matches];
return values.some(item => {
return state.matches(item);
});
}
state(cb) {
return {
matches: cb,
};
}
interpret(chart, options) {
chart = this.prepareChart(chart);
const service = interpret(this.machine(chart, options));
// returns subscription
service.subscribe(state => {
if (state.changed) {
this.log(chart, state);
options.onTransition(state);
}
});
return service;
}
guards(chart) {
return Object.entries(flat(chart)).filter(([key]) => key.endsWith('.cond'));
}
}