Skip to content

Instantly share code, notes, and snippets.

@amogower
Created February 4, 2021 18:31
Show Gist options
  • Select an option

  • Save amogower/82f9fd6b89f8fdb93e0459f237d3ec7b to your computer and use it in GitHub Desktop.

Select an option

Save amogower/82f9fd6b89f8fdb93e0459f237d3ec7b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const mapperMachine = Machine({
id: 'mapper',
initial: 'idle',
context: {
manifestHeaders: [],
manifestRows: [],
templateHeaders: [],
mappings: {}
},
states: {
idle: {
on: {
SELECT_TEMPLATE: 'active.parsing_template',
SELECT_MANIFEST: 'active.parsing_manifest'
}
},
active: {
states: {
parsing_template: {
on: {
PARSE_TEMPLATE_SUCCESS: 'template',
PARSE_TEMPLATE_FAILURE: '#mapper.error',
}
},
template: {
on: {
UPLOAD_MANIFEST: 'uploading_manifest',
}
},
parsing_manifest: {
on: {
PARSE_MANIFEST_SUCCESS: [
{ target: 'mapping' },
],
PARSE_MANIFEST_FAILURE: '#mapper.error',
}
},
mapping: {
on: {
DOWNLOAD: 'downloading',
SET_MAPPING: {
actions: assign({
mapping: (context, { key, value }) => ({
...context.mappings,
[key]: {
type: 'mapping',
value,
},
})
})
},
SET_VALUE: {
actions: assign({
mapping: (context, { key, value }) => ({
...context.mappings,
[key]: {
type: 'value',
value,
},
})
})
}
}
},
downloading: {
on: {
DOWNLOAD_SUCCESS: 'downloaded',
DOWNLOAD_FAILURE: '#mapper.error',
}
},
downloaded: {
type: 'final'
},
},
},
error: {
on: {
CLEAR: [
{ target: 'idle', cond: 'noParsedManifest' },
{ target: 'template', cond: 'noParsedTemplate' },
]
}
},
}
}, {
guards: {
noParsedManifest: (context, event) =>
context.manifestHeaders.length > 0,
noParsedTemplate: (context, event) =>
context.templateHeaders.length > 0,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment