Comparing version 4.3.3 to 5.0.0
@@ -54,3 +54,3 @@ 'use strict'; | ||
}; | ||
const hasLength = arr => arr == null || arr.length > 0; | ||
const hasLength = arr => arr != null && arr.length > 0; | ||
@@ -64,3 +64,3 @@ function constant(name, obj) { | ||
fn: () => obj, | ||
lifecycle: 'application', | ||
lifecycle: 'singleton', | ||
value: obj, | ||
@@ -82,3 +82,3 @@ resolved: true | ||
dependencies, | ||
lifecycle: opts.lifecycle || this.$$config.lifecycle | ||
lifecycle: opts.lifecycle | ||
}; | ||
@@ -196,4 +196,4 @@ if (opts.alias) { | ||
const cacheResult = (jpex, name, factory, value, namedParameters, withArg) => { | ||
switch (factory.lifecycle) { | ||
case 'application': | ||
switch (factory.lifecycle || jpex.$$config.lifecycle) { | ||
case 'singleton': | ||
factory.resolved = true; | ||
@@ -203,3 +203,3 @@ factory.value = value; | ||
break; | ||
case 'class': | ||
case 'container': | ||
jpex.$$resolved[name] = { ...factory, | ||
@@ -213,3 +213,3 @@ resolved: true, | ||
break; | ||
case 'instance': | ||
case 'invocation': | ||
default: | ||
@@ -257,2 +257,7 @@ namedParameters[name] = value; | ||
}; | ||
const invokeFactory = (jpex, name, factory, namedParameters, opts, args) => { | ||
const value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
}; | ||
const resolveFactory = (jpex, name, factory, namedParameters, opts, stack) => { | ||
@@ -269,5 +274,8 @@ if (factory == null) { | ||
} | ||
const value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
if (args instanceof Promise) { | ||
return args.then(args => { | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}); | ||
} | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}; | ||
@@ -292,6 +300,14 @@ const resolveOne = (jpex, name, initialParameters, opts, stack) => { | ||
} | ||
let isAsync = false; | ||
const dependencies = ensureArray(definition.dependencies); | ||
const values = dependencies.map(dependency => { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
const value = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
if (opts && opts.async && value instanceof Promise) { | ||
isAsync = true; | ||
} | ||
return value; | ||
}); | ||
if (isAsync) { | ||
return Promise.all(values); | ||
} | ||
return values; | ||
@@ -303,2 +319,7 @@ }; | ||
} | ||
function resolveAsync(name, opts) { | ||
return resolveOne(this, name, void 0, { ...opts, | ||
async: true | ||
}, []); | ||
} | ||
function resolveDependencies(definition, opts) { | ||
@@ -324,2 +345,6 @@ return resolveMany(this, definition, void 0, opts, []); | ||
let result; | ||
const invokeFn = (deps, args) => { | ||
result = fn.apply(jpex, deps); | ||
return result.apply(this, args); | ||
}; | ||
const encased = function encased(...args) { | ||
@@ -331,5 +356,9 @@ if (result && allResolved.call(jpex, dependencies)) { | ||
dependencies | ||
}, { | ||
async: true | ||
}); | ||
result = fn.apply(jpex, deps); | ||
return result.apply(this, args); | ||
if (deps instanceof Promise) { | ||
return deps.then(deps => invokeFn(deps, args)); | ||
} | ||
return invokeFn(deps, args); | ||
}; | ||
@@ -355,3 +384,3 @@ encased.encased = fn; | ||
const defaultConfig = { | ||
lifecycle: 'class', | ||
lifecycle: 'container', | ||
precedence: 'active', | ||
@@ -377,5 +406,7 @@ globals: true, | ||
factory, | ||
factoryAsync: factory, | ||
service, | ||
alias, | ||
resolve, | ||
resolveAsync, | ||
encase, | ||
@@ -392,2 +423,8 @@ clearCache, | ||
}, | ||
resolveAsyncWith(name, namedParameters, opts) { | ||
return this.resolveAsync(name, { | ||
with: namedParameters, | ||
...opts | ||
}); | ||
}, | ||
raw(name) { | ||
@@ -394,0 +431,0 @@ return getFactory(this, name, {}).fn; |
@@ -50,3 +50,3 @@ const isString = obj => typeof obj === 'string'; | ||
}; | ||
const hasLength = arr => arr == null || arr.length > 0; | ||
const hasLength = arr => arr != null && arr.length > 0; | ||
@@ -60,3 +60,3 @@ function constant(name, obj) { | ||
fn: () => obj, | ||
lifecycle: 'application', | ||
lifecycle: 'singleton', | ||
value: obj, | ||
@@ -78,3 +78,3 @@ resolved: true | ||
dependencies, | ||
lifecycle: opts.lifecycle || this.$$config.lifecycle | ||
lifecycle: opts.lifecycle | ||
}; | ||
@@ -192,4 +192,4 @@ if (opts.alias) { | ||
const cacheResult = (jpex, name, factory, value, namedParameters, withArg) => { | ||
switch (factory.lifecycle) { | ||
case 'application': | ||
switch (factory.lifecycle || jpex.$$config.lifecycle) { | ||
case 'singleton': | ||
factory.resolved = true; | ||
@@ -199,3 +199,3 @@ factory.value = value; | ||
break; | ||
case 'class': | ||
case 'container': | ||
jpex.$$resolved[name] = { ...factory, | ||
@@ -209,3 +209,3 @@ resolved: true, | ||
break; | ||
case 'instance': | ||
case 'invocation': | ||
default: | ||
@@ -253,2 +253,7 @@ namedParameters[name] = value; | ||
}; | ||
const invokeFactory = (jpex, name, factory, namedParameters, opts, args) => { | ||
const value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
}; | ||
const resolveFactory = (jpex, name, factory, namedParameters, opts, stack) => { | ||
@@ -265,5 +270,8 @@ if (factory == null) { | ||
} | ||
const value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
if (args instanceof Promise) { | ||
return args.then(args => { | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}); | ||
} | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}; | ||
@@ -288,6 +296,14 @@ const resolveOne = (jpex, name, initialParameters, opts, stack) => { | ||
} | ||
let isAsync = false; | ||
const dependencies = ensureArray(definition.dependencies); | ||
const values = dependencies.map(dependency => { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
const value = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
if (opts && opts.async && value instanceof Promise) { | ||
isAsync = true; | ||
} | ||
return value; | ||
}); | ||
if (isAsync) { | ||
return Promise.all(values); | ||
} | ||
return values; | ||
@@ -299,2 +315,7 @@ }; | ||
} | ||
function resolveAsync(name, opts) { | ||
return resolveOne(this, name, void 0, { ...opts, | ||
async: true | ||
}, []); | ||
} | ||
function resolveDependencies(definition, opts) { | ||
@@ -320,2 +341,6 @@ return resolveMany(this, definition, void 0, opts, []); | ||
let result; | ||
const invokeFn = (deps, args) => { | ||
result = fn.apply(jpex, deps); | ||
return result.apply(this, args); | ||
}; | ||
const encased = function encased(...args) { | ||
@@ -327,5 +352,9 @@ if (result && allResolved.call(jpex, dependencies)) { | ||
dependencies | ||
}, { | ||
async: true | ||
}); | ||
result = fn.apply(jpex, deps); | ||
return result.apply(this, args); | ||
if (deps instanceof Promise) { | ||
return deps.then(deps => invokeFn(deps, args)); | ||
} | ||
return invokeFn(deps, args); | ||
}; | ||
@@ -351,3 +380,3 @@ encased.encased = fn; | ||
const defaultConfig = { | ||
lifecycle: 'class', | ||
lifecycle: 'container', | ||
precedence: 'active', | ||
@@ -373,5 +402,7 @@ globals: true, | ||
factory, | ||
factoryAsync: factory, | ||
service, | ||
alias, | ||
resolve, | ||
resolveAsync, | ||
encase, | ||
@@ -388,2 +419,8 @@ clearCache, | ||
}, | ||
resolveAsyncWith(name, namedParameters, opts) { | ||
return this.resolveAsync(name, { | ||
with: namedParameters, | ||
...opts | ||
}); | ||
}, | ||
raw(name) { | ||
@@ -390,0 +427,0 @@ return getFactory(this, name, {}).fn; |
@@ -92,3 +92,3 @@ 'use strict'; | ||
var hasLength = function hasLength(arr) { | ||
return arr == null || arr.length > 0; | ||
return arr != null && arr.length > 0; | ||
}; | ||
@@ -105,3 +105,3 @@ | ||
}, | ||
lifecycle: 'application', | ||
lifecycle: 'singleton', | ||
value: obj, | ||
@@ -127,3 +127,3 @@ resolved: true | ||
dependencies: dependencies, | ||
lifecycle: opts.lifecycle || this.$$config.lifecycle | ||
lifecycle: opts.lifecycle | ||
}; | ||
@@ -252,4 +252,4 @@ if (opts.alias) { | ||
var cacheResult = function cacheResult(jpex, name, factory, value, namedParameters, withArg) { | ||
switch (factory.lifecycle) { | ||
case 'application': | ||
switch (factory.lifecycle || jpex.$$config.lifecycle) { | ||
case 'singleton': | ||
factory.resolved = true; | ||
@@ -259,3 +259,3 @@ factory.value = value; | ||
break; | ||
case 'class': | ||
case 'container': | ||
jpex.$$resolved[name] = _extends({}, factory, { | ||
@@ -269,3 +269,3 @@ resolved: true, | ||
break; | ||
case 'instance': | ||
case 'invocation': | ||
default: | ||
@@ -318,2 +318,7 @@ namedParameters[name] = value; | ||
}; | ||
var invokeFactory = function invokeFactory(jpex, name, factory, namedParameters, opts, args) { | ||
var value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
}; | ||
var resolveFactory = function resolveFactory(jpex, name, factory, namedParameters, opts, stack) { | ||
@@ -330,5 +335,8 @@ if (factory == null) { | ||
} | ||
var value = factory.fn.apply(jpex, args); | ||
cacheResult(jpex, name, factory, value, namedParameters, opts == null ? void 0 : opts.with); | ||
return value; | ||
if (args instanceof Promise) { | ||
return args.then(function (args) { | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}); | ||
} | ||
return invokeFactory(jpex, name, factory, namedParameters, opts, args); | ||
}; | ||
@@ -356,6 +364,14 @@ var resolveOne = function resolveOne(jpex, name, initialParameters, opts, stack) { | ||
} | ||
var isAsync = false; | ||
var dependencies = ensureArray(definition.dependencies); | ||
var values = dependencies.map(function (dependency) { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
var value = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
if (opts && opts.async && value instanceof Promise) { | ||
isAsync = true; | ||
} | ||
return value; | ||
}); | ||
if (isAsync) { | ||
return Promise.all(values); | ||
} | ||
return values; | ||
@@ -367,2 +383,7 @@ }; | ||
} | ||
function resolveAsync(name, opts) { | ||
return resolveOne(this, name, void 0, _extends({}, opts, { | ||
async: true | ||
}), []); | ||
} | ||
function resolveDependencies(definition, opts) { | ||
@@ -386,4 +407,9 @@ return resolveMany(this, definition, void 0, opts, []); | ||
function encase(dependencies, fn) { | ||
var _this = this; | ||
var jpex = this; | ||
var result; | ||
var invokeFn = function invokeFn(deps, args) { | ||
result = fn.apply(jpex, deps); | ||
return result.apply(_this, args); | ||
}; | ||
var encased = function encased() { | ||
@@ -398,5 +424,11 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
dependencies: dependencies | ||
}, { | ||
async: true | ||
}); | ||
result = fn.apply(jpex, deps); | ||
return result.apply(this, args); | ||
if (deps instanceof Promise) { | ||
return deps.then(function (deps) { | ||
return invokeFn(deps, args); | ||
}); | ||
} | ||
return invokeFn(deps, args); | ||
}; | ||
@@ -425,3 +457,3 @@ encased.encased = fn; | ||
var defaultConfig = { | ||
lifecycle: 'class', | ||
lifecycle: 'container', | ||
precedence: 'active', | ||
@@ -445,5 +477,7 @@ globals: true, | ||
factory: factory, | ||
factoryAsync: factory, | ||
service: service, | ||
alias: alias, | ||
resolve: resolve, | ||
resolveAsync: resolveAsync, | ||
encase: encase, | ||
@@ -459,2 +493,7 @@ clearCache: clearCache, | ||
}, | ||
resolveAsyncWith: function resolveAsyncWith(name, namedParameters, opts) { | ||
return this.resolveAsync(name, _extends({ | ||
with: namedParameters | ||
}, opts)); | ||
}, | ||
raw: function raw(name) { | ||
@@ -461,0 +500,0 @@ return getFactory(this, name, {}).fn; |
@@ -5,1 +5,2 @@ export { default as constant } from './constant'; | ||
export { default as alias } from './alias'; | ||
export { default as factoryAsync } from './factoryAsync'; |
@@ -1,6 +0,7 @@ | ||
import { JpexInstance, Definition, Dependency, ResolveOpts } from '../types'; | ||
import { JpexInstance, Dependency, ResolveOpts, Factory } from '../types'; | ||
export { getFactory } from './utils'; | ||
export declare function resolve(this: JpexInstance, name: Dependency, opts?: ResolveOpts): unknown; | ||
export declare function resolveDependencies(this: JpexInstance, definition: Definition, opts?: ResolveOpts): any[]; | ||
export declare function resolve(this: JpexInstance, name: Dependency, opts?: ResolveOpts): any; | ||
export declare function resolveAsync(this: JpexInstance, name: Dependency, opts?: ResolveOpts): any; | ||
export declare function resolveDependencies(this: JpexInstance, definition: Factory, opts?: ResolveOpts): any[] | Promise<any[]>; | ||
export declare function isResolved(this: JpexInstance, dependency: Dependency): boolean; | ||
export declare function allResolved(this: JpexInstance, dependencies: Dependency[]): boolean; |
@@ -1,3 +0,3 @@ | ||
import { JpexInstance, Dependency, Definition, NamedParameters, ResolveOpts } from '../types'; | ||
export declare const resolveOne: <R extends unknown>(jpex: JpexInstance, name: Dependency, initialParameters: NamedParameters, opts: ResolveOpts, stack: string[]) => R; | ||
export declare const resolveMany: <R extends any[]>(jpex: JpexInstance, definition: Definition, namedParameters: NamedParameters, opts: ResolveOpts, stack?: string[]) => R; | ||
import { JpexInstance, Dependency, NamedParameters, ResolveOpts, Factory } from '../types'; | ||
export declare const resolveOne: (jpex: JpexInstance, name: Dependency, initialParameters: NamedParameters, opts: ResolveOpts, stack: string[]) => any | Promise<any>; | ||
export declare const resolveMany: (jpex: JpexInstance, definition: Factory, namedParameters: NamedParameters, opts: ResolveOpts, stack?: string[]) => any[] | Promise<any[]>; |
import { Factory, JpexInstance, ResolveOpts, NamedParameters, Dependency } from '../types'; | ||
export declare const getFactory: (jpex: JpexInstance, name: string, opts?: ResolveOpts) => import("../types/base").Factory; | ||
export declare const getFactory: (jpex: JpexInstance, name: string, opts?: ResolveOpts) => Factory; | ||
export declare const cacheResult: (jpex: JpexInstance, name: string, factory: Factory, value: any, namedParameters: NamedParameters, withArg: Record<string, any>) => void; | ||
export declare const checkStack: (jpex: JpexInstance, name: Dependency, stack: string[]) => boolean; |
@@ -1,2 +0,2 @@ | ||
export declare type Lifecycle = 'application' | 'class' | 'instance' | 'none'; | ||
export declare type Lifecycle = 'singleton' | 'container' | 'invocation' | 'none'; | ||
export declare type Precedence = 'active' | 'passive'; | ||
@@ -16,2 +16,3 @@ export declare type AnyFunction<R = any> = (...args: any[]) => R; | ||
value?: any; | ||
with?: Record<string, any>; | ||
} |
@@ -5,18 +5,1 @@ export * from './base'; | ||
export * from './custom'; | ||
export declare type Lifecycle = 'application' | 'class' | 'instance' | 'none'; | ||
export declare type Precedence = 'active' | 'passive'; | ||
export declare type AnyFunction<R = any> = (...args: any[]) => R; | ||
export interface AnyConstructor<T = any> { | ||
new (...args: any[]): T; | ||
} | ||
export declare type Dependency = string; | ||
export interface Definition { | ||
dependencies?: Dependency[]; | ||
} | ||
export interface Factory extends Definition { | ||
fn: AnyFunction; | ||
lifecycle: Lifecycle; | ||
resolved?: boolean; | ||
value?: any; | ||
with?: Record<string, any>; | ||
} |
@@ -22,2 +22,3 @@ import type { Lifecycle, AnyFunction, Dependency, AnyConstructor, Factory, Precedence } from './base'; | ||
with?: NamedParameters; | ||
async?: boolean; | ||
} | ||
@@ -31,2 +32,5 @@ export interface JpexInstance { | ||
factory<T>(name: string, deps: Dependency[], fn: AnyFunction<T>, opts?: FactoryOpts): void; | ||
factoryAsync(name: string, deps: Dependency[], fn: AnyFunction, opts?: FactoryOpts): void; | ||
factoryAsync<T>(fn: AnyFunction<Promise<T>>, opts?: FactoryOpts): void; | ||
factoryAsync<T>(name: string, deps: Dependency[], fn: AnyFunction<Promise<T>>, opts?: FactoryOpts): void; | ||
service(name: string, deps: Dependency[], fn: AnyConstructor | AnyFunction, opts?: ServiceOpts): void; | ||
@@ -43,2 +47,5 @@ service<T>(fn: AnyConstructor<T> | AnyFunction, opts?: ServiceOpts): void; | ||
resolve<T>(name: Dependency, opts?: ResolveOpts): T; | ||
resolveAsync(name: Dependency, opts?: ResolveOpts): Promise<any>; | ||
resolveAsync<T>(opts?: ResolveOpts): Promise<T>; | ||
resolveAsync<T>(name: Dependency, opts?: ResolveOpts): Promise<T>; | ||
resolveWith(name: Dependency, namedParameters: NamedParameters, opts?: ResolveOpts): any; | ||
@@ -58,2 +65,16 @@ resolveWith<T>(namedParameters: NamedParameters, opts?: ResolveOpts): T; | ||
resolveWith<T, A, B, C, D, E, F>(namedParameters: NamedParameters, opts?: ResolveOpts): T; | ||
resolveAsyncWith(name: Dependency, namedParameters: NamedParameters, opts?: ResolveOpts): any; | ||
resolveAsyncWith<T>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D, E>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D, E, F>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D, E>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
resolveAsyncWith<T, A, B, C, D, E, F>(namedParameters: NamedParameters, opts?: ResolveOpts): Promise<T>; | ||
encase<F extends AnyFunction<AnyFunction>>(dependencies: Dependency[], fn: F): ReturnType<F> & { | ||
@@ -60,0 +81,0 @@ encased: F; |
{ | ||
"name": "jpex", | ||
"version": "4.3.3", | ||
"version": "5.0.0", | ||
"description": "Javascript Prototype Extension", | ||
@@ -11,2 +11,3 @@ "main": "dist/cjs/jpex.js", | ||
"test": "jest", | ||
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand", | ||
"coverage": "jest --coverage", | ||
@@ -21,3 +22,3 @@ "lint": "eslint './src/**/*.ts' --fix && tsc --noEmit", | ||
"semantic-release": "semantic-release", | ||
"ci": "yarn lint && yarn test && yarn build", | ||
"ci": "yarn install && yarn lint && yarn test && yarn build", | ||
"prepare": "husky install" | ||
@@ -24,0 +25,0 @@ }, |
@@ -26,2 +26,3 @@ # ![Jpex](https://jpex-js.github.io/dist/jpex.svg) | ||
- [service](#jpexservice) | ||
- [factoryAsync](#jpexfactoryAsync) | ||
- [alias](#jpexalias) | ||
@@ -31,2 +32,3 @@ - [resolve](#jpexresolve) | ||
- [with](#with) | ||
- [resolveAsync][#jpexresolveasync] | ||
- [resolveWith](#jpexresolvewith) | ||
@@ -196,3 +198,3 @@ - [encase](#jpexencase) | ||
```ts | ||
'application' | 'class' | 'instance' | 'none'; | ||
'singleton' | 'container' | 'invocation' | 'none'; | ||
``` | ||
@@ -202,8 +204,8 @@ | ||
- `application` is resolved forever across all containers | ||
- `class` is resolved for the current jpex container, if you `.extend()` the new container will resolve it again | ||
- `instance` if you request the same dependency multiple times in the same `resolve` call, this will use the same value, but the next time you call `resolve` it will start again | ||
- `singleton` is resolved forever across all containers | ||
- `container` is resolved for the current jpex container, if you `.extend()` the new container will resolve it again | ||
- `invocation` if you request the same dependency multiple times in the same `resolve` call, this will use the same value, but the next time you call `resolve` it will start again | ||
- `none` never caches anything | ||
The default lifecycle is `class` | ||
The default lifecycle is `container` | ||
@@ -269,2 +271,10 @@ ##### precedence | ||
#### jpex.factoryAsync | ||
```ts | ||
<T>(fn: (...deps: any[] => Promise<T>), opts?: object): void | ||
``` | ||
Registers an asynchronous factory. The factory should return a promise that resolves to type `T`. If you are using async factories, you should ensure you are using `resolveAsync`, this will wait for asynchronous factories to resolve before passing them to their dependents. | ||
#### jpex.alias | ||
@@ -300,2 +310,10 @@ | ||
#### jpex.resolveAsync | ||
```ts | ||
<T>(opts?: object): Promise<T> | ||
``` | ||
Locates and resolves the desired factory. Unlike `resolve`, this method returns a promise and allows all asynchronous dependents to resolve before returning the final value. | ||
#### jpex.resolveWith | ||
@@ -313,2 +331,10 @@ | ||
#### jpex.resolveWithAsync | ||
```ts | ||
<T, ...Rest[]>(values: Rest, opts?: object): Promise<T> | ||
``` | ||
This is an asynchronous version of `resolveWith` and returns a promise that will resolve all dependent factories. | ||
#### jpex.encase | ||
@@ -330,2 +356,4 @@ | ||
The dependencies are only resolved at call time and are then cached and reused on subsequent calls (based on their lifecycles). | ||
To help with testing, the returned function also has an `encased` property containng the outer function | ||
@@ -337,2 +365,4 @@ | ||
> If you include any factoryAsync dependencies, jpex will ensure the encased function returns a promise as well. | ||
#### jpex.extend | ||
@@ -358,5 +388,5 @@ | ||
`'application' | 'class' | 'instance' | 'none'` | ||
`'singleton' | 'container' | 'invocation' | 'none'` | ||
The default lifecycle for factories. `class` by default | ||
The default lifecycle for factories. `container` by default | ||
@@ -516,2 +546,24 @@ ##### precedence | ||
## node | ||
Jpex was originally written for node and works out of the box in a node environment. However, the `@jpex-js/node` library was created to provide similar benefits to `react-jpex`. It can be used to create a provider/context pattern where the top level application (or testing environment) can choose which dependencies to inject into the app. | ||
```ts | ||
import { encase } from '@jpex-js/node'; | ||
const myFunction = encase((dep: MyDep) => () => { | ||
const someValue = dep(); | ||
}); | ||
``` | ||
```ts | ||
import { provide } from '@jpex-js/node'; | ||
provide((jpex) => { | ||
jpex.constant<MyDep>(mockDep); | ||
myFunction(); // will be called with mockDep | ||
}); | ||
``` | ||
## Vanilla JS mode | ||
@@ -518,0 +570,0 @@ |
75495
27
1484
572