@stream-io/video-client 0.0.11 → 0.0.13

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/dist/index.cjs.js CHANGED
@@ -6366,6 +6366,130 @@ var rxUtils = /*#__PURE__*/Object.freeze({
6366
6366
  setCurrentValue: setCurrentValue
6367
6367
  });
6368
6368
 
6369
+ class StreamVideoWriteableStateStore {
6370
+ constructor() {
6371
+ /**
6372
+ * A store keeping data of a successfully connected user over WS to the coordinator server.
6373
+ */
6374
+ this.connectedUserSubject = new rxjs.BehaviorSubject(undefined);
6375
+ /**
6376
+ * A list of {@link Call} objects created/tracked by this client.
6377
+ */
6378
+ this.callsSubject = new rxjs.BehaviorSubject([]);
6379
+ /**
6380
+ * Gets the current value of an observable, or undefined if the observable has
6381
+ * not emitted a value yet.
6382
+ *
6383
+ * @param observable$ the observable to get the value from.
6384
+ */
6385
+ this.getCurrentValue = getCurrentValue;
6386
+ /**
6387
+ * Updates the value of the provided Subject.
6388
+ * An `update` can either be a new value or a function which takes
6389
+ * the current value and returns a new value.
6390
+ *
6391
+ * @param subject the subject to update.
6392
+ * @param update the update to apply to the subject.
6393
+ * @return the updated value.
6394
+ */
6395
+ this.setCurrentValue = setCurrentValue;
6396
+ /**
6397
+ * Sets the currently connected user.
6398
+ *
6399
+ * @internal
6400
+ * @param user the user to set as connected.
6401
+ */
6402
+ this.setConnectedUser = (user) => {
6403
+ return this.setCurrentValue(this.connectedUserSubject, user);
6404
+ };
6405
+ /**
6406
+ * Sets the list of {@link Call} objects created/tracked by this client.
6407
+ * @param calls
6408
+ */
6409
+ this.setCalls = (calls) => {
6410
+ return this.setCurrentValue(this.callsSubject, calls);
6411
+ };
6412
+ /**
6413
+ * Adds a {@link Call} object to the list of {@link Call} objects created/tracked by this client.
6414
+ *
6415
+ * @param call the call to add.
6416
+ */
6417
+ this.registerCall = (call) => {
6418
+ if (!this.calls.find((c) => c.cid === call.cid)) {
6419
+ this.setCalls((calls) => [...calls, call]);
6420
+ }
6421
+ };
6422
+ /**
6423
+ * Removes a {@link Call} object from the list of {@link Call} objects created/tracked by this client.
6424
+ *
6425
+ * @param call the call to remove
6426
+ */
6427
+ this.unregisterCall = (call) => {
6428
+ return this.setCalls((calls) => calls.filter((c) => c !== call));
6429
+ };
6430
+ /**
6431
+ * Finds a {@link Call} object in the list of {@link Call} objects created/tracked by this client.
6432
+ *
6433
+ * @param type the type of call to find.
6434
+ * @param id the id of the call to find.
6435
+ */
6436
+ this.findCall = (type, id) => {
6437
+ return this.calls.find((c) => c.type === type && c.id === id);
6438
+ };
6439
+ this.connectedUserSubject.subscribe((user) => __awaiter(this, void 0, void 0, function* () {
6440
+ // leave all calls when the user disconnects.
6441
+ if (!user) {
6442
+ for (const call of this.calls) {
6443
+ yield call.leave();
6444
+ }
6445
+ }
6446
+ }));
6447
+ }
6448
+ /**
6449
+ * The currently connected user.
6450
+ */
6451
+ get connectedUser() {
6452
+ return this.getCurrentValue(this.connectedUserSubject);
6453
+ }
6454
+ /**
6455
+ * A list of {@link Call} objects created/tracked by this client.
6456
+ */
6457
+ get calls() {
6458
+ return this.getCurrentValue(this.callsSubject);
6459
+ }
6460
+ }
6461
+ /**
6462
+ * A reactive store that exposes state variables in a reactive manner.
6463
+ * You can subscribe to changes of the different state variables.
6464
+ * This central store contains all the state variables related to [`StreamVideoClient`](./StreamVideClient.md) and [`Call`](./Call.md).
6465
+ */
6466
+ class StreamVideoReadOnlyStateStore {
6467
+ constructor(store) {
6468
+ /**
6469
+ * This method allows you the get the current value of a state variable.
6470
+ *
6471
+ * @param observable the observable to get the current value of.
6472
+ * @returns the current value of the observable.
6473
+ */
6474
+ this.getCurrentValue = getCurrentValue;
6475
+ // convert and expose subjects as observables
6476
+ this.connectedUser$ = store.connectedUserSubject.asObservable();
6477
+ this.calls$ = store.callsSubject.asObservable();
6478
+ }
6479
+ /**
6480
+ * The current user connected over WS to the backend.
6481
+ */
6482
+ get connectedUser() {
6483
+ return getCurrentValue(this.connectedUser$);
6484
+ }
6485
+ /**
6486
+ * A list of {@link Call} objects created/tracked by this client.
6487
+ */
6488
+ get calls() {
6489
+ return getCurrentValue(this.calls$);
6490
+ }
6491
+ }
6492
+
6369
6493
  /**
6370
6494
  * Creates a new combined {@link Comparator<T>} which sorts items by the given comparators.
6371
6495
  * The comparators are applied in the order they are given (left -> right).
@@ -7025,171 +7149,6 @@ class CallState {
7025
7149
  }
7026
7150
  }
7027
7151
 
7028
- class StreamVideoWriteableStateStore {
7029
- constructor() {
7030
- /**
7031
- * A store keeping data of a successfully connected user over WS to the coordinator server.
7032
- */
7033
- this.connectedUserSubject = new rxjs.BehaviorSubject(undefined);
7034
- /**
7035
- * A list of {@link Call} objects created/tracked by this client.
7036
- */
7037
- this.callsSubject = new rxjs.BehaviorSubject([]);
7038
- /**
7039
- * Gets the current value of an observable, or undefined if the observable has
7040
- * not emitted a value yet.
7041
- *
7042
- * @param observable$ the observable to get the value from.
7043
- */
7044
- this.getCurrentValue = getCurrentValue;
7045
- /**
7046
- * Updates the value of the provided Subject.
7047
- * An `update` can either be a new value or a function which takes
7048
- * the current value and returns a new value.
7049
- *
7050
- * @param subject the subject to update.
7051
- * @param update the update to apply to the subject.
7052
- * @return the updated value.
7053
- */
7054
- this.setCurrentValue = setCurrentValue;
7055
- /**
7056
- * Sets the currently connected user.
7057
- *
7058
- * @internal
7059
- * @param user the user to set as connected.
7060
- */
7061
- this.setConnectedUser = (user) => {
7062
- return this.setCurrentValue(this.connectedUserSubject, user);
7063
- };
7064
- /**
7065
- * Sets the list of {@link Call} objects created/tracked by this client.
7066
- * @param calls
7067
- */
7068
- this.setCalls = (calls) => {
7069
- return this.setCurrentValue(this.callsSubject, calls);
7070
- };
7071
- /**
7072
- * Adds a {@link Call} object to the list of {@link Call} objects created/tracked by this client.
7073
- *
7074
- * @param call the call to add.
7075
- */
7076
- this.registerCall = (call) => {
7077
- if (!this.calls.find((c) => c.cid === call.cid)) {
7078
- this.setCalls((calls) => [...calls, call]);
7079
- }
7080
- };
7081
- /**
7082
- * Removes a {@link Call} object from the list of {@link Call} objects created/tracked by this client.
7083
- *
7084
- * @param call the call to remove
7085
- */
7086
- this.unregisterCall = (call) => {
7087
- return this.setCalls((calls) => calls.filter((c) => c !== call));
7088
- };
7089
- /**
7090
- * Finds a {@link Call} object in the list of {@link Call} objects created/tracked by this client.
7091
- *
7092
- * @param type the type of call to find.
7093
- * @param id the id of the call to find.
7094
- */
7095
- this.findCall = (type, id) => {
7096
- return this.calls.find((c) => c.type === type && c.id === id);
7097
- };
7098
- this.connectedUserSubject.subscribe((user) => __awaiter(this, void 0, void 0, function* () {
7099
- // leave all calls when the user disconnects.
7100
- if (!user) {
7101
- for (const call of this.calls) {
7102
- yield call.leave();
7103
- }
7104
- }
7105
- }));
7106
- this.incomingCalls$ = this.callsSubject.pipe(operators.combineLatestWith(this.connectedUserSubject), operators.map(([calls, connectedUser]) => calls.filter((call) => {
7107
- const { metadata, callingState } = call.state;
7108
- return ((metadata === null || metadata === void 0 ? void 0 : metadata.created_by.id) !== (connectedUser === null || connectedUser === void 0 ? void 0 : connectedUser.id) &&
7109
- callingState === exports.CallingState.RINGING);
7110
- })));
7111
- this.outgoingCalls$ = this.callsSubject.pipe(operators.combineLatestWith(this.connectedUserSubject), operators.map(([calls, connectedUser]) => calls.filter((call) => {
7112
- const { metadata, callingState } = call.state;
7113
- return ((metadata === null || metadata === void 0 ? void 0 : metadata.created_by.id) === (connectedUser === null || connectedUser === void 0 ? void 0 : connectedUser.id) &&
7114
- callingState === exports.CallingState.RINGING);
7115
- })));
7116
- }
7117
- /**
7118
- * The currently connected user.
7119
- */
7120
- get connectedUser() {
7121
- return this.getCurrentValue(this.connectedUserSubject);
7122
- }
7123
- /**
7124
- * A list of {@link Call} objects created/tracked by this client.
7125
- */
7126
- get calls() {
7127
- return this.getCurrentValue(this.callsSubject);
7128
- }
7129
- /**
7130
- * A list of objects describing incoming calls.
7131
- * @deprecated derive from calls$ instead.
7132
- */
7133
- get incomingCalls() {
7134
- return this.getCurrentValue(this.incomingCalls$);
7135
- }
7136
- /**
7137
- * A list of objects describing calls initiated by the current user.
7138
- * @deprecated derive from calls$ instead.
7139
- */
7140
- get outgoingCalls() {
7141
- return this.getCurrentValue(this.outgoingCalls$);
7142
- }
7143
- }
7144
- /**
7145
- * A reactive store that exposes state variables in a reactive manner.
7146
- * You can subscribe to changes of the different state variables.
7147
- * This central store contains all the state variables related to [`StreamVideoClient`](./StreamVideClient.md) and [`Call`](./Call.md).
7148
- */
7149
- class StreamVideoReadOnlyStateStore {
7150
- constructor(store) {
7151
- /**
7152
- * This method allows you the get the current value of a state variable.
7153
- *
7154
- * @param observable the observable to get the current value of.
7155
- * @returns the current value of the observable.
7156
- */
7157
- this.getCurrentValue = getCurrentValue;
7158
- // convert and expose subjects as observables
7159
- this.connectedUser$ = store.connectedUserSubject.asObservable();
7160
- this.calls$ = store.callsSubject.asObservable();
7161
- // re-expose observables
7162
- this.incomingCalls$ = store.incomingCalls$;
7163
- this.outgoingCalls$ = store.outgoingCalls$;
7164
- }
7165
- /**
7166
- * The current user connected over WS to the backend.
7167
- */
7168
- get connectedUser() {
7169
- return getCurrentValue(this.connectedUser$);
7170
- }
7171
- /**
7172
- * A list of {@link Call} objects created/tracked by this client.
7173
- */
7174
- get calls() {
7175
- return getCurrentValue(this.calls$);
7176
- }
7177
- /**
7178
- * A list of objects describing incoming calls.
7179
- * @deprecated derive from calls$ instead.
7180
- */
7181
- get incomingCalls() {
7182
- return getCurrentValue(this.incomingCalls$);
7183
- }
7184
- /**
7185
- * A list of objects describing calls initiated by the current user.
7186
- * @deprecated derive from calls$ instead.
7187
- */
7188
- get outgoingCalls() {
7189
- return getCurrentValue(this.outgoingCalls$);
7190
- }
7191
- }
7192
-
7193
7152
  /**
7194
7153
  * Event handler that watched the delivery of `call.accepted`.
7195
7154
  * Once the event is received, the call is joined.
@@ -10532,6 +10491,14 @@ class StreamClient {
10532
10491
  this.nextRequestAbortController = null;
10533
10492
  this._getConnectionID = () => { var _a, _b; return ((_a = this.wsConnection) === null || _a === void 0 ? void 0 : _a.connectionID) || ((_b = this.wsFallback) === null || _b === void 0 ? void 0 : _b.connectionID); };
10534
10493
  this._hasConnectionID = () => Boolean(this._getConnectionID());
10494
+ /**
10495
+ * This will start a promise to hold API calls until `connectUser` is called, useful when user is set in `StreamVideoClient constructor`
10496
+ */
10497
+ this.startWaitingForConnection = () => {
10498
+ this.waitForConnectPromise = new Promise((resolve) => {
10499
+ this.resolveConnectPromise = resolve;
10500
+ });
10501
+ };
10535
10502
  /**
10536
10503
  * connectUser - Set the current user and open a WebSocket connection
10537
10504
  *
@@ -10566,6 +10533,11 @@ class StreamClient {
10566
10533
  this._setUser(user);
10567
10534
  const wsPromise = this.openConnection();
10568
10535
  this.setUserPromise = Promise.all([setTokenPromise, wsPromise]).then((result) => result[1]);
10536
+ if (this.resolveConnectPromise) {
10537
+ this.resolveConnectPromise();
10538
+ this.waitForConnectPromise = undefined;
10539
+ this.resolveConnectPromise = undefined;
10540
+ }
10569
10541
  try {
10570
10542
  return yield this.setUserPromise;
10571
10543
  }
@@ -10664,6 +10636,11 @@ class StreamClient {
10664
10636
  this.connectAnonymousUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
10665
10637
  this.anonymous = true;
10666
10638
  yield this._setToken(user, tokenOrProvider, this.anonymous);
10639
+ if (this.resolveConnectPromise) {
10640
+ this.resolveConnectPromise();
10641
+ this.waitForConnectPromise = undefined;
10642
+ this.resolveConnectPromise = undefined;
10643
+ }
10667
10644
  this._setUser(user);
10668
10645
  // some endpoints require a connection_id to be resolved.
10669
10646
  // as anonymous users aren't allowed to open WS connections, we just
@@ -10673,6 +10650,9 @@ class StreamClient {
10673
10650
  this.doAxiosRequest = (type, url, data, options = {}) => __awaiter(this, void 0, void 0, function* () {
10674
10651
  var _g;
10675
10652
  if (!options.publicEndpoint || this.user) {
10653
+ if (this.waitForConnectPromise) {
10654
+ yield this.waitForConnectPromise;
10655
+ }
10676
10656
  yield this.tokenManager.tokenReady();
10677
10657
  }
10678
10658
  const requestConfig = this._enrichAxiosOptions(options);
@@ -11027,7 +11007,7 @@ class StreamClient {
11027
11007
  }
11028
11008
  getUserAgent() {
11029
11009
  return (this.userAgent ||
11030
- `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.10"}`);
11010
+ `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.12"}`);
11031
11011
  }
11032
11012
  setUserAgent(userAgent) {
11033
11013
  this.userAgent = userAgent;
@@ -11095,12 +11075,7 @@ class StreamClient {
11095
11075
  * A `StreamVideoClient` instance lets you communicate with our API, and authenticate users.
11096
11076
  */
