From ea170c747756b4a54c3106a8c42383ac5c5e4e44 Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Wed, 9 May 2018 23:42:59 -0500 Subject: [PATCH] spec out handleUndo --- machine-def.struct.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/machine-def.struct.js b/machine-def.struct.js index 5e6f82c..fa3d7cf 100644 --- a/machine-def.struct.js +++ b/machine-def.struct.js @@ -12,13 +12,35 @@ module.exports = { moreInfoUrl: 'http://hello.com', - sideEffects: 'idempotent', // either omit or set as "cacheable" or "idempotent" + habitat: 'sails', // either omit or set as "request", "sails", "node", or "browser". - habitat: 'sails', // either omit or set as "request" or "sails" + //sync: true, // either omit or set as `true` - sync: true, // either omit or set as `true` + // sideEffects: 'idempotent', // either omit or set as "cacheable" or "idempotent" + + + handleUndo: async function(inputs){ // either omit or set as a function + // ^^ should never be defined for a "cacheable" machine + + // Reverse `fn`'s side effect. + // (Note that this is not a GLOBAL UNDO!!! In this example, this code could + // not attempt to handle any changes that might have befallen the global var + // from other logic. It can really only attempt to reverse the side effects + // of running `fn` with these inputs, and even then only assuming `fn` + // triggered its success exit.) + + // Realistically, it's not super possible w/ this example, but we can at + // least fudge it a little bit: + if (global.foo) { + // if we knew how much to subtract, we could do that here, for example + } + + // (In reality, this example isn't really reversible, so we would not + // want to define a handleUndo function at all!) + + }, inputs: { @@ -35,15 +57,18 @@ module.exports = { }, - fn: function (inputs, exits) { + fn: async function (inputs, exits) { + // Import any dependencies here var _ = require('@sailshq/lodash'); + // Do asynchronous things with callbacks if you like + // (or better yet, use `await`!) setTimeout(function (){ try { - var luckyNum = Math.random(); + let luckyNum = Math.random(); if (luckyNum > 0.5) { throw new Error('whatever'); } @@ -62,6 +87,9 @@ module.exports = { // --• OK so if we made it here, `luckyNum` must be between 0.1 and 0.5 (exclusive). + // Maybe do some kind of side effect. + global.foo = (global.foo || 0) + luckyNum; + // Exit `success` with no output. return exits.success(); // > NOTE: