mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-19 16:49:45 +08:00
# Conflicts: # amcharts/AmCharts.d.ts # angular-gettext/angular-gettext.d.ts # angular-jwt/angular-jwt.d.ts # angular-material/angular-material.d.ts # angularjs/angular.d.ts # auth0-js/auth0-js.d.ts # aws-lambda/aws-lambda.d.ts # aws-sdk/aws-sdk.d.ts # azure-mobile-apps/azure-mobile-apps.d.ts # azure-mobile-services-client/AzureMobileServicesClient.d.ts # blessed/blessed-tests.ts # blessed/blessed.d.ts # bootbox/bootbox.d.ts # bowser/bowser.d.ts # cache-manager/cache-manager.d.ts # chai-things/chai-things.d.ts # colors/colors.d.ts # cordova/cordova-tests.ts # cordova/plugins/Contacts.d.ts # cordova/plugins/FileSystem.d.ts # couchbase/couchbase.d.ts # cucumber/cucumber-tests.ts # cucumber/cucumber.d.ts # d3/d3.d.ts # dojo/dojo.d.ts # dustjs-linkedin/dustjs-linkedin.d.ts # esprima/esprima.d.ts # express-serve-static-core/express-serve-static-core.d.ts # express-session/express-session.d.ts # fetch-mock/fetch-mock.d.ts # fs-extra/fs-extra.d.ts # fullCalendar/fullCalendar.d.ts # github-electron/github-electron.d.ts # gulp-uglify/gulp-uglify.d.ts # gulp/gulp.d.ts # highcharts/highcharts.d.ts # imap/imap.d.ts # incremental-dom/incremental-dom.d.ts # inversify/inversify.d.ts # ionic/ionic.d.ts # ioredis/ioredis.d.ts # isomorphic-fetch/isomorphic-fetch-tests.ts # jake/jake.d.ts # joi/joi.d.ts # jquery-mockjax/jquery-mockjax.d.ts # jquery/jquery.d.ts # js-data-http/js-data-http-tests.ts # js-data-http/js-data-http.d.ts # js-data/js-data.d.ts # jsdom/jsdom.d.ts # jsts/jsts-tests.ts # knex/knex.d.ts # koa-favicon/koa-favicon.d.ts # koa-router/koa-router-tests.ts # koa-router/koa-router.d.ts # lodash/lodash.d.ts # mailparser/mailparser.d.ts # maquette/maquette.d.ts # material-ui/material-ui.d.ts # matter-js/matter-js.d.ts # moment/moment.d.ts # mongoose-promise/mongoose-promise-tests.ts # mongoose/mongoose-tests.ts # mongoose/mongoose.d.ts # multer/multer.d.ts # ncp/ncp.d.ts # nock/nock.d.ts # node/node-tests.ts # node/node.d.ts # nvd3/nvd3.d.ts # object-assign/object-assign.d.ts # openlayers/openlayers.d.ts # parse/parse.d.ts # pdf/pdf.d.ts # pdfkit/pdfkit.d.ts # pg/pg.d.ts # pixi.js/pixi.js.d.ts # progress/progress.d.ts # pusher-js/pusher-js.d.ts # quill/quill-tests.ts # quill/quill.d.ts # radium/radium.d.ts # ravenjs/ravenjs-tests.ts # react-dropzone/react-dropzone.d.ts # react-mdl/react-mdl.d.ts # react-native/react-native.d.ts # react-notification-system/react-notification-system.d.ts # react-router/history-tests.ts # react-router/react-router-tests.tsx # react-router/react-router.d.ts # react-select/react-select-tests.tsx # react-select/react-select.d.ts # react/react.d.ts # redux-form/redux-form.d.ts # request-promise/request-promise.d.ts # resolve-from/resolve-from.d.ts # riot-api-nodejs/riot-api-nodejs.d.ts # sanitize-html/sanitize-html.d.ts # segment-analytics/segment-analytics.d.ts # simple-assign/simple-assign-tests.ts # simple-assign/simple-assign.d.ts # slate-irc/slate-irc.d.ts # soap/soap.d.ts # socket.io/socket.io.d.ts # sql.js/sql.js-tests.ts # sql.js/sql.js.d.ts # steam/steam.d.ts # stylus/stylus.d.ts # swiper/swiper.d.ts # tedious/tedious.d.ts # threejs/three.d.ts # twilio/twilio.d.ts # underscore/underscore.d.ts # ws/ws.d.ts # yeoman-generator/yeoman-generator.d.ts
376 lines
12 KiB
TypeScript
376 lines
12 KiB
TypeScript
import {
|
|
Kernel,
|
|
injectable, tagged, named, targetName,
|
|
inject, multiInject, traverseAncerstors,
|
|
taggedConstraint, namedConstraint, typeConstraint,
|
|
KernelModule, interfaces, unmanaged
|
|
} from "inversify";
|
|
|
|
import * as Proxy from "harmony-proxy";
|
|
|
|
module external_module_test {
|
|
|
|
interface Warrior {
|
|
fight(): string;
|
|
sneak(): string;
|
|
}
|
|
|
|
interface Weapon {
|
|
hit(): string;
|
|
}
|
|
|
|
interface ThrowableWeapon extends Weapon {
|
|
throw(): string;
|
|
}
|
|
|
|
@injectable()
|
|
class Katana implements Weapon {
|
|
public hit() {
|
|
return "cut!";
|
|
}
|
|
}
|
|
|
|
@injectable()
|
|
class Shuriken implements ThrowableWeapon {
|
|
public throw() {
|
|
return "hit!";
|
|
}
|
|
public hit() {
|
|
return "hit!";
|
|
}
|
|
}
|
|
|
|
@injectable()
|
|
class Ninja implements Warrior {
|
|
|
|
private _katana: Weapon;
|
|
private _shuriken: ThrowableWeapon;
|
|
|
|
public constructor(
|
|
@inject("Weapon") katana: Weapon,
|
|
@inject("ThrowableWeapon") shuriken: ThrowableWeapon
|
|
) {
|
|
this._katana = katana;
|
|
this._shuriken = shuriken;
|
|
}
|
|
|
|
public fight() { return this._katana.hit(); };
|
|
public sneak() { return this._shuriken.throw(); };
|
|
|
|
}
|
|
|
|
let kernel: interfaces.Kernel = new Kernel();
|
|
kernel.bind<Warrior>("Warrior").to(Ninja);
|
|
kernel.bind<Weapon>("Weapon").to(Katana);
|
|
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).inSingletonScope();
|
|
|
|
let ninja = kernel.get<Warrior>("Warrior");
|
|
console.log(ninja);
|
|
|
|
// Unbind
|
|
kernel.unbind("Warrior");
|
|
kernel.unbindAll();
|
|
|
|
// Kernel modules
|
|
let warriors: interfaces.KernelModule = new KernelModule((bind: interfaces.Bind) => {
|
|
bind<Warrior>("Warrior").to(Ninja);
|
|
});
|
|
|
|
let weapons: interfaces.KernelModule = new KernelModule((bind: interfaces.Bind) => {
|
|
bind<Weapon>("Weapon").to(Katana);
|
|
bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken);
|
|
});
|
|
|
|
kernel = new Kernel();
|
|
kernel.load(warriors, weapons);
|
|
let ninja2 = kernel.get<Warrior>("Warrior");
|
|
console.log(ninja2);
|
|
|
|
// middleware
|
|
function logger(planAndResolve: interfaces.PlanAndResolve<any>): interfaces.PlanAndResolve<any> {
|
|
return (args: interfaces.PlanAndResolveArgs) => {
|
|
let start = new Date().getTime();
|
|
let result = planAndResolve(args);
|
|
let end = new Date().getTime();
|
|
console.log(end - start);
|
|
return result;
|
|
};
|
|
}
|
|
|
|
kernel.applyMiddleware(logger, logger);
|
|
|
|
// binding types
|
|
kernel.bind<Weapon>("Weapon").to(Katana);
|
|
kernel.bind<Weapon>("Weapon").toConstantValue(new Katana());
|
|
kernel.bind<Weapon>("Weapon").toDynamicValue((context: interfaces.Context) => { return new Katana(); });
|
|
|
|
kernel.bind<interfaces.Newable<Weapon>>("Weapon").toConstructor<Weapon>(Katana);
|
|
|
|
kernel.bind<interfaces.Factory<Weapon>>("Weapon").toFactory<Weapon>((context) => {
|
|
return () => {
|
|
return kernel.get<Weapon>("Weapon");
|
|
};
|
|
});
|
|
|
|
kernel.bind<interfaces.Factory<Weapon>>("Weapon").toAutoFactory<Weapon>("Weapon");
|
|
|
|
kernel.bind<interfaces.Provider<Weapon>>("Weapon").toProvider<Weapon>((context) => {
|
|
return () => {
|
|
return new Promise<Weapon>((resolve) => {
|
|
let katana = kernel.get<Weapon>("Weapon");
|
|
resolve(katana);
|
|
});
|
|
};
|
|
});
|
|
|
|
kernel.bind<Weapon>("Weapon").to(Katana).onActivation((context: interfaces.Context, katanaToBeInjected: Weapon) => {
|
|
let handler = {
|
|
apply: function(target: any, thisArgument: any, argumentsList: any[]) {
|
|
console.log(`Starting: ${performance.now()}`);
|
|
let result = target.apply(thisArgument, argumentsList);
|
|
console.log(`Finished: ${performance.now()}`);
|
|
return result;
|
|
}
|
|
};
|
|
return new Proxy(katanaToBeInjected, handler);
|
|
});
|
|
|
|
|
|
@injectable()
|
|
class Samurai implements Warrior {
|
|
public katana: Weapon;
|
|
public shuriken: ThrowableWeapon;
|
|
public constructor(
|
|
@inject("Weapon") @tagged("canThrow", false) katana: Weapon,
|
|
@inject("ThrowableWeapon") @tagged("canThrow", true) shuriken: ThrowableWeapon
|
|
) {
|
|
this.katana = katana;
|
|
this.shuriken = shuriken;
|
|
}
|
|
public fight() { return this.katana.hit(); };
|
|
public sneak() { return this.shuriken.throw(); };
|
|
}
|
|
|
|
kernel.bind<Samurai>("Samurai").to(Samurai);
|
|
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetTagged("canThrow", false);
|
|
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
|
kernel.getAllTagged<Weapon>("Weapon", "canThrow", false);
|
|
|
|
let throwable = tagged("canThrow", true);
|
|
let notThrowable = tagged("canThrow", false);
|
|
|
|
@injectable()
|
|
class Samurai2 implements Samurai {
|
|
public katana: Weapon;
|
|
public shuriken: ThrowableWeapon;
|
|
public constructor(
|
|
@inject("Weapon") @throwable katana: Weapon,
|
|
@inject("ThrowableWeapon") @notThrowable shuriken: ThrowableWeapon
|
|
) {
|
|
this.katana = katana;
|
|
this.shuriken = shuriken;
|
|
}
|
|
public fight() { return this.katana.hit(); };
|
|
public sneak() { return this.shuriken.throw(); };
|
|
}
|
|
|
|
@injectable()
|
|
class Samurai3 implements Samurai {
|
|
public katana: Weapon;
|
|
public shuriken: ThrowableWeapon;
|
|
public constructor(
|
|
@inject("Weapon") @named("strong") katana: Weapon,
|
|
@inject("ThrowableWeapon") @named("weak") shuriken: ThrowableWeapon
|
|
) {
|
|
this.katana = katana;
|
|
this.shuriken = shuriken;
|
|
}
|
|
public fight() { return this.katana.hit(); };
|
|
public sneak() { return this.shuriken.throw(); };
|
|
}
|
|
|
|
kernel.bind<Warrior>("Warrior").to(Samurai3);
|
|
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("strong");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("weak");
|
|
kernel.getAllNamed<Weapon>("Weapon", "weak");
|
|
|
|
@injectable()
|
|
class Samurai4 implements Samurai {
|
|
public katana: Weapon;
|
|
public shuriken: ThrowableWeapon;
|
|
public constructor(
|
|
@inject("Weapon") @targetName("katana") katana: Weapon,
|
|
@inject("ThrowableWeapon") @targetName("shuriken") shuriken: ThrowableWeapon
|
|
) {
|
|
this.katana = katana;
|
|
this.shuriken = shuriken;
|
|
}
|
|
public fight() { return this.katana.hit(); };
|
|
public sneak() { return this.shuriken.throw(); };
|
|
}
|
|
|
|
kernel.bind<Warrior>("Warrior").to(Samurai4);
|
|
|
|
kernel.bind<Weapon>("Weapon").to(Katana).when((request: interfaces.Request) => {
|
|
return request.target.name.equals("katana");
|
|
});
|
|
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).when((request: interfaces.Request) => {
|
|
return request.target.name.equals("shuriken");
|
|
});
|
|
|
|
// custom constraints
|
|
let whenParentNamedCanThrowConstraint = (request: interfaces.Request) => {
|
|
return namedConstraint("canThrow")(request.parentRequest);
|
|
};
|
|
|
|
let whenAnyAncestorIsConstraint = (request: interfaces.Request) => {
|
|
return traverseAncerstors(request, typeConstraint(Ninja));
|
|
};
|
|
|
|
let whenAnyAncestorTaggedConstraint = (request: interfaces.Request) => {
|
|
return traverseAncerstors(request, taggedConstraint("canThrow")(true));
|
|
};
|
|
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenParentNamedCanThrowConstraint);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorIsConstraint);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorTaggedConstraint);
|
|
|
|
// Constraint helpers
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto(Ninja);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto("Ninja");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentNamed("chinese");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentTagged("canThrow", true);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("strong");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs(Ninja);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs("Ninja");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorNamed("strong");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorTagged("canThrow", true);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorMatches(whenParentNamedCanThrowConstraint);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs(Ninja);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs("Ninja");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorNamed("strong");
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorTagged("canThrow", true);
|
|
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorMatches(whenParentNamedCanThrowConstraint);
|
|
|
|
// multi-injection
|
|
@injectable()
|
|
class Samurai5 implements Warrior {
|
|
public katana: Weapon;
|
|
public shuriken: Weapon;
|
|
public constructor(
|
|
@multiInject("Weapon") wpns: Weapon[]
|
|
) {
|
|
this.katana = wpns[0];
|
|
this.shuriken = wpns[1];
|
|
}
|
|
public fight() { return this.katana.hit(); };
|
|
public sneak() { return this.shuriken.hit(); };
|
|
}
|
|
|
|
// symbols
|
|
let SYMBOLS = {
|
|
ThrowableWeapon: Symbol("ThrowableWeapon"),
|
|
Warrior: Symbol("Warrior"),
|
|
Weapon: Symbol("Weapon"),
|
|
};
|
|
|
|
@injectable()
|
|
class Ninja1 implements Warrior {
|
|
|
|
private _katana: Weapon;
|
|
private _shuriken: ThrowableWeapon;
|
|
|
|
public constructor(
|
|
@inject(SYMBOLS.Weapon) katana: Weapon,
|
|
@inject(SYMBOLS.ThrowableWeapon) shuriken: ThrowableWeapon
|
|
) {
|
|
this._katana = katana;
|
|
this._shuriken = shuriken;
|
|
}
|
|
|
|
public fight() { return this._katana.hit(); };
|
|
public sneak() { return this._shuriken.throw(); };
|
|
|
|
}
|
|
|
|
let kernel3 = new Kernel();
|
|
kernel3.bind<Warrior>(SYMBOLS.Warrior).to(Ninja);
|
|
kernel3.bind<Weapon>(SYMBOLS.Weapon).to(Katana);
|
|
kernel3.bind<ThrowableWeapon>(SYMBOLS.ThrowableWeapon).to(Shuriken).inSingletonScope();
|
|
|
|
let ninja4 = kernel3.get<Warrior>("Warrior");
|
|
console.log(ninja4);
|
|
|
|
// classes
|
|
|
|
@injectable()
|
|
class Ninja2 implements Warrior {
|
|
|
|
private _katana: Katana;
|
|
private _shuriken: Shuriken;
|
|
|
|
public constructor(
|
|
katana: Katana,
|
|
shuriken: Shuriken
|
|
) {
|
|
this._katana = katana;
|
|
this._shuriken = shuriken;
|
|
}
|
|
|
|
public fight() { return this._katana.hit(); };
|
|
public sneak() { return this._shuriken.throw(); };
|
|
|
|
}
|
|
|
|
let kernel4 = new Kernel();
|
|
kernel4.bind<Ninja>(Ninja).to(Ninja);
|
|
kernel4.bind<Katana>(Katana).to(Katana);
|
|
kernel4.bind<Shuriken>(Shuriken).to(Shuriken).inSingletonScope();
|
|
|
|
let ninja5 = kernel4.get<Ninja>(Ninja);
|
|
console.log(ninja5);
|
|
|
|
}
|
|
|
|
module snapshot {
|
|
|
|
let kernel = new Kernel();
|
|
|
|
kernel.snapshot();
|
|
kernel.restore();
|
|
|
|
@injectable()
|
|
class Test { }
|
|
|
|
kernel.bind<Test>(Test).toSelf();
|
|
kernel.bind<Test>(Test).toSelf().inSingletonScope();
|
|
}
|
|
|
|
module unmanaged_injection {
|
|
|
|
let kernel = new Kernel();
|
|
|
|
const BaseId = "Base";
|
|
|
|
@injectable()
|
|
class Base {
|
|
public prop: string;
|
|
public constructor(@unmanaged() arg: string) { // injected by user
|
|
this.prop = arg;
|
|
}
|
|
}
|
|
|
|
@injectable()
|
|
class Derived extends Base {
|
|
public constructor() {
|
|
super("unmanaged-injected-value"); // user injection
|
|
}
|
|
}
|
|
|
|
kernel.bind<Base>(BaseId).to(Derived);
|
|
console.log(kernel.get(BaseId) instanceof Base); // true
|
|
|
|
}
|