dexie-cloud-addon 4.0.0-beta.14 → 4.0.0-beta.17

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.
@@ -101,7 +101,7 @@ function __spreadArray$1(to, from, pack) {
101
101
  *
102
102
  * ==========================================================================
103
103
  *
104
- * Version 4.0.0-beta.14, Mon Dec 20 2021
104
+ * Version 4.0.0-beta.17, Fri Apr 08 2022
105
105
  *
106
106
  * https://dexie.org
107
107
  *
@@ -5296,166 +5296,22 @@ function overrideParseStoresSpec(origFunc, dexie) {
5296
5296
  return rv;
5297
5297
  };
5298
5298
  }
5299
- var SECONDS = 1000;
5300
- var MINUTES = 60 * SECONDS;
5301
- var myId = randomString(16);
5302
- var GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
5303
- var GUARDED_JOB_TIMEOUT = 1 * MINUTES;
5304
- function performGuardedJob(db, jobName, jobsTableName, job, _e) {
5305
- var _f = _e === void 0 ? {} : _e, awaitRemoteJob = _f.awaitRemoteJob;
5306
- return __awaiter$1(this, void 0, void 0, function () {
5307
- function aquireLock() {
5308
- return __awaiter$1(this, void 0, void 0, function () {
5309
- var gotTheLock, jobDoneObservable, err_1;
5310
- var _this_1 = this;
5311
- return __generator$1(this, function (_e) {
5312
- switch (_e.label) {
5313
- case 0: return [4 /*yield*/, db.transaction('rw!', jobsTableName, function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5314
- var currentWork;
5315
- return __generator$1(this, function (_e) {
5316
- switch (_e.label) {
5317
- case 0: return [4 /*yield*/, jobsTable.get(jobName)];
5318
- case 1:
5319
- currentWork = _e.sent();
5320
- if (!!currentWork) return [3 /*break*/, 3];
5321
- // No one else is working. Let's record that we are.
5322
- return [4 /*yield*/, jobsTable.add({
5323
- nodeId: myId,
5324
- started: new Date(),
5325
- heartbeat: new Date()
5326
- }, jobName)];
5327
- case 2:
5328
- // No one else is working. Let's record that we are.
5329
- _e.sent();
5330
- return [2 /*return*/, true];
5331
- case 3:
5332
- if (!(currentWork.heartbeat.getTime() <
5333
- Date.now() - GUARDED_JOB_TIMEOUT)) return [3 /*break*/, 5];
5334
- console.warn("Latest ".concat(jobName, " worker seem to have died.\n"), "The dead job started:", currentWork.started, "\n", "Last heart beat was:", currentWork.heartbeat, '\n', "We're now taking over!");
5335
- // Now, take over!
5336
- return [4 /*yield*/, jobsTable.put({
5337
- nodeId: myId,
5338
- started: new Date(),
5339
- heartbeat: new Date()
5340
- }, jobName)];
5341
- case 4:
5342
- // Now, take over!
5343
- _e.sent();
5344
- return [2 /*return*/, true];
5345
- case 5: return [2 /*return*/, false];
5346
- }
5347
- });
5348
- }); })];
5349
- case 1:
5350
- gotTheLock = _e.sent();
5351
- if (gotTheLock)
5352
- return [2 /*return*/, true];
5353
- if (!awaitRemoteJob) return [3 /*break*/, 6];
5354
- _e.label = 2;
5355
- case 2:
5356
- _e.trys.push([2, 4, , 6]);
5357
- jobDoneObservable = from$1(liveQuery(function () { return jobsTable.get(jobName); })).pipe(timeout(GUARDED_JOB_TIMEOUT), filter(function (job) { return !job; }));
5358
- return [4 /*yield*/, jobDoneObservable.toPromise()];
5359
- case 3:
5360
- _e.sent();
5361
- return [2 /*return*/, false];
5362
- case 4:
5363
- err_1 = _e.sent();
5364
- if (err_1.name !== 'TimeoutError') {
5365
- throw err_1;
5366
- }
5367
- return [4 /*yield*/, aquireLock()];
5368
- case 5:
5369
- // Timeout stopped us! Try aquire the lock now.
5370
- // It will likely succeed this time unless
5371
- // another client took it.
5372
- return [2 /*return*/, _e.sent()];
5373
- case 6: return [2 /*return*/, false];
5374
- }
5375
- });
5376
- });
5377
- }
5378
- var jobsTable, heartbeat;
5379
- var _this_1 = this;
5380
- return __generator$1(this, function (_g) {
5381
- switch (_g.label) {
5382
- case 0:
5383
- jobsTable = db.table(jobsTableName);
5384
- return [4 /*yield*/, aquireLock()];
5385
- case 1:
5386
- if (!_g.sent()) return [3 /*break*/, 6];
5387
- heartbeat = setInterval(function () {
5388
- jobsTable.update(jobName, function (job) {
5389
- if (job.nodeId === myId) {
5390
- job.heartbeat = new Date();
5391
- }
5392
- });
5393
- }, GUARDED_JOB_HEARTBEAT);
5394
- _g.label = 2;
5395
- case 2:
5396
- _g.trys.push([2, , 4, 6]);
5397
- return [4 /*yield*/, job()];
5398
- case 3: return [2 /*return*/, _g.sent()];
5399
- case 4:
5400
- // Stop heartbeat
5401
- clearInterval(heartbeat);
5402
- // Remove the persisted job state:
5403
- return [4 /*yield*/, db.transaction('rw!', jobsTableName, function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5404
- var currentWork;
5405
- return __generator$1(this, function (_e) {
5406
- switch (_e.label) {
5407
- case 0: return [4 /*yield*/, jobsTable.get(jobName)];
5408
- case 1:
5409
- currentWork = _e.sent();
5410
- if (currentWork && currentWork.nodeId === myId) {
5411
- jobsTable.delete(jobName);
5412
- }
5413
- return [2 /*return*/];
5414
- }
5415
- });
5416
- }); })];
5417
- case 5:
5418
- // Remove the persisted job state:
5419
- _g.sent();
5420
- return [7 /*endfinally*/];
5421
- case 6: return [2 /*return*/];
5422
- }
5423
- });
5424
- });
5425
- }
5426
5299
  function performInitialSync(db, cloudOptions, cloudSchema) {
5427
5300
  return __awaiter$1(this, void 0, void 0, function () {
5428
- var _this_1 = this;
5429
5301
  return __generator$1(this, function (_e) {
5430
5302
  switch (_e.label) {
5431
5303
  case 0:
5432
- console.debug("Performing initial sync");
5433
- return [4 /*yield*/, performGuardedJob(db, 'initialSync', '$jobs', function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5434
- var syncState;
5435
- return __generator$1(this, function (_e) {
5436
- switch (_e.label) {
5437
- case 0: return [4 /*yield*/, db.getPersistedSyncState()];
5438
- case 1:
5439
- syncState = _e.sent();
5440
- if (!!(syncState === null || syncState === void 0 ? void 0 : syncState.initiallySynced)) return [3 /*break*/, 3];
5441
- return [4 /*yield*/, sync(db, cloudOptions, cloudSchema, { isInitialSync: true })];
5442
- case 2:
5443
- _e.sent();
5444
- _e.label = 3;
5445
- case 3: return [2 /*return*/];
5446
- }
5447
- });
5448
- }); }, { awaitRemoteJob: true } // Don't return until the job is done!
5449
- )];
5304
+ console.debug('Performing initial sync');
5305
+ return [4 /*yield*/, sync(db, cloudOptions, cloudSchema, { isInitialSync: true })];
5450
5306
  case 1:
5451
5307
  _e.sent();
5452
- console.debug("Done initial sync");
5308
+ console.debug('Done initial sync');
5453
5309
  return [2 /*return*/];
5454
5310
  }
5455
5311
  });
