Skip to content

Instantly share code, notes, and snippets.

@akkys77
Last active September 28, 2020 14:53
Show Gist options
  • Select an option

  • Save akkys77/dae777dba62919bca493099d9aba5f1b to your computer and use it in GitHub Desktop.

Select an option

Save akkys77/dae777dba62919bca493099d9aba5f1b to your computer and use it in GitHub Desktop.

Revisions

  1. akkys77 revised this gist Sep 28, 2020. No changes.
  2. akkys77 revised this gist Sep 28, 2020. 1 changed file with 17 additions and 8 deletions.
    25 changes: 17 additions & 8 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,9 @@
    modelName: "Model Name",
    companyName: "Company Name",
    projectName: "Project Name",
    modelUrn: undefined
    tmpFile: undefined,
    modelUrn: undefined,
    mongoDBElementsCount: undefined
    },
    states: {
    idle: {
    @@ -33,12 +35,18 @@
    onDone: {
    target: 'uploadingToForge',
    actions: assign({
    modelUrn: (context, event) => event.data
    })
    tmpFile: (context, event) => {
    // console.log(event.data)
    // return event.data
    return `/tmp/${context.companyName} - ${context.projectName}/${context.modelName}`

    }
    }),
    message:"Uploaded to Web Back End"
    },
    onError: {
    target: 'failure',
    actions: assign({ error: (context, event) => event.data })
    actions: assign({ error: (context, event) => event.data, step: 'uploadingToWebBackEnd' })
    }
    }
    },
    @@ -48,7 +56,7 @@
    onDone: {
    target: 'uploadingToMongoDB',
    actions: assign({
    modelUrn: (context, event) => event.data
    modelUrn: (context,event) =>`urn::forge:${encodeURI(context.modelName)}`
    })
    },
    onError: {
    @@ -63,7 +71,7 @@
    onDone: {
    target: 'success',
    actions: assign({
    mongoDB: (context, event) => event.data
    mongoDBElementsCount: (context, event) => `45312`
    })
    },
    onError: {
    @@ -80,6 +88,7 @@
    on: {
    RETRY: {
    target: 'uploadingToWebBackEnd',
    // target: (context) => context.step,
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    @@ -90,8 +99,8 @@
    });

    function uploadToForge(context) {
    return Promise.resolve()
    // return {modelUrn:"ModelURN"}
    // return "urn::forge.com:Just a test"
    return setTimeout(context => Promise.resolve(context),1000)
    }

    function uploadToMongoDB(context) {
  3. akkys77 revised this gist Sep 28, 2020. 1 changed file with 63 additions and 9 deletions.
    72 changes: 63 additions & 9 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -10,23 +10,67 @@
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'upload',
    const modelUploadMachine = Machine({
    id: 'modelUpload',
    initial: 'idle',
    context: {
    retries: 0,
    step: "initial"
    step: "initial",
    modelName: "Model Name",
    companyName: "Company Name",
    projectName: "Project Name",
    modelUrn: undefined
    },
    states: {
    idle: {
    on: {
    UPLOADING: 'uploaded'
    UPLOADING: 'uploadingToWebBackEnd'
    }
    },
    uploaded: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    uploadingToWebBackEnd: {
    invoke: {
    // src: uploadToForge,
    onDone: {
    target: 'uploadingToForge',
    actions: assign({
    modelUrn: (context, event) => event.data
    })
    },
    onError: {
    target: 'failure',
    actions: assign({ error: (context, event) => event.data })
    }
    }
    },
    uploadingToForge: {
    invoke: {
    src: uploadToForge,
    onDone: {
    target: 'uploadingToMongoDB',
    actions: assign({
    modelUrn: (context, event) => event.data
    })
    },
    onError: {
    target: 'failure',
    actions: assign({ error: (context, event) => event.data })
    }
    }
    },
    uploadingToMongoDB: {
    invoke: {
    src: uploadToMongoDB,
    onDone: {
    target: 'success',
    actions: assign({
    mongoDB: (context, event) => event.data
    })
    },
    onError: {
    target: 'failure',
    actions: assign({ error: (context, event) => event.data })
    }

    }
    },
    success: {
    @@ -35,7 +79,7 @@
    failure: {
    on: {
    RETRY: {
    target: 'uploaded',
    target: 'uploadingToWebBackEnd',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    @@ -44,4 +88,14 @@
    }
    }
    });

    function uploadToForge(context) {
    return Promise.resolve()
    // return {modelUrn:"ModelURN"}
    }

    function uploadToMongoDB(context) {
    // return Promise.resolve()
    return setTimeout(context => Promise.resolve(context),1000)
    }

  4. akkys77 created this gist Sep 28, 2020.
    47 changes: 47 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@

    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'upload',
    initial: 'idle',
    context: {
    retries: 0,
    step: "initial"
    },
    states: {
    idle: {
    on: {
    UPLOADING: 'uploaded'
    }
    },
    uploaded: {
    on: {
    RESOLVE: 'success',
    REJECT: 'failure'
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    on: {
    RETRY: {
    target: 'uploaded',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });