Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 0851b7e

Browse files
author
Daniel Brain
committed
Set up state to be cleaned up on demand
1 parent 77bc4de commit 0851b7e

File tree

12 files changed

+38
-32
lines changed

12 files changed

+38
-32
lines changed

src/conf/config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CONSTANTS } from './constants';
33

44
export let CONFIG = {
55

6-
ALLOW_POSTMESSAGE_POPUP: true,
6+
ALLOW_POSTMESSAGE_POPUP: false,
77

88
LOG_LEVEL: 'info',
99

@@ -23,4 +23,4 @@ export let CONFIG = {
2323

2424
if (window.location.href.indexOf(CONSTANTS.FILE_PROTOCOL) === 0) {
2525
CONFIG.ALLOW_POSTMESSAGE_POPUP = true;
26-
}
26+
}

src/drivers/listeners.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ export function addRequestListener(name, win, domain, options, override) {
123123
}
124124
}
125125

126-
listeners.request.push({ name, win, domain, options });
127-
}
126+
global.clean.push(global.listeners.request, { name, win, domain, options });
127+
}

src/drivers/receive/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function receiveMessage(event) {
6565
}
6666

6767
if (global.receivedMessages.indexOf(message.id) === -1) {
68-
global.receivedMessages.push(message.id);
68+
global.clean.push(global.receivedMessages, message.id);
6969
} else {
7070
return;
7171
}
@@ -117,5 +117,9 @@ export function messageListener(event) {
117117
}
118118

119119
export function listenForMessages() {
120-
util.listen(window, 'message', messageListener);
120+
let listener = util.listen(window, 'message', messageListener);
121+
122+
global.clean.register('listener', () => {
123+
listener.cancel();
124+
});
121125
}

src/global.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

22
import { CONSTANTS } from './conf';
3+
import { cleanup } from './lib/cleanup';
34

4-
export let global = window[CONSTANTS.WINDOW_PROPS.POSTROBOT] = window[CONSTANTS.WINDOW_PROPS.POSTROBOT] || {};
5+
export let global = window[CONSTANTS.WINDOW_PROPS.POSTROBOT] = window[CONSTANTS.WINDOW_PROPS.POSTROBOT] || {};
6+
7+
global.clean = global.clean || cleanup(global);

src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { listenForMessages } from './drivers';
44
import { global } from './global';
55
import { openTunnelToOpener } from './bridge';
66

7-
function init() {
7+
export function init() {
88

99
if (!global.initialized) {
1010
listenForMessages();
@@ -18,7 +18,14 @@ function init() {
1818

1919
init();
2020

21+
export function reset() {
22+
return global.clean.all().then(() => {
23+
global.initialized = false;
24+
return init();
25+
});
26+
}
27+
2128
export * from './interface';
2229
export { Promise } from './lib';
2330

24-
export default module.exports;
31+
export default module.exports;

src/interface/client.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import { CONFIG, CONSTANTS } from '../conf';
3-
import { listeners, sendMessage } from '../drivers';
3+
import { sendMessage } from '../drivers';
4+
import { global } from '../global';
45
import { util, promise, getAncestor, isAncestor, onWindowReady, isWindowClosed } from '../lib';
56

67

@@ -41,7 +42,7 @@ export function request(options) {
4142
options.domain = options.domain || '*';
4243

4344
let hash = `${options.name}_${util.uniqueID()}`;
44-
listeners.response[hash] = options;
45+
global.clean.setItem(global.listeners.response, hash, options);
4546

4647
if (isWindowClosed(options.window)) {
4748
throw new Error('Target window is closed');

src/interface/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import * as windowUtil from '../lib/windows';
99

1010
export let parent = getAncestor();
1111

12-
export { resetListeners as reset } from '../drivers';
13-
14-
export { openBridge, linkUrl, isBridge, needsBridge, needsBridgeForBrowser, needsBridgeForWin, needsBridgeForDomain, openTunnelToOpener } from '../bridge';
12+
export { openBridge, linkUrl, isBridge, needsBridge, needsBridgeForBrowser, needsBridgeForWin, needsBridgeForDomain, openTunnelToOpener, destroyBridges } from '../bridge';
1513

1614
export { util } from '../lib/util';
1715

src/lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './log';
55
export * from './windows';
66
export * from './methods';
77
export * from './tick';
8-
export * from './ready';
8+
export * from './ready';
9+
export * from './cleanup';

src/lib/methods.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ export function serializeMethod(destination, domain, method, name) {
4949

5050
let id = util.uniqueID();
5151

52-
global.methods[id] = {
53-
destination,
54-
domain,
55-
method
56-
};
52+
global.clean.setItem(global.methods, id, { destination, domain, method });
5753

5854
return {
5955
__type__: CONSTANTS.SERIALIZATION_TYPES.METHOD,
@@ -89,6 +85,8 @@ export function deserializeMethod(source, origin, obj) {
8985
}
9086

9187
wrapper.__name__ = obj.__name__;
88+
wrapper.source = source;
89+
wrapper.origin = origin;
9290

9391
return wrapper;
9492
}

src/lib/ready.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function initOnReady() {
1919
}
2020
}
2121

22-
global.readyPromises.push({
22+
global.clean.push(global.readyPromises, {
2323
win: event.source,
2424
promise: new Promise().resolve(event)
2525
});
@@ -44,12 +44,9 @@ export function onWindowReady(win, timeout = 5000, name = 'Window') {
4444

4545
let promise = new Promise();
4646

47-
global.readyPromises.push({
48-
win,
49-
promise
50-
});
47+
global.clean.push(global.readyPromises, { win, promise });
5148

5249
setTimeout(() => promise.reject(new Error(`${name} did not load after ${timeout}ms`)), timeout);
5350

5451
return promise;
55-
}
52+
}

src/lib/windows.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ export function isSameDomain(win) {
2727
match = false;
2828
}
2929

30-
global.domainMatches.push({
31-
win,
32-
match
33-
});
30+
global.clean.push(global.domainMatches, { win, match });
3431

3532
if (!domainMatchTimeout) {
3633
domainMatchTimeout = setTimeout(() => {
@@ -581,4 +578,4 @@ export function jsonStringify() {
581578

582579
export function jsonParse() {
583580
return JSON.parse.apply(this, arguments);
584-
}
581+
}

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ describe('[post-robot] error cases', function() {
351351
return postRobot.send(childFrame, 'foo', {}, { domain: 'http://www.zombo.com' }).then(function() {
352352
throw new Error('Expected success handler to not be called');
353353
}, function(err) {
354-
assert.ok(err);
354+
assert.ok(err instanceof Error);
355355
});
356356
});
357357
});

0 commit comments

Comments
 (0)