@simplito/privmx-webendpoint 2.6.2 → 2.6.4

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/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) => this._listeners.get(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
- return this.apiUnsubscribeFrom(subscriptionsId);
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.ConnectionEventType.LIB_CONNECTED]: "channel/lib_connected",
135
- [subscriptions_1.ConnectionEventType.LIB_DISCONNECTED]: "channel/lib_disconnected",
136
- [subscriptions_1.ConnectionEventType.LIB_PLATFORM_DISCONNECTED]: "channel/lib_platform_disconnected",
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}/${subscriptions_1.ConnectionEventType[x.type]}`;
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;
@@ -1,5 +1,4 @@
1
1
  import { Types } from "..";
2
- import { ThreadEventSelectorType, ThreadEventType } from "../Types";
3
2
  import { GenericEvent } from "./managers";
4
3
  export type ThreadCallbackPayload = {
5
4
  [Types.ThreadEventType.THREAD_CREATE]: Types.Thread;
@@ -75,8 +74,14 @@ export type KvdbCallbackPayload = {
75
74
  };
76
75
  [Types.KvdbEventType.COLLECTION_CHANGE]: Types.CollectionChangedEventData;
77
76
  };
77
+ export type UserEventCallbackPayload = {
78
+ [Types.ConnectionEventType.USER_ADD]: Types.ContextUserEventData;
79
+ [Types.ConnectionEventType.USER_REMOVE]: Types.ContextUserEventData;
80
+ [Types.ConnectionEventType.USER_STATUS]: Types.ContextUsersStatusChangedEventData;
81
+ };
82
+ export type EventsCallbackPayload = Types.ContextCustomEventData;
78
83
  export type EventCallback = {
79
- callback: (e: Types.Event) => void;
84
+ callback: (e: Types.Event | GenericEvent<unknown>) => void;
80
85
  symbol: Symbol;
81
86
  };
82
87
  export interface Subscription<T, S> {
@@ -129,37 +134,54 @@ export declare function createInboxSubscription<T extends Types.InboxEventType,
129
134
  selector: S;
130
135
  id: string;
131
136
  };
132
- export declare enum ConnectionEventType {
137
+ export declare function createEventSubscription(s: {
138
+ channel: string;
139
+ selector: Types.EventsEventSelectorType;
140
+ id: string;
141
+ callbacks: ((arg: GenericEvent<EventsCallbackPayload>) => void)[];
142
+ }): Subscription<string, Types.EventsEventSelectorType> & {
143
+ channel: string;
144
+ };
145
+ export declare enum ConnectionStatusEventType {
133
146
  LIB_DISCONNECTED = 0,
134
147
  LIB_PLATFORM_DISCONNECTED = 1,
135
148
  LIB_CONNECTED = 2
136
149
  }
137
- export declare function createConnectionSubscription(s: {
138
- type: ConnectionEventType;
139
- callbacks: ((arg: GenericEvent<undefined>) => void)[];
140
- }): {
150
+ export type ConnectionSubscription = {
151
+ type: ConnectionStatusEventType;
141
152
  callbacks: EventCallback[];
142
- type: ConnectionEventType;
143
153
  };
154
+ export declare function createConnectionSubscription(s: {
155
+ type: ConnectionStatusEventType;
156
+ callbacks: ((arg: GenericEvent<undefined>) => void)[];
157
+ }): ConnectionSubscription;
158
+ export declare function createUserEventSubscription<T extends Types.ConnectionEventType, S extends Types.ConnectionEventSelectorType>(s: {
159
+ type: T;
160
+ selector: S;
161
+ id: string;
162
+ callbacks: ((arg: GenericEvent<UserEventCallbackPayload[T]>) => void)[];
163
+ }): Subscription<T, S>;
144
164
  export interface EventSubscriber<E, S> {
145
165
  /**
146
- * Subscribe for the Thread events on the given subscription query.
166
+ * Subscribe for events on the given subscription queries.
147
167
  * @param {string[]} subscriptionQueries list of queries
148
168
  */
149
169
  subscribeFor(subscriptionQueries: string[]): Promise<string[]>;
150
170
  /**
151
- * Unsubscribes from events for the given subscriptionId.
171
+ * Unsubscribe from events for the given subscriptionIds.
152
172
  */
153
173
  unsubscribeFrom(subscriptionIds: string[]): Promise<void>;
154
174
  /**
155
- * Generate subscription Query for the Thread events.
156
- * @param {EventType} eventType type of event which you listen for
157
- * @param {EventSelectorType} selectorType scope on which you listen for events
175
+ * Generate subscription query string for the requested event scope.
176
+ * @param {E} eventType type of event which you listen for
177
+ * @param {S} selectorType scope on which you listen for events
158
178
  * @param {string} selectorId ID of the selector
159
179
  */
160
180
  buildSubscriptionQuery(eventType: E, selectorType: S, selectorId: string): Promise<string>;
161
181
  }
162
- export type SubscriberForThreadsEvents = EventSubscriber<ThreadEventType, ThreadEventSelectorType>;
182
+ export type SubscriberForThreadsEvents = EventSubscriber<Types.ThreadEventType, Types.ThreadEventSelectorType>;
163
183
  export type SubscriberForStoreEvents = EventSubscriber<Types.StoreEventType, Types.StoreEventSelectorType>;
164
184
  export type SubscriberForInboxEvents = EventSubscriber<Types.InboxEventType, Types.InboxEventSelectorType>;
165
185
  export type SubscriberForKvdbEvents = EventSubscriber<Types.KvdbEventType, Types.KvdbEventSelectorType>;
186
+ export type SubscriberForUserEvents = EventSubscriber<Types.ConnectionEventType, Types.ConnectionEventSelectorType>;
187
+ export type SubscriberForEvents = EventSubscriber<string, Types.EventsEventSelectorType>;
@@ -1,51 +1,68 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectionEventType = void 0;
3
+ exports.ConnectionStatusEventType = void 0;
4
4
  exports.createThreadSubscription = createThreadSubscription;
5
5
  exports.createStoreSubscription = createStoreSubscription;
6
6
  exports.createKvdbSubscription = createKvdbSubscription;
7
7
  exports.createInboxSubscription = createInboxSubscription;
8
+ exports.createEventSubscription = createEventSubscription;
8
9
  exports.createConnectionSubscription = createConnectionSubscription;
10
+ exports.createUserEventSubscription = createUserEventSubscription;
9
11
  const __1 = require("..");
10
12
  function toEventCallback(f) {
11
13
  return {
12
14
  callback: f,
13
- symbol: Symbol()
15
+ symbol: Symbol(),
14
16
  };
15
17
  }
16
18
  function createThreadSubscription(s) {
17
19
  return {
18
20
  ...s,
19
- callbacks: s.callbacks.map(toEventCallback)
21
+ callbacks: s.callbacks.map(toEventCallback),
20
22
  };
21
23
  }
22
24
  function createStoreSubscription(s) {
23
25
  return {
24
26
  ...s,
25
- callbacks: s.callbacks.map(toEventCallback)
27
+ callbacks: s.callbacks.map(toEventCallback),
26
28
  };
27
29
  }
28
30
  function createKvdbSubscription(s) {
29
31
  return {
30
32
  ...s,
31
- callbacks: s.callbacks.map(toEventCallback)
33
+ callbacks: s.callbacks.map(toEventCallback),
32
34
  };
33
35
  }
34
36
  function createInboxSubscription(s) {
35
37
  return {
36
38
  ...s,
37
- callbacks: s.callbacks.map(toEventCallback)
39
+ callbacks: s.callbacks.map(toEventCallback),
38
40
  };
39
41
  }
40
- var ConnectionEventType;
41
- (function (ConnectionEventType) {
42
- ConnectionEventType[ConnectionEventType["LIB_DISCONNECTED"] = 0] = "LIB_DISCONNECTED";
43
- ConnectionEventType[ConnectionEventType["LIB_PLATFORM_DISCONNECTED"] = 1] = "LIB_PLATFORM_DISCONNECTED";
44
- ConnectionEventType[ConnectionEventType["LIB_CONNECTED"] = 2] = "LIB_CONNECTED";
45
- })(ConnectionEventType || (exports.ConnectionEventType = ConnectionEventType = {}));
42
+ function createEventSubscription(s) {
43
+ return {
44
+ type: s.channel,
45
+ selector: s.selector,
46
+ id: s.id,
47
+ callbacks: s.callbacks.map(toEventCallback),
48
+ channel: s.channel,
49
+ };
50
+ }
51
+ var ConnectionStatusEventType;
52
+ (function (ConnectionStatusEventType) {
53
+ ConnectionStatusEventType[ConnectionStatusEventType["LIB_DISCONNECTED"] = 0] = "LIB_DISCONNECTED";
54
+ ConnectionStatusEventType[ConnectionStatusEventType["LIB_PLATFORM_DISCONNECTED"] = 1] = "LIB_PLATFORM_DISCONNECTED";
55
+ ConnectionStatusEventType[ConnectionStatusEventType["LIB_CONNECTED"] = 2] = "LIB_CONNECTED";
56
+ })(ConnectionStatusEventType || (exports.ConnectionStatusEventType = ConnectionStatusEventType = {}));
46
57
  function createConnectionSubscription(s) {
47
58
  return {
48
59
  ...s,
49
- callbacks: s.callbacks.map(toEventCallback)
60
+ callbacks: s.callbacks.map(toEventCallback),
61
+ };
62
+ }
63
+ function createUserEventSubscription(s) {
64
+ return {
65
+ ...s,
66
+ callbacks: s.callbacks.map(toEventCallback),
50
67
  };
51
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplito/privmx-webendpoint",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "PrivMX Web Endpoint library",
5
5
  "author": "Simplito",
6
6
  "license": "PrivMX Free License",