Commit 0c0a639b authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Use `WeakMap` to store `MessageChannel`

We use `Window` objects as keys on a map. Windows can be removed.
`WeakMap` has the characteristic that keys that are claimed by the
garbage collector are removed from the map. In this way, we are memory
friendly and don't retain any defunct object.
parent 492ab6d5
...@@ -70,8 +70,10 @@ export class PortProvider { ...@@ -70,8 +70,10 @@ export class PortProvider {
// Although some channels (v.gr. `notebook-sidebar`) have only one // Although some channels (v.gr. `notebook-sidebar`) have only one
// `MessageChannel`, other channels (v.gr. `guest-sidebar`) can have multiple // `MessageChannel`, other channels (v.gr. `guest-sidebar`) can have multiple
// `MessageChannel`s. The `Window` refers to the frame that sends the initial // `MessageChannel`s. The `Window` refers to the frame that sends the initial
// request that triggers creation of a channel. // request that triggers the creation of a channel. We use `WeakMap` so that
/** @type {Map<Channel, Map<Window, MessageChannel>>} */ // entries are removed from the map when the garbage collector reclaims the
// removed window.
/** @type {Map<Channel, WeakMap<Window, MessageChannel>>} */
this._channels = new Map(); this._channels = new Map();
// Two important characteristics of `MessagePort`: // Two important characteristics of `MessagePort`:
...@@ -214,7 +216,7 @@ export class PortProvider { ...@@ -214,7 +216,7 @@ export class PortProvider {
let windowChannelMap = this._channels.get(channel); let windowChannelMap = this._channels.get(channel);
if (!windowChannelMap) { if (!windowChannelMap) {
windowChannelMap = new Map(); windowChannelMap = new WeakMap();
this._channels.set(channel, windowChannelMap); this._channels.set(channel, windowChannelMap);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment