Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Add XState machines to model flows #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
},
"devDependencies": {
"prettier": "2.5.1"
},
"dependencies": {
"@xstate/react": "^1.6.3",
"xstate": "^4.27.0"
}
}
114 changes: 114 additions & 0 deletions provisionersdk/proto/xstate_models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createMachine } from "xstate"

/** If using VSCode, install the XState VSCode extension to get a
* "Open Inspector" link above each machine in your code that will
* visualize the machine. Otherwise, you can paste code into stately.ai/viz.
*/

interface WorkspaceContext {
errorMessage?: string
}

type WorkspaceEvent = { type: "START" } | { type: "STOP" } | { type: "REBUILD" }

export const workspaceModel = createMachine<WorkspaceContext, WorkspaceEvent>({
id: "workspaceV2Model",
initial: "off",
states: {
off: {
on: {
START: "starting",
},
},
starting: {
invoke: {
src: "buildWorkspace",
onDone: "running",
onError: "error",
},
},
running: {
on: {
STOP: "stopping",
REBUILD: "rebuilding",
},
},
stopping: {
invoke: {
src: "stopWorkspace",
onDone: "off",
onError: "error",
},
},
rebuilding: {
invoke: {
src: "stopAndStartWorkspace",
onDone: "running",
onError: "error",
},
},
error: {
entry: "saveErrorMessage",
},
},
})

export const provisionerModel = createMachine({
id: "provisionerModel",
initial: "starting",
context: {
automator: undefined,
supportedProjects: [],
},
states: {
starting: {
on: {
PROVISION: "provisioning",
PARSE: "parsing",
},
},
provisioning: {
invoke: {
src: "provision",
onDone: "off",
onError: "off",
},
},
parsing: {
invoke: {
src: "parse",
onDone: "off",
onError: "off",
},
},
off: {
type: "final",
},
},
})

export const provisionerDaemonModel = createMachine(
{
id: "provisionerd",
initial: "polling",
states: {
polling: {
on: {
JOB_HAS_PROVISIONER_REGISTERED: "executing",
NO_JOB_READY: "polling",
},
},
executing: {
invoke: {
id: "callProvisionerWithParameters",
src: "provisionerModel",
onDone: { target: "polling", actions: ["returnState", "parseProjectCode"] },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What returnState does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not implemented since this is for diagramming purposes, but it represents the fact that when provisionerd calls a provisioner and the provisioner executes the code, the resulting state is returned.

onError: { target: "polling", actions: ["returnState", "markJobFailed"] },
},
},
},
},
{
services: { provisionerModel },
},
)
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,37 @@
# yarn lockfile v1


"@xstate/react@^1.6.3":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@xstate/react/-/react-1.6.3.tgz#706f3beb7bc5879a78088985c8fd43b9dab7f725"
integrity sha512-NCUReRHPGvvCvj2yLZUTfR0qVp6+apc8G83oXSjN4rl89ZjyujiKrTff55bze/HrsvCsP/sUJASf2n0nzMF1KQ==
dependencies:
use-isomorphic-layout-effect "^1.0.0"
use-subscription "^1.3.0"

object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=

[email protected]:
version "2.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==

use-isomorphic-layout-effect@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/use-isomorphic-layout-effect/-/use-isomorphic-layout-effect-1.1.1.tgz#7bb6589170cd2987a152042f9084f9effb75c225"
integrity sha512-L7Evj8FGcwo/wpbv/qvSfrkHFtOpCzvM5yl2KVyDJoylVuSvzphiiasmjgQPttIGBAy2WKiBNR98q8w7PiNgKQ==

use-subscription@^1.3.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/use-subscription/-/use-subscription-1.5.1.tgz#73501107f02fad84c6dd57965beb0b75c68c42d1"
integrity sha512-Xv2a1P/yReAjAbhylMfFplFKj9GssgTwN7RlcTxBujFQcloStWNDQdc4g4NRWH9xS4i/FDk04vQBptAXoF3VcA==
dependencies:
object-assign "^4.1.1"

xstate@^4.27.0:
version "4.27.0"
resolved "https://registry.yarnpkg.com/xstate/-/xstate-4.27.0.tgz#f3c918ac4229bd5e6dec2231e991ba55c6bfa559"
integrity sha512-ohOwDM9tViC/zSSmY9261CHblDPqiaAk5vyjVbi69uJv9fGWMzlm0VDQwM2OvC61GKfXVBeuWSMkL7LPUsTpfA==