async-proxy
Create asynchronous nesting proxy in Node.js and browser.
Installation
npm install --save async-proxy
# or
yarn add async-proxy
import createAsyncProxy from 'async-proxy'
Usage
Just modify handlers to suit your project.
Default handler(get, set, remove methods come from object-path-operator module):
{
async get(target, path) {
return get(target, path)
}
, async apply(target, path, caller, args) {
return Reflect.apply(get(target, path), caller, args)
}
, async set(target, path, value) {
set(target, path, value) // The return value will be ignored
}
, async deleteProperty(target, path) {
remove(target, path) // The return value will be ignored
}
, async construct(target, path, args) {
return Reflect.construct(get(target, path), args)
}
, async setPrototypeOf(target, path, prototype) {
return Reflect.setPrototypeOf(get(target, path), prototype) // The return value will be ignored
}
, async getPrototypeOf(target, path) {
return Reflect.getPrototypeOf(get(target, path))
}
, async defineProperty(target, path, prop, descriptor) {
return Reflect.defineProperty(get(target, path), prop, descriptor) // The return value will be ignored
}
}
Example
create
const localBaseObj = {}
const proxy = createAsyncProxy(localBaseObj, {})
get
;(async () => {
await proxy.something.asynchronous.property
})()
apply
;(async () => {
// handled by apply handler
await proxy.something.asynchronous.method(some, args)
// handled by get handler
;(await proxy.something.asynchronous.method)(some, args)
})()
set
;(async () => {
// The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
// Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
proxy.something.asynchronous.property = 'something'
})()
delete
;(async () => {
// The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
// Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
delete proxy.something.asynchronous.property
})()
new
;(async () => {
// handled by construct handler
await new proxy.something.asynchronous.construct()
// handled by get handler
new (await proxy.something.construct)()
})()
for await
;(async () => {
for await (const prop of await proxy.something.enumerable) {
...
}
})()
setPrototypeOf
;(async () => {
// The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
// Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
Object.setPrototypeOf(proxy.something.asynchronous.something, Array.prototype)
})
getPrototypeOf
;(async () => {
// handled by getPrototypeOf handler
await Object.getPrototypeOf(proxy.something.asynchronous.property)
// handled by get handler
Object.getPrototypeOf(await proxy.something.asynchronous.property)
})()
defineProperty
;(async () => {
// The return value is not reliable, ou should check the results by other means to ensure that the operation is successful.
// Non-blocking when it is an asynchronous operation, so your asynchronous operation should have an inside sequence queue.
Object.defineProperty(proxy.something.asynchronous, 'property', {
value: 'something'
})
})()
API
Table of Contents
createAsyncProxy
Create a Proxy to asynchronous operate object.
Parameters
-
target
Object Target object (optional, default{}
) -
handlers
Object Async Proxy handlers (optional, default{}
)-
handlers.get
function (target: Object, path: Array<string>): Promise<any> "get" handler -
handlers.apply
function (target: Object, path: Array<string>, caller: Object, args: Array<any>): Promsie<any> "apply" handler -
handlers.set
function (target: Object, path: Array<string>, value: any): void "set" handler -
handlers.deleteProperty
function (target: Object, path: Array<string>): void "deleteProperty" handler -
handlers.construct
function (target: Object, path: Array<string>, args: Array<any>): Promise<any> "construct" handler -
handlers.setPrototypeOf
function (target: Object, path: Array<string>, prototype: Object): void "setPrototypeOf" handler -
handlers.getPrototypeOf
function (target: Object, path: Array<string>): Promise<any> "getPrototypeOf" handler -
handlers.defineProperty
function (target: Object, path: Array<string>, prop: string, descriptor: Object): void "defineProperty" handler
-
Returns Proxy Async Proxy