Comparing version 6.0.2 to 7.0.0
@@ -104,3 +104,3 @@ /** | ||
// (i.e. so node callback expectations are fulfilled) | ||
if (exitName === (machine.catchallExit||'error')) { | ||
if (exitName === 'error') { | ||
voided = false; | ||
@@ -107,0 +107,0 @@ value = typeof value === 'undefined' ? new Error('Unexpected error occurred while running machine.') : value; |
@@ -26,2 +26,7 @@ /** | ||
// If nothing was provided, build a no-op machine. | ||
if (!machineDefinition) { | ||
return Machine.buildNoopMachine(); | ||
} | ||
// Understand functions and wrap them automatically | ||
@@ -96,70 +101,36 @@ if (_.isFunction(machineDefinition)) { | ||
// Be nice and allow "catchAllExit" (with the "All" capitalized) | ||
if (machineDefinition.catchAllExit) { | ||
machineDefinition.catchallExit = machineDefinition.catchAllExit; | ||
delete machineDefinition.catchAllExit; | ||
} | ||
// Ensure error exit exists | ||
if (!machineDefinition.catchallExit){ | ||
machineDefinition.catchallExit = 'error'; | ||
} | ||
if (!machineDefinition.exits[machineDefinition.catchallExit]){ | ||
machineDefinition.exits[machineDefinition.catchallExit] = { description: 'An unexpected error occurred.' }; | ||
} | ||
// If the default exit is defined as a non-existent exit, throw an error | ||
if (machineDefinition.defaultExit && !machineDefinition.exits[machineDefinition.defaultExit]) { | ||
err.machine = machineDefinition.identity; | ||
err.code = 'MACHINE_DEFAULT_EXIT_INVALID'; | ||
err.message = util.format( | ||
// If the catchall exit is defined and not equal to "error", fail with a deprecation error. | ||
if ( | ||
// Be nice and allow "catchAllExit" (with the "All" capitalized) | ||
(machineDefinition.catchAllExit && machineDefinition.catchAllExit !== 'error') || | ||
(machineDefinition.catchallExit && machineDefinition.catchallExit !== 'error') | ||
) { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine from the specified machine definition.\n'+ | ||
'The `defaultExit` property was set to %s, but no such exit is defined.', | ||
machineDefinition.defaultExit); | ||
throw err; | ||
} | ||
// If the catchall exit is defined as a non-existent exit, throw an error | ||
if (machineDefinition.catchallExit && !machineDefinition.exits[machineDefinition.catchallExit]) { | ||
'The `defaultExit` setting (%s) was deprecated in machine@7.0.0. The default exit should always be `success`.', | ||
machineDefinition.catchAllExit||machineDefinition.catchallExit)); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'MACHINE_CATCHALL_EXIT_INVALID'; | ||
err.message = util.format( | ||
'Failed to instantiate machine from the specified machine definition.\n'+ | ||
'The `catchallExit` property was set to %s, but no such exit is defined.', | ||
machineDefinition.catchallExit); | ||
err.code = 'CATCHALL_EXIT_SETTING_DEPRECATED'; | ||
throw err; | ||
} | ||
if (machineDefinition.defaultExit && machineDefinition.defaultExit != 'success' && machineDefinition.exits.success) { | ||
err.machine = machineDefinition.identity; | ||
err.code = 'MACHINE_DEFAULT_EXIT_INVALID'; | ||
err.message = util.format( | ||
// If the default exit is defined and not equal to "success", fail with a deprecation error. | ||
if (machineDefinition.defaultExit && machineDefinition.defaultExit !== 'success') { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine from the specified machine definition.\n'+ | ||
'The `defaultExit` property was set to %s, but a `success` exit was also specified.\n'+ | ||
'If a `success` exit is configured explicitly, it _must_ be the default exit.', | ||
machineDefinition.defaultExit); | ||
throw err; | ||
} | ||
if (machineDefinition.catchallExit && machineDefinition.catchallExit != 'error' && machineDefinition.exits.error) { | ||
'The `defaultExit` setting (%s) was deprecated in machine@7.0.0. The default exit should always be `success`.', | ||
machineDefinition.defaultExit)); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'MACHINE_DEFAULT_EXIT_INVALID'; | ||
err.message = util.format( | ||
'Failed to instantiate machine from the specified machine definition.\n'+ | ||
'The `catchallExit` property was set to %s, but an `error` exit was also specified.\n'+ | ||
'If an `error` exit is configured explicitly, it _must_ be the catchall exit.', | ||
machineDefinition.catchallExit); | ||
err.code = 'DEFAULT_EXIT_SETTING_DEPRECATED'; | ||
throw err; | ||
} | ||
// If a default exit is not set and there's no "success" exit, warn that the catchall will be used | ||
if (!machineDefinition.defaultExit && !machineDefinition.exits.success) { | ||
err.machine = machineDefinition.identity; | ||
// console.warn("No defaultExit set; defaulting to the catchall exit."); | ||
// Ensure "error" exists in the machine def. | ||
if (!machineDefinition.exits.error){ | ||
machineDefinition.exits.error = { description: 'An unexpected error occurred.' }; | ||
} | ||
// If a catchall exit is not set and there's no "error" exit, warn that this machine may throw | ||
if (!machineDefinition.catchallExit && !machineDefinition.exits.error) { | ||
err.machine = machineDefinition.identity; | ||
// console.warn("No catchallExit set; if an uncaught exception is thrown or `exits(err)` is called, this machine will throw!"); | ||
// Ensure "success" exists in the machine def. | ||
if (!machineDefinition.exits.success){ | ||
machineDefinition.exits.success = { description: 'Done.' }; | ||
} | ||
@@ -220,7 +191,4 @@ | ||
_callableMachineWrapper.variableName = machineDefinition.variableName; | ||
_callableMachineWrapper.defaultExit = machineDefinition.defaultExit; | ||
_callableMachineWrapper.catchallExit = machineDefinition.catchallExit; | ||
// Last but not least, inject an `.inspect` method to provide usage info | ||
@@ -227,0 +195,0 @@ _callableMachineWrapper.inspect = require('./build-inspect-fn')(_callableMachineWrapper); |
@@ -13,2 +13,10 @@ /** | ||
* | ||
* ---------------------------------------------------------------------------------------- | ||
* Note that the API for this constructor is private, and it should not be called | ||
* directly from userland. Instead use the `Machine.build()` static method to construct | ||
* machine instances. | ||
* ---------------------------------------------------------------------------------------- | ||
* | ||
* @private | ||
* | ||
* @optional {Object} machineDefinition | ||
@@ -28,40 +36,11 @@ * • defaults to an anonymous "noop" machine definition which, when | ||
* | ||
* ---------------------------------------------------------------------------------------- | ||
* Note that the API for this constructor is private, and it should not be called | ||
* directly from userland. Instead use the `Machine.build()` static method to construct | ||
* construct callable machines. | ||
* ---------------------------------------------------------------------------------------- | ||
*/ | ||
function Machine (machineDefinition) { | ||
if (!machineDefinition) return Machine.buildNoopMachine(); | ||
// Ensure input and exit schemas are defined | ||
machineDefinition.inputs = machineDefinition.inputs||{}; | ||
machineDefinition.exits = machineDefinition.exits||{}; | ||
// Fold in the rest of the provided `machineDefinition` | ||
_.extend(this, machineDefinition); | ||
var self = this; | ||
// Ensure defaultExit is set and that default and error exits exist in the machine def. | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
//////////////////////////////////////////////////////////////////////////////////// | ||
// but these cause issues: | ||
// if (!this.defaultExit){ | ||
// this.defaultExit = 'success'; | ||
// } | ||
// if (!this.exits[this.defaultExit]){ | ||
// this.exits[this.defaultExit] = { description: 'Done.' }; | ||
// } | ||
//////////////////////////////////////////////////////////////////////////////////// | ||
if (!this.catchallExit){ | ||
this.catchallExit = 'error'; | ||
} | ||
if (!this.exits[this.catchallExit]){ | ||
this.exits[this.catchallExit] = { description: 'An unexpected error occurred.' }; | ||
} | ||
// | ||
@@ -78,3 +57,9 @@ // Initialize private state for this machine instance | ||
// the runtime environment variables configured so far | ||
this._configuredEnvironment = {}; | ||
// (always provide `self` (the current machine) and `Machine` the runner) | ||
this._configuredEnvironment = { | ||
thisMachine: function (){ | ||
return (self.constructor.build(self)).apply(self, Array.prototype.slice.call(arguments)); | ||
}, | ||
build: this.constructor.build | ||
}; | ||
@@ -111,3 +96,2 @@ // TODO: detail the keys which end up in here | ||
// Should only be used for debugging for now.) | ||
var self = this; | ||
this._onInvoke = function defaultOnInvoke (msElapsed) { | ||
@@ -134,4 +118,2 @@ /** | ||
} | ||
@@ -224,74 +206,27 @@ | ||
/** | ||
* @param {Object} configuredExits | ||
* @param {Object} callbacks | ||
* @chainable | ||
*/ | ||
Machine.prototype.setExits = function (configuredExits) { | ||
var self = this; | ||
Machine.prototype.setExits = function (callbacks) { | ||
// If we have a catchall exit that's not "error", make an "error" exit | ||
// that references the catchall | ||
if (self.catchallExit && self.catchallExit != 'error') { | ||
// (here for backwards-compatibility) | ||
configuredExits.error = configuredExits[self.catchallExit]; | ||
} | ||
// If we have a default exit that's not "success", make a "success" exit | ||
// that references the default | ||
if (self.defaultExit && self.defaultExit != 'success') { | ||
configuredExits.success = configuredExits[self.defaultExit]; | ||
// Handle callback function | ||
if (_.isFunction(callbacks)) { | ||
this._configuredExits.success = function (result){ | ||
return callbacks(null, result); | ||
}; | ||
this._configuredExits.error = function (err){ | ||
return callbacks(err); | ||
}; | ||
} | ||
// If we don't have a default exit OR an explicit "success" in our schema, | ||
// hook the `success` exit up so it calls whatever's configured as our `error` callback | ||
// (NOTE: this will not help if no `error` callback is configured!!) | ||
else if (!self.defaultExit && !self.exits.success) { | ||
configuredExits.success = configuredExits.error; | ||
else if (!_.isObject(callbacks)) { | ||
throw new Error('Machine must be configured with a single callback or an object of exit callbacks- not:'+configuredExits); | ||
} | ||
///////////////////////////////////////////////////////////////////////////////////// | ||
// Support traditional callback usage by setting up an "exit forwarding" dictionary | ||
// for use by the switchback module. | ||
// | ||
// TODO: remove the need for this stuff | ||
///////////////////////////////////////////////////////////////////////////////////// | ||
var forwards = {}; | ||
if (self.defaultExit && self.defaultExit !== 'success') { | ||
// Special case for a defaultExit which is !== "success" | ||
forwards[self.defaultExit] = 'success'; | ||
// Handle exits obj | ||
else { | ||
_.extend(this._configuredExits, callbacks); | ||
} | ||
if (self.catchallExit && self.catchallExit !== 'error') { | ||
// Special case for a catchallExit which is !== "error" | ||
// (here for backwards-compatibility) | ||
forwards[self.catchallExit] = 'error'; | ||
} | ||
// Now make any user-provided callbacks (with names which are known in the exit schema) | ||
// forward to the provided callback named "error" | ||
// (unless they already have handlers bound) | ||
// _.each(self.exits, function (exitDef, exitName){ | ||
// // Ignore catch-all exit and default exit | ||
// if (exitName === (self.catchallExit||'error') || exitName === (self.defaultExit||'success')) { | ||
// return; | ||
// } | ||
// if (!configuredExits[exitName]) { | ||
// forwards[exitName] = (self.catchallExit||'error'); | ||
// } | ||
// }); | ||
///////////////////////////////////////////////////////////////////////////////////// | ||
return this; | ||
// Create the ***USERLAND*** switchback | ||
// (this takes the callback function or object of callback functions provided by the user | ||
// of this machine and upgrades it to a switchback-- this could probably be done without | ||
// requiring the extra switchback, but this doesn't hurt anything, and helps ensure there | ||
// won't be any throwing or anything like that.) | ||
// | ||
// TODO: remove the need for this | ||
var switchbackifiedExits = switchback(configuredExits, forwards, undefined, true); | ||
// Mix in the switchback-ified exits into the exits stored within | ||
// this configured machine instance. | ||
_.extend(self._configuredExits, switchbackifiedExits); | ||
return self; | ||
}; | ||
@@ -298,0 +233,0 @@ |
@@ -133,3 +133,3 @@ /** | ||
// By default, the default (or "success") exit is cached | ||
exit: self.defaultExit||'success' | ||
exit: 'success' | ||
@@ -300,3 +300,3 @@ }); | ||
// Skip default exit and error exit | ||
if ((exitName === (self.defaultExit||'success')) || (exitName === 'error')) { | ||
if ((exitName === 'success') || (exitName === 'error')) { | ||
return; | ||
@@ -303,0 +303,0 @@ } |
@@ -64,6 +64,6 @@ /** | ||
// Apply default exit | ||
exitCallbacks[this.defaultExit||'success'] = exitCallbacks.success; | ||
exitCallbacks.success = exitCallbacks.success; | ||
// Build exit callbacks for all other exits. | ||
_.each(this.exits, function (exitDef, exitName) { | ||
if (exitName === this.defaultExit || exitName === 'success') return; | ||
if (exitName === 'success') return; | ||
exitCallbacks[exitName] = function (_data){ | ||
@@ -102,4 +102,4 @@ isMachineActuallySynchronousCv = true; | ||
// If any exit other than the `defaultExit` was traversed, throw the result from the catch-all | ||
// error exit. | ||
// If any exit other than `success` was traversed, throw the result from the | ||
// `error` exit. | ||
if (err) { | ||
@@ -111,4 +111,4 @@ if (typeof err === 'object' && err instanceof Error) throw err; | ||
// Otherwise, if the `defaultExit` was traversed, return its result value. | ||
// Otherwise, if `success` was traversed, return its result value. | ||
return result; | ||
}; |
{ | ||
"name": "machine", | ||
"version": "6.0.2", | ||
"version": "7.0.0", | ||
"description": "Configure and execute machines", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69747
1580