Skip to content

Instantly share code, notes, and snippets.

@aceslick911
Created January 2, 2022 06:26
Show Gist options
  • Save aceslick911/6764109f574151a994c60152d1b305db to your computer and use it in GitHub Desktop.
Save aceslick911/6764109f574151a994c60152d1b305db to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/* #region Types & Constants */
AEMO_CONSTANTS = {
testingMessageDelay: 2000,
CATS_CHANGE_REQUEST: 'TRF-REQ',
CATS_CHANGE_REQUEST_REJECTED: 'NACK',
};
/* #endregion */
/* #region Callbacks and Helper Methods */
// const fetchUser = (val) =>
// fetch(`url/to/user/${val}`).then((response) => response.json());
/* #endregion */
const MarketOperator = (name) =>
(0, Machine)(
{
id: name,
context: {
activeTransferRequest: undefined,
},
initial: 'ready',
states: {
ready: {
on: {
CATS_CHANGE_REQUEST: {
actions: [
(0, assign)({
activeTransferRequest: (context, event) => {
const {
networkOperatorRef,
currentUserRef,
incomingUserRef,
} = event;
return {
networkOperatorRef,
currentUserRef,
incomingUserRef,
};
},
}),
],
},
[AEMO_CONSTANTS.CATS_CHANGE_REQUEST]: [
{
target: 'invalidRequest',
cond: 'requestNotValid',
},
{
target: 'processTransferRequest',
cond: 'requestValid',
},
],
},
},
invalidRequest: {
entry: [
(0, send)(
(context, event) => ({
type: AEMO_CONSTANTS.CATS_CHANGE_REQUEST_REJECTED,
}),
{
delay: AEMO_CONSTANTS.testingMessageDelay,
to: (context, event) => {
const { incomingUserRef } = context.activeTransferRequest;
return incomingUserRef;
},
},
),
],
after: {
[AEMO_CONSTANTS.testingMessageDelay]: {
target: 'transferRequestFailed',
},
},
},
transferRequestFailed: {
type: 'final',
},
processTransferRequest: {},
},
},
{
actions: {},
guards: {
requestValid: (context, event) => false,
requestNotValid: (context, event) => true,
},
},
);
const NetworkOperator = (name) =>
(0, Machine)(
{
id: name,
context: {},
initial: 'ready',
states: { ready: {} },
},
{
actions: {},
guards: {},
},
);
const Customer = (name) =>
(0, Machine)(
{
id: name,
context: {
marketOperatorRef: undefined,
},
initial: 'ready',
states: {
ready: {
on: {
CATS_CHANGE_REQUEST: {
actions: [
(0, assign)({
marketOperatorRef: (context, event) =>
event.marketOperatorRef,
}),
(0, send)(
(context, event) => ({
type: AEMO_CONSTANTS.CATS_CHANGE_REQUEST,
}),
{
delay: AEMO_CONSTANTS.testingMessageDelay,
to: (context, event) => context.marketOperatorRef,
},
),
],
target: 'transferRequest.submitted',
},
},
},
transferRequest: {
on: {
[AEMO_CONSTANTS.CATS_CHANGE_REQUEST_REJECTED]: {
target: '.rejected',
},
},
states: {
submitted: {},
rejected: {
type: 'final',
},
},
onDone: {
target: 'rejected',
},
},
rejected: {
type: 'final',
},
},
},
{
actions: {},
guards: {},
},
);
/* #region Context */
AemoContext = {
error: undefined,
transferMoveIn: [],
};
/* #endregion */
Aemo = (0, Machine)(
{
id: 'aemo-process',
context: AemoContext,
initial: 'ready',
states: {
ready: {
on: {
CATS_CHANGE_REQUEST: {
actions: [
(0, assign)({
transferMoveIn: (context, event) => [
...context.transferMoveIn,
Object.assign(Object.assign({}, event), {
incomingUserRef: (0, spawn)(
Customer('incomingUser'),
`incoming-user-${event.id}`,
),
currentUserRef: (0, spawn)(
Customer('currentUser'),
`current-user-${event.id}`,
),
networkOperatorRef: (0, spawn)(
NetworkOperator('networkOperator'),
`network-operator-${event.id}`,
),
marketOperatorRef: (0, spawn)(
MarketOperator('marketOperator'),
`market-operator-${event.id}`,
),
}),
],
}),
// Initialize market operator with actor refs
(0, send)(
(context, event) => {
ActiveAction =
context.transferMoveIn[context.transferMoveIn.length - 1];
const {
networkOperatorRef,
currentUserRef,
incomingUserRef,
} = activeAction;
return {
type: 'CATS_CHANGE_REQUEST',
networkOperatorRef,
currentUserRef,
incomingUserRef,
};
},
{
delay: AEMO_CONSTANTS.testingMessageDelay,
to: (context, event) => {
ActiveAction =
context.transferMoveIn[context.transferMoveIn.length - 1];
const { marketOperatorRef } = activeAction;
return marketOperatorRef;
},
},
),
// Initialize incoming user with market operator actor ref
(0, send)(
(context, event) => {
ActiveAction =
context.transferMoveIn[context.transferMoveIn.length - 1];
const { marketOperatorRef } = activeAction;
return {
type: 'CATS_CHANGE_REQUEST',
marketOperatorRef,
};
},
{
delay: AEMO_CONSTANTS.testingMessageDelay,
to: (context, event) => {
ActiveAction =
context.transferMoveIn[context.transferMoveIn.length - 1];
const { incomingUserRef } = activeAction;
return incomingUserRef;
},
},
),
],
},
},
},
},
},
{
actions: {
// assignSalesPresentedOffer: assign({
// salesPresentedOffer: (context, event) => true as boolean,
// }),
},
guards: {
// offerIsReady: (context, event) => context.offerReady === true,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment