-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.ts
97 lines (85 loc) · 3.23 KB
/
index.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { BackgroundMessenger, createBackgroundMessenger, generalizeBackgroundMessenger } from './BackgroundMessenger';
import { ChannelName, ConsumeRemoteApiOptions, ExposeApiProps, MessengerDependencies } from './types';
import { FinalizationRegistryDestructor } from './util';
import { consumeMessengerRemoteApi, exposeMessengerApi } from './remoteApi';
import { createNonBackgroundMessenger } from './NonBackgroundMessenger';
export * from './BackgroundMessenger';
export * from './NonBackgroundMessenger';
export * from './remoteApi';
export * from './runContentScriptMessageProxy';
export * from './types';
export * from './util';
export * from './injectedRuntime';
export * from './errors';
export type BaseChannel = { baseChannel: ChannelName };
const isInBackgroundProcess = typeof window === 'undefined';
const getBackgroundMessenger = (() => {
let backgroundMessenger: BackgroundMessenger | null = null;
return (dependencies: MessengerDependencies) => (backgroundMessenger ||= createBackgroundMessenger(dependencies));
})();
const _backgroundExposeApi = <T extends object>(
props: ExposeApiProps<T> & BaseChannel,
dependencies: MessengerDependencies
) => {
const messenger = generalizeBackgroundMessenger(
props.baseChannel,
getBackgroundMessenger(dependencies),
dependencies.logger
);
return exposeMessengerApi(props, {
messenger,
...dependencies
});
};
export type ExposeApi = typeof _backgroundExposeApi;
const _nonBackgroundExposeApi: ExposeApi = <T extends object>(
props: ExposeApiProps<T> & BaseChannel,
dependencies: MessengerDependencies
) =>
exposeMessengerApi(props, {
messenger: createNonBackgroundMessenger(props, dependencies),
...dependencies
});
const _nonBackgroundConsumeRemoteApi = <T extends object>(
props: ConsumeRemoteApiOptions<T> & BaseChannel,
dependencies: MessengerDependencies
) =>
consumeMessengerRemoteApi(props, {
destructor: new FinalizationRegistryDestructor(dependencies.logger),
messenger: createNonBackgroundMessenger(props, dependencies),
...dependencies
});
export type ConsumeApi = typeof _nonBackgroundConsumeRemoteApi;
const _backgroundConsumeRemoteApi: ConsumeApi = <T extends object>(
props: ConsumeRemoteApiOptions<T> & BaseChannel,
dependencies: MessengerDependencies
) => {
const messenger = generalizeBackgroundMessenger(
props.baseChannel,
getBackgroundMessenger(dependencies),
dependencies.logger
);
return consumeMessengerRemoteApi(props, {
destructor: new FinalizationRegistryDestructor(dependencies.logger),
messenger,
...dependencies
});
};
/**
* Bind an API object to handle messages from other parts of extension.
* Only compatible with interfaces where all members are either:
* - Methods that return a Promise
* - Observable
*
* This can only used once per channelName per process.
*/
export const exposeApi: ExposeApi = isInBackgroundProcess ? _backgroundExposeApi : _nonBackgroundExposeApi;
/**
* Create a client to remote api, exposed via `exposeApi`.
* Only compatible with interfaces where all members are either:
* - Methods that return a Promise
* - Observable
*/
export const consumeRemoteApi: ConsumeApi = isInBackgroundProcess
? _backgroundConsumeRemoteApi
: _nonBackgroundConsumeRemoteApi;