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