Skip to content

Commit

Permalink
fix: trigger getters once then cache result on cloned object
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Mar 19, 2022
1 parent 735f612 commit b68456b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,13 +1022,18 @@ function fillTransferList(data: any, transferList: TransferListItem[]): any {
}
if (data && data.constructor === Object) {
let cloned = false
for (const [key, value] of Object.entries(data)) {
for (const key of Object.getOwnPropertyNames(data)) {
const value = data[key]
if (isTransferable(value)) {
if (!cloned) {
cloned = true
data = { ...data }
// Avoid calling getters again by not using {...data}
data = Object.defineProperties(
{},
Object.getOwnPropertyDescriptors(data)
)
}
// use defineProperty in case of a getter
// Overwrite getters in the cloned data.
Object.defineProperty(data, key, {
value: value[kValue],
configurable: true,
Expand Down

0 comments on commit b68456b

Please sign in to comment.