-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-server.ts
181 lines (176 loc) · 6.82 KB
/
cache-server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { SimpleStorage } from '@cache-server/storage/storage';
import packageInfo from 'package.json';
import { NEVER, of } from 'rxjs';
import {
distinctUntilChanged,
endWith,
mergeScan,
takeUntil,
} from 'rxjs/operators';
import type { ApiConversation } from './api/api-conversation';
import type { UUID } from './api/uuid';
import type {
CacheConversation,
CacheNormalConversation,
ProgressConversation,
} from './cache-conversation';
import { Client } from './client';
import type { ClientChannel } from './client-channel';
import { CustomHttpHandler } from './http-handler/custom-http-handler';
import { HttpProgressHandler } from './http-handler/http-progress-handler';
import { SubscriptionsHandler } from './subscriptions-handler/subscriptions-handler';
const version = packageInfo.version;
export class CacheServer {
private readonly storage = new SimpleStorage();
private readonly http = new CustomHttpHandler();
private readonly httpProgress = new HttpProgressHandler();
private readonly subscriptionsHandler = new SubscriptionsHandler(
this.storage,
this.http,
this.httpProgress
);
private readonly actionsHandler = this.subscriptionsHandler.actionsHandler;
/**
* All clients that have send a message to the service-worker and
* are expected to be still alive
*/
private readonly activeClients: {
[clientId: string]: Client;
} = {};
public handleMessage(
message: CacheConversation['message'],
channel: ClientChannel
) {
const clientId = channel.id;
let client = this.activeClients[clientId];
if (!client) {
this.activeClients[clientId] = new Client(
clientId,
(
subscriptionId: UUID,
messageI: ApiConversation['subscribable']['message']
) =>
this.subscriptionsHandler.unsubscribe(
subscriptionId,
messageI
),
channel
);
client = this.activeClients[clientId]!;
channel.postMessage({
type: 'connect',
messageId: message.id,
version,
});
this.actionsHandler.auth.latestAuthEvent$
.pipe(takeUntil(client.destroyed))
.subscribe((data) =>
channel.postMessage({
type: 'isLoggedIn',
data,
})
);
client.progress$.subscribe((progressEvent) => {
const progressResponse: ProgressConversation['response'] = {
type: 'progress',
data: progressEvent,
};
channel.postMessage(progressResponse);
});
}
switch (message.type) {
case 'connect':
return;
case 'subscribe': {
client.addSubscription(message.id, message.action);
const observables = this.subscriptionsHandler
// This observable completes when the client unsubscribes from it -> no memory leak
.subscribe(message.action, message.id);
observables.data$.subscribe((data) =>
channel.postMessage({ ...message, data })
);
observables.error$.subscribe((error) => {
channel.postMessage({ type: 'error', error });
});
observables.progress$
.pipe(
// do not end with taskStarted
endWith('taskFinished' as const),
distinctUntilChanged(),
// when there was no 'taskStarted' before the 'taskFinished' nothing should be emitted
mergeScan(
(previouse, next) =>
previouse !== 'taskStarted' &&
next === 'taskFinished'
? NEVER
: of(next),
undefined as
| 'taskFinished'
| 'taskStarted'
| undefined
)
)
// only the next call is relevant (this stream should not end the progressSubject of the whole client)
.subscribe((e) => client!.progress$.next(e!));
break;
}
case 'unsubscribe':
client.removeSubscription(message.data.subscriptionId);
channel.postMessage({
...message,
});
break;
case 'unsubscribeFromAll':
this.removeClient(clientId);
break;
case 'renewItems':
this.subscriptionsHandler
.renewItems()
.then((data) =>
channel.postMessage({
id: message.id,
type: message.type,
action: message.action,
data: null,
})
)
.catch((error) =>
channel.postMessage({
id: message.id,
type: message.type,
action: message.action,
data: undefined,
error,
})
);
break;
default:
client.progress$.next('taskStarted');
this.actionsHandler
.handleActions({
type: message.type,
action: message.action,
} as CacheNormalConversation['message'])
.then((data) =>
channel.postMessage({
...message,
data,
// ts doesn't get that this is the right combination of data and type, action etc..
} as CacheNormalConversation['response'])
)
.catch((error) =>
channel.postMessage({
...message,
error,
})
)
.finally(() => {
client!.progress$.next('taskFinished');
});
}
}
private removeClient(clientId: string) {
this.activeClients[clientId]!.destroy();
delete this.activeClients[clientId];
}
}