now returning correct Promise types

This commit is contained in:
Philipp Kunz 2016-08-03 14:51:43 +02:00
parent 7ded7b036e
commit 1d0a85e6ca
8 changed files with 73 additions and 20 deletions

View File

@ -1,6 +1,8 @@
/// <reference types="q" /> /// <reference types="q" />
import plugins = require("./taskbuffer.plugins"); import plugins = require("./taskbuffer.plugins");
import { Task, ITaskFunction } from "./taskbuffer.classes.task"; import { Task, ITaskFunction } from "./taskbuffer.classes.task";
import { Promise } from "q";
export { Promise } from "q";
export declare let emptyTaskFunction: ITaskFunction; export declare let emptyTaskFunction: ITaskFunction;
export declare let isTask: (taskArg: any) => boolean; export declare let isTask: (taskArg: any) => boolean;
export declare let isTaskTouched: (taskArg: Task, touchedTasksArray: Task[]) => boolean; export declare let isTaskTouched: (taskArg: Task, touchedTasksArray: Task[]) => boolean;
@ -17,7 +19,7 @@ export declare class CycleCounter {
cycleObjectArray: cycleObject[]; cycleObjectArray: cycleObject[];
constructor(taskArg: Task); constructor(taskArg: Task);
getPromiseForCycle(cycleCountArg: number): plugins.Q.Promise<{}>; getPromiseForCycle(cycleCountArg: number): plugins.Q.Promise<{}>;
informOfCycle(): void; informOfCycle(x: any): void;
} }
export declare class BufferRunner { export declare class BufferRunner {
task: Task; task: Task;
@ -27,5 +29,5 @@ export declare class BufferRunner {
constructor(taskArg: Task); constructor(taskArg: Task);
private _run(x); private _run(x);
setBufferMax(bufferMaxArg: number): void; setBufferMax(bufferMaxArg: number): void;
trigger(x: any): PromiseLike<any>; trigger(x: any): Promise<any>;
} }

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,7 @@
/// <reference types="q" />
import * as helpers from "./taskbuffer.classes.helpers"; import * as helpers from "./taskbuffer.classes.helpers";
import { Promise } from "q";
export { Promise } from "q";
export interface ITaskFunction { export interface ITaskFunction {
(x?: any): PromiseLike<any>; (x?: any): PromiseLike<any>;
} }
@ -24,14 +27,14 @@ export declare class Task {
/** /**
* trigger the task. Will trigger buffered if this.buffered is true * trigger the task. Will trigger buffered if this.buffered is true
*/ */
trigger(x?: any): PromiseLike<any>; trigger(x?: any): Promise<any>;
/** /**
* trigger task unbuffered. * trigger task unbuffered.
*/ */
triggerUnBuffered(x?: any): PromiseLike<any>; triggerUnBuffered(x?: any): Promise<any>;
/** /**
* trigger task buffered. * trigger task buffered.
*/ */
triggerBuffered(x?: any): PromiseLike<any>; triggerBuffered(x?: any): Promise<any>;
state: string; state: string;
} }

View File

