Created
January 2, 2022 06:28
-
-
Save aceslick911/1860faa1bcc844f15c3b81ebfd5ff5df to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* #region Types & Constants */ | |
const 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) => | |
Machine( | |
{ | |
id: name, | |
context: { | |
activeTransferRequest: undefined, | |
}, | |
initial: 'ready', | |
states: { | |
ready: { | |
on: { | |
CATS_CHANGE_REQUEST: { | |
actions: [ | |
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: [ | |
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) => | |
Machine( | |
{ | |
id: name, | |
context: {}, | |
initial: 'ready', | |
states: { ready: {} }, | |
}, | |
{ | |
actions: {}, | |
guards: {}, | |
}, | |
); | |
const Customer = (name) => | |
Machine( | |
{ | |
id: name, | |
context: { | |
marketOperatorRef: undefined, | |
}, | |
initial: 'ready', | |
states: { | |
ready: { | |
on: { | |
CATS_CHANGE_REQUEST: { | |
actions: [ | |
assign({ | |
marketOperatorRef: (context, event) => | |
event.marketOperatorRef, | |
}), | |
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 */ | |
const aemoContext = { | |
error: undefined, | |
transferMoveIn: [], | |
}; | |
// type AemoContextType = typeof aemoContext; | |
/* #endregion */ | |
const Aemo = Machine( | |
{ | |
id: 'aemo-process', | |
context: aemoContext, | |
initial: 'ready', | |
states: { | |
ready: { | |
on: { | |
CATS_CHANGE_REQUEST: { | |
actions: [ | |
assign({ | |
transferMoveIn: (context, event) => [ | |
...context.transferMoveIn, | |
{ | |
...event, | |
incomingUserRef: spawn( | |
Customer('incomingUser'), | |
`incoming-user-${event.id}`, | |
), | |
currentUserRef: spawn( | |
Customer('currentUser'), | |
`current-user-${event.id}`, | |
), | |
networkOperatorRef: spawn( | |
NetworkOperator('networkOperator'), | |
`network-operator-${event.id}`, | |
), | |
marketOperatorRef: spawn( | |
MarketOperator('marketOperator'), | |
`market-operator-${event.id}`, | |
), | |
}, | |
], | |
}), | |
// Initialize market operator with actor refs | |
send( | |
(context, event) => { | |
const 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) => { | |
const activeAction = | |
context.transferMoveIn[context.transferMoveIn.length - 1]; | |
const { marketOperatorRef } = activeAction; | |
return marketOperatorRef; | |
}, | |
}, | |
), | |
// Initialize incoming user with market operator actor ref | |
send( | |
(context, event) => { | |
const activeAction = | |
context.transferMoveIn[context.transferMoveIn.length - 1]; | |
const { marketOperatorRef } = activeAction; | |
return { | |
type: 'CATS_CHANGE_REQUEST', | |
marketOperatorRef, | |
}; | |
}, | |
{ | |
delay: AEMO_CONSTANTS.testingMessageDelay, | |
to: (context, event) => { | |
const 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