diff --git a/.npmignore b/.npmignore index 44df4681b..6a8d1a688 100644 --- a/.npmignore +++ b/.npmignore @@ -19,11 +19,4 @@ wallaby.js .travis.yml .gitignore .vscode - -# This will ignore: -# inversify-test.js, inversify-test.ts -# inversify-global-tests.js, inversify-global-tests.ts -type_definitions/inversify/*-test.js -type_definitions/inversify/*-test.ts -type_definitions/inversify/*-tests.js -type_definitions/inversify/*-tests.ts +type_definitions diff --git a/gulpfile.js b/gulpfile.js index 36b57a35f..d8b6c1c1e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -33,8 +33,7 @@ gulp.task("lint", function() { return gulp.src([ "src/**/**.ts", - "test/**/**.test.ts", - "type_definitions/inversify/*.ts" + "test/**/**.test.ts" ]) .pipe(tslint()) .pipe(tslint.report("verbose", config)); @@ -185,28 +184,13 @@ gulp.task("test", function(cb) { runSequence("istanbul:hook", "mocha", "cover", cb); }); -//****************************************************************************** -//* TYPE DEFINITIONS -//****************************************************************************** -var tsTypeDefinitionsProject = tsc.createProject("tsconfig.json"); - -gulp.task("build-type-definitions", function() { - return gulp.src("type_definitions/**/*.ts") - .pipe(tsc(tsTypeDefinitionsProject)) - .on("error", function (err) { - process.exit(1); - }) - .js.pipe(gulp.dest("type_definitions/")); -}); - gulp.task("build", function(cb) { runSequence( "lint", "build-bundle-src", // for nodejs "build-bundle-compress-src", // for browsers ["build-src", "build-es", "build-lib"], // tests + build es and lib - "build-test", - "build-type-definitions", cb); + "build-test", cb); }); //****************************************************************************** diff --git a/package.json b/package.json index 7e3aff1ed..0fd1f8dd7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "inversify", - "version": "2.0.0-beta.4", + "version": "2.0.0-beta.5", "description": "A lightweight IoC container written in TypeScript.", "main": "lib/inversify.js", "jsnext:main": "es/inversify.js", diff --git a/type_definitions/bluebird/bluebird.d.ts b/type_definitions/bluebird/bluebird.d.ts deleted file mode 100644 index 6528ce47a..000000000 --- a/type_definitions/bluebird/bluebird.d.ts +++ /dev/null @@ -1,755 +0,0 @@ -// Compiled using typings@0.6.8 -// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/dd328830dddffbe19e9addd7cf8532cbd3600816/bluebird/bluebird.d.ts -// Type definitions for bluebird 2.0.0 -// Project: https://github.com/petkaantonov/bluebird -// Definitions by: Bart van der Schoor , falsandtru -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -// ES6 model with generics overload was sourced and trans-multiplied from es6-promises.d.ts -// By: Campredon - -// Warning: recommended to use `tsc > v0.9.7` (critical bugs in earlier generic code): -// - https://github.com/borisyankov/DefinitelyTyped/issues/1563 - -// Note: replicate changes to all overloads in both definition and test file -// Note: keep both static and instance members inline (so similar) - -// TODO fix remaining TODO annotations in both definition and test - -// TODO verify support to have no return statement in handlers to get a Promise (more overloads?) - -declare var Promise: PromiseConstructor; - -interface PromiseConstructor { - /** - * Create a new promise. The passed in function will receive functions `resolve` and `reject` as its arguments which can be called to seal the fate of the created promise. - */ - new (callback: (resolve: (thenableOrResult?: T | PromiseLike) => void, reject: (error: any) => void) => void): Promise; - - // Ideally, we'd define e.g. "export class RangeError extends Error {}", - // but as Error is defined as an interface (not a class), TypeScript doesn't - // allow extending Error, only implementing it. - // However, if we want to catch() only a specific error type, we need to pass - // a constructor function to it. So, as a workaround, we define them here as such. - RangeError(): RangeError; - CancellationError(): Promise.CancellationError; - TimeoutError(): Promise.TimeoutError; - TypeError(): Promise.TypeError; - RejectionError(): Promise.RejectionError; - OperationalError(): Promise.OperationalError; - - /** - * Changes how bluebird schedules calls a-synchronously. - * - * @param scheduler Should be a function that asynchronously schedules - * the calling of the passed in function - */ - setScheduler(scheduler: (callback: (...args: any[]) => void) => void): void; - - /** - * Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise. - * - * Note about second argument: if it's specifically a true array, its values become respective arguments for the function call. Otherwise it is passed as is as the first argument for the function call. - * - * Alias for `attempt();` for compatibility with earlier ECMAScript version. - */ - try(fn: () => T | PromiseLike, args?: any[], ctx?: any): Promise; - - attempt(fn: () => T | PromiseLike, args?: any[], ctx?: any): Promise; - - /** - * Returns a new function that wraps the given function `fn`. The new function will always return a promise that is fulfilled with the original functions return values or rejected with thrown exceptions from the original function. - * This method is convenient when a function can sometimes return synchronously or throw synchronously. - */ - method(fn: Function): Function; - - /** - * Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state. - */ - resolve(value: T | PromiseLike): Promise; - resolve(): Promise; - - /** - * Create a promise that is rejected with the given `reason`. - */ - reject(reason: any): Promise; - reject(reason: any): Promise; - - /** - * Create a promise with undecided fate and return a `PromiseResolver` to control it. See resolution?: Promise(#promise-resolution). - */ - defer(): Promise.Resolver; - - /** - * Cast the given `value` to a trusted promise. If `value` is already a trusted `Promise`, it is returned as is. If `value` is not a thenable, a fulfilled is: Promise returned with `value` as its fulfillment value. If `value` is a thenable (Promise-like object, like those returned by jQuery's `$.ajax`), returns a trusted that: Promise assimilates the state of the thenable. - */ - cast(value: T | PromiseLike): Promise; - - /** - * Sugar for `Promise.resolve(undefined).bind(thisArg);`. See `.bind()`. - */ - bind(thisArg: any): Promise; - - /** - * See if `value` is a trusted Promise. - */ - is(value: any): boolean; - - /** - * Call this right after the library is loaded to enabled long stack traces. Long stack traces cannot be disabled after being enabled, and cannot be enabled after promises have alread been created. Long stack traces imply a substantial performance penalty, around 4-5x for throughput and 0.5x for latency. - */ - longStackTraces(): void; - - /** - * Returns a promise that will be fulfilled with `value` (or `undefined`) after given `ms` milliseconds. If `value` is a promise, the delay will start counting down when it is fulfilled and the returned promise will be fulfilled with the fulfillment value of the `value` promise. - */ - // TODO enable more overloads - delay(ms: number, value: T | PromiseLike): Promise; - delay(ms: number): Promise; - - /** - * Returns a function that will wrap the given `nodeFunction`. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument. - * - * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them. - * - * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`. - */ - promisify(func: (callback: (err: any, result: T) => void) => void, receiver?: any): () => Promise; - promisify(func: (arg1: A1, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1) => Promise; - promisify(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2) => Promise; - promisify(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3) => Promise; - promisify(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise; - promisify(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise; - promisify(nodeFunction: Function, receiver?: any): Function; - - /** - * Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain. The promisified method name will be the original method name postfixed with `Async`. Returns the input object. - * - * Note that the original methods on the object are not overwritten but new methods are created with the `Async`-postfix. For example, if you `promisifyAll()` the node.js `fs` object use `fs.statAsync()` to call the promisified `stat` method. - */ - // TODO how to model promisifyAll? - promisifyAll(target: Object, options?: Promise.PromisifyAllOptions): any; - - - /** - * Returns a promise that is resolved by a node style callback function. - */ - fromNode(resolver: (callback: (err: any, result?: any) => void) => void, options? : {multiArgs? : boolean}): Promise; - fromCallback(resolver: (callback: (err: any, result?: any) => void) => void, options? : {multiArgs? : boolean}): Promise; - - /** - * Returns a function that can use `yield` to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch. - */ - // TODO fix coroutine GeneratorFunction - coroutine(generatorFunction: Function): Function; - - /** - * Spawn a coroutine which may yield promises to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch. - */ - // TODO fix spawn GeneratorFunction - spawn(generatorFunction: Function): Promise; - - /** - * This is relevant to browser environments with no module loader. - * - * Release control of the `Promise` namespace to whatever it was before this library was loaded. Returns a reference to the library namespace so you can attach it to something else. - */ - noConflict(): typeof Promise; - - /** - * Add `handler` as the handler to call when there is a possibly unhandled rejection. The default handler logs the error stack to stderr or `console.error` in browsers. - * - * Passing no value or a non-function will have the effect of removing any kind of handling for possibly unhandled rejections. - */ - onPossiblyUnhandledRejection(handler: (reason: any) => any): void; - - /** - * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled when all the items in the array are fulfilled. The promise's fulfillment value is an array with fulfillment values at respective positions to the original array. If any promise in the array rejects, the returned promise is rejected with the rejection reason. - */ - // TODO enable more overloads - // promise of array with promises of value - all(values: PromiseLike[]>): Promise; - // promise of array with values - all(values: PromiseLike): Promise; - // array with promises of value - all(values: PromiseLike[]): Promise; - // array with promises of different types - all(values: [PromiseLike, PromiseLike, PromiseLike, PromiseLike, PromiseLike]): Promise<[T1, T2, T3, T4, T5]>; - all(values: [PromiseLike, PromiseLike, PromiseLike, PromiseLike]): Promise<[T1, T2, T3, T4]>; - all(values: [PromiseLike, PromiseLike, PromiseLike]): Promise<[T1, T2, T3]>; - all(values: [PromiseLike, PromiseLike]): Promise<[T1, T2]>; - // array with values - all(values: T[]): Promise; - - /** - * Like ``Promise.all`` but for object properties instead of array items. Returns a promise that is fulfilled when all the properties of the object are fulfilled. The promise's fulfillment value is an object with fulfillment values at respective keys to the original object. If any promise in the object rejects, the returned promise is rejected with the rejection reason. - * - * If `object` is a trusted `Promise`, then it will be treated as a promise for object rather than for its properties. All other objects are treated for their properties as is returned by `Object.keys` - the object's own enumerable properties. - * - * *The original object is not modified.* - */ - // TODO verify this is correct - // trusted promise for object - props(object: Promise): Promise; - // object - props(object: Object): Promise; - - /** - * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled when all the items in the array are either fulfilled or rejected. The fulfillment value is an array of ``PromiseInspection`` instances at respective positions in relation to the input array. - * - * *original: The array is not modified. The input array sparsity is retained in the resulting array.* - */ - // promise of array with promises of value - settle(values: PromiseLike[]>): Promise[]>; - // promise of array with values - settle(values: PromiseLike): Promise[]>; - // array with promises of value - settle(values: PromiseLike[]): Promise[]>; - // array with values - settle(values: T[]): Promise[]>; - - /** - * Like `Promise.some()`, with 1 as `count`. However, if the promise fulfills, the fulfillment value is not an array of 1 but the value directly. - */ - // promise of array with promises of value - any(values: PromiseLike[]>): Promise; - // promise of array with values - any(values: PromiseLike): Promise; - // array with promises of value - any(values: PromiseLike[]): Promise; - // array with values - any(values: T[]): Promise; - - /** - * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled or rejected as soon as a promise in the array is fulfilled or rejected with the respective rejection reason or fulfillment value. - * - * **Note** If you pass empty array or a sparse array with no values, or a promise/thenable for such, it will be forever pending. - */ - // promise of array with promises of value - race(values: PromiseLike[]>): Promise; - // promise of array with values - race(values: PromiseLike): Promise; - // array with promises of value - race(values: PromiseLike[]): Promise; - // array with values - race(values: T[]): Promise; - - /** - * Initiate a competetive race between multiple promises or values (values will become immediately fulfilled promises). When `count` amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of the winners in order of resolution. - * - * If too many promises are rejected so that the promise can never become fulfilled, it will be immediately rejected with an array of rejection reasons in the order they were thrown in. - * - * *The original array is not modified.* - */ - // promise of array with promises of value - some(values: PromiseLike[]>, count: number): Promise; - // promise of array with values - some(values: PromiseLike, count: number): Promise; - // array with promises of value - some(values: PromiseLike[], count: number): Promise; - // array with values - some(values: T[], count: number): Promise; - - /** - * Like `Promise.all()` but instead of having to pass an array, the array is generated from the passed variadic arguments. - */ - // variadic array with promises of value - join(...values: PromiseLike[]): Promise; - // variadic array with values - join(...values: T[]): Promise; - - /** - * Map an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `mapper` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. - * - * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well. - * - * *The original array is not modified.* - */ - // promise of array with promises of value - map(values: PromiseLike[]>, mapper: (item: T, index: number, arrayLength: number) => U | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - // promise of array with values - map(values: PromiseLike, mapper: (item: T, index: number, arrayLength: number) => U | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - // array with promises of value - map(values: PromiseLike[], mapper: (item: T, index: number, arrayLength: number) => U | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - // array with values - map(values: T[], mapper: (item: T, index: number, arrayLength: number) => U | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - /** - * Similar to `map` with concurrency set to 1 but guaranteed to execute in sequential order - * - * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well. - * - * *The original array is not modified.* - */ - // promise of array with promises of value - mapSeries(values: PromiseLike[]>, mapper: (item: R, index: number, arrayLength: number) => U | PromiseLike): Promise; - - // promise of array with values - mapSeries(values: PromiseLike, mapper: (item: R, index: number, arrayLength: number) => U | PromiseLike): Promise; - - // array with promises of value - mapSeries(values: PromiseLike[], mapper: (item: R, index: number, arrayLength: number) => U | PromiseLike): Promise; - - // array with values - mapSeries(values: R[], mapper: (item: R, index: number, arrayLength: number) => U | PromiseLike): Promise; - - - /** - * Reduce an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `reducer` function with the signature `(total, current, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. - * - * If the reducer function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration. - * - * *The original array is not modified. If no `intialValue` is given and the array doesn't contain at least 2 items, the callback will not be called and `undefined` is returned. If `initialValue` is given and the array doesn't have at least 1 item, `initialValue` is returned.* - */ - // promise of array with promises of value - reduce(values: PromiseLike[]>, reducer: (total: U, current: T, index: number, arrayLength: number) => U | PromiseLike, initialValue?: U): Promise; - - // promise of array with values - reduce(values: PromiseLike, reducer: (total: U, current: T, index: number, arrayLength: number) => U | PromiseLike, initialValue?: U): Promise; - - // array with promises of value - reduce(values: PromiseLike[], reducer: (total: U, current: T, index: number, arrayLength: number) => U | PromiseLike, initialValue?: U): Promise; - - // array with values - reduce(values: T[], reducer: (total: U, current: T, index: number, arrayLength: number) => U | PromiseLike, initialValue?: U): Promise; - - /** - * Filter an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `filterer` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. - * - * The return values from the filtered functions are coerced to booleans, with the exception of promises and thenables which are awaited for their eventual result. - * - * *The original array is not modified. - */ - // promise of array with promises of value - filter(values: PromiseLike[]>, filterer: (item: T, index: number, arrayLength: number) => boolean | PromiseLike, option?: Promise.ConcurrencyOption): Promise; - - // promise of array with values - filter(values: PromiseLike, filterer: (item: T, index: number, arrayLength: number) => boolean | PromiseLike, option?: Promise.ConcurrencyOption): Promise; - - // array with promises of value - filter(values: PromiseLike[], filterer: (item: T, index: number, arrayLength: number) => boolean | PromiseLike, option?: Promise.ConcurrencyOption): Promise; - - // array with values - filter(values: T[], filterer: (item: T, index: number, arrayLength: number) => boolean | PromiseLike, option?: Promise.ConcurrencyOption): Promise; - - /** - * Iterate over an array, or a promise of an array, which contains promises (or a mix of promises and values) with the given iterator function with the signature (item, index, value) where item is the resolved value of a respective promise in the input array. Iteration happens serially. If any promise in the input array is rejected the returned promise is rejected as well. - * - * Resolves to the original array unmodified, this method is meant to be used for side effects. If the iterator function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration. - */ - // promise of array with promises of value - each(values: PromiseLike[]>, iterator: (item: T, index: number, arrayLength: number) => U | PromiseLike): Promise; - // array with promises of value - each(values: PromiseLike[], iterator: (item: T, index: number, arrayLength: number) => U | PromiseLike): Promise; - // array with values OR promise of array with values - each(values: T[] | PromiseLike, iterator: (item: T, index: number, arrayLength: number) => U | PromiseLike): Promise; -} - -interface Promise extends PromiseLike, Promise.Inspection { - /** - * Promises/A+ `.then()` with progress handler. Returns a new promise chained from this promise. The new promise will be rejected or resolved dedefer on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise. - */ - then(onFulfill: (value: T) => U | PromiseLike, onReject?: (error: any) => U | PromiseLike, onProgress?: (note: any) => any): Promise; - then(onFulfill: (value: T) => U | PromiseLike, onReject?: (error: any) => void | PromiseLike, onProgress?: (note: any) => any): Promise; - - /** - * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise. Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler. - * - * Alias `.caught();` for compatibility with earlier ECMAScript version. - */ - catch(onReject?: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - caught(onReject?: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - - catch(onReject?: (error: any) => U | PromiseLike): Promise; - caught(onReject?: (error: any) => U | PromiseLike): Promise; - - /** - * This extends `.catch` to work more like catch-clauses in languages like Java or C#. Instead of manually checking `instanceof` or `.name === "SomeError"`, you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called. - * - * This method also supports predicate-based filters. If you pass a predicate function instead of an error constructor, the predicate will receive the error as an argument. The return result of the predicate will be used determine whether the error handler should be called. - * - * Alias `.caught();` for compatibility with earlier ECMAScript version. - */ - catch(predicate: (error: any) => boolean, onReject: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - caught(predicate: (error: any) => boolean, onReject: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - - catch(predicate: (error: any) => boolean, onReject: (error: any) => U | PromiseLike): Promise; - caught(predicate: (error: any) => boolean, onReject: (error: any) => U | PromiseLike): Promise; - - catch(ErrorClass: Function, onReject: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - caught(ErrorClass: Function, onReject: (error: any) => T | PromiseLike | void | PromiseLike): Promise; - - catch(ErrorClass: Function, onReject: (error: any) => U | PromiseLike): Promise; - caught(ErrorClass: Function, onReject: (error: any) => U | PromiseLike): Promise; - - - /** - * Like `.catch` but instead of catching all types of exceptions, it only catches those that don't originate from thrown errors but rather from explicit rejections. - */ - error(onReject: (reason: any) => PromiseLike): Promise; - error(onReject: (reason: any) => U): Promise; - - /** - * Pass a handler that will be called regardless of this promise's fate. Returns a new promise chained from this promise. There are special semantics for `.finally()` in that the final value cannot be modified from the handler. - * - * Alias `.lastly();` for compatibility with earlier ECMAScript version. - */ - finally(handler: () => U | PromiseLike): Promise; - - lastly(handler: () => U | PromiseLike): Promise; - - /** - * Create a promise that follows this promise, but is bound to the given `thisArg` value. A bound promise will call its handlers with the bound value set to `this`. Additionally promises derived from a bound promise will also be bound promises with the same `thisArg` binding as the original promise. - */ - bind(thisArg: any): Promise; - - /** - * Like `.then()`, but any unhandled rejection that ends up here will be thrown as an error. - */ - done(onFulfilled?: (value: T) => PromiseLike, onRejected?: (error: any) => U | PromiseLike, onProgress?: (note: any) => any): void; - done(onFulfilled?: (value: T) => U, onRejected?: (error: any) => U | PromiseLike, onProgress?: (note: any) => any): void; - - /** - * Like `.finally()`, but not called for rejections. - */ - tap(onFulFill: (value: T) => U | PromiseLike): Promise; - - /** - * Shorthand for `.then(null, null, handler);`. Attach a progress handler that will be called if this promise is progressed. Returns a new promise chained from this promise. - */ - progressed(handler: (note: any) => any): Promise; - - /** - * Same as calling `Promise.delay(this, ms)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - delay(ms: number): Promise; - - /** - * Returns a promise that will be fulfilled with this promise's fulfillment value or rejection reason. However, if this promise is not fulfilled or rejected within `ms` milliseconds, the returned promise is rejected with a `Promise.TimeoutError` instance. - * - * You may specify a custom error message with the `message` parameter. - */ - timeout(ms: number, message?: string): Promise; - - /** - * Register a node-style callback on this promise. When this promise is is either fulfilled or rejected, the node callback will be called back with the node.js convention where error reason is the first argument and success value is the second argument. The error argument will be `null` in case of success. - * Returns back this promise instead of creating a new one. If the `callback` argument is not a function, this method does not do anything. - */ - nodeify(callback: (err: any, value?: T) => void, options?: Promise.SpreadOption): Promise; - nodeify(...sink: any[]): Promise; - - /** - * Marks this promise as cancellable. Promises by default are not cancellable after v0.11 and must be marked as such for `.cancel()` to have any effect. Marking a promise as cancellable is infectious and you don't need to remark any descendant promise. - */ - cancellable(): Promise; - - /** - * Cancel this promise. The cancellation will propagate to farthest cancellable ancestor promise which is still pending. - * - * That ancestor will then be rejected with a `CancellationError` (get a reference from `Promise.CancellationError`) object as the rejection reason. - * - * In a promise rejection handler you may check for a cancellation by seeing if the reason object has `.name === "Cancel"`. - * - * Promises are by default not cancellable. Use `.cancellable()` to mark a promise as cancellable. - */ - // TODO what to do with this? - cancel(reason?: any): Promise; - - /** - * Like `.then()`, but cancellation of the the returned promise or any of its descendant will not propagate cancellation to this promise or this promise's ancestors. - */ - fork(onFulfilled?: (value: T) => U | PromiseLike, onRejected?: (error: any) => U | PromiseLike, onProgress?: (note: any) => any): Promise; - - /** - * Create an uncancellable promise based on this promise. - */ - uncancellable(): Promise; - - /** - * See if this promise can be cancelled. - */ - isCancellable(): boolean; - - /** - * See if this `promise` has been fulfilled. - */ - isFulfilled(): boolean; - - /** - * See if this `promise` has been rejected. - */ - isRejected(): boolean; - - /** - * See if this `promise` is still defer. - */ - isPending(): boolean; - - /** - * See if this `promise` is resolved -> either fulfilled or rejected. - */ - isResolved(): boolean; - - /** - * Get the fulfillment value of the underlying promise. Throws if the promise isn't fulfilled yet. - * - * throws `TypeError` - */ - value(): T; - - /** - * Get the rejection reason for the underlying promise. Throws if the promise isn't rejected yet. - * - * throws `TypeError` - */ - reason(): any; - - /** - * Synchronously inspect the state of this `promise`. The `PromiseInspection` will represent the state of the promise as snapshotted at the time of calling `.inspect()`. - */ - inspect(): Promise.Inspection; - - /** - * This is a convenience method for doing: - * - * - * promise.then(function(obj){ - * return obj[propertyName].call(obj, arg...); - * }); - * - */ - call(propertyName: string, ...args: any[]): Promise; - - /** - * This is a convenience method for doing: - * - * - * promise.then(function(obj){ - * return obj[propertyName]; - * }); - * - */ - // TODO find way to fix get() - // get(propertyName: string): Promise; - - /** - * Convenience method for: - * - * - * .then(function() { - * return value; - * }); - * - * - * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.return()` - * - * Alias `.thenReturn();` for compatibility with earlier ECMAScript version. - */ - return(): Promise; - thenReturn(): Promise; - return(value: U): Promise; - thenReturn(value: U): Promise; - - /** - * Convenience method for: - * - * - * .then(function() { - * throw reason; - * }); - * - * Same limitations apply as with `.return()`. - * - * Alias `.thenThrow();` for compatibility with earlier ECMAScript version. - */ - throw(reason: Error): Promise; - thenThrow(reason: Error): Promise; - - /** - * Convert to String. - */ - toString(): string; - - /** - * This is implicitly called by `JSON.stringify` when serializing the object. Returns a serialized representation of the `Promise`. - */ - toJSON(): Object; - - /** - * Like calling `.then`, but the fulfillment value or rejection reason is assumed to be an array, which is flattened to the formal parameters of the handlers. - */ - // TODO how to model instance.spread()? like Q? - spread(onFulfill: Function, onReject?: (reason: any) => U | PromiseLike): Promise; - /* - // TODO or something like this? - spread(onFulfill: (...values: W[]) => PromiseLike, onReject?: (reason: any) => PromiseLike): Promise; - spread(onFulfill: (...values: W[]) => PromiseLike, onReject?: (reason: any) => U): Promise; - spread(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => PromiseLike): Promise; - spread(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => U): Promise; - */ - /** - * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - all(): Promise; - - /** - * Same as calling `Promise.props(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO how to model instance.props()? - props(): Promise; - - /** - * Same as calling `Promise.settle(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - settle(): Promise[]>; - - /** - * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - any(): Promise; - - /** - * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - some(count: number): Promise; - - /** - * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - race(): Promise; - - /** - * Same as calling `Promise.map(thisPromise, mapper)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - map(mapper: (item: Q, index: number, arrayLength: number) => U | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - /** - * Same as `Promise.mapSeries(thisPromise, mapper)`. - */ - // TODO type inference from array-resolving promise? - mapSeries(mapper: (item: Q, index: number, arrayLength: number) => U | PromiseLike): Promise; - - /** - * Same as calling `Promise.reduce(thisPromise, Function reducer, initialValue)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - reduce(reducer: (memo: U, item: Q, index: number, arrayLength: number) => U | PromiseLike, initialValue?: U): Promise; - - /** - * Same as calling ``Promise.filter(thisPromise, filterer)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - // TODO type inference from array-resolving promise? - filter(filterer: (item: U, index: number, arrayLength: number) => boolean | PromiseLike, options?: Promise.ConcurrencyOption): Promise; - - /** - * Same as calling ``Promise.each(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. - */ - each(iterator: (item: T, index: number, arrayLength: number) => U | PromiseLike): Promise; -} - -/** - * Don't use variable namespace such as variables, functions, and classes. - * If you use this namespace, it will conflict in es6. - */ -declare namespace Promise { - export interface RangeError extends Error { - } - export interface CancellationError extends Error { - } - export interface TimeoutError extends Error { - } - export interface TypeError extends Error { - } - export interface RejectionError extends Error { - } - export interface OperationalError extends Error { - } - - export interface ConcurrencyOption { - concurrency: number; - } - export interface SpreadOption { - spread: boolean; - } - export interface PromisifyAllOptions { - suffix?: string; - filter?: (name: string, func: Function, target?: any, passesDefaultFilter?: boolean) => boolean; - // The promisifier gets a reference to the original method and should return a function which returns a promise - promisifier?: (originalMethod: Function) => () => PromiseLike; - } - - export interface Resolver { - /** - * Returns a reference to the controlled promise that can be passed to clients. - */ - promise: Promise; - - /** - * Resolve the underlying promise with `value` as the resolution value. If `value` is a thenable or a promise, the underlying promise will assume its state. - */ - resolve(value: T): void; - resolve(): void; - - /** - * Reject the underlying promise with `reason` as the rejection reason. - */ - reject(reason: any): void; - - /** - * Progress the underlying promise with `value` as the progression value. - */ - progress(value: any): void; - - /** - * Gives you a callback representation of the `PromiseResolver`. Note that this is not a method but a property. The callback accepts error object in first argument and success values on the 2nd parameter and the rest, I.E. node js conventions. - * - * If the the callback is called with multiple success values, the resolver fullfills its promise with an array of the values. - */ - // TODO specify resolver callback - callback: (err: any, value: T, ...values: T[]) => void; - } - - export interface Inspection { - /** - * See if the underlying promise was fulfilled at the creation time of this inspection object. - */ - isFulfilled(): boolean; - - /** - * See if the underlying promise was rejected at the creation time of this inspection object. - */ - isRejected(): boolean; - - /** - * See if the underlying promise was defer at the creation time of this inspection object. - */ - isPending(): boolean; - - /** - * Get the fulfillment value of the underlying promise. Throws if the promise wasn't fulfilled at the creation time of this inspection object. - * - * throws `TypeError` - */ - value(): T; - - /** - * Get the rejection reason for the underlying promise. Throws if the promise wasn't rejected at the creation time of this inspection object. - * - * throws `TypeError` - */ - reason(): any; - } -} - -declare module 'bluebird' { - export = Promise; -} \ No newline at end of file diff --git a/type_definitions/harmony-proxy/harmony-proxy.d.ts b/type_definitions/harmony-proxy/harmony-proxy.d.ts deleted file mode 100644 index 8aa5a5bec..000000000 --- a/type_definitions/harmony-proxy/harmony-proxy.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Type definitions for harmony-proxy 1.0.0 -// Project: https://www.npmjs.com/package/harmony-proxy -// Definitions by: Remo Jansen -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -declare module harmonyProxy { - type PropertyKey = string | number | symbol; - - interface ProxyHandler { - getPrototypeOf? (target: T): any; - setPrototypeOf? (target: T, v: any): boolean; - isExtensible? (target: T): boolean; - preventExtensions? (target: T): boolean; - getOwnPropertyDescriptor? (target: T, p: PropertyKey): PropertyDescriptor; - has? (target: T, p: PropertyKey): boolean; - get? (target: T, p: PropertyKey, receiver: any): any; - set? (target: T, p: PropertyKey, value: any, receiver: any): boolean; - deleteProperty? (target: T, p: PropertyKey): boolean; - defineProperty? (target: T, p: PropertyKey, attributes: PropertyDescriptor): boolean; - enumerate? (target: T): PropertyKey[]; - ownKeys? (target: T): PropertyKey[]; - apply? (target: T, thisArg: any, argArray?: any): any; - construct? (target: T, thisArg: any, argArray?: any): any; - } - - interface ProxyConstructor { - revocable(target: T, handler: ProxyHandler): { proxy: T; revoke: () => void; }; - new (target: T, handler: ProxyHandler): T; - } -} - -declare module "harmony-proxy" { - let _Proxy: harmonyProxy.ProxyConstructor; - export = _Proxy; -} diff --git a/type_definitions/inversify/inversify-global-tests.ts b/type_definitions/inversify/inversify-global-tests.ts deleted file mode 100644 index 14b215c57..000000000 --- a/type_definitions/inversify/inversify-global-tests.ts +++ /dev/null @@ -1,428 +0,0 @@ -/// - -import * as Proxy from "harmony-proxy"; - -module global_module_test { - - interface INinja { - fight(): string; - sneak(): string; - } - - interface IKatana { - hit(): string; - } - - interface IShuriken { - throw(): string; - } - - @inversify.injectable() - class Katana implements IKatana { - public hit() { - return "cut!"; - } - } - - @inversify.injectable() - class Shuriken implements IShuriken { - public throw() { - return "hit!"; - } - } - - @inversify.injectable() - class Ninja implements INinja { - - private _katana: IKatana; - private _shuriken: IShuriken; - - public constructor( - @inversify.inject("IKatana") katana: IKatana, - @inversify.inject("IShuriken") shuriken: IShuriken - ) { - this._katana = katana; - this._shuriken = shuriken; - } - - public fight() { return this._katana.hit(); }; - public sneak() { return this._shuriken.throw(); }; - - } - - let kernel = new inversify.Kernel(); - kernel.bind("INinja").to(Ninja); - kernel.bind("IKatana").to(Katana); - kernel.bind("IShuriken").to(Shuriken).inSingletonScope(); - - let ninja = kernel.get("INinja"); - console.log(ninja); - - // Unbind - kernel.unbind("INinja"); - kernel.unbindAll(); - - // Kernel modules - let warriors: inversify.IKernelModule = (k: inversify.IKernel) => { - k.bind("INinja").to(Ninja); - }; - - let weapons: inversify.IKernelModule = (k: inversify.IKernel) => { - k.bind("IKatana").to(Katana); - k.bind("IShuriken").to(Shuriken).inSingletonScope(); - }; - - kernel = new inversify.Kernel(); - kernel.load(warriors, weapons); - let ninja2 = kernel.get("INinja"); - console.log(ninja2); - - // middleware - function logger(next: (context: inversify.IContext) => any) { - return (context: inversify.IContext) => { - let result = next(context); - console.log("CONTEXT: ", context); - console.log("RESULT: ", result); - return result; - }; - }; - - function visualReporter(next: (context: inversify.IContext) => any) { - return (context: inversify.IContext) => { - let result = next(context); - let _window: any = window; - let devTools = _window.__inversify_devtools__; - if (devTools !== undefined) { devTools.log(context, result); } - return result; - }; - }; - - kernel.applyMiddleware(logger, visualReporter); - - // binding types - kernel.bind("IKatana").to(Katana); - kernel.bind("IKatana").toConstantValue(new Katana()); - kernel.bind("IKatana").toDynamicValue(() => { return new Katana(); }); - - kernel.bind>("IKatana").toConstructor(Katana); - - kernel.bind>("IKatana").toFactory((context) => { - return () => { - return kernel.get("IKatana"); - }; - }); - - kernel.bind>("IKatana").toAutoFactory("IKatana"); - - kernel.bind>("IKatana").toProvider((context) => { - return () => { - return new Promise((resolve) => { - let katana = kernel.get("IKatana"); - resolve(katana); - }); - }; - }); - - kernel.bind("IKatana").to(Katana).onActivation((context: inversify.IContext, katanaToBeInjected: IKatana) => { - 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); - }); - - interface IWeapon {} - interface ISamurai { - katana: IWeapon; - shuriken: IWeapon; - } - - @inversify.injectable() - class Samurai implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inversify.inject("IWeapon") @inversify.tagged("canThrow", false) katana: IWeapon, - @inversify.inject("IWeapon") @inversify.tagged("canThrow", true) shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("Samurai").to(Samurai); - kernel.bind("IWeapon").to(Katana).whenTargetTagged("canThrow", false); - kernel.bind("IWeapon").to(Shuriken).whenTargetTagged("canThrow", true); - - let throwable = inversify.tagged("canThrow", true); - let notThrowable = inversify.tagged("canThrow", false); - - @inversify.injectable() - class Samurai2 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inversify.inject("IWeapon") @throwable katana: IWeapon, - @inversify.inject("IWeapon") @notThrowable shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - @inversify.injectable() - class Samurai3 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inversify.inject("IWeapon") @inversify.named("strong") katana: IWeapon, - @inversify.inject("IWeapon") @inversify.named("weak") shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("ISamurai").to(Samurai3); - kernel.bind("IWeapon").to(Katana).whenTargetNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenTargetNamed("weak"); - - @inversify.injectable() - class Samurai4 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inversify.inject("IWeapon") @inversify.targetName("katana") katana: IWeapon, - @inversify.inject("IWeapon") @inversify.targetName("shuriken") shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("ISamurai").to(Samurai4); - - kernel.bind("IWeapon").to(Katana).when((request: inversify.IRequest) => { - return request.target.name.equals("katana"); - }); - - kernel.bind("IWeapon").to(Shuriken).when((request: inversify.IRequest) => { - return request.target.name.equals("shuriken"); - }); - - // custom constraints - let whenParentNamedCanThrowConstraint = (request: inversify.IRequest) => { - return inversify.namedConstraint("canThrow")(request.parentRequest); - }; - - let whenAnyAncestorIsConstraint = (request: inversify.IRequest) => { - return inversify.traverseAncerstors(request, inversify.typeConstraint(Ninja)); - }; - - let whenAnyAncestorTaggedConstraint = (request: inversify.IRequest) => { - return inversify.traverseAncerstors(request, inversify.taggedConstraint("canThrow")(true)); - }; - - kernel.bind("IWeapon").to(Shuriken).when(whenParentNamedCanThrowConstraint); - kernel.bind("IWeapon").to(Shuriken).when(whenAnyAncestorIsConstraint); - kernel.bind("IWeapon").to(Shuriken).when(whenAnyAncestorTaggedConstraint); - - // Constraint helpers - kernel.bind("IWeapon").to(Shuriken).whenInjectedInto(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenInjectedInto("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenParentNamed("chinese"); - kernel.bind("IWeapon").to(Shuriken).whenParentTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenTargetNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenTargetTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorIs(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorIs("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorMatches(whenParentNamedCanThrowConstraint); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorIs(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorIs("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorMatches(whenParentNamedCanThrowConstraint); - - // multi-injection - @inversify.injectable() - class Samurai5 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inversify.multiInject("IWeapon") wpns: IWeapon[] - ) { - this.katana = wpns[0]; - this.shuriken = wpns[1]; - } - } - - // symbols - let SYMBOLS = { - IKatana: Symbol("IKatana"), - INinja: Symbol("INinja"), - IShuriken: Symbol("IShuriken"), - }; - - @inversify.injectable() - class Ninja1 implements INinja { - - private _katana: IKatana; - private _shuriken: IShuriken; - - public constructor( - @inversify.inject(SYMBOLS.IKatana) katana: IKatana, - @inversify.inject(SYMBOLS.IShuriken) shuriken: IShuriken - ) { - this._katana = katana; - this._shuriken = shuriken; - } - - public fight() { return this._katana.hit(); }; - public sneak() { return this._shuriken.throw(); }; - - } - - let kernel3 = new inversify.Kernel(); - kernel3.bind(SYMBOLS.INinja).to(Ninja); - kernel3.bind(SYMBOLS.IKatana).to(Katana); - kernel3.bind(SYMBOLS.IShuriken).to(Shuriken).inSingletonScope(); - - let ninja4 = kernel3.get("INinja"); - console.log(ninja4); - - // classes - - @inversify.injectable() - class Ninja2 implements INinja { - - 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 inversify.Kernel(); - kernel4.bind(Ninja).to(Ninja); - kernel4.bind(Katana).to(Katana); - kernel4.bind(Shuriken).to(Shuriken).inSingletonScope(); - - let ninja5 = kernel4.get("INinja"); - console.log(ninja5); -} - -module property_injection { - - let kernel = new inversify.Kernel(); - - let TYPES = { IWeapon: "IWeapon" }; - - interface IWeapon { - durability: number; - use(): void; - } - - @inversify.injectable() - class Sword implements IWeapon { - public durability: number; - public constructor() { - this.durability = 100; - } - public use() { - this.durability = this.durability - 10; - } - } - - @inversify.injectable() - class WarHammer implements IWeapon { - public durability: number; - public constructor() { - this.durability = 100; - } - public use() { - this.durability = this.durability - 10; - } - } - - let propertyMultiInject = inversify.makePropertyMultiInjectDecorator(kernel); - - class Warrior1 { - @propertyMultiInject(TYPES.IWeapon) - public weapons: IWeapon[]; - } - - let propertyInject = inversify.makePropertyInjectDecorator(kernel); - - interface ISomeService { - count: number; - increment(): void; - } - - @inversify.injectable() - class SomeService implements ISomeService { - public count: number; - public constructor() { - this.count = 0; - } - public increment() { - this.count = this.count + 1; - } - } - - class SomeWebComponent { - @propertyInject("ISomeService") - private _service: ISomeService; - public doSomething() { - let count = this._service.count; - this._service.increment(); - return count; - } - } - - let propertyInjectNammed = inversify.makePropertyInjectNamedDecorator(kernel); - - class Warrior2 { - - @propertyInjectNammed(TYPES.IWeapon, "not-throwwable") - @inversify.named("not-throwwable") - public primaryWeapon: IWeapon; - - @propertyInjectNammed(TYPES.IWeapon, "throwwable") - @inversify.named("throwwable") - public secondaryWeapon: IWeapon; - - } - - let propertyInjectTagged = inversify.makePropertyInjectTaggedDecorator(kernel); - - class Warrior3 { - - @propertyInjectTagged(TYPES.IWeapon, "throwwable", false) - @inversify.tagged("throwwable", false) - public primaryWeapon: IWeapon; - - @propertyInjectTagged(TYPES.IWeapon, "throwwable", true) - @inversify.tagged("throwwable", true) - public secondaryWeapon: IWeapon; - - } - - kernel.snapshot(); - kernel.restore(); - -} diff --git a/type_definitions/inversify/inversify-test.ts b/type_definitions/inversify/inversify-test.ts deleted file mode 100644 index 414e4dde6..000000000 --- a/type_definitions/inversify/inversify-test.ts +++ /dev/null @@ -1,440 +0,0 @@ -/// - -import { - Kernel, - injectable, tagged, named, targetName, inject, multiInject, - IKernel, INewable, IContext, IKernelModule, IFactory, IProvider, IRequest, - traverseAncerstors, taggedConstraint, namedConstraint, typeConstraint, - makePropertyMultiInjectDecorator, - makePropertyInjectTaggedDecorator, - makePropertyInjectNamedDecorator, - makePropertyInjectDecorator, -} from "inversify"; - -import * as Proxy from "harmony-proxy"; - -module external_module_test { - - interface INinja { - fight(): string; - sneak(): string; - } - - interface IKatana { - hit(): string; - } - - interface IShuriken { - throw(): string; - } - - @injectable() - class Katana implements IKatana { - public hit() { - return "cut!"; - } - } - - @injectable() - class Shuriken implements IShuriken { - public throw() { - return "hit!"; - } - } - - @injectable() - class Ninja implements INinja { - - private _katana: IKatana; - private _shuriken: IShuriken; - - public constructor( - @inject("IKatana") katana: IKatana, - @inject("IShuriken") shuriken: IShuriken - ) { - this._katana = katana; - this._shuriken = shuriken; - } - - public fight() { return this._katana.hit(); }; - public sneak() { return this._shuriken.throw(); }; - - } - - let kernel = new Kernel(); - kernel.bind("INinja").to(Ninja); - kernel.bind("IKatana").to(Katana); - kernel.bind("IShuriken").to(Shuriken).inSingletonScope(); - - let ninja = kernel.get("INinja"); - console.log(ninja); - - // Unbind - kernel.unbind("INinja"); - kernel.unbindAll(); - - // Kernel modules - let warriors: IKernelModule = (k: IKernel) => { - k.bind("INinja").to(Ninja); - }; - - let weapons: IKernelModule = (k: IKernel) => { - k.bind("IKatana").to(Katana); - k.bind("IShuriken").to(Shuriken).inSingletonScope(); - }; - - kernel = new Kernel(); - kernel.load(warriors, weapons); - let ninja2 = kernel.get("INinja"); - console.log(ninja2); - - // middleware - function logger(next: (context: IContext) => any) { - return (context: IContext) => { - let result = next(context); - console.log("CONTEXT: ", context); - console.log("RESULT: ", result); - return result; - }; - }; - - function visualReporter(next: (context: IContext) => any) { - return (context: IContext) => { - let result = next(context); - let _window: any = window; - let devTools = _window.__inversify_devtools__; - if (devTools !== undefined) { devTools.log(context, result); } - return result; - }; - }; - - kernel.applyMiddleware(logger, visualReporter); - - // binding types - kernel.bind("IKatana").to(Katana); - kernel.bind("IKatana").toConstantValue(new Katana()); - kernel.bind("IKatana").toDynamicValue(() => { return new Katana(); }); - - kernel.bind>("IKatana").toConstructor(Katana); - - kernel.bind>("IKatana").toFactory((context) => { - return () => { - return kernel.get("IKatana"); - }; - }); - - kernel.bind>("IKatana").toAutoFactory("IKatana"); - - kernel.bind>("IKatana").toProvider((context) => { - return () => { - return new Promise((resolve) => { - let katana = kernel.get("IKatana"); - resolve(katana); - }); - }; - }); - - kernel.bind("IKatana").to(Katana).onActivation((context: IContext, katanaToBeInjected: IKatana) => { - 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); - }); - - interface IWeapon {} - interface ISamurai { - katana: IWeapon; - shuriken: IWeapon; - } - - @injectable() - class Samurai implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inject("IWeapon") @tagged("canThrow", false) katana: IWeapon, - @inject("IWeapon") @tagged("canThrow", true) shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("Samurai").to(Samurai); - kernel.bind("IWeapon").to(Katana).whenTargetTagged("canThrow", false); - kernel.bind("IWeapon").to(Shuriken).whenTargetTagged("canThrow", true); - - let throwable = tagged("canThrow", true); - let notThrowable = tagged("canThrow", false); - - @injectable() - class Samurai2 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inject("IWeapon") @throwable katana: IWeapon, - @inject("IWeapon") @notThrowable shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - @injectable() - class Samurai3 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inject("IWeapon") @named("strong") katana: IWeapon, - @inject("IWeapon") @named("weak") shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("ISamurai").to(Samurai3); - kernel.bind("IWeapon").to(Katana).whenTargetNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenTargetNamed("weak"); - - @injectable() - class Samurai4 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @inject("IWeapon") @targetName("katana") katana: IWeapon, - @inject("IWeapon") @targetName("shuriken") shuriken: IWeapon - ) { - this.katana = katana; - this.shuriken = shuriken; - } - } - - kernel.bind("ISamurai").to(Samurai4); - - kernel.bind("IWeapon").to(Katana).when((request: IRequest) => { - return request.target.name.equals("katana"); - }); - - kernel.bind("IWeapon").to(Shuriken).when((request: IRequest) => { - return request.target.name.equals("shuriken"); - }); - - // custom constraints - let whenParentNamedCanThrowConstraint = (request: IRequest) => { - return namedConstraint("canThrow")(request.parentRequest); - }; - - let whenAnyAncestorIsConstraint = (request: IRequest) => { - return traverseAncerstors(request, typeConstraint(Ninja)); - }; - - let whenAnyAncestorTaggedConstraint = (request: IRequest) => { - return traverseAncerstors(request, taggedConstraint("canThrow")(true)); - }; - - kernel.bind("IWeapon").to(Shuriken).when(whenParentNamedCanThrowConstraint); - kernel.bind("IWeapon").to(Shuriken).when(whenAnyAncestorIsConstraint); - kernel.bind("IWeapon").to(Shuriken).when(whenAnyAncestorTaggedConstraint); - - // Constraint helpers - kernel.bind("IWeapon").to(Shuriken).whenInjectedInto(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenInjectedInto("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenParentNamed("chinese"); - kernel.bind("IWeapon").to(Shuriken).whenParentTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenTargetNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenTargetTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorIs(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorIs("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenAnyAncestorMatches(whenParentNamedCanThrowConstraint); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorIs(Ninja); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorIs("INinja"); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorNamed("strong"); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorTagged("canThrow", true); - kernel.bind("IWeapon").to(Shuriken).whenNoAncestorMatches(whenParentNamedCanThrowConstraint); - - // multi-injection - @injectable() - class Samurai5 implements ISamurai { - public katana: IWeapon; - public shuriken: IWeapon; - public constructor( - @multiInject("IWeapon") wpns: IWeapon[] - ) { - this.katana = wpns[0]; - this.shuriken = wpns[1]; - } - } - - // symbols - let SYMBOLS = { - IKatana: Symbol("IKatana"), - INinja: Symbol("INinja"), - IShuriken: Symbol("IShuriken"), - }; - - @injectable() - class Ninja1 implements INinja { - - private _katana: IKatana; - private _shuriken: IShuriken; - - public constructor( - @inject(SYMBOLS.IKatana) katana: IKatana, - @inject(SYMBOLS.IShuriken) shuriken: IShuriken - ) { - this._katana = katana; - this._shuriken = shuriken; - } - - public fight() { return this._katana.hit(); }; - public sneak() { return this._shuriken.throw(); }; - - } - - let kernel3 = new Kernel(); - kernel3.bind(SYMBOLS.INinja).to(Ninja); - kernel3.bind(SYMBOLS.IKatana).to(Katana); - kernel3.bind(SYMBOLS.IShuriken).to(Shuriken).inSingletonScope(); - - let ninja4 = kernel3.get("INinja"); - console.log(ninja4); - - // classes - - @injectable() - class Ninja2 implements INinja { - - 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).to(Ninja); - kernel4.bind(Katana).to(Katana); - kernel4.bind(Shuriken).to(Shuriken).inSingletonScope(); - - let ninja5 = kernel4.get("INinja"); - console.log(ninja5); - -} - -module property_injection { - - let kernel = new Kernel(); - - let TYPES = { IWeapon: "IWeapon" }; - - interface IWeapon { - durability: number; - use(): void; - } - - @injectable() - class Sword implements IWeapon { - public durability: number; - public constructor() { - this.durability = 100; - } - public use() { - this.durability = this.durability - 10; - } - } - - @injectable() - class WarHammer implements IWeapon { - public durability: number; - public constructor() { - this.durability = 100; - } - public use() { - this.durability = this.durability - 10; - } - } - - let propertyMultiInject = makePropertyMultiInjectDecorator(kernel); - - class Warrior1 { - @propertyMultiInject(TYPES.IWeapon) - public weapons: IWeapon[]; - } - - let propertyInject = makePropertyInjectDecorator(kernel); - - interface ISomeService { - count: number; - increment(): void; - } - - @injectable() - class SomeService implements ISomeService { - public count: number; - public constructor() { - this.count = 0; - } - public increment() { - this.count = this.count + 1; - } - } - - class SomeWebComponent { - @propertyInject("ISomeService") - private _service: ISomeService; - public doSomething() { - let count = this._service.count; - this._service.increment(); - return count; - } - } - - let propertyInjectNammed = makePropertyInjectNamedDecorator(kernel); - - class Warrior2 { - - @propertyInjectNammed(TYPES.IWeapon, "not-throwwable") - @named("not-throwwable") - public primaryWeapon: IWeapon; - - @propertyInjectNammed(TYPES.IWeapon, "throwwable") - @named("throwwable") - public secondaryWeapon: IWeapon; - - } - - let propertyInjectTagged = makePropertyInjectTaggedDecorator(kernel); - - class Warrior3 { - - @propertyInjectTagged(TYPES.IWeapon, "throwwable", false) - @tagged("throwwable", false) - public primaryWeapon: IWeapon; - - @propertyInjectTagged(TYPES.IWeapon, "throwwable", true) - @tagged("throwwable", true) - public secondaryWeapon: IWeapon; - - } - - kernel.snapshot(); - kernel.restore(); - -} diff --git a/type_definitions/inversify/inversify.d.ts b/type_definitions/inversify/inversify.d.ts deleted file mode 100644 index fd35d06b3..000000000 --- a/type_definitions/inversify/inversify.d.ts +++ /dev/null @@ -1,201 +0,0 @@ -// Type definitions for inversify 2.0.0-beta.4 -// Project: https://github.com/inversify/InversifyJS -// Definitions by: inversify -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -/// -/// - -interface Symbol { - toString(): string; - valueOf(): Object; -} - -interface SymbolConstructor { - (description?: string|number): Symbol; -} - -declare var Symbol: SymbolConstructor; - -declare namespace inversify { - - export interface IKernelConstructor { - new(): IKernel; - } - - export interface IMiddleware extends Function { - (next: (context: IContext) => any): (context: IContext) => any; - } - - export interface IKernel { - bind(serviceIdentifier: (string|Symbol|INewable)): IBindingToSyntax; - unbind(serviceIdentifier: (string|Symbol|any)): void; - unbindAll(): void; - get(serviceIdentifier: (string|Symbol|INewable)): T; - getNamed(serviceIdentifier: (string|Symbol|INewable), named: string): T; - getTagged(serviceIdentifier: (string|Symbol|INewable), key: string, value: any): T; - getAll(serviceIdentifier: (string|Symbol|INewable)): T[]; - load(...modules: IKernelModule[]): void; - applyMiddleware(...middleware: IMiddleware[]): void; - getServiceIdentifierAsString(serviceIdentifier: (string|Symbol|INewable)): string; - snapshot(): void; - restore(): void; - } - - export interface IKernelModule extends Function { - (kernel: IKernel): void; - } - - interface IBindingOnSyntax { - onActivation(fn: (context: IContext, injectable: T) => T): IBindingWhenSyntax; - } - - interface IBindingInSyntax { - inSingletonScope(): IBindingWhenOnSyntax; - } - - interface IBindingWhenSyntax { - when(constraint: (request: IRequest) => boolean): IBindingOnSyntax; - whenTargetNamed(name: string): IBindingOnSyntax; - whenTargetTagged(tag: string, value: any): IBindingOnSyntax; - whenInjectedInto(parent: (Function|string)): IBindingOnSyntax; - whenParentNamed(name: string): IBindingOnSyntax; - whenParentTagged(tag: string, value: any): IBindingOnSyntax; - whenAnyAncestorIs(ancestor: (Function|string)): IBindingOnSyntax; - whenNoAncestorIs(ancestor: (Function|string)): IBindingOnSyntax; - whenAnyAncestorNamed(name: string): IBindingOnSyntax; - whenAnyAncestorTagged(tag: string, value: any): IBindingOnSyntax; - whenNoAncestorNamed(name: string): IBindingOnSyntax; - whenNoAncestorTagged(tag: string, value: any): IBindingOnSyntax; - whenAnyAncestorMatches(constraint: (request: IRequest) => boolean): IBindingOnSyntax; - whenNoAncestorMatches(constraint: (request: IRequest) => boolean): IBindingOnSyntax; - } - - interface IBindingToSyntax { - to(constructor: { new(...args: any[]): T; }): IBindingInWhenOnSyntax; - toConstantValue(value: T): IBindingWhenOnSyntax; - toDynamicValue(func: () => T): IBindingWhenOnSyntax; - toConstructor(constructor: INewable): IBindingWhenOnSyntax; - toFactory(factory: IFactoryCreator): IBindingWhenOnSyntax; - toAutoFactory(serviceIdentifier: (string|Symbol|T2)): IBindingWhenOnSyntax; - toProvider(provider: IProviderCreator): IBindingWhenOnSyntax; - } - - interface IBindingInWhenOnSyntax extends IBindingInSyntax, IBindingWhenOnSyntax {} - interface IBindingWhenOnSyntax extends IBindingWhenSyntax, IBindingOnSyntax {} - - export interface IFactory extends Function { - (...args: any[]): (((...args: any[]) => T)|T); - } - - interface IFactoryCreator extends Function { - (context: IContext): IFactory; - } - - export interface INewable { - new(...args: any[]): T; - } - - export interface IProvider extends Function { - (): Promise; - } - - interface IProviderCreator extends Function { - (context: IContext): IProvider; - } - - export interface IContext { - kernel: IKernel; - plan: IPlan; - addPlan(plan: IPlan): void; - } - - export interface IPlan { - parentContext: IContext; - rootRequest: IRequest; - } - - export interface IRequest { - serviceIdentifier: (string|Symbol|INewable); - parentContext: IContext; - parentRequest: IRequest; - childRequests: IRequest[]; - target: ITarget; - bindings: IBinding[]; - addChildRequest( - serviceIdentifier: (string|Symbol|INewable), - bindings: (IBinding|IBinding[]), - target: ITarget): IRequest; - } - - export interface IBinding { - activated: boolean; - serviceIdentifier: (string|Symbol|INewable); - implementationType: INewable; - factory: IFactoryCreator; - provider: IProviderCreator; - constraint: (request: IRequest) => boolean; - onActivation: (context: IContext, injectable: T) => T; - cache: T; - dynamicValue: () => T; - scope: number; // BindingScope - type: number; // BindingType - } - - export interface ITarget { - serviceIdentifier: (string|Symbol|INewable); - name: IQueryableString; - metadata: Array; - hasTag(key: string): boolean; - isArray(): boolean; - matchesArray(name: string|Symbol|any): boolean; - isNamed(): boolean; - isTagged(): boolean; - matchesNamedTag(name: string): boolean; - matchesTag(key: string): (value: any) => boolean; - } - - export interface IQueryableString { - startsWith(searchString: string): boolean; - endsWith(searchString: string): boolean; - contains(searchString: string): boolean; - equals(compareString: string): boolean; - value(): string; - } - - export interface IMetadata { - key: string; - value: any; - } - - export var Kernel: IKernelConstructor; - export var decorate: (decorator: (ClassDecorator|ParameterDecorator), target: any, parameterIndex?: number) => void; - export function injectable(): (typeConstructor: any) => void; - export function tagged(metadataKey: string, metadataValue: any): (target: any, targetKey: string, index?: number) => any; - export function named(name: string): (target: any, targetKey: string, index?: number) => any; - export function targetName(name: string): (target: any, targetKey: string, index: number) => any; - export function inject(serviceIdentifier: (string|Symbol|any)): (target: any, targetKey: string, index?: number) => any; - export function multiInject(serviceIdentifier: (string|Symbol|any)): (target: any, targetKey: string, index?: number) => any; - - export function makePropertyInjectDecorator(kernel: IKernel): - (serviceIdentifier: (string|Symbol|INewable)) => (proto: any, key: string) => void; - - export function makePropertyInjectNamedDecorator(kernel: IKernel): - (serviceIdentifier: (string|Symbol|INewable), named: string) => (proto: any, key: string) => void; - - export function makePropertyInjectTaggedDecorator(kernel: IKernel): - (serviceIdentifier: (string|Symbol|INewable), key: string, value: any) => (proto: any, propertyName: string) => void; - - export function makePropertyMultiInjectDecorator(kernel: IKernel): - (serviceIdentifier: (string|Symbol|INewable)) => (proto: any, key: string) => void; - - // constraint helpers - export var traverseAncerstors: (request: IRequest, constraint: (request: IRequest) => boolean) => boolean; - export var taggedConstraint: (tag: string) => (value: any) => (request: IRequest) => boolean; - export var namedConstraint: (value: any) => (request: IRequest) => boolean; - export var typeConstraint: (type: (Function|string)) => (request: IRequest) => boolean; -} - -declare module "inversify" { - export = inversify; -}