long-long-job
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -6,8 +6,15 @@ "use strict"; | ||
}); | ||
class Action { | ||
constructor(state) { | ||
this.state = state; | ||
} | ||
} | ||
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Action = function Action(state) { | ||
(0, _classCallCheck3.default)(this, Action); | ||
this.state = state; | ||
}; | ||
exports.default = Action; |
@@ -7,15 +7,35 @@ 'use strict'; | ||
var _Action = require('./Action'); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _Action2 = _interopRequireDefault(_Action); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _Action2 = require('./Action'); | ||
var _Action3 = _interopRequireDefault(_Action2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class Goto extends _Action2.default { | ||
var Goto = function (_Action) { | ||
(0, _inherits3.default)(Goto, _Action); | ||
constructor(label, state) { | ||
super(state); | ||
this.label = label; | ||
function Goto(label, state) { | ||
(0, _classCallCheck3.default)(this, Goto); | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (Goto.__proto__ || Object.getPrototypeOf(Goto)).call(this, state)); | ||
_this.label = label; | ||
return _this; | ||
} | ||
} | ||
return Goto; | ||
}(_Action3.default); | ||
exports.default = Goto; |
@@ -7,9 +7,31 @@ 'use strict'; | ||
var _Action = require('./Action'); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _Action2 = _interopRequireDefault(_Action); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _Action2 = require('./Action'); | ||
var _Action3 = _interopRequireDefault(_Action2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class Next extends _Action2.default {} | ||
var Next = function (_Action) { | ||
(0, _inherits3.default)(Next, _Action); | ||
function Next() { | ||
(0, _classCallCheck3.default)(this, Next); | ||
return (0, _possibleConstructorReturn3.default)(this, (Next.__proto__ || Object.getPrototypeOf(Next)).apply(this, arguments)); | ||
} | ||
return Next; | ||
}(_Action3.default); | ||
exports.default = Next; |
@@ -7,9 +7,31 @@ 'use strict'; | ||
var _Action = require('./Action'); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _Action2 = _interopRequireDefault(_Action); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _Action2 = require('./Action'); | ||
var _Action3 = _interopRequireDefault(_Action2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class Repeat extends _Action2.default {} | ||
var Repeat = function (_Action) { | ||
(0, _inherits3.default)(Repeat, _Action); | ||
function Repeat() { | ||
(0, _classCallCheck3.default)(this, Repeat); | ||
return (0, _possibleConstructorReturn3.default)(this, (Repeat.__proto__ || Object.getPrototypeOf(Repeat)).apply(this, arguments)); | ||
} | ||
return Repeat; | ||
}(_Action3.default); | ||
exports.default = Repeat; |
@@ -6,8 +6,15 @@ "use strict"; | ||
}); | ||
class Label { | ||
constructor(label) { | ||
this.label = label; | ||
} | ||
} | ||
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Label = function Label(label) { | ||
(0, _classCallCheck3.default)(this, Label); | ||
this.label = label; | ||
}; | ||
exports.default = Label; |
@@ -7,2 +7,18 @@ 'use strict'; | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _events = require('events'); | ||
@@ -18,83 +34,102 @@ | ||
exports.default = stateService => class LongLongJob extends _events2.default { | ||
exports.default = function (stateService) { | ||
return function (_EventEmitter) { | ||
(0, _inherits3.default)(LongLongJob, _EventEmitter); | ||
constructor(id, tasks) { | ||
super(); | ||
this.id = id; | ||
this.tasks = tasks; | ||
this.isRunning = false; | ||
} | ||
function LongLongJob(id, tasks) { | ||
(0, _classCallCheck3.default)(this, LongLongJob); | ||
async start(initialState) { | ||
if (this.isRunning) { | ||
throw new Error('Already started'); | ||
} | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (LongLongJob.__proto__ || Object.getPrototypeOf(LongLongJob)).call(this)); | ||
this.isRunning = true; | ||
const { labels, tasks } = (0, _util.groupTaskUnits)(this.tasks); | ||
if (await this.hasStoredState()) { | ||
this.emit('resume'); | ||
} else { | ||
this.emit('start'); | ||
_this.id = id; | ||
_this.tasks = tasks; | ||
_this.isRunning = false; | ||
return _this; | ||
} | ||
let { cursor, state } = await this.getTaskState(initialState); | ||
(0, _createClass3.default)(LongLongJob, [{ | ||
key: 'start', | ||
value: async function start(initialState) { | ||
if (this.isRunning) { | ||
throw new Error('Already started'); | ||
} | ||
while (tasks[cursor] !== undefined) { | ||
if (!this.isRunning) { | ||
throw new Error('Job terminated'); | ||
} | ||
this.isRunning = true; | ||
this.emit('task', cursor, state); | ||
var _groupTaskUnits = (0, _util.groupTaskUnits)(this.tasks), | ||
labels = _groupTaskUnits.labels, | ||
tasks = _groupTaskUnits.tasks; | ||
const action = await tasks[cursor](state); | ||
if (action instanceof _actions.Next) { | ||
cursor += 1; | ||
} else if (action instanceof _actions.Repeat) { | ||
/* Blank */ | ||
} else if (action instanceof _actions.Goto) { | ||
if (labels[action.label] === undefined) { | ||
throw new Error(`Label ${action.label} does not exist`); | ||
if (await this.hasStoredState()) { | ||
this.emit('resume'); | ||
} else { | ||
this.emit('start'); | ||
} | ||
cursor = labels[action.label]; | ||
} else { | ||
throw new Error(`Task should return an action`); | ||
} | ||
state = action.state; | ||
var _ref = await this.getTaskState(initialState), | ||
cursor = _ref.cursor, | ||
state = _ref.state; | ||
await stateService.setState(this.id, { cursor, state }); | ||
} | ||
while (tasks[cursor] !== undefined) { | ||
if (!this.isRunning) { | ||
throw new Error('Job terminated'); | ||
} | ||
this.emit('done', state); | ||
this.emit('task', cursor, state); | ||
await this.clearStoredState(); | ||
var action = await tasks[cursor](state); | ||
this.isRunning = false; | ||
if (action instanceof _actions.Next) { | ||
cursor += 1; | ||
} else if (action instanceof _actions.Repeat) { | ||
/* Blank */ | ||
} else if (action instanceof _actions.Goto) { | ||
if (labels[action.label] === undefined) { | ||
throw new Error('Label ' + action.label + ' does not exist'); | ||
} | ||
cursor = labels[action.label]; | ||
} else { | ||
throw new Error('Task should return an action'); | ||
} | ||
return state; | ||
} | ||
state = action.state; | ||
terminate() { | ||
this.isRunning = false; | ||
} | ||
await stateService.setState(this.id, { cursor: cursor, state: state }); | ||
} | ||
async getTaskState(initialState) { | ||
const taskState = await stateService.getState(this.id); | ||
if (taskState === null || taskState === undefined) { | ||
return { cursor: 0, state: initialState }; | ||
} | ||
return taskState; | ||
} | ||
this.emit('done', state); | ||
async hasStoredState() { | ||
return stateService.hasState(this.id); | ||
} | ||
await this.clearStoredState(); | ||
async clearStoredState() { | ||
await stateService.clean(this.id); | ||
} | ||
this.isRunning = false; | ||
return state; | ||
} | ||
}, { | ||
key: 'terminate', | ||
value: function terminate() { | ||
this.isRunning = false; | ||
} | ||
}, { | ||
key: 'getTaskState', | ||
value: async function getTaskState(initialState) { | ||
var taskState = await stateService.getState(this.id); | ||
if (taskState === null || taskState === undefined) { | ||
return { cursor: 0, state: initialState }; | ||
} | ||
return taskState; | ||
} | ||
}, { | ||
key: 'hasStoredState', | ||
value: async function hasStoredState() { | ||
return stateService.hasState(this.id); | ||
} | ||
}, { | ||
key: 'clearStoredState', | ||
value: async function clearStoredState() { | ||
await stateService.clean(this.id); | ||
} | ||
}]); | ||
return LongLongJob; | ||
}(_events2.default); | ||
}; |
@@ -15,15 +15,36 @@ 'use strict'; | ||
function groupTaskUnits(taskUnits) { | ||
const labels = {}; | ||
const tasks = []; | ||
var labels = {}; | ||
var tasks = []; | ||
for (const taskUnit of taskUnits) { | ||
if (taskUnit instanceof _Label2.default) { | ||
labels[taskUnit.label] = tasks.length; | ||
} else { | ||
const action = taskUnit; | ||
tasks.push(action); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = taskUnits[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var taskUnit = _step.value; | ||
if (taskUnit instanceof _Label2.default) { | ||
labels[taskUnit.label] = tasks.length; | ||
} else { | ||
var action = taskUnit; | ||
tasks.push(action); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return { labels, tasks }; | ||
return { labels: labels, tasks: tasks }; | ||
} |
{ | ||
"name": "long-long-job", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Library for execution of long jobs with resume support.", | ||
@@ -19,2 +19,3 @@ "main": "dist/index.js", | ||
"babel-plugin-transform-runtime": "~6.23.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-stage-3": "~6.24.1", | ||
@@ -21,0 +22,0 @@ "coveralls": "^3.0.0", |
Sorry, the diff of this file is not supported yet
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
28020
599
19