Added async-polling definition (#11827)

* Added definition for async-polling

* async-polling tests with noImplicitAny
This commit is contained in:
Goldsmith42
2016-10-07 12:48:58 -07:00
committed by Mohamed Hegazy
parent 3e31546e34
commit 7dbdbc0187
2 changed files with 82 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
/// <reference path="async-polling.d.ts" />
import * as AsyncPolling from "async-polling";
// Tests based on examples in https://github.com/cGuille/async-polling#readme
AsyncPolling(end => {
end();
}, 3000).run();
function someAsynchroneProcess(callback: (error?: Error, response?: any) => any): any {
callback();
}
let polling = AsyncPolling(end => {
someAsynchroneProcess(function (error, response) {
if (error) {
end(error);
return;
}
end(null, response);
});
}, 3000);
polling.on("error", (error: Error) => {});
polling.on("result", (result: any) => {});
polling.run();
polling.stop();
AsyncPolling(function(end) {
this.stop();
end();
}, 3000).run();
let i = 0;
polling = AsyncPolling(function(end) {
++i;
if (i === 3) {
return end(new Error("i is " + i));
}
if (i >= 5) {
this.stop();
return end(null, `#${i} stop`);
}
end(null, `#${i} wait a second...`);
}, 1000);
const eventNames: AsyncPolling.EventName[] = ["run", "start", "end", "schedule", "stop"];
eventNames.forEach(eventName => {
polling.on(eventName, () => {
console.log("lifecycle:", eventName);
});
});
polling.on("result", (result: any) => {
console.log("result:", result);
});
polling.on("error", (error: Error) => {
console.error("error:", error);
});
polling.run();
+18
View File
@@ -0,0 +1,18 @@
// Type definitions for AsyncPolling
// Project: https://github.com/cGuille/async-polling
// Definitions by: Zlatko Andonovski <https://github.com/Goldsmith42/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "async-polling" {
module AsyncPolling {
export type EventName = "run"|"start"|"error"|"result"|"end"|"schedule"|"stop";
}
function AsyncPolling<Result>(pollingFunc: (end: (err?: Error, result?: Result) => any) => any, delay: number): {
run: () => any;
stop: () => any;
on: (eventName: AsyncPolling.EventName, listener: Function) => any;
}
export = AsyncPolling;
}