Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import { filter, map, publish } from 'rxjs/operators';
import { Observable } from 'rxjs';
const RENDERER_ORIGIN =
process.env.NODE_ENV === 'production' ? 'file://' : 'http://localhost:3000';
const messages$ = Observable.create(observer => {
const handler = event => {
const { data, origin } = event;
if (origin !== RENDERER_ORIGIN) {
return;
}
if (!data) {
return;
}
const { from } = data;
if (from === 'fether:react') {
// Since `payload` and `frontend` have the same origin, we use the `from`
// field to differentiate who's sending the postMessage to whom. If the
// message has been sent by `frontend`, we ignore.
return;
}
observer.next(data);
};
window.addEventListener('message', handler);
// Remove listener on unsubscribe
return () => {
window.removeEventListener('message', handler);
};
}).pipe(publish());
// Create hot observable
messages$.connect();
export function listen$ (action) {
return messages$.pipe(
filter(data => data.action === action),
map(({ payload }) => payload)
);
}
export function send (action, payload) {
window.postMessage(
{
action,
from: 'fether:react',
payload
},
RENDERER_ORIGIN
);
}