Create asynchronous nesting proxy in Node.js and browser
npm install async-proxyCreate asynchronous nesting proxy in Node.js and browser.
``sh`
npm install --save async-proxyor
yarn add async-proxy
`javascript`
import createAsyncProxy from 'async-proxy'
Just modify handlers to suit your project.
Default handler(get, set, remove methods come from object-path-operator module):
`javascript`
{
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
}
}
#### create
`javascript
const localBaseObj = {}
const proxy = createAsyncProxy(localBaseObj, {})
`
#### get
`javascript`
;(async () => {
await proxy.something.asynchronous.property
})()
#### apply
`javascript
;(async () => {
// handled by apply handler
await proxy.something.asynchronous.method(some, args)
// handled by get handler
;(await proxy.something.asynchronous.method)(some, args)
})()
`
#### set
`javascript`
;(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
`javascript`
;(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
`javascript
;(async () => {
// handled by construct handler
await new proxy.something.asynchronous.construct()
// handled by get handler
new (await proxy.something.construct)()
})()
`
#### for await
`javascript`
;(async () => {
for await (const prop of await proxy.something.enumerable) {
...
}
})()
#### setPrototypeOf
`javascript`
;(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
`javascript
;(async () => {
// handled by getPrototypeOf handler
await Object.getPrototypeOf(proxy.something.asynchronous.property)
// handled by get handler
Object.getPrototypeOf(await proxy.something.asynchronous.property)
})()
`
#### defineProperty
`javascript`
;(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'
})
})()
#### Table of Contents
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" handlerhandlers.apply
- function (target: Object, path: Array<string>, caller: Object, args: Array<any>): Promsie<any> "apply" handlerhandlers.set
- function (target: Object, path: Array<string>, value: any): void "set" handlerhandlers.deleteProperty
- function (target: Object, path: Array<string>): void "deleteProperty" handlerhandlers.construct
- function (target: Object, path: Array<string>, args: Array<any>): Promise<any> "construct" handlerhandlers.setPrototypeOf
- function (target: Object, path: Array<string>, prototype: Object): void "setPrototypeOf" handlerhandlers.getPrototypeOf
- function (target: Object, path: Array<string>): Promise<any> "getPrototypeOf" handlerhandlers.defineProperty` function (target: Object, path: Array<string>, prop: string, descriptor: Object): void "defineProperty" handler
-
Returns Proxy Async Proxy