5456
5312
  });
5457
5313
  }
5458
- var USER_INACTIVITY_TIMEOUT = 300000; // 300_000;
5314
+ var USER_INACTIVITY_TIMEOUT = 180000; // 3 minutes
5459
5315
  var INACTIVE_WAIT_TIME = 20000;
5460
5316
  // This observable will be emitted to later down....
5461
5317
  var userIsActive = new BehaviorSubject(true);
@@ -5469,9 +5325,12 @@ var userIsActive = new BehaviorSubject(true);
5469
5325
  // for just a short time.
5470
5326
  var userIsReallyActive = new BehaviorSubject(true);
5471
5327
  userIsActive
5472
- .pipe(switchMap(function (isActive) { return isActive
5473
- ? of(true)
5474
- : of(false).pipe(delay(INACTIVE_WAIT_TIME)); }), distinctUntilChanged())
5328
+ .pipe(switchMap(function (isActive) {
5329
+ //console.debug('SyncStatus: DUBB: isActive changed to', isActive);
5330
+ return isActive
5331
+ ? of(true)
5332
+ : of(false).pipe(delay(INACTIVE_WAIT_TIME));
5333
+ }), distinctUntilChanged())
5475
5334
  .subscribe(userIsReallyActive);
5476
5335
  //
5477
5336
  // First create some corner-stone observables to build the flow on
