long-long-job
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -70,3 +70,3 @@ // @flow | ||
const onStart = jest.fn(); | ||
const onContinue = jest.fn(); | ||
const onResume = jest.fn(); | ||
const onTask = jest.fn(); | ||
@@ -82,3 +82,3 @@ const onDone = jest.fn(); | ||
job.on('start', onStart); | ||
job.on('continue', onContinue); | ||
job.on('resume', onResume); | ||
job.on('task', onTask); | ||
@@ -92,3 +92,3 @@ job.on('done', onDone); | ||
expect(onContinue.mock.calls.length).toBe(0); | ||
expect(onResume.mock.calls.length).toBe(0); | ||
@@ -107,3 +107,3 @@ expect(onTask.mock.calls.length).toBe(2); | ||
const onStart = jest.fn(); | ||
const onContinue = jest.fn(); | ||
const onResume = jest.fn(); | ||
const onTask = jest.fn(); | ||
@@ -119,3 +119,3 @@ const onDone = jest.fn(); | ||
job.on('start', onStart); | ||
job.on('continue', onContinue); | ||
job.on('resume', onResume); | ||
job.on('task', onTask); | ||
@@ -128,4 +128,4 @@ job.on('done', onDone); | ||
expect(onContinue.mock.calls.length).toBe(1); | ||
expect(onContinue.mock.calls[0]).toEqual([]); | ||
expect(onResume.mock.calls.length).toBe(1); | ||
expect(onResume.mock.calls[0]).toEqual([]); | ||
@@ -132,0 +132,0 @@ expect(onTask.mock.calls.length).toBe(1); |
{ | ||
"name": "long-long-job", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Library for execution of long jobs with resume support.", | ||
@@ -31,2 +31,3 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"start": "node example/main.js", | ||
"clean": "rm -rf coverage dist tmp", | ||
@@ -42,4 +43,4 @@ "lint": "eslint .", | ||
"prebuild": "npm run clean", | ||
"build": "babel src/**/*.js -d dist", | ||
"build:watch": "babel --watch src/**/*.js -d dist", | ||
"build": "babel src -d dist", | ||
"build:watch": "babel --watch src/**.js -d dist", | ||
"postinstall": "flow-typed install --skip", | ||
@@ -46,0 +47,0 @@ "prepublish": "npm run build" |
@@ -12,1 +12,60 @@ # long-long-job | ||
## Example | ||
First of all we need to define where job's state will be stored during transitions. Then we must initialize module with it. | ||
For example, we will store state in `Map` object. This is simplest storage method and it won't preserve state during restarts. | ||
### `LongLongJob.js` | ||
```javascript | ||
const { longLongJob } = require('long-long-job'); | ||
const mapBasedStorage = () => { | ||
const stateStore = new Map(); | ||
return { | ||
async hasState(id) { | ||
return stateStore.has(id); | ||
}, | ||
async setState(id, state) { | ||
stateStore.set(id, state); | ||
}, | ||
async getState(id) { | ||
return stateStore.get(id); | ||
}, | ||
async clean(id) { | ||
stateStore.delete(id); | ||
}, | ||
}; | ||
}; | ||
module.exports = longLongJob(mapBasedStorage()); | ||
``` | ||
### incrementJob.js | ||
```javascript | ||
const LongLongJob = require('./LongLongJob'); | ||
const { repeat, next } = require('long-long-job'); | ||
const incrementJob = new LongLongJob('increment-job', [ | ||
async ({ value, threshold }) => value < threshold | ||
? repeat({ value: value + 1, threshold }) | ||
: next({ value, threshold }), | ||
]); | ||
module.exports = incrementJob; | ||
``` | ||
### main.js | ||
```javascript | ||
const incrementJob = require('./incrementJob'); | ||
incrementJob.on('start', () => console.log('Job started')); | ||
incrementJob.on('resume', () => console.log('Job resumed')); | ||
incrementJob.on('task', (cursor, { value }) => console.log('Current value is %d', value)); | ||
incrementJob.on('done', () => console.log('Job finished')); | ||
incrementJob | ||
.start({ value: 0, threshold: 1000 }) | ||
.then(({ value }) => console.log('Task is done with value %d', value)); | ||
``` |
// @flow | ||
import { next, repeat, goto, label } from './helpers'; | ||
import longLongJob from './LongLongJob'; | ||
export { next, repeat, goto, label }; | ||
export { next, repeat, goto, label, longLongJob }; | ||
@@ -29,3 +29,3 @@ // @flow | ||
if (await this.hasStoredState()) { | ||
this.emit('continue'); | ||
this.emit('resume'); | ||
} else { | ||
@@ -79,3 +79,3 @@ this.emit('start'); | ||
const taskState = await stateService.getState(this.id); | ||
if (taskState === null) { | ||
if (taskState === null || taskState === undefined) { | ||
return { cursor: 0, state: initialState }; | ||
@@ -82,0 +82,0 @@ } |
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
23132
35
511
71