Skip to content

Commit

Permalink
fix: getOwnPropertyDescriptor becomes invalid after optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Aug 31, 2022
1 parent 4feb1ae commit 2d38ab4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-trainers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'async-call-rpc': patch
---

fix getOwnPropertyDescriptor becomes invalid after optimized
30 changes: 12 additions & 18 deletions src/Async-Call-Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,30 +155,24 @@ export function AsyncGeneratorCall<OtherSideImplementedFunctions = {}>(
} as AsyncGeneratorInternalMethods
const remote = AsyncCall<AsyncGeneratorInternalMethods>(server, options)

const getTrap = new Proxy(
{},
{
get(_, method) {
if (!isString(method))
throw makeHostedMessage(Err_Only_string_can_be_the_RPC_method_name, new TypeError(''))
const f = {
[method]: (..._: unknown[]) => {
const id = remote[AsyncIteratorStart](method, _)
return new _AsyncGenerator(remote, id)
},
}[method]!
Object.defineProperty(methodContainer, method, { value: f, configurable: true })
return f
const getTrap = (_: any, method: PropertyKey) => {
if (!isString(method)) throw makeHostedMessage(Err_Only_string_can_be_the_RPC_method_name, new TypeError(''))
const f = {
[method]: (..._: unknown[]) => {
const id = remote[AsyncIteratorStart](method, _)
return new _AsyncGenerator(remote, id)
},
},
)
const methodContainer = { __proto__: getTrap } as any
}[method]!
Object.defineProperty(methodContainer, method, { value: f, configurable: true })
return f
}
const methodContainer: any = { __proto__: new Proxy({}, { get: getTrap }) }
return new Proxy(methodContainer, {
getPrototypeOf: () => null,
setPrototypeOf: (_, val) => val === null,
// some library will treat this object as a normal object and run algorithm steps in https://tc39.es/ecma262/#sec-ordinaryget
getOwnPropertyDescriptor(_, method) {
if (!(method in methodContainer)) (getTrap as any)[method] // trigger [[Get]]
if (!(method in methodContainer)) getTrap(_, method) // trigger [[Get]]
return Object.getOwnPropertyDescriptor(methodContainer, method)
},
}) as AsyncGeneratorVersionOf<OtherSideImplementedFunctions>
Expand Down
35 changes: 15 additions & 20 deletions src/Async-Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,20 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
})
})
}
const getTrap = new Proxy(
{},
{
get(_, method) {
const f = {
// This function will be logged to the console so it must be 1 line
[method]: (..._: unknown[]) => call(method, _, new Error().stack),
}[method as any]!
const f2 = {
[method]: (..._: unknown[]) => call(method, _, new Error().stack, true),
}[method as any]!
// @ts-expect-error
f[AsyncCallNotify] = f2[AsyncCallNotify] = f2
isString(method) && Object.defineProperty(methodContainer, method, { value: f, configurable: true })
return f
},
},
)
const methodContainer = { __proto__: getTrap } as any
const getTrap = (_: any, method: string | symbol) => {
const f = {
// This function will be logged to the console so it must be 1 line
[method]: (..._: unknown[]) => call(method, _, new Error().stack),
}[method as any]!
const f2 = {
[method]: (..._: unknown[]) => call(method, _, new Error().stack, true),
}[method as any]!
// @ts-expect-error
f[AsyncCallNotify] = f2[AsyncCallNotify] = f2
isString(method) && Object.defineProperty(methodContainer, method, { value: f, configurable: true })
return f
}
const methodContainer: any = { __proto__: new Proxy({}, { get: getTrap }) }
if (thenable === false) methodContainer.then = undefined
else if (thenable === undefined) {
Object.defineProperty(methodContainer, 'then', {
Expand All @@ -383,7 +378,7 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
setPrototypeOf: (_, value) => value === null,
// some library will treat this object as a normal object and run algorithm steps in https://tc39.es/ecma262/#sec-ordinaryget
getOwnPropertyDescriptor(_, method) {
if (!(method in methodContainer)) (getTrap as any)[method] // trigger [[Get]]
if (!(method in methodContainer)) getTrap(_, method) // trigger [[Get]]
return Object.getOwnPropertyDescriptor(methodContainer, method)
},
}) as AsyncVersionOf<OtherSideImplementedFunctions>
Expand Down

0 comments on commit 2d38ab4

Please sign in to comment.