Skip to content

Instantly share code, notes, and snippets.

@snikch
Last active March 1, 2021 01:34
Show Gist options
  • Save snikch/fccdf9330e9a526a28d353883341fff8 to your computer and use it in GitHub Desktop.
Save snikch/fccdf9330e9a526a28d353883341fff8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const mergeTrust = (key) => ({
// id: 'mergeTrust'+key,
initial: "checkSuperseded",
context: {},
states: {
checkSuperseded: {
// always: [
// { target: "ignore", cond: "checkSuperseded" },
// { target: "checkTrust" }
// ],
on: {
[`${key}_CURRENT`]: "checkTrust",
[`${key}_SUPERSEDED`]: "ignore"
}
},
ignore: { type: "final" },
checkTrust: {
// always: [
// { target: "noChange", cond: "checkTrust" },
// { target: "setTrust", cond: "checkTrust" }
// ],
on: {
[`${key}_NOOP`]: "noChange",
[`${key}_UPDATE`]: "setTrust"
}
},
noChange: { type: "final" },
setTrust: {
entry: ["setTrust"],
type: "final"
}
}
});
const accept = {
initial: "setDetails",
context: {},
states: {
setDetails: {
id: "setDetals",
entry: [],
// always: [{ target: "mergeTrust" }],
on: {
DETAILS_SET: "mergeTrust"
}
},
mergeTrust: {
// id: 'mergeTrust',
type: "parallel",
states: {
pk_1: mergeTrust("1"),
pk_2: mergeTrust("2"),
pk_3: mergeTrust("3")
},
onDone: "complete"
},
complete: { type: "final" }
}
};
const actionsMap = {
lock: () => {},
unlock: () => {},
setPayload: () => {}
};
const services = {
lock: () => {},
setPayload: () => {},
setDetails: () => {},
setTrust: () => {}
};
const guards = {
checkTrust: () => true,
checkSigCache: () => true,
parse: () => true,
verify: () => true,
validateSigPk: () => true,
checkSigTs: () => true,
checkSuperseded: () => true
};
const networkTrust = {
id: "networkTrust",
initial: "recv",
context: {
trust: {},
state: {},
meta: {}
},
states: {
recv: {
entry: ["lock", "setPayload"],
// always: [{ target: "checkTrust" }],
on: {
RECEIVED: "checkTrust"
}
},
complete: {
entry: ["unlock"],
type: "final"
},
checkTrust: {
// always: [
// { target: "checkSigCache", cond: "checkTrust" },
// { target: "untrusted", cond: "checkTrust" }
// ],
on: {
TRUST: "checkSigCache",
DONT_TRUST: "untrusted"
}
},
untrusted: {
// always: [{ target: "complete" }]
},
checkSigCache: {
// always: [
// { target: "parse", cond: "checkSigCache" },
// { target: "noChange", cond: "checkSigCache" }
// ],
on: {
CHANGED: "parse",
UNCHANGED: "noChange"
}
},
noChange: {
// always: [{ target: "complete" }]
},
parse: {
// always: [
// { target: "verify", cond: "parse" },
// { target: "parseError", cond: "parse" }
// ],
on: {
PARSED: "verify",
FAILED: "parseError"
}
},
parseError: {
// always: [{ target: "complete" }]
},
verify: {
// always: [
// { target: "validateSigPk", cond: "verify" },
// { target: "invalidSig", cond: "verify" }
// ],
on: {
VERIFIED: "validateSigPk",
UNVERIFIED: "invalidSig"
}
},
invalidSig: {
// always: [{ target: "complete" }]
},
validateSigPk: {
// always: [
// { target: "checkSigTs", cond: "validateSigPk" },
// { target: "invalidPk", cond: "validateSigPk" }
// ],
on: {
SIG_PK_VALID: "checkSigTs",
SIG_PK_INVALID: "invalidPk"
}
},
invalidPk: {
// always: [{ target: "complete" }]
},
checkSigTs: {
// always: [
// { target: "accept", cond: "checkSigTs" },
// { target: "outdated", cond: "checkSigTs" }
// ],
on: {
SIG_TS_VALID: "accept",
SIG_TS_INVALID: "outdated"
}
},
outdated: {
// always: [{ target: "complete" }]
},
accept: {
on: {
COMPLETE: "complete"
},
onDone: "complete",
...accept
}
}
};
const fetchMachine = Machine(networkTrust, {
actions: actionsMap,
services,
guards
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment