1.1.0 • Published 3 years ago

@tawaship/task v1.1.0

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

@tawaship/task

Process tasks serially.

Build Status MIT License


How to install

  1. on you npm project
npm install --save @tawaship/task
  1. on your code
import { Task } from '@tawaship/task';

// or

const { Task } = require('@tawaship/task');

How to build

git clone https://github.com/tawaship/Task

cd Emitter

npm install

npm run build

How to load on browser

After install or build

<script src="/path/to/dist/Task.min.js"></script>

Usage

Single

let a = 0;
const task = new Task(
	function() {
		a++;
	}
);

task.done();
console.log(a); // 1

task.done();
console.log(a); // 2

Chaining

const task = new Task([
	function() {
		return task.next().done();
	},
	function() {
		return 3;
	}
]);

console.log(task.done()); // 3

Change position

const task = new Task([
	function() {
		return 1
	},
	function() {
		return 2;
	},
	function() {
		return 3;
	}
]);

console.log(task.first().done()); // 1
console.log(task.next().done()); // 2
console.log(task.prev().done()); // 1
console.log(task.to(2).done()); // 3

Disabled

const task = new Task([
	function() {
		return 1
	},
	function() {
		return 2;
	},
	function() {
		return 3;
	},
	function() {
		return 4;
	}
]);

task.enabled = false;
console.log(task.first().done()); // undefined
console.log(task.next().done()); // undefined
console.log(task.prev().done()); // undefined
console.log(task.to(2).done()); // undefined

Add

const task = new Task([
	function() {
		return 1
	}
])
.add(function() {
	return 2;
});

console.log(task.next().done()); // 2

Reset

const task = new Task([
	function() {
		return 1
	}
])
.reset();

console.log(task.first().done()); // undefined

Send arguments

const task = new Task(
	function(a, b, c) {
		console.log(a, b, c); // 1 2 3
	}
);
task.done(1, 2, 3);

Returned value

const task = new Task([
	function() {
		return 1;
	},
	function() {
		return 2;
	}
]);
task.first().done();
console.log(task.value); // 1

task.next().done();
console.log(task.value); // 2

Specify the context

let a = {hoge: 1};
const task = new Task(
	function() {
		this.moga = 2;
		return this;
	}
, a);

console.log(task.done()); // { hoge: 1, moga: 2 }

However, using the arrow function invalidates the context specification.

// on window
let a = {hoge: 1};
const task = new Task(
	() => {
		return this;
	}
, a);

console.log(task.done()); // Window

Finish

let a = {hoge: 1};
const task = new Task(
	function() {
		this.moga = 2;
		return this;
	}
, a);
task.first().finish().done();
console.log(a); // {hoge: 1}