Skip to content

Instantly share code, notes, and snippets.

@sylvanaar
Last active August 24, 2020 14:54
Show Gist options
  • Save sylvanaar/c5aa6406bd4705def1006c298527edb6 to your computer and use it in GitHub Desktop.
Save sylvanaar/c5aa6406bd4705def1006c298527edb6 to your computer and use it in GitHub Desktop.
modeling
const onlineState = {
states: {
idle: {
on: {
CONFIG_CHANGE: "fetching",
DATA_STALE: "fetching",
},
},
retrieving: {},
fetching: {
// call the API, and populate the store
// "": [
// { target: "idle", cond: "accessUnavailable" },
// { target: "on", cond: "accessAvailable" },
// ],
},
dataRetrieved: {
// check that the new data is valid
// write it to the destination and go idle
// otherwise - perhaps follow some retry logic
},
},
};
const dataPointManager = {
initial: "offline",
context: {
requestInfo: {},
dataOutputKeyx = undefined,
dataExpiration: "datetime",
},
states: {
online: {
on: {
OFFLINE: "offline",
},
...onlineState,
},
offline: {
on: {
ONLINE: "online",
},
"": [{ target: "on", cond: "networkOnline" }],
},
},
};
const offlineStates = {
initial: "idle",
states: {
idle: {
on: {
NETCHANGE: "checking",
},
},
checking: {
"": [
{ target: "idle", cond: "accessUnavailable" },
{ target: "on", cond: "accessAvailable" },
],
},
},
};
const networkMachine = Machine({
id: "network",
initial: "offline",
context: {
retries: 0,
},
states: {
offline: {
on: {
NETUP: "online",
},
...offlineStates,
},
online: {
on: {
NETDOWN: "offline",
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment