-
Notifications
You must be signed in to change notification settings - Fork 14
/
interfaces.ts
248 lines (227 loc) · 5.7 KB
/
interfaces.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-use-before-define */
import type {
BaseInteraction,
EmitParameter,
MergeInteraction,
Transport,
} from 'data-transport';
import type { Config as BaseConfig, App, Renderer } from 'reactant';
import type { ILastActionData } from 'reactant-last-action';
import type { RouterState } from 'reactant-router';
import {
isClientName,
lastActionName,
loadFullStateActionName,
preloadedStateActionName,
proxyClientActionName,
proxyServerActionName,
syncRouterName,
syncWorkerRouterName,
syncToClientsName,
syncClientIdsFromClientsName,
syncClientIdToServerName,
removeClientIdToServerName,
proxyExecutorKey,
} from './constants';
export type { Transport } from 'data-transport';
export type Port = 'server' | 'client';
export interface Transports {
/**
* Server Transport
*/
server?: ServerTransport;
/**
* Client Transport
*/
client?: ClientTransport;
}
export type ServerTransport<T extends BaseInteraction = {}> = Transport<
MergeInteraction<{ emit: ServerEvents; listen: ClientEvents }, T>
>;
export type ClientTransport<T extends BaseInteraction = {}> = Transport<
MergeInteraction<
{
emit: ClientEvents;
listen: ServerEvents;
},
T
>
>;
export interface ISharedAppOptions {
/**
* Reactant shared app name.
*/
name: string;
/**
* Reactant shared app type.
*/
type: 'SharedTab' | 'SharedWorker' | 'Base';
/**
* Shared app's transports
*/
transports?: Transports;
/**
* Specify 'client' or 'server' port.
*/
port?: Port;
/**
* Specify a port name for routing and fork() args.
*/
portName?: string;
/**
* Specify a SharedWorker URL
*/
workerURL?: string;
/**
* Specify a SharedWorker
*/
worker?: SharedWorker;
/**
* Enable patches filter to support minimized modules collections in client port.
*/
enablePatchesFilter?: boolean;
/**
* Enable patches checker to support minimized patches in server port.
*/
enablePatchesChecker?: boolean;
/**
* Enable transport debugger for shared app.
*/
enableTransportDebugger?: boolean;
/**
* Transform client/server port
*/
transform?: Transform;
/**
* Forced Sync for all client, enabled by default.
*
* If forcedSyncClient is false, then only the client's visibilityState is visible will the state be synchronized from server port.
*
* `forcedSyncClient` is only true in `SharedTab` type.
*/
forcedSyncClient?: boolean;
/**
* forced share, disabled by default.
*/
forcedShare?: boolean;
}
export interface Config<T, S extends any[], R extends Renderer<S>>
extends BaseConfig<T, S, R> {
/**
* Reactant shared app options.
*/
share: ISharedAppOptions;
}
export type Transform = (changedPort: Port) => void;
export type Callback = () => void | Promise<void>;
export type CallbackWithHook<T extends Transport = Transport<any>> = (
/**
* Shared app's transport
*/
transport: T
) => void | (() => void);
export type PortApp = Partial<Record<Port, App<any, any, any>>>;
export interface ProxyExecParams {
/**
* module name
*/
module: string;
/**
* method name
*/
method: string;
/**
* method arguments
*/
args: any[];
}
export type ClientEvents = {
[proxyClientActionName](options: ProxyExecParams): Promise<any>;
[preloadedStateActionName](): Promise<Record<string, any>>;
[loadFullStateActionName](
sequence: number
): Promise<Record<string, any> | null | undefined>;
[isClientName](): Promise<boolean>;
[syncRouterName](
name: string,
timestamp: number,
router?: RouterState
): Promise<RouterState>;
[syncClientIdToServerName](clientId: string): void;
[removeClientIdToServerName](clientId: string): void;
};
export type ServerEvents = {
[proxyServerActionName](
options: ProxyExecParams & {
portName?: string;
clientIds?: string[];
excludeClientIds?: string[];
}
): Promise<void>;
[lastActionName](options: ILastActionData): Promise<void>;
[syncToClientsName](
options: Record<string, any> | null | undefined
): Promise<void>;
[syncWorkerRouterName](name: string): Promise<RouterState | undefined>;
[syncClientIdsFromClientsName](): Promise<void>;
};
export interface HandleServerOptions {
app: App<any, any, any>;
transport: Transports['server'];
disposeClient?: () => void;
disposeServer?: () => void;
enablePatchesChecker?: boolean;
}
export interface HandleClientOptions {
app: App<any, any, any>;
transport: Transports['client'];
disposeServer?: () => void;
disposeClient?: () => void;
enablePatchesFilter?: boolean;
preloadedState?: Record<string, any>;
}
export type FunctionKeys<T> = Exclude<
{
[K in keyof T]: T[K] extends Function ? K : never;
}[keyof T],
void
>;
export type ProxyExec = <
T extends Record<string | number | symbol, any>,
K extends FunctionKeys<T>,
O extends EmitParameter<any>['respond']
>(
/**
* Designate an execution module from the server side.
*/
module: T,
/**
* Specify the name of a method in this module.
*/
key: K,
/**
* Pass in the parameters for this method.
*/
args: Parameters<T[K]>,
/**
* proxy execution options
*/
options?: { respond?: O; portName?: string; clientIds?: string[] } & Pick<
EmitParameter<any>,
Exclude<keyof EmitParameter<any>, 'name' | 'respond'>
>
) => O extends false
? void
: ReturnType<T[K]> extends Promise<infer R>
? Promise<R>
: Promise<ReturnType<T[K]>>;
export type SymmetricTransport<
T extends Record<string, (...args: any[]) => any>
> = Transport<{
emit: T;
listen: T;
}>;
export interface ProxyExecutor {
[proxyExecutorKey]?: (params: ProxyExecParams) => Promise<unknown>;
}