From 7dbdbc01878b44a8d7239181c73cfcdd51a6c85a Mon Sep 17 00:00:00 2001 From: Goldsmith42 Date: Fri, 7 Oct 2016 21:48:58 +0200 Subject: [PATCH] Added async-polling definition (#11827) * Added definition for async-polling * async-polling tests with noImplicitAny --- async-polling/async-polling-tests.ts | 64 ++++++++++++++++++++++++++++ async-polling/async-polling.d.ts | 18 ++++++++ 2 files changed, 82 insertions(+) create mode 100644 async-polling/async-polling-tests.ts create mode 100644 async-polling/async-polling.d.ts diff --git a/async-polling/async-polling-tests.ts b/async-polling/async-polling-tests.ts new file mode 100644 index 0000000000..ec9e5200f2 --- /dev/null +++ b/async-polling/async-polling-tests.ts @@ -0,0 +1,64 @@ +/// + +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(); \ No newline at end of file diff --git a/async-polling/async-polling.d.ts b/async-polling/async-polling.d.ts new file mode 100644 index 0000000000..579d041e37 --- /dev/null +++ b/async-polling/async-polling.d.ts @@ -0,0 +1,18 @@ +// Type definitions for AsyncPolling +// Project: https://github.com/cGuille/async-polling +// Definitions by: Zlatko Andonovski +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "async-polling" { + module AsyncPolling { + export type EventName = "run"|"start"|"error"|"result"|"end"|"schedule"|"stop"; + } + + function AsyncPolling(pollingFunc: (end: (err?: Error, result?: Result) => any) => any, delay: number): { + run: () => any; + stop: () => any; + on: (eventName: AsyncPolling.EventName, listener: Function) => any; + } + + export = AsyncPolling; +} \ No newline at end of file