@simplito/privmx-webendpoint 2.6.2 → 2.6.3
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 +55 -1
- package/Types.js +4 -0
- package/assets/endpoint-wasm-module.js +2 -19
- package/assets/endpoint-wasm-module.wasm +0 -0
- package/extra/PrivmxClient.d.ts +13 -1
- package/extra/PrivmxClient.js +34 -2
- package/extra/__mocks__/constants.d.ts +5 -0
- package/extra/__mocks__/constants.js +52 -1
- package/extra/__tests__/connectionEventManager.test.d.ts +1 -0
- package/extra/__tests__/connectionEventManager.test.js +29 -0
- package/extra/__tests__/customEventManager.test.d.ts +1 -0
- package/extra/__tests__/customEventManager.test.js +65 -0
- package/extra/__tests__/userEventManager.test.d.ts +1 -0
- package/extra/__tests__/userEventManager.test.js +52 -0
- package/extra/events.d.ts +4 -2
- package/extra/events.js +13 -3
- package/extra/index.d.ts +1 -1
- package/extra/index.js +4 -1
- package/extra/managers.d.ts +18 -11
- package/extra/managers.js +55 -7
- package/extra/subscriptions.d.ts +36 -14
- package/extra/subscriptions.js +30 -13
- package/package.json +1 -1
- package/assets/endpoint-wasm-module.worker.js +0 -1
|
Binary file
|
package/extra/PrivmxClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Connection, CryptoApi, EventQueue, InboxApi, StoreApi, ThreadApi, KvdbApi, EventApi } from '../service';
|
|
2
2
|
import { PublicConnection } from './PublicConnection';
|
|
3
|
-
import { ConnectionEventsManager, InboxEventsManager, StoreEventsManager, ThreadEventsManager } from "./managers";
|
|
3
|
+
import { ConnectionEventsManager, CustomEventsManager, InboxEventsManager, StoreEventsManager, ThreadEventsManager, UserEventsManager } from "./managers";
|
|
4
4
|
import { EventManager } from "./events";
|
|
5
5
|
/**
|
|
6
6
|
* @class PrivmxClient
|
|
@@ -39,9 +39,11 @@ export declare class PrivmxClient {
|
|
|
39
39
|
private kvdbApi;
|
|
40
40
|
private eventApi;
|
|
41
41
|
private connectionEventManager;
|
|
42
|
+
private userEventManager;
|
|
42
43
|
private threadEventManager;
|
|
43
44
|
private storeEventManager;
|
|
44
45
|
private inboxEventManager;
|
|
46
|
+
private customEventsManager;
|
|
45
47
|
/**
|
|
46
48
|
* @constructor
|
|
47
49
|
* @param {Connection} connection - The connection object.
|
|
@@ -123,6 +125,11 @@ export declare class PrivmxClient {
|
|
|
123
125
|
* @returns {Promise<ConnectionEventsManager>}
|
|
124
126
|
*/
|
|
125
127
|
getConnectionEventManager(): Promise<ConnectionEventsManager>;
|
|
128
|
+
/**
|
|
129
|
+
* @description Gets the User Event Manager.
|
|
130
|
+
* @returns {Promise<UserEventsManager>}
|
|
131
|
+
*/
|
|
132
|
+
getUserEventsManager(): Promise<UserEventsManager>;
|
|
126
133
|
/**
|
|
127
134
|
* @description Gets the Thread Event Manager.
|
|
128
135
|
* @returns {Promise<ThreadEventsManager>}
|
|
@@ -138,6 +145,11 @@ export declare class PrivmxClient {
|
|
|
138
145
|
* @returns {Promise<InboxEventsManager>}
|
|
139
146
|
*/
|
|
140
147
|
getInboxEventManager(): Promise<InboxEventsManager>;
|
|
148
|
+
/**
|
|
149
|
+
* @description Gets the Custom Events Manager.
|
|
150
|
+
* @returns {Promise<CustomEventsManager>}
|
|
151
|
+
*/
|
|
152
|
+
getCustomEventsManager(): Promise<CustomEventsManager>;
|
|
141
153
|
/**
|
|
142
154
|
* @description Disconnects from the PrivMX bridge.
|
|
143
155
|
* @returns {Promise<void>}
|
package/extra/PrivmxClient.js
CHANGED
|
@@ -41,9 +41,11 @@ class PrivmxClient {
|
|
|
41
41
|
kvdbApi = null;
|
|
42
42
|
eventApi = null;
|
|
43
43
|
connectionEventManager = null;
|
|
44
|
+
userEventManager = null;
|
|
44
45
|
threadEventManager = null;
|
|
45
46
|
storeEventManager = null;
|
|
46
47
|
inboxEventManager = null;
|
|
48
|
+
customEventsManager = null;
|
|
47
49
|
/**
|
|
48
50
|
* @constructor
|
|
49
51
|
* @param {Connection} connection - The connection object.
|
|
@@ -229,11 +231,25 @@ class PrivmxClient {
|
|
|
229
231
|
this.connectionEventManager = (async () => {
|
|
230
232
|
const eventManager = await PrivmxClient.getEventManager();
|
|
231
233
|
const connection = this.getConnection();
|
|
232
|
-
const connectionId =
|
|
233
|
-
return eventManager.getConnectionEventManager(connectionId);
|
|
234
|
+
const connectionId = await connection.getConnectionId();
|
|
235
|
+
return eventManager.getConnectionEventManager(`${connectionId}`);
|
|
234
236
|
})();
|
|
235
237
|
return this.connectionEventManager;
|
|
236
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* @description Gets the User Event Manager.
|
|
241
|
+
* @returns {Promise<UserEventsManager>}
|
|
242
|
+
*/
|
|
243
|
+
async getUserEventsManager() {
|
|
244
|
+
if (this.userEventManager) {
|
|
245
|
+
return this.userEventManager;
|
|
246
|
+
}
|
|
247
|
+
this.userEventManager = (async () => {
|
|
248
|
+
const eventManager = await PrivmxClient.getEventManager();
|
|
249
|
+
return eventManager.getUserEventsManager(this.getConnection());
|
|
250
|
+
})();
|
|
251
|
+
return this.userEventManager;
|
|
252
|
+
}
|
|
237
253
|
/**
|
|
238
254
|
* @description Gets the Thread Event Manager.
|
|
239
255
|
* @returns {Promise<ThreadEventsManager>}
|
|
@@ -276,6 +292,20 @@ class PrivmxClient {
|
|
|
276
292
|
})();
|
|
277
293
|
return this.inboxEventManager;
|
|
278
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* @description Gets the Custom Events Manager.
|
|
297
|
+
* @returns {Promise<CustomEventsManager>}
|
|
298
|
+
*/
|
|
299
|
+
async getCustomEventsManager() {
|
|
300
|
+
if (this.customEventsManager) {
|
|
301
|
+
return this.customEventsManager;
|
|
302
|
+
}
|
|
303
|
+
this.customEventsManager = (async () => {
|
|
304
|
+
const eventManager = await PrivmxClient.getEventManager();
|
|
305
|
+
return eventManager.getCustomEventsManager(await this.getEventApi());
|
|
306
|
+
})();
|
|
307
|
+
return this.customEventsManager;
|
|
308
|
+
}
|
|
279
309
|
/**
|
|
280
310
|
* @description Disconnects from the PrivMX bridge.
|
|
281
311
|
* @returns {Promise<void>}
|
|
@@ -287,6 +317,8 @@ class PrivmxClient {
|
|
|
287
317
|
this.storeApi = null;
|
|
288
318
|
this.inboxApi = null;
|
|
289
319
|
this.connectionEventManager = null;
|
|
320
|
+
this.customEventsManager = null;
|
|
321
|
+
this.userEventManager = null;
|
|
290
322
|
this.threadEventManager = null;
|
|
291
323
|
this.storeEventManager = null;
|
|
292
324
|
this.inboxEventManager = null;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Types } from "../../index";
|
|
1
2
|
export declare function createBaseEvent(subsId: string): {
|
|
2
3
|
type: string;
|
|
3
4
|
data: {};
|
|
@@ -61,3 +62,7 @@ export declare const MOCK_THREAD_MESSAGE_DELETED_EVENT: (threadID: string, subsI
|
|
|
61
62
|
readonly version: number;
|
|
62
63
|
readonly timestamp: number;
|
|
63
64
|
};
|
|
65
|
+
export declare const MOCK_CONNECTION_USER_ADDED_EVENT: (subsId: string) => Types.Event;
|
|
66
|
+
export declare const MOCK_CONNECTION_USER_STATUS_EVENT: (subsId: string) => Types.Event;
|
|
67
|
+
export declare const MOCK_LIB_CONNECTED_EVENT: (connectionId: number) => Types.Event;
|
|
68
|
+
export declare const MOCK_CUSTOM_EVENT: (subsId: string, contextId?: string, channelName?: string) => Types.Event;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MOCK_THREAD_MESSAGE_DELETED_EVENT = exports.MOCK_THREAD_CREATED_EVENT = exports.MOCK_STORE_FILE_DELETED_EVENT = exports.MOCK_STORE_CREATED_EVENT = exports.MOCK_INBOX_ENTRY_DELETED_EVENT = exports.MOCK_INBOX_CREATED_EVENT = void 0;
|
|
3
|
+
exports.MOCK_CUSTOM_EVENT = exports.MOCK_LIB_CONNECTED_EVENT = exports.MOCK_CONNECTION_USER_STATUS_EVENT = exports.MOCK_CONNECTION_USER_ADDED_EVENT = exports.MOCK_THREAD_MESSAGE_DELETED_EVENT = exports.MOCK_THREAD_CREATED_EVENT = exports.MOCK_STORE_FILE_DELETED_EVENT = exports.MOCK_STORE_CREATED_EVENT = exports.MOCK_INBOX_ENTRY_DELETED_EVENT = exports.MOCK_INBOX_CREATED_EVENT = void 0;
|
|
4
4
|
exports.createBaseEvent = createBaseEvent;
|
|
5
5
|
function createBaseEvent(subsId) {
|
|
6
6
|
return {
|
|
@@ -49,3 +49,54 @@ const MOCK_THREAD_MESSAGE_DELETED_EVENT = (threadID, subsId) => ({
|
|
|
49
49
|
channel: `thread/${threadID}/messages`,
|
|
50
50
|
});
|
|
51
51
|
exports.MOCK_THREAD_MESSAGE_DELETED_EVENT = MOCK_THREAD_MESSAGE_DELETED_EVENT;
|
|
52
|
+
const MOCK_CONNECTION_USER_ADDED_EVENT = (subsId) => ({
|
|
53
|
+
...createBaseEvent(subsId),
|
|
54
|
+
type: "contextUserAdded",
|
|
55
|
+
channel: "context/userAdded",
|
|
56
|
+
data: {
|
|
57
|
+
contextId: "ctx-1",
|
|
58
|
+
user: {
|
|
59
|
+
userId: "user-1",
|
|
60
|
+
pubKey: "pub-1",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
exports.MOCK_CONNECTION_USER_ADDED_EVENT = MOCK_CONNECTION_USER_ADDED_EVENT;
|
|
65
|
+
const MOCK_CONNECTION_USER_STATUS_EVENT = (subsId) => ({
|
|
66
|
+
...createBaseEvent(subsId),
|
|
67
|
+
type: "contextUserStatusChanged",
|
|
68
|
+
channel: "context/userStatus",
|
|
69
|
+
data: {
|
|
70
|
+
contextId: "ctx-1",
|
|
71
|
+
users: [
|
|
72
|
+
{
|
|
73
|
+
user: {
|
|
74
|
+
userId: "user-1",
|
|
75
|
+
pubKey: "pub-1",
|
|
76
|
+
},
|
|
77
|
+
action: "login",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
exports.MOCK_CONNECTION_USER_STATUS_EVENT = MOCK_CONNECTION_USER_STATUS_EVENT;
|
|
83
|
+
const MOCK_LIB_CONNECTED_EVENT = (connectionId) => ({
|
|
84
|
+
...createBaseEvent("ignored"),
|
|
85
|
+
type: "libConnected",
|
|
86
|
+
channel: "channel/lib_connected",
|
|
87
|
+
connectionId,
|
|
88
|
+
});
|
|
89
|
+
exports.MOCK_LIB_CONNECTED_EVENT = MOCK_LIB_CONNECTED_EVENT;
|
|
90
|
+
const MOCK_CUSTOM_EVENT = (subsId, contextId = "ctx-1", channelName = "custom-channel") => ({
|
|
91
|
+
...createBaseEvent(subsId),
|
|
92
|
+
type: "contextCustom",
|
|
93
|
+
channel: `context/${contextId}/${channelName}`,
|
|
94
|
+
data: {
|
|
95
|
+
contextId,
|
|
96
|
+
userId: "user-1",
|
|
97
|
+
payload: new Uint8Array([1, 2, 3]),
|
|
98
|
+
statusCode: 0,
|
|
99
|
+
schemaVersion: 5,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
exports.MOCK_CUSTOM_EVENT = MOCK_CUSTOM_EVENT;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../__mocks__/constants");
|
|
4
|
+
const utils_1 = require("../__mocks__/utils");
|
|
5
|
+
const subscriptions_1 = require("../subscriptions");
|
|
6
|
+
describe("Connection event manager", () => {
|
|
7
|
+
let { q, manager } = (0, utils_1.createTestSetup)();
|
|
8
|
+
let connectionEventsManager;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
let { q: _q, manager: _manager } = (0, utils_1.createTestSetup)();
|
|
11
|
+
q = _q;
|
|
12
|
+
manager = _manager;
|
|
13
|
+
connectionEventsManager = manager.getConnectionEventManager("1");
|
|
14
|
+
});
|
|
15
|
+
it("supports legacy lib connection events", async () => {
|
|
16
|
+
const callback = jest.fn();
|
|
17
|
+
const libSubscription = (0, subscriptions_1.createConnectionSubscription)({
|
|
18
|
+
type: subscriptions_1.ConnectionStatusEventType.LIB_CONNECTED,
|
|
19
|
+
callbacks: [callback],
|
|
20
|
+
});
|
|
21
|
+
const [libSubscriptionId] = await connectionEventsManager.subscribeFor([
|
|
22
|
+
libSubscription,
|
|
23
|
+
]);
|
|
24
|
+
expect(libSubscriptionId).toBe("1/channel/lib_connected");
|
|
25
|
+
q.dispatchEvent((0, constants_1.MOCK_LIB_CONNECTED_EVENT)(1));
|
|
26
|
+
await (0, utils_1.waitForNextTick)();
|
|
27
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_1 = require("../../index");
|
|
4
|
+
const constants_1 = require("../__mocks__/constants");
|
|
5
|
+
const mockContainerSubscriber_1 = require("../__mocks__/mockContainerSubscriber");
|
|
6
|
+
const utils_1 = require("../__mocks__/utils");
|
|
7
|
+
const subscriptions_1 = require("../subscriptions");
|
|
8
|
+
describe("Custom event manager", () => {
|
|
9
|
+
let { q, manager } = (0, utils_1.createTestSetup)();
|
|
10
|
+
let customEventsManager = manager.getCustomEventsManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
let { q: _q, manager: _manager } = (0, utils_1.createTestSetup)();
|
|
13
|
+
q = _q;
|
|
14
|
+
manager = _manager;
|
|
15
|
+
customEventsManager = _manager.getCustomEventsManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
|
|
16
|
+
});
|
|
17
|
+
it("dispatches custom events to registered callbacks", async () => {
|
|
18
|
+
const subscriber = new mockContainerSubscriber_1.MockContainerSubscriber(q);
|
|
19
|
+
const buildQuerySpy = jest
|
|
20
|
+
.spyOn(subscriber, "buildSubscriptionQuery")
|
|
21
|
+
.mockImplementation(async (channel, selector, selectorId) => {
|
|
22
|
+
expect(channel).toBe("custom-channel");
|
|
23
|
+
expect(selector).toBe(index_1.Types.EventsEventSelectorType.CONTEXT_ID);
|
|
24
|
+
expect(selectorId).toBe("ctx-1");
|
|
25
|
+
return `${channel}|${selectorId}`;
|
|
26
|
+
});
|
|
27
|
+
const managerWithSpy = manager.getCustomEventsManager(subscriber);
|
|
28
|
+
const callback = jest.fn();
|
|
29
|
+
const subscription = (0, subscriptions_1.createEventSubscription)({
|
|
30
|
+
channel: "custom-channel",
|
|
31
|
+
selector: index_1.Types.EventsEventSelectorType.CONTEXT_ID,
|
|
32
|
+
id: "ctx-1",
|
|
33
|
+
callbacks: [callback],
|
|
34
|
+
});
|
|
35
|
+
const [subscriptionId] = await managerWithSpy.subscribeFor([subscription]);
|
|
36
|
+
expect(buildQuerySpy).toHaveBeenCalled();
|
|
37
|
+
q.dispatchEvent((0, constants_1.MOCK_CUSTOM_EVENT)(subscriptionId));
|
|
38
|
+
await (0, utils_1.waitForNextTick)();
|
|
39
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
40
|
+
const eventArg = callback.mock.calls[0][0];
|
|
41
|
+
expect(eventArg.data).toEqual({
|
|
42
|
+
contextId: "ctx-1",
|
|
43
|
+
userId: "user-1",
|
|
44
|
+
payload: new Uint8Array([1, 2, 3]),
|
|
45
|
+
statusCode: 0,
|
|
46
|
+
schemaVersion: 5,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it("stops invoking callbacks after unsubscribe", async () => {
|
|
50
|
+
const callback = jest.fn();
|
|
51
|
+
const subscription = (0, subscriptions_1.createEventSubscription)({
|
|
52
|
+
channel: "custom-channel",
|
|
53
|
+
selector: index_1.Types.EventsEventSelectorType.CONTEXT_ID,
|
|
54
|
+
id: "ctx-1",
|
|
55
|
+
callbacks: [callback],
|
|
56
|
+
});
|
|
57
|
+
const [subscriptionId] = await customEventsManager.subscribeFor([
|
|
58
|
+
subscription,
|
|
59
|
+
]);
|
|
60
|
+
await customEventsManager.unsubscribeFrom([subscriptionId]);
|
|
61
|
+
q.dispatchEvent((0, constants_1.MOCK_CUSTOM_EVENT)(subscriptionId));
|
|
62
|
+
await (0, utils_1.waitForNextTick)();
|
|
63
|
+
expect(callback).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_1 = require("../../index");
|
|
4
|
+
const constants_1 = require("../__mocks__/constants");
|
|
5
|
+
const mockContainerSubscriber_1 = require("../__mocks__/mockContainerSubscriber");
|
|
6
|
+
const utils_1 = require("../__mocks__/utils");
|
|
7
|
+
const subscriptions_1 = require("../subscriptions");
|
|
8
|
+
describe("User event manager", () => {
|
|
9
|
+
let { q, manager } = (0, utils_1.createTestSetup)();
|
|
10
|
+
let userEventsManager;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
let { q: _q, manager: _manager } = (0, utils_1.createTestSetup)();
|
|
13
|
+
q = _q;
|
|
14
|
+
manager = _manager;
|
|
15
|
+
userEventsManager = manager.getUserEventsManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
|
|
16
|
+
});
|
|
17
|
+
it("dispatches user events to registered callbacks", async () => {
|
|
18
|
+
const callback = jest.fn();
|
|
19
|
+
const subscription = (0, subscriptions_1.createUserEventSubscription)({
|
|
20
|
+
type: index_1.Types.ConnectionEventType.USER_ADD,
|
|
21
|
+
selector: index_1.Types.ConnectionEventSelectorType.CONTEXT_ID,
|
|
22
|
+
id: "ctx-1",
|
|
23
|
+
callbacks: [callback],
|
|
24
|
+
});
|
|
25
|
+
const [subscriptionId] = await userEventsManager.subscribeFor([
|
|
26
|
+
subscription,
|
|
27
|
+
]);
|
|
28
|
+
q.dispatchEvent((0, constants_1.MOCK_CONNECTION_USER_ADDED_EVENT)(subscriptionId));
|
|
29
|
+
await (0, utils_1.waitForNextTick)();
|
|
30
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
31
|
+
expect(callback.mock.calls[0][0].data).toEqual({
|
|
32
|
+
contextId: "ctx-1",
|
|
33
|
+
user: { userId: "user-1", pubKey: "pub-1" },
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it("stops invoking callbacks after unsubscribe", async () => {
|
|
37
|
+
const callback = jest.fn();
|
|
38
|
+
const subscription = (0, subscriptions_1.createUserEventSubscription)({
|
|
39
|
+
type: index_1.Types.ConnectionEventType.USER_STATUS,
|
|
40
|
+
selector: index_1.Types.ConnectionEventSelectorType.CONTEXT_ID,
|
|
41
|
+
id: "ctx-1",
|
|
42
|
+
callbacks: [callback],
|
|
43
|
+
});
|
|
44
|
+
const [subscriptionId] = await userEventsManager.subscribeFor([
|
|
45
|
+
subscription,
|
|
46
|
+
]);
|
|
47
|
+
await userEventsManager.unsubscribeFrom([subscriptionId]);
|
|
48
|
+
q.dispatchEvent((0, constants_1.MOCK_CONNECTION_USER_STATUS_EVENT)(subscriptionId));
|
|
49
|
+
await (0, utils_1.waitForNextTick)();
|
|
50
|
+
expect(callback).not.toHaveBeenCalled();
|
|
51
|
+
});
|
|
52
|
+
});
|
package/extra/events.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Types } from "..";
|
|
2
|
-
import { SubscriberForInboxEvents, SubscriberForKvdbEvents, SubscriberForStoreEvents, SubscriberForThreadsEvents } from "./subscriptions";
|
|
3
|
-
import { BaseEventDispatcherManager, ConnectionEventsManager, InboxEventsManager, KvdbEventsManager, StoreEventsManager, ThreadEventsManager } from "./managers";
|
|
2
|
+
import { SubscriberForInboxEvents, SubscriberForUserEvents, SubscriberForEvents, SubscriberForKvdbEvents, SubscriberForStoreEvents, SubscriberForThreadsEvents } from "./subscriptions";
|
|
3
|
+
import { BaseEventDispatcherManager, ConnectionEventsManager, CustomEventsManager, InboxEventsManager, KvdbEventsManager, StoreEventsManager, ThreadEventsManager, UserEventsManager } from "./managers";
|
|
4
4
|
export declare class EventManager {
|
|
5
5
|
private _isEventLoopRunning;
|
|
6
6
|
dispatchers: ((event: Types.Event) => void)[];
|
|
@@ -18,5 +18,7 @@ export declare class EventManager {
|
|
|
18
18
|
getStoreEventManager(storeApi: SubscriberForStoreEvents): StoreEventsManager;
|
|
19
19
|
getKvdbEventManager(kvdbApi: SubscriberForKvdbEvents): KvdbEventsManager;
|
|
20
20
|
getInboxEventManager(inboxApi: SubscriberForInboxEvents): InboxEventsManager;
|
|
21
|
+
getCustomEventsManager(eventApi: SubscriberForEvents): CustomEventsManager;
|
|
21
22
|
getConnectionEventManager(connectionId: string): ConnectionEventsManager;
|
|
23
|
+
getUserEventsManager(connectionApi: SubscriberForUserEvents): UserEventsManager;
|
|
22
24
|
}
|
package/extra/events.js
CHANGED
|
@@ -6,11 +6,11 @@ const managers_1 = require("./managers");
|
|
|
6
6
|
function normalizeConnectionEvent(e) {
|
|
7
7
|
switch (e.type) {
|
|
8
8
|
case 'libDisconnected':
|
|
9
|
-
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.
|
|
9
|
+
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.ConnectionStatusEventType.LIB_DISCONNECTED]}`] };
|
|
10
10
|
case 'libPlatformDisconnected':
|
|
11
|
-
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.
|
|
11
|
+
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.ConnectionStatusEventType.LIB_PLATFORM_DISCONNECTED]}`] };
|
|
12
12
|
case 'libConnected':
|
|
13
|
-
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.
|
|
13
|
+
return { ...e, subscriptions: [`${e.connectionId}/${managers_1.ConnectionChannels[subscriptions_1.ConnectionStatusEventType.LIB_CONNECTED]}`] };
|
|
14
14
|
default:
|
|
15
15
|
return e;
|
|
16
16
|
}
|
|
@@ -73,10 +73,20 @@ class EventManager {
|
|
|
73
73
|
this.registerDispatcher(manager);
|
|
74
74
|
return manager;
|
|
75
75
|
}
|
|
76
|
+
getCustomEventsManager(eventApi) {
|
|
77
|
+
const manager = new managers_1.CustomEventsManager(eventApi);
|
|
78
|
+
this.registerDispatcher(manager);
|
|
79
|
+
return manager;
|
|
80
|
+
}
|
|
76
81
|
getConnectionEventManager(connectionId) {
|
|
77
82
|
const manager = new managers_1.ConnectionEventsManager(connectionId);
|
|
78
83
|
this.registerDispatcher(manager);
|
|
79
84
|
return manager;
|
|
80
85
|
}
|
|
86
|
+
getUserEventsManager(connectionApi) {
|
|
87
|
+
const manager = new managers_1.UserEventsManager(connectionApi);
|
|
88
|
+
this.registerDispatcher(manager);
|
|
89
|
+
return manager;
|
|
90
|
+
}
|
|
81
91
|
}
|
|
82
92
|
exports.EventManager = EventManager;
|
package/extra/index.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ import { FileUploader, StreamReader, downloadFile } from './files';
|
|
|
6
6
|
import { PrivmxClient } from './PrivmxClient';
|
|
7
7
|
import { PublicConnection } from './PublicConnection';
|
|
8
8
|
export { EventManager } from "./events";
|
|
9
|
-
export { createInboxSubscription, createThreadSubscription, createConnectionSubscription, createKvdbSubscription, createStoreSubscription, EventCallback, Subscription } from "./subscriptions";
|
|
9
|
+
export { createInboxSubscription, createThreadSubscription, createConnectionSubscription, createUserEventSubscription, createKvdbSubscription, createStoreSubscription, createEventSubscription, EventCallback, Subscription, ConnectionStatusEventType, ConnectionSubscription, } from "./subscriptions";
|
|
10
10
|
export { Files, Inboxes, Utils, Generics, FileUploader, downloadFile, StreamReader, PrivmxClient, PublicConnection, };
|
package/extra/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PublicConnection = exports.PrivmxClient = exports.StreamReader = exports.downloadFile = exports.FileUploader = exports.Generics = exports.Utils = exports.Inboxes = exports.Files = exports.createStoreSubscription = exports.createKvdbSubscription = exports.createConnectionSubscription = exports.createThreadSubscription = exports.createInboxSubscription = exports.EventManager = void 0;
|
|
3
|
+
exports.PublicConnection = exports.PrivmxClient = exports.StreamReader = exports.downloadFile = exports.FileUploader = exports.Generics = exports.Utils = exports.Inboxes = exports.Files = exports.ConnectionStatusEventType = exports.createEventSubscription = exports.createStoreSubscription = exports.createKvdbSubscription = exports.createUserEventSubscription = exports.createConnectionSubscription = exports.createThreadSubscription = exports.createInboxSubscription = exports.EventManager = void 0;
|
|
4
4
|
const Files = require("./files");
|
|
5
5
|
exports.Files = Files;
|
|
6
6
|
const Utils = require("./utils");
|
|
@@ -23,5 +23,8 @@ var subscriptions_1 = require("./subscriptions");
|
|
|
23
23
|
Object.defineProperty(exports, "createInboxSubscription", { enumerable: true, get: function () { return subscriptions_1.createInboxSubscription; } });
|
|
24
24
|
Object.defineProperty(exports, "createThreadSubscription", { enumerable: true, get: function () { return subscriptions_1.createThreadSubscription; } });
|
|
25
25
|
Object.defineProperty(exports, "createConnectionSubscription", { enumerable: true, get: function () { return subscriptions_1.createConnectionSubscription; } });
|
|
26
|
+
Object.defineProperty(exports, "createUserEventSubscription", { enumerable: true, get: function () { return subscriptions_1.createUserEventSubscription; } });
|
|
26
27
|
Object.defineProperty(exports, "createKvdbSubscription", { enumerable: true, get: function () { return subscriptions_1.createKvdbSubscription; } });
|
|
27
28
|
Object.defineProperty(exports, "createStoreSubscription", { enumerable: true, get: function () { return subscriptions_1.createStoreSubscription; } });
|
|
29
|
+
Object.defineProperty(exports, "createEventSubscription", { enumerable: true, get: function () { return subscriptions_1.createEventSubscription; } });
|
|
30
|
+
Object.defineProperty(exports, "ConnectionStatusEventType", { enumerable: true, get: function () { return subscriptions_1.ConnectionStatusEventType; } });
|
package/extra/managers.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Types } from "..";
|
|
2
|
-
import {
|
|
2
|
+
import { ConnectionStatusEventType, ConnectionSubscription, EventCallback, SubscriberForEvents, SubscriberForInboxEvents, SubscriberForKvdbEvents, SubscriberForStoreEvents, SubscriberForThreadsEvents, SubscriberForUserEvents, Subscription } from "./subscriptions";
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
5
5
|
* General usage
|
|
@@ -33,7 +33,7 @@ import { ConnectionEventType, EventCallback, SubscriberForInboxEvents, Subscribe
|
|
|
33
33
|
*
|
|
34
34
|
* await manager.removeCallback(subscriptionA.callbacks[0])
|
|
35
35
|
*/
|
|
36
|
-
export type Channel = "inbox" | `inbox/${string}/entries` | "store" | `store/${string}/files` | "thread" | `thread/${string}/messages` | `connection/${string}`;
|
|
36
|
+
export type Channel = "inbox" | `inbox/${string}/entries` | "store" | `store/${string}/files` | "thread" | `thread/${string}/messages` | `connection/${string}` | "context/userAdded" | "context/userRemoved" | "context/userStatus" | `context/${string}/${string}`;
|
|
37
37
|
export interface GenericEvent<K> extends Types.Event {
|
|
38
38
|
/**
|
|
39
39
|
* Data associated with the event.
|
|
@@ -81,18 +81,25 @@ export declare class KvdbEventsManager extends BaseEventDispatcherManager {
|
|
|
81
81
|
protected apiUnsubscribeFrom(subscriptionId: string[]): Promise<void>;
|
|
82
82
|
subscribeFor(subscriptions: Subscription<Types.KvdbEventType, Types.KvdbEventSelectorType>[]): Promise<string[]>;
|
|
83
83
|
}
|
|
84
|
-
export declare
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
export declare class CustomEventsManager extends BaseEventDispatcherManager {
|
|
85
|
+
private eventsApi;
|
|
86
|
+
constructor(eventsApi: SubscriberForEvents);
|
|
87
|
+
protected apiSubscribeFor(channels: string[]): Promise<string[]>;
|
|
88
|
+
protected apiUnsubscribeFrom(subscriptionId: string[]): Promise<void>;
|
|
89
|
+
subscribeFor(subscriptions: Subscription<string, Types.EventsEventSelectorType>[]): Promise<string[]>;
|
|
90
|
+
}
|
|
91
|
+
export declare const ConnectionChannels: Record<ConnectionStatusEventType, string>;
|
|
89
92
|
export declare class ConnectionEventsManager extends BaseEventDispatcherManager {
|
|
90
93
|
private connectionId;
|
|
91
94
|
constructor(connectionId: string);
|
|
92
95
|
protected apiSubscribeFor(channels: string[]): Promise<string[]>;
|
|
93
96
|
protected apiUnsubscribeFrom(): Promise<void>;
|
|
94
|
-
subscribeFor(subscriptions:
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
97
|
+
subscribeFor(subscriptions: ConnectionSubscription[]): Promise<string[]>;
|
|
98
|
+
}
|
|
99
|
+
export declare class UserEventsManager extends BaseEventDispatcherManager {
|
|
100
|
+
private userEventsApi;
|
|
101
|
+
constructor(userEventsApi: SubscriberForUserEvents);
|
|
102
|
+
protected apiSubscribeFor(channels: string[]): Promise<string[]>;
|
|
103
|
+
protected apiUnsubscribeFrom(subscriptionId: string[]): Promise<void>;
|
|
104
|
+
subscribeFor(subscriptions: Subscription<Types.ConnectionEventType, Types.ConnectionEventSelectorType>[]): Promise<string[]>;
|
|
98
105
|
}
|
package/extra/managers.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConnectionEventsManager = exports.ConnectionChannels = exports.KvdbEventsManager = exports.InboxEventsManager = exports.StoreEventsManager = exports.ThreadEventsManager = exports.BaseEventDispatcherManager = void 0;
|
|
3
|
+
exports.UserEventsManager = exports.ConnectionEventsManager = exports.ConnectionChannels = exports.CustomEventsManager = exports.KvdbEventsManager = exports.InboxEventsManager = exports.StoreEventsManager = exports.ThreadEventsManager = exports.BaseEventDispatcherManager = void 0;
|
|
4
4
|
const subscriptions_1 = require("./subscriptions");
|
|
5
5
|
class BaseEventDispatcherManager {
|
|
6
6
|
_listenersSymbols = new Map();
|
|
@@ -9,7 +9,10 @@ class BaseEventDispatcherManager {
|
|
|
9
9
|
return this._listeners;
|
|
10
10
|
}
|
|
11
11
|
dispatchEvent(event) {
|
|
12
|
-
const callbacks = event.subscriptions.flatMap((s) =>
|
|
12
|
+
const callbacks = event.subscriptions.flatMap((s) => {
|
|
13
|
+
const listeners = this._listeners.get(s);
|
|
14
|
+
return listeners ?? [];
|
|
15
|
+
});
|
|
13
16
|
for (const listener of callbacks) {
|
|
14
17
|
listener.callback(event);
|
|
15
18
|
}
|
|
@@ -39,14 +42,19 @@ class BaseEventDispatcherManager {
|
|
|
39
42
|
return subscriptionIds;
|
|
40
43
|
}
|
|
41
44
|
async unsubscribeFrom(subscriptionsId) {
|
|
45
|
+
const knownIds = [];
|
|
42
46
|
for (const subscriptionId of subscriptionsId) {
|
|
43
47
|
for (const [key, callbackSubscription,] of this._listenersSymbols.entries()) {
|
|
44
48
|
if (callbackSubscription === subscriptionId) {
|
|
49
|
+
knownIds.push(subscriptionId);
|
|
45
50
|
this.unregisterCallback(key);
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
|
-
|
|
54
|
+
if (knownIds.length === 0) {
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
}
|
|
57
|
+
return this.apiUnsubscribeFrom(knownIds);
|
|
50
58
|
}
|
|
51
59
|
}
|
|
52
60
|
exports.BaseEventDispatcherManager = BaseEventDispatcherManager;
|
|
@@ -130,10 +138,30 @@ class KvdbEventsManager extends BaseEventDispatcherManager {
|
|
|
130
138
|
}
|
|
131
139
|
}
|
|
132
140
|
exports.KvdbEventsManager = KvdbEventsManager;
|
|
141
|
+
class CustomEventsManager extends BaseEventDispatcherManager {
|
|
142
|
+
eventsApi;
|
|
143
|
+
constructor(eventsApi) {
|
|
144
|
+
super();
|
|
145
|
+
this.eventsApi = eventsApi;
|
|
146
|
+
}
|
|
147
|
+
apiSubscribeFor(channels) {
|
|
148
|
+
return this.eventsApi.subscribeFor(channels);
|
|
149
|
+
}
|
|
150
|
+
apiUnsubscribeFrom(subscriptionId) {
|
|
151
|
+
return this.eventsApi.unsubscribeFrom(subscriptionId);
|
|
152
|
+
}
|
|
153
|
+
async subscribeFor(subscriptions) {
|
|
154
|
+
const subscriptionChannels = await Promise.all(subscriptions.map((s) => {
|
|
155
|
+
return this.eventsApi.buildSubscriptionQuery(s.type, s.selector, s.id);
|
|
156
|
+
}));
|
|
157
|
+
return this.prepareSubscription(subscriptionChannels, subscriptions);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.CustomEventsManager = CustomEventsManager;
|
|
133
161
|
exports.ConnectionChannels = {
|
|
134
|
-
[subscriptions_1.
|
|
135
|
-
[subscriptions_1.
|
|
136
|
-
[subscriptions_1.
|
|
162
|
+
[subscriptions_1.ConnectionStatusEventType.LIB_CONNECTED]: "channel/lib_connected",
|
|
163
|
+
[subscriptions_1.ConnectionStatusEventType.LIB_DISCONNECTED]: "channel/lib_disconnected",
|
|
164
|
+
[subscriptions_1.ConnectionStatusEventType.LIB_PLATFORM_DISCONNECTED]: "channel/lib_platform_disconnected",
|
|
137
165
|
};
|
|
138
166
|
class ConnectionEventsManager extends BaseEventDispatcherManager {
|
|
139
167
|
connectionId;
|
|
@@ -149,9 +177,29 @@ class ConnectionEventsManager extends BaseEventDispatcherManager {
|
|
|
149
177
|
}
|
|
150
178
|
async subscribeFor(subscriptions) {
|
|
151
179
|
const subscriptionChannels = subscriptions.map((x) => {
|
|
152
|
-
return `${this.connectionId}/${
|
|
180
|
+
return `${this.connectionId}/${exports.ConnectionChannels[x.type]}`;
|
|
153
181
|
});
|
|
154
182
|
return this.prepareSubscription(subscriptionChannels, subscriptions);
|
|
155
183
|
}
|
|
156
184
|
}
|
|
157
185
|
exports.ConnectionEventsManager = ConnectionEventsManager;
|
|
186
|
+
class UserEventsManager extends BaseEventDispatcherManager {
|
|
187
|
+
userEventsApi;
|
|
188
|
+
constructor(userEventsApi) {
|
|
189
|
+
super();
|
|
190
|
+
this.userEventsApi = userEventsApi;
|
|
191
|
+
}
|
|
192
|
+
apiSubscribeFor(channels) {
|
|
193
|
+
return this.userEventsApi.subscribeFor(channels);
|
|
194
|
+
}
|
|
195
|
+
apiUnsubscribeFrom(subscriptionId) {
|
|
196
|
+
return this.userEventsApi.unsubscribeFrom(subscriptionId);
|
|
197
|
+
}
|
|
198
|
+
async subscribeFor(subscriptions) {
|
|
199
|
+
const subscriptionChannels = await Promise.all(subscriptions.map((s) => {
|
|
200
|
+
return this.userEventsApi.buildSubscriptionQuery(s.type, s.selector, s.id);
|
|
201
|
+
}));
|
|
202
|
+
return this.prepareSubscription(subscriptionChannels, subscriptions);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.UserEventsManager = UserEventsManager;
|