1.1.0 • Published 5 years ago
@tawaship/task v1.1.0
@tawaship/task
Process tasks serially.
How to install
- on you npm project
npm install --save @tawaship/task- 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 buildHow to load on browser
<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); // 2Chaining
const task = new Task([
	function() {
		return task.next().done();
	},
	function() {
		return 3;
	}
]);
console.log(task.done()); // 3Change 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()); // 3Disabled
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()); // undefinedAdd
const task = new Task([
	function() {
		return 1
	}
])
.add(function() {
	return 2;
});
console.log(task.next().done()); // 2Reset
const task = new Task([
	function() {
		return 1
	}
])
.reset();
console.log(task.first().done()); // undefinedSend 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); // 2Specify 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()); // WindowFinish
let a = {hoge: 1};
const task = new Task(
	function() {
		this.moga = 2;
		return this;
	}
, a);
task.first().finish().done();
console.log(a); // {hoge: 1}