@ -1,6 +1,8 @@
"use strict"; "use strict";
const plugins = require("./taskbuffer.plugins"); const plugins = require("./taskbuffer.plugins");
const helpers = require("./taskbuffer.classes.helpers"); const helpers = require("./taskbuffer.classes.helpers");
var q_1 = require("q");
exports.Promise = q_1.Promise;
class Task { class Task {
constructor(optionsArg) { constructor(optionsArg) {
// initialize by default // initialize by default
@ -56,4 +58,4 @@ class Task {
} }
} }
exports.Task = Task; exports.Task = Task;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMudGFzay50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0sc0JBQ3pCLENBQUMsQ0FEOEM7QUFDL0MsTUFBWSxPQUFPLFdBQU0sOEJBR3pCLENBQUMsQ0FIc0Q7QUFPdkQ7SUFjSSxZQUFZLFVBT1g7UUFkRCx3QkFBd0I7UUFDeEIsWUFBTyxHQUFXLEtBQUssQ0FBQztRQUN4QixpQkFBWSxHQUFHLElBQUksT0FBTyxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUM5QyxpQkFBWSxHQUFHLElBQUksT0FBTyxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUM5QyxTQUFJLEdBQVcsSUFBSSxDQUFDO1FBQ1osV0FBTSxHQUFVLE9BQU8sQ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ENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxJQUFJLEVBQUMsRUFBQyxDQUFDLEVBQUMsQ0FBQyxFQUFDLENBQUMsQ0FBQztJQUN2QyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxlQUFlLENBQUMsQ0FBRTtRQUNkLE1BQU0sQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUMsQ0FBQztJQUN4QyxDQUFDO0lBRUQsSUFBSSxLQUFLO1FBQ0wsTUFBTSxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUM7SUFDdkIsQ0FBQztJQUNELElBQUksS0FBSyxDQUFDLFFBQWU7UUFDckIsRUFBRSxDQUFDLENBQUMsUUFBUSxJQUFJLFFBQVEsQ0FBQyxDQUFBLENBQUM7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxRQUFRLENBQUM7UUFDM0IsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ0osT0FBTyxDQUFDLFNBQVMsQ0FBQyxLQUFLLENBQUMsYUFBYSxHQUFHLFFBQVEsQ0FBQyxJQUFJLEdBQUcsbUJBQW1CLENBQUMsQ0FBQztRQUNqRixDQUFDO0lBQ0wsQ0FBQztBQUNMLENBQUM7QUFwRVksWUFBSSxPQW9FaEIsQ0FBQSJ9

File diff suppressed because one or more lines are too long

View File

@ -167,6 +167,19 @@ describe("taskbuffer", function () {
return done.promise; return done.promise;
} }
}); });
let flowTaskBuffered = new taskbuffer.Task({
taskFunction: (x:number) => {
let done = q.defer();
console.log("flowTask1");
console.log(x);
done.resolve(x);
return done.promise;
},
buffered:true,
bufferMax: 1
});
let flowTask2 = new taskbuffer.Task({ let flowTask2 = new taskbuffer.Task({
taskFunction: (x:number) => { taskFunction: (x:number) => {
let done = q.defer(); let done = q.defer();
@ -178,7 +191,7 @@ describe("taskbuffer", function () {
preTask:flowTask1 preTask:flowTask1
}); });
let flowTask3 = new taskbuffer.Taskchain({ let flowTask3 = new taskbuffer.Taskchain({
taskArray:[flowTask1,flowTask2]; taskArray:[flowTask1,flowTask2]
}); });
it("should let a value flow through a task",function(done){ it("should let a value flow through a task",function(done){
flowTask1.trigger(12).then((x) => { flowTask1.trigger(12).then((x) => {
@ -192,6 +205,14 @@ describe("taskbuffer", function () {
done(); done();
}).catch(done); }).catch(done);
}); });
it("should let a values flow between tasks when buffered",function(done){
flowTaskBuffered.trigger(12).then((x) => {
should.equal(x,12);
done();
}).catch(done);
});
it("should let a values flow between tasks in Taskchain",function(done){ it("should let a values flow between tasks in Taskchain",function(done){
flowTask3.trigger(12).then((x) => { flowTask3.trigger(12).then((x) => {
should.equal(x,12); should.equal(x,12);

View File

@ -1,6 +1,9 @@
import plugins = require("./taskbuffer.plugins"); import plugins = require("./taskbuffer.plugins");
import { Task, ITaskFunction } from "./taskbuffer.classes.task"; import { Task, ITaskFunction } from "./taskbuffer.classes.task";
import {Promise} from "q";
export {Promise} from "q";
export let emptyTaskFunction: ITaskFunction = function (x) { export let emptyTaskFunction: ITaskFunction = function (x) {
let done = plugins.Q.defer(); let done = plugins.Q.defer();
done.resolve(); done.resolve();
@ -100,12 +103,12 @@ export class CycleCounter {
this.cycleObjectArray.push(cycleObject); this.cycleObjectArray.push(cycleObject);
return done.promise; return done.promise;
}; };
informOfCycle(){ informOfCycle(x){
let newCycleObjectArray:cycleObject[] = []; let newCycleObjectArray:cycleObject[] = [];
this.cycleObjectArray.forEach(cycleObjectArg => { this.cycleObjectArray.forEach(cycleObjectArg => {
cycleObjectArg.cycleCounter--; cycleObjectArg.cycleCounter--;
if(cycleObjectArg.cycleCounter <= 0){ if(cycleObjectArg.cycleCounter <= 0){
cycleObjectArg.deferred.resolve(); cycleObjectArg.deferred.resolve(x);
} else { } else {
newCycleObjectArray.push(cycleObjectArg); newCycleObjectArray.push(cycleObjectArg);
}; };
@ -131,7 +134,7 @@ export class BufferRunner {
runTask(this.task,{x:x}) runTask(this.task,{x:x})
.then((x) => { .then((x) => {
this.bufferCounter--; this.bufferCounter--;
this.task.cycleCounter.informOfCycle(); this.task.cycleCounter.informOfCycle(x);
recursiveBufferRunner(x); recursiveBufferRunner(x);
}); });
} else { } else {
@ -144,11 +147,11 @@ export class BufferRunner {
setBufferMax(bufferMaxArg:number){ setBufferMax(bufferMaxArg:number){
this.bufferMax = bufferMaxArg; this.bufferMax = bufferMaxArg;
}; };
trigger(x): PromiseLike<any> { trigger(x): Promise<any> {
if(!(this.bufferCounter >= this.bufferMax)){ if(!(this.bufferCounter >= this.bufferMax)){
this.bufferCounter++ this.bufferCounter++
}; };
let returnPromise:PromiseLike<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1); let returnPromise:Promise<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
if(!this.running){ if(!this.running){
this._run(x); this._run(x);
} }

View File

@ -1,6 +1,9 @@
import * as plugins from "./taskbuffer.plugins" import * as plugins from "./taskbuffer.plugins"
import * as helpers from "./taskbuffer.classes.helpers" import * as helpers from "./taskbuffer.classes.helpers"
//interfaces
import {Promise} from "q";
export {Promise} from "q";
export interface ITaskFunction { export interface ITaskFunction {
(x?:any):PromiseLike<any>; (x?:any):PromiseLike<any>;
@ -41,7 +44,7 @@ export class Task {
/** /**
* trigger the task. Will trigger buffered if this.buffered is true * trigger the task. Will trigger buffered if this.buffered is true
*/ */
trigger(x?):PromiseLike<any> { trigger(x?):Promise<any> {
if(this.buffered) { if(this.buffered) {
return this.triggerBuffered(x) return this.triggerBuffered(x)
} }
@ -53,14 +56,14 @@ export class Task {
/** /**
* trigger task unbuffered. * trigger task unbuffered.
*/ */
triggerUnBuffered(x?):PromiseLike<any>{ triggerUnBuffered(x?):Promise<any>{
return helpers.runTask(this,{x:x}); return helpers.runTask(this,{x:x});
} }
/** /**
* trigger task buffered. * trigger task buffered.
*/ */
triggerBuffered(x?):PromiseLike<any>{ triggerBuffered(x?):Promise<any>{
return this.bufferRunner.trigger(x); return this.bufferRunner.trigger(x);
} }