Skip to content

Commit

Permalink
Use WeakMap to store cross-domain methods, to avoid memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Apr 3, 2017
1 parent 81bae73 commit 3532149
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/lib/methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import { WeakMap } from 'cross-domain-safe-weakmap/src';

import { CONSTANTS } from '../conf';
import { util } from './util';
import { matchDomain } from './domain';
Expand All @@ -7,19 +9,21 @@ import { log } from './log';
import { promise } from './promise';
import { global } from '../global';

global.methods = global.methods || {};
global.methods = global.methods || new WeakMap();

export let listenForMethods = util.once(() => {
on(CONSTANTS.POST_MESSAGE_NAMES.METHOD, { window: CONSTANTS.WILDCARD, origin: CONSTANTS.WILDCARD }, ({ source, origin, data }) => {

let meth = global.methods[data.id];
let methods = global.methods.get(source);

if (!meth) {
throw new Error(`Could not find method with id: ${data.id}`);
if (!methods) {
throw new Error(`Could not find any methods this window has privileges to call`);
}

if (meth.destination !== source) {
throw new Error(`Method window does not match`);
let meth = methods[data.id];

if (!meth) {
throw new Error(`Could not find method with id: ${data.id}`);
}

if (!matchDomain(meth.domain, origin)) {
Expand Down Expand Up @@ -50,7 +54,14 @@ export function serializeMethod(destination, domain, method, name) {

let id = util.uniqueID();

global.clean.setItem(global.methods, id, { destination, domain, method });
let methods = global.methods.get(destination);

if (!methods) {
methods = {};
global.methods.set(destination, methods);
}

methods[id] = { domain, method };

return {
__type__: CONSTANTS.SERIALIZATION_TYPES.METHOD,
Expand Down

0 comments on commit 3532149

Please # to comment.