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

Simplify `this.link` property in `bridge.js`

`this.link` was defined as `Array<{channel: RPC, window: Window}>`
however, the `window` property was never used. I am guessing that the
original idea was to avoid creating additional channels for the same
frame, however, that was never implemented.

I take the decision of removing the `window` property and converting the
`this.link` to a type of `RPC[]`.
parent 34b319ce
...@@ -10,7 +10,7 @@ import { RPC } from './frame-rpc'; ...@@ -10,7 +10,7 @@ import { RPC } from './frame-rpc';
*/ */
export default class Bridge { export default class Bridge {
constructor() { constructor() {
/** @type {Array<{channel: RPC, window: Window}>} */ /** @type {RPC[]} */
this.links = []; this.links = [];
/** @type {Record<string, (...args: any[]) => void>} */ /** @type {Record<string, (...args: any[]) => void>} */
this.channelListeners = {}; this.channelListeners = {};
...@@ -24,7 +24,7 @@ export default class Bridge { ...@@ -24,7 +24,7 @@ export default class Bridge {
* This removes the event listeners for messages arriving from other windows. * This removes the event listeners for messages arriving from other windows.
*/ */
destroy() { destroy() {
this.links.map(link => link.channel.destroy()); this.links.forEach(channel => channel.destroy());
} }
/** /**
...@@ -66,10 +66,7 @@ export default class Bridge { ...@@ -66,10 +66,7 @@ export default class Bridge {
channel.call('connect', token, ready); channel.call('connect', token, ready);
// Store the newly created channel in our collection // Store the newly created channel in our collection
this.links.push({ this.links.push(channel);
channel,
window: source,
});
return channel; return channel;
} }
...@@ -94,20 +91,22 @@ export default class Bridge { ...@@ -94,20 +91,22 @@ export default class Bridge {
args = args.slice(0, -1); args = args.slice(0, -1);
} }
/** @param {RPC} c */ /** @param {RPC} channel */
const _makeDestroyFn = c => { const _makeDestroyFn = channel => {
return error => { return error => {
c.destroy(); channel.destroy();
this.links = this.links.filter(l => l.channel !== c); this.links = this.links.filter(
registeredChannel => registeredChannel !== channel
);
throw error; throw error;
}; };
}; };
const promises = this.links.map(l => { const promises = this.links.map(channel => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => resolve(null), 1000); const timeout = setTimeout(() => resolve(null), 1000);
try { try {
l.channel.call(method, ...args, (err, result) => { channel.call(method, ...args, (err, result) => {
clearTimeout(timeout); clearTimeout(timeout);
if (err) { if (err) {
reject(err); reject(err);
...@@ -121,7 +120,7 @@ export default class Bridge { ...@@ -121,7 +120,7 @@ export default class Bridge {
}); });
// Don't assign here. The disconnect is handled asynchronously. // Don't assign here. The disconnect is handled asynchronously.
return promise.catch(_makeDestroyFn(l.channel)); return promise.catch(_makeDestroyFn(channel));
}); });
let resultPromise = Promise.all(promises); let resultPromise = Promise.all(promises);
......
...@@ -49,9 +49,7 @@ describe('shared/bridge', () => { ...@@ -49,9 +49,7 @@ describe('shared/bridge', () => {
it('adds the channel to the .links property', () => { it('adds the channel to the .links property', () => {
const channel = createChannel(); const channel = createChannel();
assert.isTrue( assert.isTrue(
bridge.links.some( bridge.links.some(registeredChannel => registeredChannel === channel)
link => link.channel === channel && link.window === fakeWindow
)
); );
}); });
......
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