11097
11077
  class StreamVideoClient {
11098
- /**
11099
- * You should create only one instance of `StreamVideoClient`.
11100
- * @param apiKey your Stream API key
11101
- * @param opts the options for the client.
11102
- */
11103
- constructor(apiKey, opts) {
11078
+ constructor(apiKeyOrArgs, opts) {
11104
11079
  this.eventHandlersToUnregister = [];
11105
11080
  /**
11106
11081
  * Disconnects the currently connected user from the client.
@@ -11111,6 +11086,9 @@ class StreamVideoClient {
11111
11086
  * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
11112
11087
  */
11113
11088
  this.disconnectUser = (timeout) => __awaiter(this, void 0, void 0, function* () {
11089
+ if (!this.streamClient.user) {
11090
+ return;
11091
+ }
11114
11092
  const disconnectUser = () => this.streamClient.disconnectUser(timeout);
11115
11093
  this.disconnectionPromise = this.connectionPromise
11116
11094
  ? this.connectionPromise.then(() => disconnectUser())
@@ -11257,7 +11235,17 @@ class StreamVideoClient {
11257
11235
  this.connectionPromise.finally(() => (this.connectionPromise = undefined));
11258
11236
  return this.connectionPromise;
11259
11237
  });
11260
- this.streamClient = new StreamClient(apiKey, Object.assign({ persistUserOnConnectionFailure: true }, opts));
11238
+ if (typeof apiKeyOrArgs === 'string') {
11239
+ this.streamClient = new StreamClient(apiKeyOrArgs, Object.assign({ persistUserOnConnectionFailure: true }, opts));
11240
+ }
11241
+ else {
11242
+ this.streamClient = new StreamClient(apiKeyOrArgs.apiKey, Object.assign({ persistUserOnConnectionFailure: true }, apiKeyOrArgs.options));
11243
+ this.user = apiKeyOrArgs.user;
11244
+ this.token = apiKeyOrArgs.token || apiKeyOrArgs.tokenProvider;
11245
+ if (this.user) {
11246
+ this.streamClient.startWaitingForConnection();
11247
+ }
11248
+ }
11261
11249
  this.writeableStateStore = new StreamVideoWriteableStateStore();
11262
11250
  this.readOnlyStateStore = new StreamVideoReadOnlyStateStore(this.writeableStateStore);
11263
11251
  }
@@ -11267,23 +11255,28 @@ class StreamVideoClient {
11267
11255
  * If the connection is successful, the connected user [state variable](#readonlystatestore) will be updated accordingly.
11268
11256
  *
11269
11257
  * @param user the user to connect.
11270
- * @param tokenOrProvider a token or a function that returns a token.
11258
+ * @param token a token or a function that returns a token.
11271
11259
  */
11272
11260
  connectUser(user, token) {
11273
11261
  var _a;
11274
11262
  return __awaiter(this, void 0, void 0, function* () {
11275
- if (user.type === 'anonymous') {
11276
- user.id = '!anon';
11277
- return this.connectAnonymousUser(user, token);
11263
+ const userToConnect = user || this.user;
11264
+ const tokenToUse = token || this.token;
11265
+ if (!userToConnect) {
11266
+ throw new Error('Connect user is called without user');
11267
+ }
11268
+ if (userToConnect.type === 'anonymous') {
11269
+ userToConnect.id = '!anon';
11270
+ return this.connectAnonymousUser(userToConnect, tokenToUse);
11278
11271
  }
11279
- if (user.type === 'guest') {
11272
+ if (userToConnect.type === 'guest') {
11280
11273
  const response = yield this.createGuestUser({
11281
- user: Object.assign(Object.assign({}, user), { role: 'guest' }),
11274
+ user: Object.assign(Object.assign({}, userToConnect), { role: 'guest' }),
11282
11275
  });
11283
11276
  return this.connectUser(response.user, response.access_token);
11284
11277
  }
11285
11278
  const connectUser = () => {
11286
- return this.streamClient.connectUser(user, token);
11279
+ return this.streamClient.connectUser(userToConnect, tokenToUse);
11287
11280
  };
11288
11281
  this.connectionPromise = this.disconnectionPromise
11289
11282
  ? this.disconnectionPromise.then(() => connectUser())
@@ -11317,7 +11310,7 @@ class StreamVideoClient {
11317
11310
  if (event.type !== 'call.created')
11318
11311
  return;
11319
11312
  const { call, members } = event;
11320
- if (user.id === call.created_by.id) {
11313
+ if (userToConnect.id === call.created_by.id) {
11321
11314
  console.warn('Received `call.created` sent by the current user');
11322
11315
  return;
11323
11316
  }
@@ -11334,27 +11327,26 @@ class StreamVideoClient {
11334
11327
  if (event.type !== 'call.ring')
11335
11328
  return;
11336
11329
  const { call, members } = event;
11337
- if (user.id === call.created_by.id) {
11330
+ if (userToConnect.id === call.created_by.id) {
11338
11331
  console.warn('Received `call.ring` sent by the current user');
11339
11332
  return;
11340
11333
  }
11341
11334
  // The call might already be tracked by the client,
11342
11335
  // if `call.created` was received before `call.ring`.
11343
- // In that case, we just reuse the already tracked call.
11344
- let theCall = this.writeableStateStore.findCall(call.type, call.id);
11345
- if (!theCall) {
11346
- // otherwise, we create a new call
11347
- theCall = new Call({
11348
- streamClient: this.streamClient,
11349
- type: call.type,
11350
- id: call.id,
11351
- members,
11352
- clientStore: this.writeableStateStore,
11353
- ringing: true,
11354
- });
11355
- }
11336
+ // In that case, we cleanup the already tracked call.
11337
+ const prevCall = this.writeableStateStore.findCall(call.type, call.id);
11338
+ yield (prevCall === null || prevCall === void 0 ? void 0 : prevCall.leave());
11339
+ // we create a new call
11340
+ const theCall = new Call({
11341
+ streamClient: this.streamClient,
11342
+ type: call.type,
11343
+ id: call.id,
11344
+ members,
11345
+ clientStore: this.writeableStateStore,
11346
+ ringing: true,
11347
+ });
11356
11348
  // we fetch the latest metadata for the call from the server
11357
- yield theCall.get({ ring: true });
11349
+ yield theCall.get();
11358
11350
  this.writeableStateStore.registerCall(theCall);
11359
11351
  })));
11360
11352
  return connectUserResponse;