@pluv/client 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ["pluv"],
4
+ };
@@ -0,0 +1,17 @@
1
+
2
+ > @pluv/client@0.0.0 build /home/runner/work/pluv/pluv/packages/client
3
+ > tsup src/index.ts --format esm,cjs --dts
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v6.5.0
8
+ CLI Target: es6
9
+ ESM Build start
10
+ CJS Build start
11
+ CJS dist/index.js 29.80 KB
12
+ CJS ⚡️ Build success in 129ms
13
+ ESM dist/index.mjs 28.72 KB
14
+ ESM ⚡️ Build success in 136ms
15
+ DTS Build start
16
+ DTS ⚡️ Build success in 3389ms
17
+ DTS dist/index.d.ts 8.01 KB
@@ -0,0 +1,193 @@
1
+ import { InferYjsSharedTypeJson, array, map, text, unstable__object } from '@pluv/crdt-yjs';
2
+ export { InferYjsSharedTypeJson } from '@pluv/crdt-yjs';
3
+ import * as _pluv_types from '@pluv/types';
4
+ import { IOLike, InferIOOutput, Id, IOEventMessage, JsonObject, InferIOAuthorizeUser, InferIOAuthorize, InputZodLike, InferIOInput, InferEventMessage, InferIOAuthorizeRequired } from '@pluv/types';
5
+ export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
6
+ import { AbstractType } from 'yjs';
7
+ export { AbstractType } from 'yjs';
8
+ import { Subject } from 'wonka';
9
+
10
+ interface CrdtManagerOptions<TStorage extends Record<string, AbstractType<any>> = {}> {
11
+ encodedState?: string | Uint8Array;
12
+ initialStorage?: () => TStorage;
13
+ }
14
+
15
+ type EventNotifierSubscriptionCallback<TIO extends IOLike, TEvent extends keyof InferIOOutput<TIO>> = (value: Id<IOEventMessage<TIO, TEvent>>) => void;
16
+
17
+ interface AuthorizationState<TIO extends IOLike> {
18
+ token: string | null;
19
+ user: InferIOAuthorizeUser<InferIOAuthorize<TIO>> | null;
20
+ }
21
+ declare enum ConnectionState {
22
+ Closed = "Closed",
23
+ Connecting = "Connecting",
24
+ Open = "Open",
25
+ Unavailable = "Unavailable",
26
+ Untouched = "Untouched"
27
+ }
28
+ interface WebSocketConnection {
29
+ id: string | null;
30
+ state: ConnectionState;
31
+ }
32
+ interface WebSocketState<TIO extends IOLike> {
33
+ authorization: AuthorizationState<TIO>;
34
+ connection: WebSocketConnection;
35
+ webSocket: WebSocket | null;
36
+ }
37
+ interface UserInfo<TIO extends IOLike, TPresence extends JsonObject = {}> {
38
+ connectionId: string;
39
+ presence: TPresence;
40
+ user: Id<InferIOAuthorizeUser<InferIOAuthorize<TIO>>>;
41
+ }
42
+
43
+ type OtherNotifierSubscriptionCallback<TIO extends IOLike> = (value: Id<UserInfo<TIO>> | null) => void;
44
+
45
+ interface StateNotifierSubjects<TIO extends IOLike, TPresence extends JsonObject> {
46
+ connection: Subject<Id<WebSocketState<TIO>>>;
47
+ "my-presence": Subject<TPresence | null>;
48
+ myself: Subject<Readonly<Id<UserInfo<TIO>>> | null>;
49
+ others: Subject<readonly Id<UserInfo<TIO>>[]>;
50
+ }
51
+ type InferSubjectValue<TIO extends IOLike, TPresence extends JsonObject, TSubject extends keyof StateNotifierSubjects<TIO, TPresence>> = StateNotifierSubjects<TIO, TPresence>[TSubject] extends Subject<infer IValue> ? Id<IValue> : never;
52
+ type SubscriptionCallback<TIO extends IOLike, TPresence extends JsonObject, TSubject extends keyof StateNotifierSubjects<TIO, TPresence>> = (value: InferSubjectValue<TIO, TPresence, TSubject>) => void;
53
+
54
+ type UsersManagerConfig<TPresence extends JsonObject = {}> = {
55
+ initialPresence?: TPresence;
56
+ presence?: InputZodLike<TPresence>;
57
+ };
58
+
59
+ declare global {
60
+ var process: {
61
+ env: {
62
+ [key: string]: string | undefined;
63
+ };
64
+ };
65
+ }
66
+ type AuthEndpoint = string | ((room: string) => FetchOptions);
67
+ type WsEndpoint = string | ((room: string) => string);
68
+ type FetchOptions = string | {
69
+ url: string;
70
+ options?: RequestInit;
71
+ };
72
+ type RoomEndpoints<TIO extends IOLike> = {
73
+ wsEndpoint: WsEndpoint;
74
+ } & (InferIOAuthorizeRequired<InferIOAuthorize<TIO>> extends true ? {
75
+ authEndpoint: AuthEndpoint;
76
+ } : {
77
+ authEndpoint?: AuthEndpoint;
78
+ });
79
+ type PluvRoomDebug<TIO extends IOLike> = Id<{
80
+ output: readonly (keyof InferIOOutput<TIO>)[];
81
+ input: readonly (keyof InferIOInput<TIO>)[];
82
+ }>;
83
+ type PluvRoomOptions<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> = {
84
+ debug?: boolean | PluvRoomDebug<TIO>;
85
+ onAuthorizationFail?: (error: Error) => void;
86
+ } & CrdtManagerOptions<TStorage> & UsersManagerConfig<TPresence>;
87
+ type RoomConfig<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> = RoomEndpoints<TIO> & PluvRoomOptions<TIO, TPresence, TStorage>;
88
+ declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> {
89
+ readonly _endpoints: RoomEndpoints<TIO>;
90
+ readonly room: string;
91
+ private _crdtManager;
92
+ private _crdtNotifier;
93
+ private _debug;
94
+ private _eventNotifier;
95
+ private _initialStorage;
96
+ private _intervals;
97
+ private _listeners;
98
+ private _otherNotifier;
99
+ private _state;
100
+ private _stateNotifier;
101
+ private _subscriptions;
102
+ private _timeouts;
103
+ private _windowListeners;
104
+ private _wsListeners;
105
+ private _usersManager;
106
+ constructor(room: string, options: RoomConfig<TIO, TPresence, TStorage>);
107
+ get webSocket(): WebSocket | null;
108
+ broadcast(message: InferEventMessage<InferIOInput<TIO>>): void;
109
+ connect(): Promise<void>;
110
+ disconnect(): void;
111
+ event: <TEvent extends keyof InferIOOutput<TIO>>(name: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => (() => void);
112
+ getConnection(): WebSocketConnection;
113
+ getMyPresence: () => TPresence | null;
114
+ getMyself: () => Id<UserInfo<TIO, TPresence>> | null;
115
+ getOther: (connectionId: string) => Id<UserInfo<TIO, TPresence>> | null;
116
+ getOthers: () => readonly Id<UserInfo<TIO, TPresence>>[];
117
+ getStorage: <TKey extends keyof TStorage>(key: TKey) => TStorage[TKey] | null;
118
+ other: (connectionId: string, callback: OtherNotifierSubscriptionCallback<TIO>) => (() => void);
119
+ storage: <TKey extends keyof TStorage>(key: TKey, fn: (value: InferYjsSharedTypeJson<TStorage[TKey]>) => void) => (() => void);
120
+ subscribe: <TSubject extends keyof StateNotifierSubjects<TIO, TPresence>>(name: TSubject, callback: SubscriptionCallback<TIO, TPresence, TSubject>) => (() => void);
121
+ private _attachWindowListeners;
122
+ private _attachWsListeners;
123
+ private _clearInterval;
124
+ private _clearTimeout;
125
+ private _closeWs;
126
+ private _detachWindowListeners;
127
+ private _detachWsListeners;
128
+ private _getAuthorization;
129
+ private _getWsEndpoint;
130
+ private _handleExit;
131
+ private _handlePresenceUpdatedMessage;
132
+ private _handleReceiveOthers;
133
+ private _handleRegisteredMessage;
134
+ private _handleStorageReceivedMessage;
135
+ private _handleStorageUpdatedMessage;
136
+ private _handleUserJoinedMessage;
137
+ private _heartbeat;
138
+ private _logDebug;
139
+ private _observeCrdt;
140
+ private _onAuthorizationFail;
141
+ /**
142
+ * TODO
143
+ * @description Determine if ws was closed with a code for closing without retries.
144
+ * Otherwise, attempt to retry again with incremental backoff
145
+ * @author leedavidcs
146
+ * @date August 19, 2022
147
+ */
148
+ private _onClose;
149
+ /**
150
+ * TODO
151
+ * @description Handle websocket errors in a meaningful way, should they occur
152
+ * @author leedavidcs
153
+ * @date August 19, 2022
154
+ */
155
+ private _onError;
156
+ private _onMessage;
157
+ private _onOpen;
158
+ private _onNavigatorOnline;
159
+ private _onVisibilityChange;
160
+ private _parseMessage;
161
+ private _reconnect;
162
+ private _sendMessage;
163
+ private _updateState;
164
+ }
165
+
166
+ type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO>;
167
+ declare class PluvClient<TIO extends IOLike = IOLike> {
168
+ private _authEndpoint;
169
+ private _wsEndpoint;
170
+ private _rooms;
171
+ constructor(options: PluvClientOptions<TIO>);
172
+ createRoom: <TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}>(room: string, options: PluvRoomOptions<TIO, TPresence, TStorage>) => PluvRoom<TIO, TPresence, TStorage>;
173
+ enter: (room: string) => Promise<void>;
174
+ getRoom: <TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}>(room: string) => PluvRoom<TIO, TPresence, TStorage> | null;
175
+ leave: (room: string) => void;
176
+ }
177
+
178
+ declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any>, {}, {}> = IOLike<_pluv_types.IOAuthorize<any, any>, {}, {}>>(options: PluvClientOptions<TIO>) => PluvClient<TIO>;
179
+
180
+ declare const y_array: typeof array;
181
+ declare const y_map: typeof map;
182
+ declare const y_text: typeof text;
183
+ declare const y_unstable__object: typeof unstable__object;
184
+ declare namespace y {
185
+ export {
186
+ y_array as array,
187
+ y_map as map,
188
+ y_text as text,
189
+ y_unstable__object as unstable__object,
190
+ };
191
+ }
192
+
193
+ export { ConnectionState, PluvClient, PluvClientOptions, PluvRoom, PluvRoomDebug, UserInfo, WebSocketConnection, WebSocketState, createClient, y };