@simplito/privmx-webendpoint 2.1.1 → 2.2.1
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/Types.d.ts +407 -0
- package/Types.js +27 -0
- package/api/Api.d.ts +20 -0
- package/api/Api.js +48 -0
- package/api/BaseNative.d.ts +20 -0
- package/api/BaseNative.js +35 -0
- package/api/ConnectionNative.d.ts +24 -0
- package/api/ConnectionNative.js +51 -0
- package/api/CryptoApiNative.d.ts +25 -0
- package/api/CryptoApiNative.js +54 -0
- package/api/EventQueueNative.d.ts +20 -0
- package/api/EventQueueNative.js +37 -0
- package/api/IdGenerator.d.ts +14 -0
- package/api/IdGenerator.js +21 -0
- package/api/InboxApiNative.d.ts +38 -0
- package/api/InboxApiNative.js +90 -0
- package/api/StoreApiNative.d.ts +37 -0
- package/api/StoreApiNative.js +87 -0
- package/api/ThreadApiNative.d.ts +31 -0
- package/api/ThreadApiNative.js +69 -0
- package/assets/endpoint-wasm-module.js +20 -0
- package/{webAssets → assets}/endpoint-wasm-module.wasm +0 -0
- package/bundle/privmx-endpoint-web.js +2 -0
- package/bundle/privmx-endpoint-web.js.LICENSE.txt +10 -0
- package/bundle.d.ts +12 -0
- package/bundle.js +16 -0
- package/extra/__tests__/__mocks__/constants.d.ts +36 -0
- package/extra/__tests__/__mocks__/constants.js +42 -0
- package/extra/__tests__/__mocks__/mockContainerSubscriber.d.ts +12 -0
- package/extra/__tests__/__mocks__/mockContainerSubscriber.js +25 -0
- package/extra/__tests__/__mocks__/mockEventAPIs.d.ts +30 -0
- package/extra/__tests__/__mocks__/mockEventAPIs.js +70 -0
- package/extra/__tests__/__mocks__/mockEventQueue.d.ts +13 -0
- package/extra/__tests__/__mocks__/mockEventQueue.js +45 -0
- package/extra/__tests__/__mocks__/utils.d.ts +8 -0
- package/extra/__tests__/__mocks__/utils.js +21 -0
- package/extra/__tests__/eventsManager.test.d.ts +1 -0
- package/extra/__tests__/eventsManager.test.js +73 -0
- package/extra/__tests__/inboxEventManager.test.d.ts +1 -0
- package/extra/__tests__/inboxEventManager.test.js +56 -0
- package/extra/__tests__/storeEventManager.test.d.ts +1 -0
- package/extra/__tests__/storeEventManager.test.js +56 -0
- package/extra/__tests__/threadEventManager.test.d.ts +1 -0
- package/extra/__tests__/threadEventManager.test.js +60 -0
- package/extra/__tests__/utils.test.d.ts +1 -0
- package/extra/__tests__/utils.test.js +52 -0
- package/extra/events.d.ts +234 -0
- package/extra/events.js +270 -0
- package/extra/files.d.ts +120 -0
- package/extra/files.js +216 -0
- package/extra/generics.d.ts +19 -0
- package/extra/generics.js +2 -0
- package/extra/inbox.d.ts +38 -0
- package/extra/inbox.js +35 -0
- package/extra/index.d.ts +8 -0
- package/extra/index.js +23 -0
- package/extra/utils.d.ts +24 -0
- package/extra/utils.js +68 -0
- package/index.d.ts +14 -0
- package/index.js +25 -0
- package/package.json +45 -3
- package/service/BaseApi.d.ts +16 -0
- package/service/BaseApi.js +29 -0
- package/service/Connection.d.ts +45 -0
- package/service/Connection.js +57 -0
- package/service/CryptoApi.d.ts +83 -0
- package/service/CryptoApi.js +113 -0
- package/service/EndpointFactory.d.ts +89 -0
- package/service/EndpointFactory.js +173 -0
- package/service/EventQueue.d.ts +20 -0
- package/service/EventQueue.js +37 -0
- package/service/InboxApi.d.ts +183 -0
- package/service/InboxApi.js +262 -0
- package/service/StoreApi.d.ts +170 -0
- package/service/StoreApi.js +246 -0
- package/service/ThreadApi.d.ts +124 -0
- package/service/ThreadApi.js +186 -0
- package/service/index.d.ts +9 -0
- package/service/index.js +19 -0
- package/webAssets/endpoint-wasm-module.js +0 -20
- package/webAssets/privmx-endpoint-web.js +0 -2
- /package/{webAssets/LICENSE.md → LICENSE.md} +0 -0
- /package/{webAssets → assets}/driver-web-context.js +0 -0
- /package/{webAssets → assets}/endpoint-wasm-module.worker.js +0 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Types } from "..";
|
|
2
|
+
export type Channel = 'inbox' | `inbox/${string}/entries` | 'store' | `store/${string}/files` | 'thread' | `thread/${string}/messages` | `connection/${string}`;
|
|
3
|
+
/**
|
|
4
|
+
* Represents a generic event structure.
|
|
5
|
+
*/
|
|
6
|
+
export interface GenericEvent<K extends keyof EventPayload> extends Types.Event {
|
|
7
|
+
/**
|
|
8
|
+
* Type of the event.
|
|
9
|
+
*/
|
|
10
|
+
type: keyof EventPayload;
|
|
11
|
+
/**
|
|
12
|
+
* The channel through which the event was emitted.
|
|
13
|
+
*/
|
|
14
|
+
channel: Channel;
|
|
15
|
+
/**
|
|
16
|
+
* Data associated with the event.
|
|
17
|
+
*/
|
|
18
|
+
data: EventPayload[K];
|
|
19
|
+
/**
|
|
20
|
+
* ID of connection to which the event was sent.
|
|
21
|
+
*/
|
|
22
|
+
connectionId: number;
|
|
23
|
+
}
|
|
24
|
+
export interface EventPayload {
|
|
25
|
+
threadCreated: Types.Thread;
|
|
26
|
+
threadUpdated: Types.Thread;
|
|
27
|
+
threadDeleted: {
|
|
28
|
+
threadId: string;
|
|
29
|
+
};
|
|
30
|
+
threadStatsChanged: {
|
|
31
|
+
lastMsgDate: number;
|
|
32
|
+
messagesCount: number;
|
|
33
|
+
};
|
|
34
|
+
threadNewMessage: Types.Message;
|
|
35
|
+
threadMessageUpdated: Types.Message;
|
|
36
|
+
threadMessageDeleted: {
|
|
37
|
+
threadId: string;
|
|
38
|
+
messageId: string;
|
|
39
|
+
};
|
|
40
|
+
storeCreated: Types.Store;
|
|
41
|
+
storeUpdated: Types.Store;
|
|
42
|
+
storeDeleted: {
|
|
43
|
+
storeId: string;
|
|
44
|
+
};
|
|
45
|
+
storeStatsChanged: {
|
|
46
|
+
contextId: string;
|
|
47
|
+
storeId: string;
|
|
48
|
+
lastFileDate: number;
|
|
49
|
+
files: number;
|
|
50
|
+
};
|
|
51
|
+
storeFileCreated: Types.File;
|
|
52
|
+
storeFileUpdated: Types.File;
|
|
53
|
+
storeFileDeleted: {
|
|
54
|
+
contextId: string;
|
|
55
|
+
storeId: string;
|
|
56
|
+
fileId: string;
|
|
57
|
+
};
|
|
58
|
+
inboxDeleted: {
|
|
59
|
+
inboxId: string;
|
|
60
|
+
};
|
|
61
|
+
inboxUpdated: Types.Inbox;
|
|
62
|
+
inboxCreated: Types.Inbox;
|
|
63
|
+
inboxEntryCreated: Types.InboxEntry;
|
|
64
|
+
inboxEntryDeleted: {
|
|
65
|
+
inboxId: string;
|
|
66
|
+
entryId: string;
|
|
67
|
+
};
|
|
68
|
+
libConnected: undefined;
|
|
69
|
+
libDisconnected: undefined;
|
|
70
|
+
libPlatformDisconnected: undefined;
|
|
71
|
+
libBreak: undefined;
|
|
72
|
+
}
|
|
73
|
+
type EventType = keyof EventPayload;
|
|
74
|
+
type EventHandler<E extends keyof EventPayload> = {
|
|
75
|
+
event: E;
|
|
76
|
+
callback: (event: GenericEvent<E>) => void;
|
|
77
|
+
};
|
|
78
|
+
export type OnMessageEventHandler = EventHandler<'threadMessageDeleted'> | EventHandler<'threadNewMessage'> | EventHandler<'threadMessageUpdated'>;
|
|
79
|
+
export type OnThreadEventHandler = EventHandler<'threadCreated'> | EventHandler<'threadDeleted'> | EventHandler<'threadUpdated'> | EventHandler<'threadStatsChanged'>;
|
|
80
|
+
export type OnFileEventHandler = EventHandler<'storeFileDeleted'> | EventHandler<'storeFileCreated'> | EventHandler<'storeFileUpdated'>;
|
|
81
|
+
export type OnStoreEventHandler = EventHandler<'storeCreated'> | EventHandler<'storeStatsChanged'> | EventHandler<'storeUpdated'> | EventHandler<'storeDeleted'>;
|
|
82
|
+
export type OnInboxEventHandler = EventHandler<'inboxDeleted'> | EventHandler<'inboxCreated'> | EventHandler<'inboxUpdated'>;
|
|
83
|
+
export type OnEntryEventHandler = EventHandler<'inboxEntryCreated'> | EventHandler<'inboxEntryDeleted'>;
|
|
84
|
+
export type OnConnectionHandler = EventHandler<'libConnected'> | EventHandler<'libDisconnected'> | EventHandler<'libBreak'> | EventHandler<'libPlatformDisconnected'>;
|
|
85
|
+
export declare abstract class BaseEventManager {
|
|
86
|
+
private _listeners;
|
|
87
|
+
get listeners(): Map<string, Function[]>;
|
|
88
|
+
protected channels: Set<Channel>;
|
|
89
|
+
protected abstract subscribeForModuleEvents(): Promise<void>;
|
|
90
|
+
protected abstract subscribeForModuleElementsEvents(containerId: string): Promise<void>;
|
|
91
|
+
protected abstract unsubscribeFromModuleEvents(): Promise<void>;
|
|
92
|
+
protected abstract unsubscribeFromModuleElementsEvents(containerId: string): Promise<void>;
|
|
93
|
+
constructor();
|
|
94
|
+
/**
|
|
95
|
+
* Checks whether the user is subscribed to given channel
|
|
96
|
+
* @param channel Channel
|
|
97
|
+
* @returns {boolean} `boolean`
|
|
98
|
+
*/
|
|
99
|
+
protected isSubscribedToChannel(channel: Channel): boolean;
|
|
100
|
+
protected hasEventsOnChannel(channel: Channel): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Removes an event listeners from given `channel` and `eventType`
|
|
103
|
+
* @param {Channel} channel channel
|
|
104
|
+
* @param {string} eventType type of the event
|
|
105
|
+
* @param {function} callback callback function
|
|
106
|
+
*/
|
|
107
|
+
protected removeEventListener: (channel: Channel, eventType: EventType, callback: Function) => void;
|
|
108
|
+
/**
|
|
109
|
+
* Removes all listeners from given channel
|
|
110
|
+
* @param {Channel} channel channel
|
|
111
|
+
* @returns {void}
|
|
112
|
+
*/
|
|
113
|
+
removeChannelEvents: (channel: Channel) => void;
|
|
114
|
+
dispatchEvent(event: Types.Event): void;
|
|
115
|
+
/**
|
|
116
|
+
* Add an event listeners on given channel and eventType
|
|
117
|
+
* @param {Channel} channel channel
|
|
118
|
+
* @param {string} eventType type of the event
|
|
119
|
+
* @param {function} callback callback function
|
|
120
|
+
* @returns {function} function to remove the event listeners
|
|
121
|
+
*/
|
|
122
|
+
private addEventListener;
|
|
123
|
+
protected addContainerListener: (channel: Channel, eventType: EventType, callback: Function) => Promise<() => Promise<void>>;
|
|
124
|
+
protected addContainerElementListener: (containerId: string, channel: Channel, eventType: EventType, callback: Function) => Promise<() => Promise<void>>;
|
|
125
|
+
}
|
|
126
|
+
export interface SubscriberForThreadsEvents {
|
|
127
|
+
subscribeForThreadEvents(): Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Unsubscribes from the Thread module main events.
|
|
130
|
+
*/
|
|
131
|
+
unsubscribeFromThreadEvents(): Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Subscribes for events in given Thread.
|
|
134
|
+
* @param {string} threadId ID of the Thread to subscribe
|
|
135
|
+
*/
|
|
136
|
+
subscribeForMessageEvents(threadId: string): Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* Unsubscribes from events in given Thread.
|
|
139
|
+
* @param {string} threadId ID of the Thread to unsubscribe
|
|
140
|
+
*/
|
|
141
|
+
unsubscribeFromMessageEvents(threadId: string): Promise<void>;
|
|
142
|
+
}
|
|
143
|
+
export declare class ThreadEventsManager extends BaseEventManager {
|
|
144
|
+
private threadApi;
|
|
145
|
+
constructor(threadApi: SubscriberForThreadsEvents);
|
|
146
|
+
subscribeForModuleEvents(): Promise<void>;
|
|
147
|
+
subscribeForModuleElementsEvents(id: string): Promise<void>;
|
|
148
|
+
unsubscribeFromModuleEvents(): Promise<void>;
|
|
149
|
+
unsubscribeFromModuleElementsEvents(id: string): Promise<void>;
|
|
150
|
+
onThreadEvent(handler: OnThreadEventHandler): Promise<() => Promise<void>>;
|
|
151
|
+
onMessageEvent(threadId: string, handler: OnMessageEventHandler): Promise<() => Promise<void>>;
|
|
152
|
+
}
|
|
153
|
+
export interface SubscriberForStoreEvents {
|
|
154
|
+
subscribeForStoreEvents(): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* Unsubscribes from the Store module main events.
|
|
157
|
+
*/
|
|
158
|
+
unsubscribeFromStoreEvents(): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* Subscribes for events in given Store.
|
|
161
|
+
* @param {string} storeId ID of the Store to subscribe
|
|
162
|
+
*/
|
|
163
|
+
subscribeForFileEvents(storeId: string): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Unsubscribes from events in given Store.
|
|
166
|
+
* @param {string} storeId ID of the Store to unsubscribe
|
|
167
|
+
*/
|
|
168
|
+
unsubscribeFromFileEvents(storeId: string): Promise<void>;
|
|
169
|
+
}
|
|
170
|
+
export declare class StoreEventsManager extends BaseEventManager {
|
|
171
|
+
private storeApi;
|
|
172
|
+
constructor(storeApi: SubscriberForStoreEvents);
|
|
173
|
+
subscribeForModuleEvents(): Promise<void>;
|
|
174
|
+
subscribeForModuleElementsEvents(id: string): Promise<void>;
|
|
175
|
+
unsubscribeFromModuleEvents(): Promise<void>;
|
|
176
|
+
unsubscribeFromModuleElementsEvents(id: string): Promise<void>;
|
|
177
|
+
onStoreEvent(handler: OnStoreEventHandler): Promise<() => Promise<void>>;
|
|
178
|
+
onFileEvent(storeId: string, handler: OnFileEventHandler): Promise<() => Promise<void>>;
|
|
179
|
+
}
|
|
180
|
+
export interface SubscriberForInboxEvents {
|
|
181
|
+
subscribeForInboxEvents(): Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Unsubscribes from the Inbox module main events.
|
|
184
|
+
*/
|
|
185
|
+
unsubscribeFromInboxEvents(): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Subscribes for events in given Inbox.
|
|
188
|
+
* @param {string} inboxId ID of the Inbox to subscribe
|
|
189
|
+
*/
|
|
190
|
+
subscribeForEntryEvents(inboxId: string): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Unsubscribes from events in given Inbox.
|
|
193
|
+
* @param {string} inboxId ID of the Inbox to unsubscribe
|
|
194
|
+
*/
|
|
195
|
+
unsubscribeFromEntryEvents(inboxId: string): Promise<void>;
|
|
196
|
+
}
|
|
197
|
+
export declare class InboxEventsManager extends BaseEventManager {
|
|
198
|
+
private inboxApi;
|
|
199
|
+
constructor(inboxApi: SubscriberForInboxEvents);
|
|
200
|
+
subscribeForModuleEvents(): Promise<void>;
|
|
201
|
+
subscribeForModuleElementsEvents(id: string): Promise<void>;
|
|
202
|
+
unsubscribeFromModuleEvents(): Promise<void>;
|
|
203
|
+
unsubscribeFromModuleElementsEvents(id: string): Promise<void>;
|
|
204
|
+
onInboxEvent(handler: OnInboxEventHandler): Promise<() => Promise<void>>;
|
|
205
|
+
onEntryEvent(inboxId: string, handler: OnEntryEventHandler): Promise<() => Promise<void>>;
|
|
206
|
+
}
|
|
207
|
+
export declare class ConnectionEventsManager extends BaseEventManager {
|
|
208
|
+
private connectionId;
|
|
209
|
+
protected subscribeForModuleEvents(): Promise<void>;
|
|
210
|
+
protected subscribeForModuleElementsEvents(): Promise<void>;
|
|
211
|
+
protected unsubscribeFromModuleEvents(): Promise<void>;
|
|
212
|
+
protected unsubscribeFromModuleElementsEvents(): Promise<void>;
|
|
213
|
+
constructor(connectionId: string);
|
|
214
|
+
onConnectionEvent(handler: OnConnectionHandler): Promise<() => Promise<void>>;
|
|
215
|
+
}
|
|
216
|
+
export declare class EventManager {
|
|
217
|
+
private _isEventLoopRunning;
|
|
218
|
+
dispatchers: ((event: Types.Event) => void)[];
|
|
219
|
+
private eventsQueue;
|
|
220
|
+
constructor();
|
|
221
|
+
private listenForEvents;
|
|
222
|
+
static startEventLoop(eventQueue: {
|
|
223
|
+
waitEvent: () => Promise<Types.Event>;
|
|
224
|
+
}): EventManager;
|
|
225
|
+
stopEventLoop(): void;
|
|
226
|
+
removeAllDispatchers: () => void;
|
|
227
|
+
protected onEvent(event: Types.Event): void;
|
|
228
|
+
registerDispatcher(manager: BaseEventManager): void;
|
|
229
|
+
getThreadEventManager(threadApi: SubscriberForThreadsEvents): ThreadEventsManager;
|
|
230
|
+
getStoreEventManager(storeApi: SubscriberForStoreEvents): StoreEventsManager;
|
|
231
|
+
getInboxEventManager(inboxApi: SubscriberForInboxEvents): InboxEventsManager;
|
|
232
|
+
getConnectionEventManager(connectionId: string): ConnectionEventsManager;
|
|
233
|
+
}
|
|
234
|
+
export {};
|
package/extra/events.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventManager = exports.ConnectionEventsManager = exports.InboxEventsManager = exports.StoreEventsManager = exports.ThreadEventsManager = exports.BaseEventManager = void 0;
|
|
4
|
+
class BaseEventManager {
|
|
5
|
+
_listeners; // event tag - "event@channel"
|
|
6
|
+
get listeners() {
|
|
7
|
+
return this._listeners;
|
|
8
|
+
}
|
|
9
|
+
channels;
|
|
10
|
+
constructor() {
|
|
11
|
+
this._listeners = new Map();
|
|
12
|
+
this.channels = new Set();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Checks whether the user is subscribed to given channel
|
|
16
|
+
* @param channel Channel
|
|
17
|
+
* @returns {boolean} `boolean`
|
|
18
|
+
*/
|
|
19
|
+
isSubscribedToChannel(channel) {
|
|
20
|
+
return this.channels.has(channel);
|
|
21
|
+
}
|
|
22
|
+
hasEventsOnChannel(channel) {
|
|
23
|
+
return (Array.from(this._listeners.keys()).findIndex((listenerTag) => listenerTag.split('@')[1] === channel) !== -1);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Removes an event listeners from given `channel` and `eventType`
|
|
27
|
+
* @param {Channel} channel channel
|
|
28
|
+
* @param {string} eventType type of the event
|
|
29
|
+
* @param {function} callback callback function
|
|
30
|
+
*/
|
|
31
|
+
removeEventListener = (channel, eventType, callback) => {
|
|
32
|
+
const eventTag = `${eventType}@${channel}`;
|
|
33
|
+
if (this._listeners.has(eventTag)) {
|
|
34
|
+
const channelCallbacks = this._listeners.get(eventTag);
|
|
35
|
+
const newCallback = channelCallbacks.filter((cb) => cb !== callback);
|
|
36
|
+
if (newCallback.length === 0) {
|
|
37
|
+
this._listeners.delete(eventTag);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this._listeners.set(eventTag, newCallback);
|
|
41
|
+
}
|
|
42
|
+
if (!this.hasEventsOnChannel(channel)) {
|
|
43
|
+
this.channels.delete(channel);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Removes all listeners from given channel
|
|
49
|
+
* @param {Channel} channel channel
|
|
50
|
+
* @returns {void}
|
|
51
|
+
*/
|
|
52
|
+
removeChannelEvents = (channel) => {
|
|
53
|
+
const listenersToRemove = Array.from(this._listeners.keys()).filter((listenerTag) => listenerTag.split('@')[1] === channel);
|
|
54
|
+
listenersToRemove.forEach((listenerTag) => {
|
|
55
|
+
this._listeners.delete(listenerTag);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
dispatchEvent(event) {
|
|
59
|
+
if (event.type == 'libConnected' ||
|
|
60
|
+
event.type == 'libDisconnected' ||
|
|
61
|
+
event.type == 'libPlatformDisconnected') {
|
|
62
|
+
event.channel = `connection/${event.connectionId}`;
|
|
63
|
+
}
|
|
64
|
+
const eventTag = `${event.type}@${event.channel}`;
|
|
65
|
+
if (this._listeners.has(eventTag)) {
|
|
66
|
+
const callbacks = this._listeners.get(eventTag);
|
|
67
|
+
if (callbacks) {
|
|
68
|
+
callbacks.forEach((callback) => callback(event));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Add an event listeners on given channel and eventType
|
|
74
|
+
* @param {Channel} channel channel
|
|
75
|
+
* @param {string} eventType type of the event
|
|
76
|
+
* @param {function} callback callback function
|
|
77
|
+
* @returns {function} function to remove the event listeners
|
|
78
|
+
*/
|
|
79
|
+
addEventListener = (channel, eventType, callback) => {
|
|
80
|
+
const eventTag = `${eventType}@${channel}`;
|
|
81
|
+
if (this._listeners.has(eventTag)) {
|
|
82
|
+
this._listeners.get(eventTag).push(callback);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this._listeners.set(eventTag, [callback]);
|
|
86
|
+
}
|
|
87
|
+
this.channels.add(channel);
|
|
88
|
+
return () => {
|
|
89
|
+
this.removeEventListener(channel, eventType, callback);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
addContainerListener = async (channel, eventType, callback) => {
|
|
93
|
+
if (!this.isSubscribedToChannel(channel)) {
|
|
94
|
+
await this.subscribeForModuleEvents();
|
|
95
|
+
}
|
|
96
|
+
const removeListener = this.addEventListener(channel, eventType, callback);
|
|
97
|
+
return async () => {
|
|
98
|
+
removeListener();
|
|
99
|
+
if (!this.hasEventsOnChannel(channel)) {
|
|
100
|
+
await this.unsubscribeFromModuleEvents();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
addContainerElementListener = async (containerId, channel, eventType, callback) => {
|
|
105
|
+
if (!this.isSubscribedToChannel(channel)) {
|
|
106
|
+
await this.subscribeForModuleElementsEvents(containerId);
|
|
107
|
+
}
|
|
108
|
+
const removeListener = this.addEventListener(channel, eventType, callback);
|
|
109
|
+
return async () => {
|
|
110
|
+
removeListener();
|
|
111
|
+
if (!this.hasEventsOnChannel(channel)) {
|
|
112
|
+
await this.unsubscribeFromModuleElementsEvents(containerId);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
exports.BaseEventManager = BaseEventManager;
|
|
118
|
+
class ThreadEventsManager extends BaseEventManager {
|
|
119
|
+
threadApi;
|
|
120
|
+
constructor(threadApi) {
|
|
121
|
+
super();
|
|
122
|
+
this.threadApi = threadApi;
|
|
123
|
+
}
|
|
124
|
+
subscribeForModuleEvents() {
|
|
125
|
+
return this.threadApi.subscribeForThreadEvents();
|
|
126
|
+
}
|
|
127
|
+
subscribeForModuleElementsEvents(id) {
|
|
128
|
+
return this.threadApi.subscribeForMessageEvents(id);
|
|
129
|
+
}
|
|
130
|
+
unsubscribeFromModuleEvents() {
|
|
131
|
+
return this.threadApi.unsubscribeFromThreadEvents();
|
|
132
|
+
}
|
|
133
|
+
unsubscribeFromModuleElementsEvents(id) {
|
|
134
|
+
return this.threadApi.unsubscribeFromMessageEvents(id);
|
|
135
|
+
}
|
|
136
|
+
async onThreadEvent(handler) {
|
|
137
|
+
const channel = 'thread';
|
|
138
|
+
return this.addContainerListener(channel, handler.event, handler.callback);
|
|
139
|
+
}
|
|
140
|
+
async onMessageEvent(threadId, handler) {
|
|
141
|
+
const channel = `thread/${threadId}/messages`;
|
|
142
|
+
return this.addContainerElementListener(threadId, channel, handler.event, handler.callback);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.ThreadEventsManager = ThreadEventsManager;
|
|
146
|
+
class StoreEventsManager extends BaseEventManager {
|
|
147
|
+
storeApi;
|
|
148
|
+
constructor(storeApi) {
|
|
149
|
+
super();
|
|
150
|
+
this.storeApi = storeApi;
|
|
151
|
+
}
|
|
152
|
+
subscribeForModuleEvents() { return this.storeApi.subscribeForStoreEvents(); }
|
|
153
|
+
subscribeForModuleElementsEvents(id) { return this.storeApi.subscribeForFileEvents(id); }
|
|
154
|
+
unsubscribeFromModuleEvents() { return this.storeApi.unsubscribeFromStoreEvents(); }
|
|
155
|
+
unsubscribeFromModuleElementsEvents(id) { return this.storeApi.unsubscribeFromFileEvents(id); }
|
|
156
|
+
async onStoreEvent(handler) {
|
|
157
|
+
const channel = 'store';
|
|
158
|
+
return this.addContainerListener(channel, handler.event, handler.callback);
|
|
159
|
+
}
|
|
160
|
+
async onFileEvent(storeId, handler) {
|
|
161
|
+
const channel = `store/${storeId}/files`;
|
|
162
|
+
return this.addContainerElementListener(storeId, channel, handler.event, handler.callback);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.StoreEventsManager = StoreEventsManager;
|
|
166
|
+
class InboxEventsManager extends BaseEventManager {
|
|
167
|
+
inboxApi;
|
|
168
|
+
constructor(inboxApi) {
|
|
169
|
+
super();
|
|
170
|
+
this.inboxApi = inboxApi;
|
|
171
|
+
}
|
|
172
|
+
subscribeForModuleEvents() { return this.inboxApi.subscribeForInboxEvents(); }
|
|
173
|
+
subscribeForModuleElementsEvents(id) { return this.inboxApi.subscribeForEntryEvents(id); }
|
|
174
|
+
unsubscribeFromModuleEvents() { return this.inboxApi.unsubscribeFromInboxEvents(); }
|
|
175
|
+
unsubscribeFromModuleElementsEvents(id) { return this.inboxApi.unsubscribeFromEntryEvents(id); }
|
|
176
|
+
async onInboxEvent(handler) {
|
|
177
|
+
const channel = 'inbox';
|
|
178
|
+
return this.addContainerListener(channel, handler.event, handler.callback);
|
|
179
|
+
}
|
|
180
|
+
async onEntryEvent(inboxId, handler) {
|
|
181
|
+
const channel = `inbox/${inboxId}/entries`;
|
|
182
|
+
return this.addContainerElementListener(inboxId, channel, handler.event, handler.callback);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.InboxEventsManager = InboxEventsManager;
|
|
186
|
+
class ConnectionEventsManager extends BaseEventManager {
|
|
187
|
+
connectionId;
|
|
188
|
+
//Connection events don't have subscribe functions
|
|
189
|
+
subscribeForModuleEvents() {
|
|
190
|
+
return new Promise((resolve) => resolve());
|
|
191
|
+
}
|
|
192
|
+
subscribeForModuleElementsEvents() {
|
|
193
|
+
return new Promise((resolve) => resolve());
|
|
194
|
+
}
|
|
195
|
+
unsubscribeFromModuleEvents() {
|
|
196
|
+
return new Promise((resolve) => resolve());
|
|
197
|
+
}
|
|
198
|
+
unsubscribeFromModuleElementsEvents() {
|
|
199
|
+
return new Promise((resolve) => resolve());
|
|
200
|
+
}
|
|
201
|
+
constructor(connectionId) {
|
|
202
|
+
super();
|
|
203
|
+
this.connectionId = connectionId;
|
|
204
|
+
}
|
|
205
|
+
async onConnectionEvent(handler) {
|
|
206
|
+
const channel = `connection/${this.connectionId}`;
|
|
207
|
+
const removeListener = this.addContainerListener(channel, handler.event, handler.callback);
|
|
208
|
+
return removeListener;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.ConnectionEventsManager = ConnectionEventsManager;
|
|
212
|
+
class EventManager {
|
|
213
|
+
_isEventLoopRunning = false;
|
|
214
|
+
dispatchers = [];
|
|
215
|
+
eventsQueue = null;
|
|
216
|
+
constructor() { }
|
|
217
|
+
listenForEvents() {
|
|
218
|
+
if (this.eventsQueue) {
|
|
219
|
+
this.eventsQueue.waitEvent().then((event) => {
|
|
220
|
+
this.onEvent(event);
|
|
221
|
+
this.listenForEvents();
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
static startEventLoop(eventQueue) {
|
|
226
|
+
const manager = new EventManager();
|
|
227
|
+
manager.eventsQueue = eventQueue;
|
|
228
|
+
manager._isEventLoopRunning = true;
|
|
229
|
+
manager.eventsQueue.waitEvent().then((event) => {
|
|
230
|
+
if (!manager._isEventLoopRunning)
|
|
231
|
+
return;
|
|
232
|
+
manager.onEvent(event);
|
|
233
|
+
manager.listenForEvents();
|
|
234
|
+
});
|
|
235
|
+
return manager;
|
|
236
|
+
}
|
|
237
|
+
stopEventLoop() {
|
|
238
|
+
this._isEventLoopRunning = false;
|
|
239
|
+
}
|
|
240
|
+
removeAllDispatchers = () => {
|
|
241
|
+
this.dispatchers = [];
|
|
242
|
+
};
|
|
243
|
+
onEvent(event) {
|
|
244
|
+
this.dispatchers.forEach((cb) => cb(event));
|
|
245
|
+
}
|
|
246
|
+
registerDispatcher(manager) {
|
|
247
|
+
this.dispatchers.push((e) => manager.dispatchEvent(e));
|
|
248
|
+
}
|
|
249
|
+
getThreadEventManager(threadApi) {
|
|
250
|
+
const manager = new ThreadEventsManager(threadApi);
|
|
251
|
+
this.registerDispatcher(manager);
|
|
252
|
+
return manager;
|
|
253
|
+
}
|
|
254
|
+
getStoreEventManager(storeApi) {
|
|
255
|
+
const manager = new StoreEventsManager(storeApi);
|
|
256
|
+
this.registerDispatcher(manager);
|
|
257
|
+
return manager;
|
|
258
|
+
}
|
|
259
|
+
getInboxEventManager(inboxApi) {
|
|
260
|
+
const manager = new InboxEventsManager(inboxApi);
|
|
261
|
+
this.registerDispatcher(manager);
|
|
262
|
+
return manager;
|
|
263
|
+
}
|
|
264
|
+
getConnectionEventManager(connectionId) {
|
|
265
|
+
const manager = new ConnectionEventsManager(connectionId);
|
|
266
|
+
this.registerDispatcher(manager);
|
|
267
|
+
return manager;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
exports.EventManager = EventManager;
|
package/extra/files.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a stream reader for reading a file in chunks.
|
|
3
|
+
*/
|
|
4
|
+
import { InboxApi, StoreApi } from '..';
|
|
5
|
+
export declare const FILE_DEFAULT_CHUNK_SIZE = 1048576;
|
|
6
|
+
export declare class StreamReader {
|
|
7
|
+
private _handle;
|
|
8
|
+
private _offset;
|
|
9
|
+
private _api;
|
|
10
|
+
private readonly chunkSize;
|
|
11
|
+
hasDataToRead: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance of StreamReader.
|
|
14
|
+
*
|
|
15
|
+
* @param {number} handle - The file handle.
|
|
16
|
+
* @param {StoreApi} api {@link StoreApi `StoreApi`} instance
|
|
17
|
+
*/
|
|
18
|
+
constructor(handle: number, api: StoreApi | InboxApi, chunkSize?: number);
|
|
19
|
+
static readFile(api: InboxApi | StoreApi, fileID: string, chunkSize?: number): Promise<StreamReader>;
|
|
20
|
+
/**
|
|
21
|
+
* Reads the next chunk of the file.
|
|
22
|
+
*
|
|
23
|
+
* @returns {Promise<boolean>} A promise that resolves to true if there are more chunks to read, or false if the end of the file is reached.
|
|
24
|
+
*/
|
|
25
|
+
[Symbol.asyncIterator](): AsyncGenerator<readonly [Uint8Array, number], void, unknown>;
|
|
26
|
+
readNextChunk(): Promise<Uint8Array>;
|
|
27
|
+
getFileContent(): Promise<Uint8Array>;
|
|
28
|
+
/**
|
|
29
|
+
* Aborts the reading process and closes the file handle.
|
|
30
|
+
*
|
|
31
|
+
* @returns {Promise<string>} A promise that resolves when the file handle is closed.
|
|
32
|
+
*/
|
|
33
|
+
abort(): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Closes the file handle.
|
|
36
|
+
*
|
|
37
|
+
* @returns {Promise<string>} A promise that resolves when the file handle is closed and returns file ID.
|
|
38
|
+
*/
|
|
39
|
+
close(): Promise<string>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Represents a file stream uploader for uploading a Browser FileHandle in chunks.
|
|
43
|
+
*/
|
|
44
|
+
interface FileContainerApi {
|
|
45
|
+
writeToFile: (chunk: Uint8Array) => Promise<void>;
|
|
46
|
+
closeFile: () => Promise<string>;
|
|
47
|
+
}
|
|
48
|
+
export declare class FileUploader {
|
|
49
|
+
private readonly _size;
|
|
50
|
+
private offset;
|
|
51
|
+
private readonly _api;
|
|
52
|
+
private _reader;
|
|
53
|
+
/**
|
|
54
|
+
* Creates an instance of FileUploader.
|
|
55
|
+
*
|
|
56
|
+
* @param {number} handle - The file handle.
|
|
57
|
+
* @param {File} file - The data (file content) to upload.
|
|
58
|
+
* @param {StoreApi} api {@link StoreApi `StoreApi`} instance
|
|
59
|
+
*/
|
|
60
|
+
constructor(file: File, api: FileContainerApi);
|
|
61
|
+
static uploadStoreFile({ storeApi, storeId, file, privateMeta, publicMeta }: {
|
|
62
|
+
storeId: string;
|
|
63
|
+
file: File;
|
|
64
|
+
storeApi: StoreApi;
|
|
65
|
+
publicMeta?: Uint8Array;
|
|
66
|
+
privateMeta?: Uint8Array;
|
|
67
|
+
}): Promise<FileUploader>;
|
|
68
|
+
static uploadInboxFile({ inboxApi, inboxHandle, preparedFileUpload }: {
|
|
69
|
+
inboxHandle: number;
|
|
70
|
+
preparedFileUpload: {
|
|
71
|
+
file: File;
|
|
72
|
+
handle: number;
|
|
73
|
+
};
|
|
74
|
+
inboxApi: InboxApi;
|
|
75
|
+
}): Promise<FileUploader>;
|
|
76
|
+
static prepareInboxUpload({ inboxApi, file, privateMeta, publicMeta }: {
|
|
77
|
+
inboxApi: InboxApi;
|
|
78
|
+
file: File;
|
|
79
|
+
publicMeta?: Uint8Array;
|
|
80
|
+
privateMeta?: Uint8Array;
|
|
81
|
+
}): Promise<{
|
|
82
|
+
file: File;
|
|
83
|
+
handle: number;
|
|
84
|
+
}>;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the progress of uploading the file as a percentage.
|
|
87
|
+
*
|
|
88
|
+
* @returns {number} The progress percentage.
|
|
89
|
+
*/
|
|
90
|
+
get progress(): number;
|
|
91
|
+
/**
|
|
92
|
+
* Sends the next chunk of the file data to the server.
|
|
93
|
+
*
|
|
94
|
+
* @returns {Promise<boolean>} A promise that resolves to true if there are more chunks to send, or false if all data has been sent.
|
|
95
|
+
*/
|
|
96
|
+
sendNextChunk(): Promise<boolean>;
|
|
97
|
+
uploadFileContent(): Promise<string>;
|
|
98
|
+
/**
|
|
99
|
+
* Aborts the uploading process, closes the file handle, and deletes the uploaded part of the file.
|
|
100
|
+
*
|
|
101
|
+
* @returns {Promise<void>} A promise that resolves when the file handle is closed and the uploaded part is deleted.
|
|
102
|
+
*/
|
|
103
|
+
abort(): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Closes the file handle.
|
|
106
|
+
*
|
|
107
|
+
* @returns {Promise<string>} A promise that resolves when the file handle is closed and returns file ID.
|
|
108
|
+
*/
|
|
109
|
+
close(): Promise<string>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Downloads a file from the server.
|
|
113
|
+
*
|
|
114
|
+
* @param {StoreApi|InboxApi} api - The API instance used for file operations.
|
|
115
|
+
* @param {string} fileId - The ID of the file to download.
|
|
116
|
+
* @param {string} [targetFileName] - The target file name for saving the downloaded file.
|
|
117
|
+
* @returns {Promise<void>} A promise that resolves when the download is complete.
|
|
118
|
+
*/
|
|
119
|
+
export declare function downloadFile(api: StoreApi | InboxApi, fileId: string, targetFileName?: string): Promise<void>;
|
|
120
|
+
export {};
|