forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.ts
More file actions
37 lines (31 loc) · 890 Bytes
/
global.ts
File metadata and controls
37 lines (31 loc) · 890 Bytes
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
import { protocolAndHost } from './url-generator';
export function getGlobal() {
try {
if (typeof window !== 'undefined') {
return (window as unknown) as Window & { BrowserFS: any };
}
if (typeof self !== 'undefined') {
const returnedGlobal: unknown = self;
return returnedGlobal as Worker & { BrowserFS: any };
}
if (typeof global !== 'undefined') {
return global;
}
} catch (e) {
/* Couldn't find anything */
}
return {};
}
const global = getGlobal();
/**
* A postmessage that works in main window and in worker.
* It will send the message to the default origin.
* @param message The message to send
*/
export function commonPostMessage(message: any) {
if (typeof Window !== 'undefined') {
(global as Window).postMessage(message, protocolAndHost());
} else {
(global as Worker).postMessage(message);
}
}