-
Notifications
You must be signed in to change notification settings - Fork 55
/
NonBackgroundMessenger.ts
152 lines (148 loc) · 4.69 KB
/
NonBackgroundMessenger.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// only tested in ../e2e tests
/* eslint-disable no-use-before-define */
import {
BehaviorSubject,
EmptyError,
Observable,
ReplaySubject,
Subject,
catchError,
debounceTime,
filter,
first,
map,
of,
takeWhile,
tap
} from 'rxjs';
import {
DeriveChannelOptions,
DisconnectEvent,
Messenger,
MessengerDependencies,
MessengerPort,
PortMessage,
ReconnectConfig
} from './types';
import { deriveChannelName } from './util';
import { isNotNil } from '@cardano-sdk/util';
import { retryBackoff } from 'backoff-rxjs';
export interface NonBackgroundMessengerOptions {
baseChannel: string;
reconnectConfig?: ReconnectConfig;
}
/** Creates and maintains a long-running connection to background process. Attempts to reconnect the port on disconnects. */
export const createNonBackgroundMessenger = (
{
baseChannel: channel,
reconnectConfig: { initialDelay, maxDelay } = { initialDelay: 10, maxDelay: 1000 }
}: NonBackgroundMessengerOptions,
{ logger, runtime }: MessengerDependencies
): Messenger => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let reconnectTimeout: any;
let delay = initialDelay;
let isDestroyed = false;
const disconnect$ = new Subject<DisconnectEvent>();
const port$ = new BehaviorSubject<MessengerPort | null | 'shutdown'>(null);
// Originally this was a 'new Subject()', but there seems to be a race between
// - when it receives a value from 'onMessage'
// - when message$ is subscribed to through `remoteApi`
// It is most likely because event listener to onMessage is added during
// createNonBackgroundMessenger and messenger on the other end emits immediately upon connection.
const message$ = new ReplaySubject<PortMessage>(1);
const connect = () => {
if (typeof port$.value === 'string' || port$.value) return;
// assuming this doesn't throw
const port = runtime.connect({ name: channel });
port$.next(port);
port.onDisconnect.addListener(onDisconnect);
// TODO: reset 'delay' if onDisconnect not called somewhat immediatelly?
port.onMessage.addListener(onMessage);
};
const reconnect = () => {
clearTimeout(reconnectTimeout);
delay = Math.min(Math.pow(delay, 2), maxDelay);
reconnectTimeout = setTimeout(connect, delay);
};
const onMessage = (data: unknown, port: MessengerPort) => {
logger.debug(`[NonBackgroundMessenger(${channel})] message`, data);
delay = initialDelay;
message$.next({ data, port });
};
const onDisconnect = (port: MessengerPort) => {
disconnect$.next({
disconnected: port,
remaining: []
});
logger.debug(`[NonBackgroundMessenger(${channel})] disconnected`);
port!.onMessage.removeListener(onMessage);
port!.onDisconnect.removeListener(onDisconnect);
port$.next(isDestroyed ? 'shutdown' : null);
if (!isDestroyed) reconnect();
};
connect();
const connect$ = port$.pipe(
debounceTime(10), // TODO: how long until onDisconnect() is called when the other end doesn't exist?
filter(isNotNil),
takeWhile((port): port is MessengerPort => typeof port !== 'string')
);
const derivedMessengers = new Set<Messenger>();
return {
channel,
connect$,
deriveChannel(path, { detached }: DeriveChannelOptions = {}) {
const messenger = createNonBackgroundMessenger(
{
baseChannel: deriveChannelName(channel, path),
reconnectConfig: { initialDelay, maxDelay }
},
{ logger, runtime }
);
if (!detached) {
derivedMessengers.add(messenger);
}
return messenger;
},
disconnect$,
get isShutdown() {
return isDestroyed;
},
message$,
/**
* @throws RxJS EmptyError if client is shutdown
*/
postMessage(message: unknown): Observable<void> {
return connect$.pipe(
first(),
tap((port) => port.postMessage(message)),
retryBackoff({
initialInterval: 10,
maxInterval: 1000,
shouldRetry: (err) => !(err instanceof EmptyError)
}),
map(() => void 0),
catchError(() => {
logger.warn("Couldn't postMessage: messenger shutdown");
return of(void 0);
})
);
},
shutdown() {
isDestroyed = true;
const port = port$.value;
if (typeof port !== 'string') {
port?.disconnect();
}
clearTimeout(reconnectTimeout);
for (const messenger of derivedMessengers.values()) {
messenger.shutdown();
derivedMessengers.delete(messenger);
}
port$.complete();
disconnect$.complete();
logger.debug(`[NonBackgroundMessenger(${channel})] shutdown`);
}
};
};
export type NonBackgroundMessenger = ReturnType<typeof createNonBackgroundMessenger>;