From f149f60a5730cfae1e62b6555ff06f4899130e84 Mon Sep 17 00:00:00 2001 From: WenJun Chi Date: Tue, 19 Nov 2019 05:48:27 +0800 Subject: [PATCH] add type definition file for node-observer (#40450) * add type definition file for node-observer * remove unnecessary comment in index.d.ts * fix test errors * fix test errors --- types/node-observer/index.d.ts | 32 ++++++++++++++++++++++ types/node-observer/node-observer-tests.ts | 16 +++++++++++ types/node-observer/tsconfig.json | 24 ++++++++++++++++ types/node-observer/tslint.json | 3 ++ 4 files changed, 75 insertions(+) create mode 100644 types/node-observer/index.d.ts create mode 100644 types/node-observer/node-observer-tests.ts create mode 100644 types/node-observer/tsconfig.json create mode 100644 types/node-observer/tslint.json diff --git a/types/node-observer/index.d.ts b/types/node-observer/index.d.ts new file mode 100644 index 0000000000..143dffa70d --- /dev/null +++ b/types/node-observer/index.d.ts @@ -0,0 +1,32 @@ +// Type definitions for node-observer 0.4 +// Project: https://github.com/hormander/node-observer +// Definitions by: wjchi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = observer; + +declare class Observer { + private subscribers: object[]; + /** + * subscirpt event + * @param targetObj objects that subscribe to events + * @param eventName the event name + * @param callback callback function executed when an event is fired + */ + subscribe(targetObj: object, eventName: string, callback: any): void; + /** + * unsubscript event + * @param targetObj objects that subscribe to events + * @param eventName the event name + */ + unsubscribe(targetObj: object, eventName: string): void; + /** + * triggering event + * @param targetObj objects that subscribe to events + * @param eventName the event name + * @param data passed to the callback function when the event is fired + */ + send(targetObj: object, eventName: string, data: any): void; +} + +declare let observer: Observer; diff --git a/types/node-observer/node-observer-tests.ts b/types/node-observer/node-observer-tests.ts new file mode 100644 index 0000000000..12235ed526 --- /dev/null +++ b/types/node-observer/node-observer-tests.ts @@ -0,0 +1,16 @@ +import observer = require("node-observer"); + +class Demo { + sayHello(msg: string) { + console.log(`hello ${msg}`); + } +} + +const obj = new Demo(); + +for (let i = 0; i < 3; i++) { + observer.subscribe(obj, "hello", obj.sayHello); +} +observer.unsubscribe(obj, "hello"); + +observer.send(obj, "hello", "sent event"); diff --git a/types/node-observer/tsconfig.json b/types/node-observer/tsconfig.json new file mode 100644 index 0000000000..d0537da6a8 --- /dev/null +++ b/types/node-observer/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-observer-tests.ts" + ] +} \ No newline at end of file diff --git a/types/node-observer/tslint.json b/types/node-observer/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/node-observer/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file