introduce TaskOnce

This commit is contained in:
Philipp Kunz 2017-07-12 16:45:18 +02:00
parent 017c0789ca
commit a3699f2869
7 changed files with 101 additions and 4 deletions

1
dist/index.d.ts vendored
View File

@ -2,4 +2,5 @@ export { Task, ITaskFunction } from './taskbuffer.classes.task';
export { Taskchain } from './taskbuffer.classes.taskchain';
export { Taskparallel } from './taskbuffer.classes.taskparallel';
export { TaskManager } from './taskbuffer.classes.taskmanager';
export { TaskOnce } from './taskbuffer.classes.taskonce';
import './taskbuffer.classes.helpers';

4
dist/index.js vendored
View File

@ -8,6 +8,8 @@ var taskbuffer_classes_taskparallel_1 = require("./taskbuffer.classes.taskparall
exports.Taskparallel = taskbuffer_classes_taskparallel_1.Taskparallel;
var taskbuffer_classes_taskmanager_1 = require("./taskbuffer.classes.taskmanager");
exports.TaskManager = taskbuffer_classes_taskmanager_1.TaskManager;
var taskbuffer_classes_taskonce_1 = require("./taskbuffer.classes.taskonce");
exports.TaskOnce = taskbuffer_classes_taskonce_1.TaskOnce;
// import for naming only
require("./taskbuffer.classes.helpers");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHFFQUE0RDtBQUFwRCx5Q0FBQSxJQUFJLENBQUE7QUFDWiwrRUFBd0Q7QUFBaEQsbURBQUEsU0FBUyxDQUFBO0FBQ2pCLHFGQUE4RDtBQUF0RCx5REFBQSxZQUFZLENBQUE7QUFDcEIsbUZBQThEO0FBQXJELHVEQUFBLFdBQVcsQ0FBQTtBQUVwQix5QkFBeUI7QUFDekIsd0NBQXFDIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHFFQUErRDtBQUF0RCx5Q0FBQSxJQUFJLENBQUE7QUFDYiwrRUFBMEQ7QUFBakQsbURBQUEsU0FBUyxDQUFBO0FBQ2xCLHFGQUFnRTtBQUF2RCx5REFBQSxZQUFZLENBQUE7QUFDckIsbUZBQThEO0FBQXJELHVEQUFBLFdBQVcsQ0FBQTtBQUNwQiw2RUFBd0Q7QUFBL0MsaURBQUEsUUFBUSxDQUFBO0FBRWpCLHlCQUF5QjtBQUN6Qix3Q0FBcUMifQ==

11
dist/taskbuffer.classes.taskonce.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
import { Task, ITaskFunction } from './taskbuffer.classes.task';
/**
* TaskOnce is run exactly once, no matter how often it is triggered
*/
export declare class TaskOnce extends Task {
hasTriggered: boolean;
constructor(optionsArg: {
name?: string;
taskFunction: ITaskFunction;
});
}

30
dist/taskbuffer.classes.taskonce.js vendored Normal file
View File

@ -0,0 +1,30 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
/**
* TaskOnce is run exactly once, no matter how often it is triggered
*/
class TaskOnce extends taskbuffer_classes_task_1.Task {
constructor(optionsArg) {
super({
name: optionsArg.name,
taskFunction: () => __awaiter(this, void 0, void 0, function* () {
if (!this.hasTriggered) {
this.hasTriggered = true;
yield optionsArg.taskFunction();
}
})
});
this.hasTriggered = false;
}
}
exports.TaskOnce = TaskOnce;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2tvbmNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvdGFza2J1ZmZlci5jbGFzc2VzLnRhc2tvbmNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFFQSx1RUFBK0Q7QUFFL0Q7O0dBRUc7QUFDSCxjQUFzQixTQUFRLDhCQUFJO0lBRWhDLFlBQWEsVUFHWjtRQUNDLEtBQUssQ0FBQztZQUNKLElBQUksRUFBRSxVQUFVLENBQUMsSUFBSTtZQUNyQixZQUFZLEVBQUU7Z0JBQ1osRUFBRSxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQztvQkFDdkIsSUFBSSxDQUFDLFlBQVksR0FBRyxJQUFJLENBQUE7b0JBQ3hCLE1BQU0sVUFBVSxDQUFDLFlBQVksRUFBRSxDQUFBO2dCQUNqQyxDQUFDO1lBQ0gsQ0FBQyxDQUFBO1NBQ0YsQ0FBQyxDQUFBO1FBYkosaUJBQVksR0FBWSxLQUFLLENBQUE7SUFjN0IsQ0FBQztDQUNGO0FBaEJELDRCQWdCQyJ9

28
test/test.6.taskonce.ts Normal file
View File

@ -0,0 +1,28 @@
import { expect, tap } from 'tapbundle'
import * as taskbuffer from '../dist/index'
let myNumber = 0
let myTaskOnce: taskbuffer.TaskOnce
tap.test('should create a valid instance of TaskOnce', async () => {
myTaskOnce = new taskbuffer.TaskOnce({
taskFunction: async () => {
myNumber++
}
})
expect(myTaskOnce).to.be.instanceof(taskbuffer.TaskOnce)
})
tap.test('myNumber should still be 0', async () => {
expect(myNumber).to.equal(0)
})
tap.test('myTaskOnce should trigger once', async () => {
await myTaskOnce.trigger()
await myTaskOnce.trigger()
await myTaskOnce.trigger()
expect(myNumber).to.equal(1)
})
tap.start()

View File

@ -1,7 +1,8 @@
export {Task,ITaskFunction} from './taskbuffer.classes.task'
export {Taskchain} from './taskbuffer.classes.taskchain'
export {Taskparallel} from './taskbuffer.classes.taskparallel'
export { Task, ITaskFunction } from './taskbuffer.classes.task'
export { Taskchain } from './taskbuffer.classes.taskchain'
export { Taskparallel } from './taskbuffer.classes.taskparallel'
export { TaskManager } from './taskbuffer.classes.taskmanager'
export { TaskOnce } from './taskbuffer.classes.taskonce'
// import for naming only
import './taskbuffer.classes.helpers'

View File

@ -0,0 +1,24 @@
import * as plugins from './taskbuffer.plugins'
import { Task, ITaskFunction} from './taskbuffer.classes.task'
/**
* TaskOnce is run exactly once, no matter how often it is triggered
*/
export class TaskOnce extends Task {
hasTriggered: boolean = false
constructor (optionsArg: {
name?: string,
taskFunction: ITaskFunction
}) {
super({
name: optionsArg.name,
taskFunction: async () => {
if (!this.hasTriggered) {
this.hasTriggered = true
await optionsArg.taskFunction()
}
}
})
}
}