Skip to content

Commit

Permalink
Ability to cache computed transforms (#513)
Browse files Browse the repository at this point in the history
* add ability to cache the computed transforms per payload

* remove workaround

* add changeset
  • Loading branch information
framini authored Apr 28, 2022
1 parent f69ef58 commit 6ebf3f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/strong-yaks-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@signalwire/core': patch
'@signalwire/js': patch
---

[internal] Add ability to cache computed transforms
30 changes: 23 additions & 7 deletions packages/core/src/BaseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export class BaseComponent<
/** @internal */
private readonly uuid = uuid()

/** @internal */
private _proxyFactoryCache = new WeakMap<any, any>()

/** @internal */
get __uuid() {
return this.uuid
Expand Down Expand Up @@ -401,14 +404,27 @@ export class BaseComponent<
payload,
})

const transformedPayload = this._parseNestedFields(payload, transform)
let proxiedObj
// A single event can have multiple event handlers
// attached. Given that the payload should be the same
// for all of them, to avoid re-applying the same
// transforms and creating a brand new Proxy for each
// handler we'll cache the computed value and pass
// that computed value instead to each handler.
if (this._proxyFactoryCache.has(payload)) {
proxiedObj = this._proxyFactoryCache.get(payload)
} else {
const transformedPayload = this._parseNestedFields(payload, transform)

const proxiedObj = proxyFactory({
instance: cachedInstance,
payload,
transformedPayload,
transform,
})
proxiedObj = proxyFactory({
instance: cachedInstance,
payload,
transformedPayload,
transform,
})

this._proxyFactoryCache.set(payload, proxiedObj)
}

// @ts-expect-error
return fn(proxiedObj)
Expand Down
8 changes: 3 additions & 5 deletions packages/js/src/BaseRoomSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ export class RoomSessionConnection
['video.room.joined'],
{
type: 'roomSession',
instanceFactory: (payload: VideoRoomSubscribedEventParams) => {
// FIXME:
return JSON.parse(JSON.stringify(payload))
instanceFactory: () => {
return {}
},
payloadTransform: (payload: VideoRoomSubscribedEventParams) => {
// FIXME:
return JSON.parse(JSON.stringify(payload))
return payload
},
nestedFieldsToProcess: {
recordings: {
Expand Down

0 comments on commit 6ebf3f6

Please sign in to comment.