Skip to content

Instantly share code, notes, and snippets.

@cellog
Last active April 11, 2020 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellog/dabc966cb91333392abf10130c17f41a to your computer and use it in GitHub Desktop.
Save cellog/dabc966cb91333392abf10130c17f41a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const attributeBoundsMachine = Machine({
id: 'attribute bounds filter',
initial: 'idle',
context: {
projectId: "",
datasetId: "",
objectType: "ingredient-runs",
attributeTemplateType: "property-templates",
selectedTemplateId: "",
selectedType: "property",
cachedPropertyTemplates: {},
cachedParameterTemplates: {},
cachedConditionTemplates: {},
lower_bound: undefined,
upper_bound: undefined,
default_units: undefined,
},
states: {
loadingTemplates: {
type: "parallel",
states: {
property: {
type: "compound",
initial: "test",
states: {
test: {
on: {
'': [{
target: "loaded",
cond: ({ cachedPropertyTemplates }) => Object.keys(cachedPropertyTemplates).length
}, {
target: "loading",
}]
}
},
loading: {
invoke: {
id: "loading property templates",
src: () => {
// TODO: load templates
return Promise.resolve({
property: {
type: "Integer"
},
})
},
onDone: {
target: "loaded",
actions: assign({
cachedPropertyTemplates: (_, { data }) => data,
})
},
onError: {
target: "#attribute bounds filter.loading error",
// TODO: set up errors
}
}
},
loaded: {
type: "final"
}
},
},
parameter: {
type: "compound",
initial: "test",
states: {
test: {
on: {
'': [{
target: "loaded",
cond: ({ cachedParameterTemplates }) => Object.keys(cachedParameterTemplates).length
}, {
target: "loading",
}]
}
},
loading: {
invoke: {
id: "loading parameter templates",
src: () => {
// TODO: load templates
return Promise.resolve({
parameter: {
type: "Real",
},
})
},
onDone: {
target: "loaded",
actions: assign({
cachedParameterTemplates: (_, { data }) => data,
})
},
onError: {
target: "#attribute bounds filter.loading error",
// TODO: set up errors
}
}
},
loaded: {
type: "final"
}
}
},
condition: {
type: "compound",
initial: "test",
states: {
test: {
on: {
'': [{
target: "loaded",
cond: ({ cachedConditionTemplates }) => Object.keys(cachedConditionTemplates).length
}, {
target: "loading",
}]
}
},
loading: {
invoke: {
id: "loading condition templates",
src: () => {
// TODO: load templates
return Promise.resolve({
condition: {
type: "Real"
},
})
},
onDone: {
target: "loaded",
actions: assign({
cachedConditionTemplates: (_, { data }) => data,
})
},
onError: {
target: "#attribute bounds filter.loading error",
// TODO: set up errors
}
}
},
loaded: {
type: "final"
}
}
}
},
onDone: {
target: "idle",
}
},
idle: {
on: {
'': [{
target: "loadingTemplates",
cond: "anyTemplateNotCached"
}, {
target: "edit",
cond: ({ selectedTemplateId }) => !!selectedTemplateId,
}],
SELECT_TEMPLATE: {
target: "edit",
actions: assign({
selectedTemplateId: (_, { id }) => id,
})
}
}
},
edit: {
type: "compound",
initial: "selectBounds",
states: {
selectBounds: {
on: {
'': [
{ target: "bounds.realBounds", cond: "isRealBounds" },
{ target: "bounds.integerBounds", cond: "isIntegerBounds" },
]
}
},
bounds: {
type: "compound",
states: {
realBounds: {
on: {
SEARCH: [{
actions: assign((context, { lower_bound, upper_bound, default_units }) => ({
...context,
lower_bound,
upper_bound,
default_units,
}))
}, {
target: "#attribute bounds filter.search",
cond: "isValidRealBounds",
}, {
actions: assign({
errors: "TODO: actually assign errors",
})
}]
},
},
integerBounds: {
on: {
SEARCH: [{
actions: assign((context, { lower_bound, upper_bound }) => ({
...context,
lower_bound,
upper_bound,
default_units: undefined,
}))
}, {
target: "#attribute bounds filter.search",
cond: "isValidIntegerBounds",
}, {
actions: assign({
errors: "TODO: actually assign errors",
})
}]
},
},
}
},
},
},
"loading error": {
on: {
RETRY: "loadingTemplates",
}
},
search: {
type: "final",
},
}
}, {
guards: {
anyTemplateNotCached: ({ cachedPropertyTemplates: pr, cachedParameterTemplates: pa, cachedConditionTemplates: co }) => {
const k = Object.keys;
return !k(pr).length || !k(pa).length || !k(co).length;
},
isRealBounds: ({ selectedTemplateId, selectedType, ...context }) => {
const template = {
property: context.cachedPropertyTemplates,
parameter: context.cachedParameterTemplates,
condition: context.cachedConditionTemplates,
}[selectedType][selectedTemplateId];
return template && template.type === "Real";
},
isIntegerBounds: ({ selectedTemplateId, selectedType, ...context }) => {
const template = {
property: context.cachedPropertyTemplates,
parameter: context.cachedParameterTemplates,
condition: context.cachedConditionTemplates,
}[selectedType][selectedTemplateId];
return template && template.type === "Integer";
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment