Skip to content

Commit

Permalink
Remove task event
Browse files Browse the repository at this point in the history
  • Loading branch information
pyldin601 committed Feb 5, 2018
1 parent 9aa0e4d commit 6dc493e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ 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 }),
async ({ value, threshold }) => {
incrementJob.emit('tick', value);
return value < threshold
? repeat({ value: value + 1, threshold })
: next({ value, threshold });
},
]);

module.exports = incrementJob;
Expand All @@ -61,7 +64,7 @@ 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('tick', value => console.log('Current value is %d', value));
incrementJob.on('done', () => console.log('Job finished'));

incrementJob
Expand Down
23 changes: 7 additions & 16 deletions __tests__/runner.test.js → __test__/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('Runner tests', () => {
test('Test event emitter (start)', async () => {
const onStart = jest.fn();
const onResume = jest.fn();
const onTask = jest.fn();
const onDone = jest.fn();

const job = new LongLongJob('test-job-6', [
Expand All @@ -79,7 +78,6 @@ describe('Runner tests', () => {

job.on('start', onStart);
job.on('resume', onResume);
job.on('task', onTask);
job.on('done', onDone);

expect(await job.start(initialState)).toEqual(30);
Expand All @@ -89,10 +87,6 @@ describe('Runner tests', () => {

expect(onResume.mock.calls.length).toBe(0);

expect(onTask.mock.calls.length).toBe(2);
expect(onTask.mock.calls[0]).toEqual([0, 5]);
expect(onTask.mock.calls[1]).toEqual([1, 15]);

expect(onDone.mock.calls.length).toBe(1);
expect(onDone.mock.calls[0]).toEqual([30]);
});
Expand All @@ -102,7 +96,6 @@ describe('Runner tests', () => {

const onStart = jest.fn();
const onResume = jest.fn();
const onTask = jest.fn();
const onDone = jest.fn();

const job = new LongLongJob('test-job-7', [
Expand All @@ -113,7 +106,6 @@ describe('Runner tests', () => {

job.on('start', onStart);
job.on('resume', onResume);
job.on('task', onTask);
job.on('done', onDone);

expect(await job.start(initialState)).toEqual(30);
Expand All @@ -123,9 +115,6 @@ describe('Runner tests', () => {
expect(onResume.mock.calls.length).toBe(1);
expect(onResume.mock.calls[0]).toEqual([]);

expect(onTask.mock.calls.length).toBe(1);
expect(onTask.mock.calls[0]).toEqual([1, 15]);

expect(onDone.mock.calls.length).toBe(1);
expect(onDone.mock.calls[0]).toEqual([30]);
});
Expand All @@ -135,10 +124,12 @@ describe('Runner tests', () => {
async ({ initial }) => goto('inc', { current: initial, threshold: initial + 10 }),

label('inc'),
async ({ current, threshold }) =>
current < threshold
async ({ current, threshold }) => {
job.emit('tick', current);
return current < threshold
? repeat({ current: current + 1, threshold })
: goto('dec', { current, threshold: current - 8 }),
: goto('dec', { current, threshold: current - 8 });
},

label('dec'),
async ({ current, threshold }) =>
Expand All @@ -147,8 +138,8 @@ describe('Runner tests', () => {
: goto('inc', { current, threshold: current + 12 }),
]);

job.on('task', (cursor, state) => {
if (state.current > 50) {
job.on('tick', (current) => {
if (current > 50) {
job.terminate();
}
});
Expand Down
9 changes: 6 additions & 3 deletions example/incrementJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ const LongLongJob = require('./LongLongJob');
const { repeat, next } = require('../dist/');

const incrementJob = new LongLongJob('increment-job', [
async ({ value, threshold }) => value < threshold
? repeat({ value: value + 1, threshold })
: next({ value, threshold }),
async ({ value, threshold }) => {
incrementJob.emit('tick', value);
return value < threshold
? repeat({ value: value + 1, threshold })
: next({ value, threshold });
},
]);

module.exports = incrementJob;
2 changes: 1 addition & 1 deletion example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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('tick', value => console.log('Current value is %d', value));
incrementJob.on('done', () => console.log('Job finished'));

incrementJob
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "long-long-job",
"version": "0.1.4",
"version": "1.0.0",
"description": "Library for execution of long jobs with resume support.",
"main": "dist/index.js",
"engineStrict": true,
Expand Down
6 changes: 2 additions & 4 deletions src/LongLongJob.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @flow
import EventEmitter from 'events';
import type { StateService, TaskUnit, TaskState } from './types';
import type { StateService, TaskUnit, TaskState, ILongLongJob } from './types';
import { Next, Goto, Repeat } from './actions';
import { groupTaskUnits } from './util';

export default (stateService: StateService) =>
class LongLongJob<In, Out> extends EventEmitter {
class LongLongJob<In, Out> extends EventEmitter implements ILongLongJob<In, Out> {
id: string;
tasks: TaskUnit<any>[];
isRunning: boolean;
Expand Down Expand Up @@ -39,8 +39,6 @@ export default (stateService: StateService) =>
throw new Error('Job terminated');
}

this.emit('task', cursor, state);

const action = await tasks[cursor](state);

if (action instanceof Next) {
Expand Down
5 changes: 5 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export type StateService = {
clean(id: string): Promise<void>,
};

export interface ILongLongJob<In, Out> {
start(initialState: In): Promise<Out>;
terminate(): void;
}

export type TaskState<S> = { cursor: number, state: S };

export type TaskAction<S> = (state: S) => Promise<Action<S>>;
Expand Down

0 comments on commit 6dc493e

Please sign in to comment.