@planningcenter/topbar 12.5.0-rc.4 → 12.5.0

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.
@@ -4114,7 +4114,18 @@ true;
4114
4114
  return undefined;
4115
4115
  }
4116
4116
  }
4117
- async function mintToken(personId) {
4117
+ const minting = new Map();
4118
+ function mintToken(personId) {
4119
+ const running = minting.get(personId);
4120
+ if (running) {
4121
+ return running;
4122
+ } else {
4123
+ const run = mint(personId).finally(() => minting.delete(personId));
4124
+ minting.set(personId, run);
4125
+ return run;
4126
+ }
4127
+ }
4128
+ async function mint(personId) {
4118
4129
  const {
4119
4130
  json,
4120
4131
  ok,
@@ -4138,6 +4149,10 @@ true;
4138
4149
  }
4139
4150
  return token;
4140
4151
  }
4152
+ function currentToken(personId) {
4153
+ var _a;
4154
+ return (_a = cachedToken(personId)) !== null && _a !== void 0 ? _a : mintToken(personId).catch(() => null);
4155
+ }
4141
4156
  async function request(personId, key, init = {}, isRetry = false) {
4142
4157
  var _a;
4143
4158
  const token = isRetry ? await mintToken(personId) : (_a = cachedToken(personId)) !== null && _a !== void 0 ? _a : await mintToken(personId);
@@ -4173,8 +4188,8 @@ true;
4173
4188
  // retrying when another tab or device wrote in between. `change` returns null
4174
4189
  // when there's nothing to write. A read-only token (staff impersonating
4175
4190
  // someone) only reads, so nothing is written into the person's history.
4176
- // Resolves to the value now stored, or null when the service couldn't be
4177
- // reached.
4191
+ // Resolves to the value now stored, with whether the token was read-only, or
4192
+ // null when the service couldn't be reached.
4178
4193
  async function updateStoredValue(personId, key, change, attemptsLeft = MAX_ATTEMPTS) {
4179
4194
  const read = await request(personId, key);
4180
4195
  if (read === null || read === void 0 ? void 0 : read.response.ok) {
@@ -4182,9 +4197,15 @@ true;
4182
4197
  value,
4183
4198
  version
4184
4199
  } = await read.response.json();
4185
- const next = read.token.readOnly ? null : change(value);
4200
+ const {
4201
+ readOnly
4202
+ } = read.token;
4203
+ const next = readOnly ? null : change(value);
4186
4204
  if (next === null) {
4187
- return Array.isArray(value) ? value : [];
4205
+ return {
4206
+ readOnly,
4207
+ value: Array.isArray(value) ? value : []
4208
+ };
4188
4209
  } else {
4189
4210
  const write = await request(personId, key, {
4190
4211
  body: JSON.stringify({
@@ -4197,7 +4218,10 @@ true;
4197
4218
  method: "PUT"
4198
4219
  });
4199
4220
  if (write === null || write === void 0 ? void 0 : write.response.ok) {
4200
- return next;
4221
+ return {
4222
+ readOnly,
4223
+ value: next
4224
+ };
4201
4225
  } else if ((write === null || write === void 0 ? void 0 : write.response.status) === 409 && attemptsLeft > 1) {
4202
4226
  return updateStoredValue(personId, key, change, attemptsLeft - 1);
4203
4227
  } else {
@@ -4249,7 +4273,8 @@ true;
4249
4273
  return isSameList(mergeEntries(shared, local), shared) ? null : mergeStored(stored, local);
4250
4274
  });
4251
4275
  if (synced) {
4252
- writeRecentlyVisited(scope, mergeEntries(entriesFrom(synced), readRecentlyVisited(scope)));
4276
+ const shared = entriesFrom(synced.value);
4277
+ writeRecentlyVisited(scope, synced.readOnly ? shared : mergeEntries(shared, readRecentlyVisited(scope)));
4253
4278
  }
4254
4279
  } catch {
4255
4280
  // The service is best-effort; the local copy stands on its own.
@@ -4280,11 +4305,23 @@ true;
4280
4305
  function syncRecentlyVisited(scope) {
4281
4306
  return sync(scope, false);
4282
4307
  }
4308
+ function record(entry, scope, token) {
4309
+ if (!(token === null || token === void 0 ? void 0 : token.readOnly)) {
4310
+ writeRecentlyVisited(scope, addVisit(readRecentlyVisited(scope), entry));
4311
+ if (token) sync(scope, true);
4312
+ }
4313
+ }
4283
4314
  function recordVisit(visit, scope) {
4284
4315
  const entry = toEntry(visit, new Date());
4285
- if (isEntry(entry)) {
4286
- writeRecentlyVisited(scope, addVisit(readRecentlyVisited(scope), entry));
4287
- sync(scope, true);
4316
+ if (!isEntry(entry)) {
4317
+ return Promise.resolve();
4318
+ }
4319
+ const token = isUserStoreSyncUnavailable() ? null : currentToken(scope.personId);
4320
+ if (token instanceof Promise) {
4321
+ return token.then(minted => record(entry, scope, minted));
4322
+ } else {
4323
+ record(entry, scope, token);
4324
+ return Promise.resolve();
4288
4325
  }
4289
4326
  }
4290
4327
  function useRecentlyVisited({