DI-Proxy
Dependency Injection UMD Module using the Built-in Proxy.
Installation
$ npm i --save di-proxy
How to include in...
ES6 import (with babel):
CommonJS:
const inject wrap =
Browser:
Proxy
inject(resolver, [memoize]) ⇒ Creates an optionally memoized proxy that invokes the resolver from trapped property accesses.
Kind: global function
Returns: Proxy
- proxy - A proxy with a get trap that invokes the resolver.
Param | Type | Default | Description |
---|---|---|---|
resolver | resolver |
A function invoked within the get trap of returned proxy. | |
[memoize] | boolean |
true |
Shadow properties with result from first invocation of resolver. |
injector
wrap(resolver, callback) ⇒ Kind: global function
Returns: injector
- injector - A function that passes arguments after the proxy in callback when invoked.
Param | Type | Description |
---|---|---|
resolver | resolver |
A function invoked within the get trap of first argument to callback. |
callback | callback |
A function that accepts a proxy as the first argument. |
*
resolver ⇒ Kind: global typedef
Returns: *
- result - Synchronously resolved object referenced by key.
See: inject()
Param | Type | Description |
---|---|---|
key | string |
A reference to some sort of dependency or query. |
*
callback ⇒ Kind: global typedef
See: wrap()
Param | Type | Description |
---|---|---|
proxy | Proxy |
Bound parameter which is the result of inject(resolver, true) . |
...rest | * |
Arguments passed when invoking injector. |
*
injector ⇒ Kind: global typedef
Returns: *
- result - Result of invoking callback.
See: wrap()
Param | Type | Description |
---|---|---|
...args | * |
Passed as rest parameter to callback. |
Usage
const http express 'socket.io': sio = const app = const server = httpconst io = // ...
Other examples
const 'input#file-input': $file 'ul#preview-names': $previewNames } = diProxy $file
const qs = diProxy
const ssdi = diProxy sessionStoragesessionStorage // get some session propertieslet test another = ssdi const formattedText = `test: , another: `documenttextContent = formattedText
Dependencies and Supported Environments
This assumes that the environment has a working implementation for Proxy
.
All modern browsers including Microsoft Edge, and Node.js >=6.0.0
are supported, according to caniuse.com and kangax/compat-table.
Some Notes on Performance
Internally this dependency injection uses Proxy
. However, any performance implications have been mitigated by optionally memoizing each property accessor by default:
const string = diProxy const value = stringsomePropertyName // 'get trap invoked!'console // 'somePropertyName'stringsomePropertyName // nothing will be printed heredelete stringsomePropertyNamestringsomePropertyName // 'get trap invoked!'
You should be careful when using the default behavior in coordination with a query function for live data (like jQuery
, for example), as it will memoize the resolver with the result of the first call for each property name unless you delete
it from the proxy
object before each subsequent call, or set the optional memoize
parameter to false
when calling inject()
.
This library allows you to re-use a created proxy or injector without taking a performance hit from the overhead of a naive proxy implementation.
License
Available under the MIT License (c) 2017 Patrick Roberts