Skip to content

Commit 6c715c2

Browse files
committed
Make onmessage event handlers execution async
1 parent 4512b19 commit 6c715c2

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"pre-commit": "^1.2.2",
4242
"prettier": "^1.18.2",
4343
"rimraf": "^3.0.0",
44+
"rxjs": "^6.5.3",
4445
"ts-jest": "^24.2.0",
4546
"typescript": "3.5.3"
4647
}

src/__tests__/postmessage.test.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { createParentPostMessage, createFigma } from "../stubs";
2+
import { Subject } from "rxjs";
3+
import { take } from "rxjs/operators";
24

35
describe("postMessage", () => {
46
beforeEach(() => {
@@ -8,19 +10,40 @@ describe("postMessage", () => {
810
global.parent.postMessage = createParentPostMessage(global.figma);
911
});
1012

11-
it("UI sends message and plugin receives it", () => {
12-
figma.ui.onmessage = jest.fn();
13+
it("UI sends message and plugin receives it", async () => {
14+
const waiting = new Subject();
15+
// @ts-ignore
16+
figma.ui.onmessage = jest.fn().mockImplementation(() => waiting.next());
1317
parent.postMessage({ pluginMessage: "abc" }, "*");
1418

15-
expect(figma.ui.onmessage).toHaveBeenCalledTimes(1);
16-
expect(figma.ui.onmessage).toHaveBeenCalledWith("abc", expect.any(Object));
19+
return new Promise(resolve => {
20+
waiting.pipe(take(1)).subscribe(() => {
21+
// @ts-ignore
22+
expect(figma.ui.onmessage).toHaveBeenCalledTimes(1);
23+
// @ts-ignore
24+
expect(figma.ui.onmessage).toHaveBeenCalledWith(
25+
"abc",
26+
expect.any(Object)
27+
);
28+
resolve();
29+
});
30+
});
1731
});
1832

1933
it("Plugin sends message and UI receives it", () => {
34+
const waiting = new Subject();
35+
2036
//@ts-ignore
21-
global.onmessage = jest.fn();
37+
global.onmessage = jest.fn().mockImplementation(() => waiting.next());
38+
// @ts-ignore
2239
figma.ui.postMessage("abc");
23-
//@ts-ignore
24-
expect(global.onmessage).toHaveBeenCalledTimes(1);
40+
41+
return new Promise(resolve => {
42+
waiting.pipe(take(1)).subscribe(() => {
43+
//@ts-ignore
44+
expect(global.onmessage).toHaveBeenCalledTimes(1);
45+
resolve();
46+
});
47+
});
2548
});
2649
});

src/stubs.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export const createFigma = (config: TConfig): PluginAPI => {
2828

2929
// @ts-ignore
3030
if (global && global.onmessage) {
31-
// @ts-ignore
32-
global.onmessage(message);
31+
setTimeout(() => {
32+
// @ts-ignore
33+
global.onmessage(message);
34+
}, 0);
3335
}
3436
}
3537
}
@@ -343,6 +345,9 @@ export const createParentPostMessage = (figma: PluginAPI) => (
343345
target: string
344346
) => {
345347
if (figma.ui.onmessage) {
346-
figma.ui.onmessage(message.pluginMessage, { origin: null });
348+
setTimeout(() => {
349+
// @ts-ignore
350+
figma.ui.onmessage(message.pluginMessage, { origin: null });
351+
}, 0);
347352
}
348353
};

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,6 +2969,13 @@ rsvp@^4.8.4:
29692969
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"
29702970
integrity sha512-nfMOlASu9OnRJo1mbEk2cz0D56a1MBNrJ7orjRZQG10XDyuvwksKbuXNp6qa+kbn839HwjwhBzhFmdsaEAfauA==
29712971

2972+
rxjs@^6.5.3:
2973+
version "6.5.3"
2974+
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a"
2975+
integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==
2976+
dependencies:
2977+
tslib "^1.9.0"
2978+
29722979
safe-buffer@^5.0.1, safe-buffer@^5.1.2:
29732980
version "5.2.0"
29742981
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.0.tgz#b74daec49b1148f88c64b68d49b1e815c1f2f519"
@@ -3432,6 +3439,11 @@ ts-jest@^24.2.0:
34323439
semver "^5.5"
34333440
yargs-parser "10.x"
34343441

3442+
tslib@^1.9.0:
3443+
version "1.10.0"
3444+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
3445+
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==
3446+
34353447
tunnel-agent@^0.6.0:
34363448
version "0.6.0"
34373449
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"

0 commit comments

Comments
 (0)