Skip to content

Commit cef5f56

Browse files
Merge pull request #57 from FormidableLabs/56-wait-for-connection-to-init
Wait for connection to be established before sending an init message
2 parents f797311 + d9abd7f commit cef5f56

File tree

5 files changed

+77
-43
lines changed

5 files changed

+77
-43
lines changed

src/exchange.test.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,6 @@ describe('on mount', () => {
6767
expect(addMessageListener).toBeCalledTimes(1);
6868
});
6969
});
70-
71-
describe('init event', () => {
72-
it('is dispatched', () => {
73-
expect(sendMessage).toBeCalledTimes(1);
74-
expect(sendMessage).toBeCalledWith({
75-
type: 'connection-init',
76-
source: 'exchange',
77-
version,
78-
});
79-
});
80-
});
8170
});
8271

8372
describe('on debug message', () => {
@@ -95,7 +84,7 @@ describe('on debug message', () => {
9584
const subscriber = client.subscribeToDebugTarget.mock.calls[0][0];
9685
subscriber(event);
9786

98-
expect(sendMessage).toBeCalledTimes(2);
87+
expect(sendMessage).toBeCalledTimes(1);
9988
expect(sendMessage).toBeCalledWith({
10089
type: 'debug-event',
10190
source: 'exchange',
@@ -119,7 +108,7 @@ describe('on operation', () => {
119108
publish
120109
);
121110
next(operation);
122-
expect(sendMessage.mock.calls[1]).toMatchInlineSnapshot(`
111+
expect(sendMessage.mock.calls[0]).toMatchInlineSnapshot(`
123112
Array [
124113
Object {
125114
"data": Object {
@@ -156,7 +145,7 @@ describe('on operation', () => {
156145
publish
157146
);
158147
next(operation);
159-
expect(sendMessage.mock.calls[1]).toMatchInlineSnapshot(`
148+
expect(sendMessage.mock.calls[0]).toMatchInlineSnapshot(`
160149
Array [
161150
Object {
162151
"data": Object {
@@ -227,7 +216,7 @@ describe('on operation response', () => {
227216
next(operation);
228217

229218
// * call number two relates to the operation response
230-
expect(sendMessage.mock.calls[2]).toMatchInlineSnapshot(`
219+
expect(sendMessage.mock.calls[1]).toMatchInlineSnapshot(`
231220
Array [
232221
Object {
233222
"data": Object {
@@ -273,7 +262,7 @@ describe('on operation response', () => {
273262
);
274263
next(operation);
275264
// * call number two relates to the operation response
276-
expect(sendMessage.mock.calls[2]).toMatchInlineSnapshot(`
265+
expect(sendMessage.mock.calls[1]).toMatchInlineSnapshot(`
277266
Array [
278267
Object {
279268
"data": Object {
@@ -430,7 +419,7 @@ describe('on connection init message', () => {
430419

431420
it('dispatches acknowledge event w/ version', () => {
432421
handler(getVersionMessage);
433-
expect(sendMessage).toBeCalledTimes(2);
422+
expect(sendMessage).toBeCalledTimes(1);
434423
expect(sendMessage).toBeCalledWith({
435424
type: 'connection-acknowledge',
436425
source: 'exchange',

src/exchange.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ const curriedDevtoolsExchange: (a: Messenger) => Exchange = ({
1919
sendMessage,
2020
addMessageListener,
2121
}) => ({ client, forward }) => {
22-
// Initialize connection
23-
sendMessage({
24-
type: 'connection-init',
25-
source: 'exchange',
26-
version: __pkg_version__,
27-
});
28-
2922
// Listen for messages from devtools
3023
addMessageListener((message) => {
3124
if (message.source !== 'devtools' || !(message.type in messageHandlers)) {

src/setupTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ global.window = {
22
addEventListener: jest.fn(),
33
postMessage: jest.fn(),
44
};
5+
(global as any).__pkg_version__ = '200.0.0';

src/utils/messaging.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ describe('on create native messenger', () => {
2525
`);
2626
});
2727

28+
describe('on open', () => {
29+
it('sends connection-init message', () => {
30+
createNativeMessenger();
31+
instance.send = jest.fn();
32+
instance.onopen();
33+
expect(instance.send).toBeCalledTimes(1);
34+
expect(instance.send.mock.calls[0]).toMatchInlineSnapshot(`
35+
Array [
36+
"{\\"source\\":\\"exchange\\",\\"type\\":\\"connection-init\\",\\"version\\":\\"200.0.0\\"}",
37+
]
38+
`);
39+
});
40+
});
41+
2842
describe('on close', () => {
2943
it('tries to establish a new connection', () => {
3044
createNativeMessenger();
@@ -77,6 +91,24 @@ describe('on create browser messenger', () => {
7791
const addEventListener = jest.spyOn(window, 'addEventListener');
7892
const postMessage = jest.spyOn(window, 'postMessage');
7993

94+
describe('on create', () => {
95+
it('sends connection-init message', () => {
96+
createBrowserMessenger();
97+
98+
expect(postMessage).toBeCalledTimes(1);
99+
expect(postMessage.mock.calls[0]).toMatchInlineSnapshot(`
100+
Array [
101+
Object {
102+
"source": "exchange",
103+
"type": "connection-init",
104+
"version": "200.0.0",
105+
},
106+
"http://localhost",
107+
]
108+
`);
109+
});
110+
});
111+
80112
describe('on trusted message', () => {
81113
it('calls message listeners', () => {
82114
const data = {
@@ -126,8 +158,8 @@ describe('on create browser messenger', () => {
126158

127159
const m = createBrowserMessenger();
128160
m.sendMessage(data as any);
129-
expect(postMessage).toBeCalledTimes(1);
130-
expect(postMessage.mock.calls[0][0]).toMatchInlineSnapshot(`
161+
expect(postMessage).toBeCalledTimes(2);
162+
expect(postMessage.mock.calls[1][0]).toMatchInlineSnapshot(`
131163
Object {
132164
"arg": 1234,
133165
"someString": "hello",

src/utils/messaging.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ExchangeMessage, DevtoolsMessage } from '../types';
1+
import {
2+
ExchangeMessage,
3+
DevtoolsMessage,
4+
ExchangeConnectionInitMessage,
5+
} from '../types';
26

37
export interface Messenger {
48
addMessageListener: (
@@ -7,6 +11,12 @@ export interface Messenger {
711
sendMessage: (m: ExchangeMessage) => void;
812
}
913

14+
const connectionInitMessage: ExchangeConnectionInitMessage = {
15+
source: 'exchange',
16+
type: 'connection-init',
17+
version: __pkg_version__,
18+
};
19+
1020
/** Create curried args for native environment. */
1121
export const createNativeMessenger = (): Messenger => {
1222
let listeners: Function[] = [];
@@ -17,6 +27,9 @@ export const createNativeMessenger = (): Messenger => {
1727
timeout = undefined;
1828
ws = new WebSocket('ws://localhost:7700');
1929

30+
ws.onopen = () => {
31+
ws.send(JSON.stringify(connectionInitMessage));
32+
};
2033
ws.onclose = () => {
2134
timeout = timeout || setTimeout(createConnection, 500);
2235
};
@@ -50,20 +63,26 @@ export const createNativeMessenger = (): Messenger => {
5063
};
5164

5265
/** Create curried args for browser environment. */
53-
export const createBrowserMessenger = (): Messenger => ({
54-
addMessageListener: (cb) => {
55-
window.addEventListener('message', ({ data, isTrusted }) => {
56-
if (!isTrusted || !data?.source) {
57-
return;
58-
}
66+
export const createBrowserMessenger = (): Messenger => {
67+
let listeners: Function[] = [];
68+
69+
window.addEventListener('message', ({ data, isTrusted }) => {
70+
if (!isTrusted || !data?.source) {
71+
return;
72+
}
5973

60-
cb(data as ExchangeMessage | DevtoolsMessage);
61-
});
62-
},
63-
sendMessage: (message) => {
64-
window.postMessage(
65-
JSON.parse(JSON.stringify(message)),
66-
window.location.origin
67-
);
68-
},
69-
});
74+
listeners.forEach((cb) => cb(data));
75+
});
76+
77+
const addMessageListener: Messenger['addMessageListener'] = (cb) =>
78+
(listeners = [...listeners, cb]);
79+
const sendMessage: Messenger['sendMessage'] = (m) =>
80+
window.postMessage(JSON.parse(JSON.stringify(m)), window.location.origin);
81+
82+
sendMessage(connectionInitMessage);
83+
84+
return {
85+
addMessageListener,
86+
sendMessage,
87+
};
88+
};

0 commit comments

Comments
 (0)