@@ -5486,7 +5345,7 @@ var documentBecomesHidden = visibilityStateIsChanged.pipe(filter(function () { r
5486
5345
  var documentBecomesVisible = visibilityStateIsChanged.pipe(filter(function () { return document.visibilityState === 'visible'; }));
5487
5346
  // Any of various user-activity-related events happen:
5488
5347
  var userDoesSomething = typeof window !== 'undefined'
5489
- ? merge(documentBecomesVisible, fromEvent(window, 'mousemove'), fromEvent(window, 'keydown'), fromEvent(window, 'wheel'), fromEvent(window, 'touchmove'))
5348
+ ? merge(documentBecomesVisible, fromEvent(window, 'mousedown'), fromEvent(window, 'mousemove'), fromEvent(window, 'keydown'), fromEvent(window, 'wheel'), fromEvent(window, 'touchmove'))
5490
5349
  : of({});
5491
5350
  if (typeof document !== 'undefined') {
5492
5351
  //
@@ -5541,6 +5400,7 @@ var WSConnection = /** @class */ (function (_super_1) {
5541
5400
  function WSConnection(databaseUrl, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus) {
5542
5401
  var _this_1 = _super_1.call(this, function () { return _this_1.teardown(); }) || this;
5543
5402
  _this_1.id = ++counter;
5403
+ _this_1.reconnecting = false;
5544
5404
  console.debug('New WebSocket Connection', _this_1.id, token ? 'authorized' : 'unauthorized');
5545
5405
  _this_1.databaseUrl = databaseUrl;
5546
5406
  _this_1.rev = rev;
@@ -5561,7 +5421,7 @@ var WSConnection = /** @class */ (function (_super_1) {
5561
5421
  this.disconnect();
5562
5422
  };
5563
5423
  WSConnection.prototype.disconnect = function () {
5564
- this.webSocketStatus.next("disconnected");
5424
+ this.webSocketStatus.next('disconnected');
5565
5425
  if (this.pinger) {
5566
5426
  clearInterval(this.pinger);
5567
5427
  this.pinger = null;
@@ -5579,17 +5439,25 @@ var WSConnection = /** @class */ (function (_super_1) {
5579
5439
  }
5580
5440
  };
5581
5441
  WSConnection.prototype.reconnect = function () {
5582
- this.disconnect();
5583
- this.connect();
5442
+ var _this_1 = this;
5443
+ if (this.reconnecting)
5444
+ return;
5445
+ this.reconnecting = true;
5446
+ try {
5447
+ this.disconnect();
5448
+ }
5449
+ catch (_e) { }
5450
+ this.connect()
5451
+ .catch(function () { })
5452
+ .then(function () { return (_this_1.reconnecting = false); }); // finally()
5584
5453
  };
5585
5454
  WSConnection.prototype.connect = function () {
5586
5455
  return __awaiter$1(this, void 0, void 0, function () {
5587
- var wsUrl, searchParams, ws;
5456
+ var wsUrl, searchParams, ws, everConnected_1;
5588
5457
  var _this_1 = this;
5589
5458
  return __generator$1(this, function (_e) {
5590
5459
  switch (_e.label) {
5591
5460
  case 0:
5592
- this.webSocketStatus.next("connecting");
5593
5461
  this.lastServerActivity = new Date();
5594
5462
  if (this.pauseUntil && this.pauseUntil > new Date()) {
5595
5463
  console.debug('WS not reconnecting just yet', {
@@ -5604,12 +5472,14 @@ var WSConnection = /** @class */ (function (_super_1) {
5604
5472
  if (!this.databaseUrl)
5605
5473
  throw new Error("Cannot connect without a database URL");
5606
5474
  if (this.closed) {
5475
+ //console.debug('SyncStatus: DUBB: Ooops it was closed!');
5607
5476
  return [2 /*return*/];
5608
5477
  }
5609
5478
  if (this.tokenExpiration && this.tokenExpiration < new Date()) {
5610
5479
  this.subscriber.error(new TokenExpiredError()); // Will be handled in connectWebSocket.ts.
5611
5480
  return [2 /*return*/];
5612
5481
  }
5482
+ this.webSocketStatus.next('connecting');
5613
5483
  this.pinger = setInterval(function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5614
5484
  var _this_1 = this;
5615
5485
  return __generator$1(this, function (_e) {
@@ -5659,7 +5529,7 @@ var WSConnection = /** @class */ (function (_super_1) {
5659
5529
  searchParams = new URLSearchParams();
5660
5530
  if (this.subscriber.closed)
5661
5531
  return [2 /*return*/];
5662
- searchParams.set('v', "2");
5532
+ searchParams.set('v', '2');
5663
5533
  searchParams.set('rev', this.rev);
5664
5534
  searchParams.set('realmsHash', this.realmSetHash);
5665
5535
  searchParams.set('clientId', this.clientIdentity);
@@ -5700,17 +5570,23 @@ var WSConnection = /** @class */ (function (_super_1) {
5700
5570
  _e.label = 1;
5701
5571
  case 1:
5702
5572
  _e.trys.push([1, 3, , 4]);
5573
+ everConnected_1 = false;
5703
5574
  return [4 /*yield*/, new Promise(function (resolve, reject) {
5704
5575
  ws.onopen = function (event) {
5705
5576
  console.debug('dexie-cloud WebSocket onopen');
5577
+ everConnected_1 = true;
5706
5578
  resolve(null);
5707
5579
  };
5708
5580
  ws.onerror = function (event) {
5709
- var error = event.error || new Error('WebSocket Error');
5710
- _this_1.disconnect();
5711
- _this_1.subscriber.error(error);
5712
- _this_1.webSocketStatus.next("error");
5713
- reject(error);
5581
+ if (!everConnected_1) {
5582
+ var error = event.error || new Error('WebSocket Error');
5583
+ _this_1.subscriber.error(error);
5584
+ _this_1.webSocketStatus.next('error');
5585
+ reject(error);
5586
+ }
5587
+ else {
5588
+ _this_1.reconnect();
5589
+ }
5714
5590
  };
5715
5591
  })];
5716
5592
  case 2:
@@ -5718,8 +5594,9 @@ var WSConnection = /** @class */ (function (_super_1) {
5718
5594
  this.messageProducerSubscription = this.messageProducer.subscribe(function (msg) {
5719
5595
  var _e;
5720
5596
  if (!_this_1.closed) {
5721
- if (msg.type === 'ready' && _this_1.webSocketStatus.value !== 'connected') {
5722
- _this_1.webSocketStatus.next("connected");
5597
+ if (msg.type === 'ready' &&
5598
+ _this_1.webSocketStatus.value !== 'connected') {
5599
+ _this_1.webSocketStatus.next('connected');
5723
5600
  }
5724
5601
  (_e = _this_1.ws) === null || _e === void 0 ? void 0 : _e.send(TSON.stringify(msg));
5725
5602
  }
@@ -5828,7 +5705,10 @@ function connectWebSocket(db) {
5828
5705
  else {
5829
5706
  return throwError(error);
5830
5707
  }
5831
- }), catchError(function (error) { return from$1(waitAndReconnectWhenUserDoesSomething(error)).pipe(switchMap(function () { return createObservable(); })); }));
5708
+ }), catchError(function (error) {
5709
+ db.cloud.webSocketStatus.next("error");
5710
+ return from$1(waitAndReconnectWhenUserDoesSomething(error)).pipe(switchMap(function () { return createObservable(); }));
5711
+ }));
5832
5712
  }
5833
5713
  return createObservable().subscribe(function (msg) {
5834
5714
  if (msg) {
@@ -5861,6 +5741,135 @@ function isSyncNeeded(db) {
5861
5741
  });
5862
5742
  });
5863
5743
  }
5744
+ var SECONDS = 1000;
5745
+ var MINUTES = 60 * SECONDS;
5746
+ var myId = randomString(16);
5747
+ var GUARDED_JOB_HEARTBEAT = 1 * SECONDS;
5748
+ var GUARDED_JOB_TIMEOUT = 1 * MINUTES;
5749
+ function performGuardedJob(db, jobName, jobsTableName, job, _e) {
5750
+ var _f = _e === void 0 ? {} : _e, awaitRemoteJob = _f.awaitRemoteJob;
5751
+ return __awaiter$1(this, void 0, void 0, function () {
5752
+ function aquireLock() {
5753
+ return __awaiter$1(this, void 0, void 0, function () {
5754
+ var gotTheLock, jobDoneObservable, err_1;
5755
+ var _this_1 = this;
5756
+ return __generator$1(this, function (_e) {
5757
+ switch (_e.label) {
5758
+ case 0: return [4 /*yield*/, db.transaction('rw!', jobsTableName, function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5759
+ var currentWork;
5760
+ return __generator$1(this, function (_e) {
5761
+ switch (_e.label) {
5762
+ case 0: return [4 /*yield*/, jobsTable.get(jobName)];
5763
+ case 1:
5764
+ currentWork = _e.sent();
5765
+ if (!!currentWork) return [3 /*break*/, 3];
5766
+ // No one else is working. Let's record that we are.
5767
+ return [4 /*yield*/, jobsTable.add({
5768
+ nodeId: myId,
5769
+ started: new Date(),
5770
+ heartbeat: new Date()
5771
+ }, jobName)];
5772
+ case 2:
5773
+ // No one else is working. Let's record that we are.
5774
+ _e.sent();
5775
+ return [2 /*return*/, true];
5776
+ case 3:
5777
+ if (!(currentWork.heartbeat.getTime() <
5778
+ Date.now() - GUARDED_JOB_TIMEOUT)) return [3 /*break*/, 5];
5779
+ console.warn("Latest ".concat(jobName, " worker seem to have died.\n"), "The dead job started:", currentWork.started, "\n", "Last heart beat was:", currentWork.heartbeat, '\n', "We're now taking over!");
5780
+ // Now, take over!
5781
+ return [4 /*yield*/, jobsTable.put({
5782
+ nodeId: myId,
5783
+ started: new Date(),
5784
+ heartbeat: new Date()
5785
+ }, jobName)];
5786
+ case 4:
5787
+ // Now, take over!
5788
+ _e.sent();
5789
+ return [2 /*return*/, true];
5790
+ case 5: return [2 /*return*/, false];
5791
+ }
5792
+ });
5793
+ }); })];
5794
+ case 1:
5795
+ gotTheLock = _e.sent();
5796
+ if (gotTheLock)
5797
+ return [2 /*return*/, true];
5798
+ if (!awaitRemoteJob) return [3 /*break*/, 6];
5799
+ _e.label = 2;
5800
+ case 2:
5801
+ _e.trys.push([2, 4, , 6]);
5802
+ jobDoneObservable = from$1(liveQuery(function () { return jobsTable.get(jobName); })).pipe(timeout(GUARDED_JOB_TIMEOUT), filter(function (job) { return !job; }));
5803
+ return [4 /*yield*/, jobDoneObservable.toPromise()];
5804
+ case 3:
5805
+ _e.sent();
5806
+ return [2 /*return*/, false];
5807
+ case 4:
5808
+ err_1 = _e.sent();
5809
+ if (err_1.name !== 'TimeoutError') {
5810
+ throw err_1;
5811
+ }
5812
+ return [4 /*yield*/, aquireLock()];
5813
+ case 5:
5814
+ // Timeout stopped us! Try aquire the lock now.
5815
+ // It will likely succeed this time unless
5816
+ // another client took it.
5817
+ return [2 /*return*/, _e.sent()];
5818
+ case 6: return [2 /*return*/, false];
5819
+ }
5820
+ });
5821
+ });
5822
+ }
5823
+ var jobsTable, heartbeat;
5824
+ var _this_1 = this;
5825
+ return __generator$1(this, function (_g) {
5826
+ switch (_g.label) {
5827
+ case 0:
5828
+ jobsTable = db.table(jobsTableName);
5829
+ return [4 /*yield*/, aquireLock()];
5830
+ case 1:
5831
+ if (!_g.sent()) return [3 /*break*/, 6];
5832
+ heartbeat = setInterval(function () {
5833
+ jobsTable.update(jobName, function (job) {
5834
+ if (job.nodeId === myId) {
5835
+ job.heartbeat = new Date();
5836
+ }
5837
+ });
5838
+ }, GUARDED_JOB_HEARTBEAT);
5839
+ _g.label = 2;
5840
+ case 2:
5841
+ _g.trys.push([2, , 4, 6]);
5842
+ return [4 /*yield*/, job()];
5843
+ case 3: return [2 /*return*/, _g.sent()];
5844
+ case 4:
5845
+ // Stop heartbeat
5846
+ clearInterval(heartbeat);
5847
+ // Remove the persisted job state:
5848
+ return [4 /*yield*/, db.transaction('rw!', jobsTableName, function () { return __awaiter$1(_this_1, void 0, void 0, function () {
5849
+ var currentWork;
5850
+ return __generator$1(this, function (_e) {
5851
+ switch (_e.label) {
5852
+ case 0: return [4 /*yield*/, jobsTable.get(jobName)];
5853
+ case 1:
5854
+ currentWork = _e.sent();
5855
+ if (!(currentWork && currentWork.nodeId === myId)) return [3 /*break*/, 3];
5856
+ return [4 /*yield*/, jobsTable.delete(jobName)];
5857
+ case 2:
5858
+ _e.sent();
5859
+ _e.label = 3;
5860
+ case 3: return [2 /*return*/];
5861
+ }
5862
+ });
5863
+ }); })];
5864
+ case 5:
5865
+ // Remove the persisted job state:
5866
+ _g.sent();
5867
+ return [7 /*endfinally*/];
5868
+ case 6: return [2 /*return*/];
5869
+ }
5870
+ });
5871
+ });
5872
+ }
5864
5873
  var ongoingSyncs = new WeakMap();
5865
5874
  function syncIfPossible(db, cloudOptions, cloudSchema, options) {
5866
5875
  var ongoing = ongoingSyncs.get(db);
@@ -6469,6 +6478,21 @@ function createSharedValueObservable(o, defaultValue) {
6469
6478
  rv.getValue = function () { return currentValue; };
6470
6479
  return rv;
6471
6480
  }
6481
+ var getGlobalRolesObservable = associate(function (db) {
6482
+ return createSharedValueObservable(liveQuery(function () { return db.roles
6483
+ .where({ realmId: 'rlm-public' })
6484
+ .toArray()
6485
+ .then(function (roles) {
6486
+ var rv = {};
6487
+ for (var _e = 0, _f = roles
6488
+ .slice()
6489
+ .sort(function (a, b) { return (a.sortOrder || 0) - (b.sortOrder || 0); }); _e < _f.length; _e++) {
6490
+ var role = _f[_e];
6491
+ rv[role.name] = role;
6492
+ }
6493
+ return rv;
6494
+ }); }), {});
6495
+ });
6472
6496
  var getCurrentUserEmitter = associate(function (db) { return new BehaviorSubject(UNAUTHORIZED_USER); });
6473
6497
  var getInternalAccessControlObservable = associate(function (db) {
6474
6498
  return createSharedValueObservable(getCurrentUserEmitter(db._novip).pipe(switchMap(function (currentUser) { return liveQuery(function () { return db.transaction('r', 'realms', 'members', function () { return Promise.all([
@@ -6571,17 +6595,40 @@ function mergePermissions() {
6571
6595
  return reduced;
6572
6596
  }
6573
6597
  var getPermissionsLookupObservable = associate(function (db) {
6574
- var o = getInternalAccessControlObservable(db._novip);
6598
+ var o = createSharedValueObservable(combineLatest([
6599
+ getInternalAccessControlObservable(db._novip),
6600
+ getGlobalRolesObservable(db._novip),
6601
+ ]).pipe(map(function (_e) {
6602
+ var _f = _e[0], selfMembers = _f.selfMembers, realms = _f.realms, userId = _f.userId, globalRoles = _e[1];
6603
+ return ({
6604
+ selfMembers: selfMembers,
6605
+ realms: realms,
6606
+ userId: userId,
6607
+ globalRoles: globalRoles,
6608
+ });
6609
+ })), {
6610
+ selfMembers: [],
6611
+ realms: [],
6612
+ userId: UNAUTHORIZED_USER.userId,
6613
+ globalRoles: {},
6614
+ });
6575
6615
  return mapValueObservable(o, function (_e) {
6576
6616
  var _f;
6577
- var selfMembers = _e.selfMembers, realms = _e.realms, userId = _e.userId;
6617
+ var selfMembers = _e.selfMembers, realms = _e.realms, userId = _e.userId, globalRoles = _e.globalRoles;
6578
6618
  var rv = realms
6579
- .map(function (realm) { return (__assign(__assign({}, realm), { permissions: realm.owner === userId
6580
- ? { manage: '*' }
6581
- : mergePermissions.apply(void 0, selfMembers
6582
- .filter(function (m) { return m.realmId === realm.realmId; })
6583
- .map(function (m) { return m.permissions; })
6584
- .filter(function (p) { return p; })) })); })
6619
+ .map(function (realm) {
6620
+ var selfRealmMembers = selfMembers.filter(function (m) { return m.realmId === realm.realmId; });
6621
+ var directPermissionSets = selfRealmMembers
6622
+ .map(function (m) { return m.permissions; })
6623
+ .filter(function (p) { return p; });
6624
+ var rolePermissionSets = flatten(selfRealmMembers.map(function (m) { return m.roles; }).filter(function (roleName) { return roleName; }))
6625
+ .map(function (role) { return globalRoles[role]; })
6626
+ .filter(function (role) { return role; })
6627
+ .map(function (role) { return role.permissions; });
6628
+ return __assign(__assign({}, realm), { permissions: realm.owner === userId
6629
+ ? { manage: '*' }
6630
+ : mergePermissions.apply(void 0, __spreadArray$1(__spreadArray$1([], directPermissionSets, false), rolePermissionSets, false)) });
6631
+ })
6585
6632
  .reduce(function (p, c) {
6586
6633
  var _e;
6587
6634
  return (__assign(__assign({}, p), (_e = {}, _e[c.realmId] = c, _e)));
@@ -6679,7 +6726,7 @@ function permissions(dexie, obj, tableName) {
6679
6726
  var realm = permissionsLookup[realmId || dexie.cloud.currentUserId];
6680
6727
  if (!realm)
6681
6728
  return new PermissionChecker({}, tableName, !owner || owner === dexie.cloud.currentUserId);
6682
- return new PermissionChecker(realm.permissions, tableName, !owner || owner === dexie.cloud.currentUserId);
6729
+ return new PermissionChecker(realm.permissions, tableName, realmId === dexie.cloud.currentUserId || owner === dexie.cloud.currentUserId);
6683
6730
  };
6684
6731
  var o = source.pipe(map(mapper));
6685
6732
  o.getValue = function () { return mapper(source.getValue()); };
@@ -6717,6 +6764,7 @@ function dexieCloud(dexie) {
6717
6764
  //
6718
6765
  var currentUserEmitter = getCurrentUserEmitter(dexie);
6719
6766
  var subscriptions = [];
6767
+ var configuredProgramatically = false;
6720
6768
  // local sync worker - used when there's no service worker.
6721
6769
  var localSyncWorker = null;
6722
6770
  dexie.on('ready', function (dexie) { return __awaiter$1(_this_1, void 0, void 0, function () {
@@ -6752,7 +6800,7 @@ function dexieCloud(dexie) {
6752
6800
  currentUserEmitter.next(UNAUTHORIZED_USER);
6753
6801
  });
6754
6802
  dexie.cloud = {
6755
- version: '4.0.0-beta.14',
6803
+ version: '4.0.0-beta.17',
6756
6804
  options: __assign({}, DEFAULT_OPTIONS),
6757
6805
  schema: null,
6758
6806
  serverState: null,
@@ -6786,8 +6834,10 @@ function dexieCloud(dexie) {
6786
6834
  });
6787
6835
  },
6788
6836
  invites: getInvitesObservable(dexie),
6837
+ roles: getGlobalRolesObservable(dexie),
6789
6838
  configure: function (options) {
6790
6839
  options = dexie.cloud.options = __assign(__assign({}, dexie.cloud.options), options);
6840
+ configuredProgramatically = true;
6791
6841
  if (options.databaseUrl && options.nameSuffix) {
6792
6842
  // @ts-ignore
6793
6843
  dexie.name = "".concat(origIdbName, "-").concat(getDbNameFromDbUrl(options.databaseUrl));
@@ -6922,7 +6972,7 @@ function dexieCloud(dexie) {
6922
6972
  ])];
6923
6973
  case 1:
6924
6974
  _f = _m.sent(), persistedOptions = _f[0], persistedSchema = _f[1], persistedSyncState = _f[2];
6925
- if (!!options) return [3 /*break*/, 2];
6975
+ if (!!configuredProgramatically) return [3 /*break*/, 2];
6926
6976
  // Options not specified programatically (use case for SW!)
6927
6977
  // Take persisted options:
6928
6978
  db.cloud.options = persistedOptions || null;
@@ -6931,9 +6981,10 @@ function dexieCloud(dexie) {
6931
6981
  if (!(!persistedOptions ||
6932
6982
  JSON.stringify(persistedOptions) !== JSON.stringify(options))) return [3 /*break*/, 4];
6933
6983
  // Update persisted options:
6984
+ if (!options)
6985
+ throw new Error("Internal error"); // options cannot be null if configuredProgramatically is set.
6934
6986
  return [4 /*yield*/, db.$syncState.put(options, 'options')];
6935
6987
  case 3:
6936
- // Update persisted options:
6937
6988
  _m.sent();
6938
6989
  _m.label = 4;
6939
6990
  case 4:
@@ -7079,7 +7130,7 @@ function dexieCloud(dexie) {
7079
7130
  });
7080
7131
  }
7081
7132
  }
7082
- dexieCloud.version = '4.0.0-beta.14';
7133
+ dexieCloud.version = '4.0.0-beta.17';
7083
7134
  Dexie.Cloud = dexieCloud;
7084
7135
 
7085
7136
  export { dexieCloud as default, dexieCloud, getTiedObjectId, getTiedRealmId };