8000 Add typings for tapable by HerringtonDarkholme · Pull Request #39 · webpack/tapable · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add typings for tapable #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0-beta.1",
"author": "Tobias Koppers @sokra",
"description": "Just a little module for plugins.",
"typings": "types/index.d.ts",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -14,7 +15,8 @@
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"codecov": "^2.3.0",
"jest": "^21.0.4"
"jest": "^21.0.4",
"typescript": "^2.6.1"
},
"engines": {
"node": ">=4.3"
Expand All @@ -26,7 +28,8 @@
"main": "lib/Tapable.js",
"scripts": {
"test": "jest",
"travis": "jest --coverage && codecov"
"travis": "jest --coverage && codecov",
"test:types": "tsc -p types/test"
},
"jest": {
"transform": {
Expand Down
62 changes: 62 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @deprecated
*/
export abstract class Tapable {
static addCompatLayer<T>(instance: T): T & Tapable;

abstract hooks: Record<string, Hook<any, any, any, any, any, any, any, any>>;
protected _pluginCompat: SyncBailHook<CompatOption>;
apply(...tapables: Plugin[]): void;
plugin(name: string, fn: Function): void;
}

type FnCall<A, B, C, D, E, F, G, H> = (a?: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, ...args: any[]) => any;
type FnUse<A, B, C, D, E, F, G, H> = (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, ...args: any[]) => any;

declare class Hook<A, B, C, D, E, F, G, H> {
constructor(args?: string[]);
call: FnCall<A, B, C, D, E, F, G, H>;
tap(name: string, fn: FnUse<A, B, C, D, E, F, G, H>): void;
tap(option: Option, fn: FnUse<A, B, C, D, E, F, G, H>): void;
withOptions(options: object): this;
isUsed(): boolean;
intercept(interceptor: Interceptor): void;
}
declare class AsyncHook<A, B, C, D, E, F, G, H> extends Hook<A, B, C, D, E, F, G, H> {
callAsync: FnCall<A, B, C, D, E, F, G, H>;
promise: (a?: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, ...args: any[]) => Promise<any>;
tapAsync(name: string, fn: FnUse<A, B, C, D, E, F, G, H>): void;
tapAsync(option: object, fn: FnUse<A, B, C, D, E, F, G, H>): void;
tapPromise(name: string, fn: FnUse<A, B, C, D, E, F, G, H>): void;
tapPromise(option: object, fn: FnUse<A, B, C, D, E, F, G, H>): void;
}

export class SyncHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends Hook<A, B, C, D, E, F, G, H> {}
export class SyncBailHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends Hook<A, B, C, D, E, F, G, H> {}
export class SyncWaterfallHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends Hook<A, B, C, D, E, F, G, H> {}
export class SyncLoopHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends Hook<A, B, C, D, E, F, G, H> {}

export class AsyncParallelHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends AsyncHook<A, B, C, D, E, F, G, H> {}
export class AsyncParallelBailHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends AsyncHook<A, B, C, D, E, F, G, H> {}
export class AsyncSequencialHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends AsyncHook<A, B, C, D, E, F, G, H> {}
export class AsyncSequencialBailHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends AsyncHook<A, B, C, D, E, F, G, H> {}
export class AsyncWaterfallHook<A=any, B=any, C=any, D=any, E=any, F=any, G=any, H=any> extends AsyncHook<A, B, C, D, E, F, G, H> {}

interface Plugin {
apply(tapable: Tapable): void;
}

interface Option {
name: string;
fn?: Function;
}

interface CompatOption extends Option {
names: Set<string>;
}

interface Interceptor {
loop: Function;
call: Function;
tap: Function;
}
78 changes: 78 additions & 0 deletions types/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
SyncHook,
SyncBailHook,
SyncWaterfallHook,
SyncLoopHook,
AsyncParallelHook,
AsyncParallelBailHook,
AsyncSequencialHook,
AsyncSequencialBailHook,
AsyncWaterfallHook
} from "../index";

declare class List<T> {
getRoutes(): T[];
add(t: T): void;
}

interface Route {
_routeBrand: any;
}

declare function findRoutePromise(source: string, target: string): Promise<Route>
declare function findRouteCallback(source: string, target: string, fn: Function): void
declare function cacheGet(source: string, target: string): Route

class Car {
hooks = {
accelerate: new SyncHook<number>(["newSpeed"]),
break: new SyncHook(),
calculateRoutes: new AsyncParallelHook<string, string, List<Route>>(["source", "target", "routesList"])
};

setSpeed(newSpeed: number) {
this.hooks.accelerate.call(newSpeed);
}

useNavigationSystemPromise(source: string, target: string) {
const routesList = new List<Route>();
return this.hooks.calculateRoutes.promise(source, target, routesList).then(() => {
return routesList.getRoutes();
});
}

useNavigationSystemAsync(source: string, target: string, callback: Function) {
const routesList = new List<Route>();
this.hooks.calculateRoutes.callAsync(source, target, routesList, (err: any) => {
if(err) return callback(err);
callback(null, routesList.getRoutes());
});
}
}

const myCar = new Car();

// Use the tap method to add a consument
myCar.hooks.break.tap("WarningLampPlugin", () => console.log("warningLamp on"));

myCar.hooks.calculateRoutes.tapPromise("GoogleMapsPlugin", (source, target, routesList) => {
// return a promise
return findRoutePromise(source, target).then(route => {
routesList.add(route);
});
});
myCar.hooks.calculateRoutes.tapAsync("BingMapsPlugin", (source, target, routesList, callback) => {
findRouteCallback(source, target, (err: any, route: any) => {
if(err) return callback(err);
routesList.add(route);
// call the callback
callback();
});
});

// You can still use sync plugins
myCar.hooks.calculateRoutes.tap("CachedRoutesPlugin", (source, target, routesList) => {
const cachedRoute = cacheGet(source, target);
if(cachedRoute)
routesList.add(cachedRoute);
})
10 changes: 10 additions & 0 deletions types/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"noEmit": true,
"lib": [ "es2015", "dom" ]
},
"include": [
"./*.ts"
]
}
8000 10 changes: 10 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"noEmit": true,
"lib": [ "es2015" ]
},
"include": [
"./*.ts"
]
}
4 changes: 4 additions & 0 deletions yarn.lock
4D52
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.1.tgz#ef39cdea27abac0b500242d6726ab90e0c846631"

uglify-js@^2.6:
version "2.8.29"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"
Expand Down
0