@forgeroute/ui 0.4.83 → 0.4.84

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.
@@ -2,7 +2,7 @@ import * as i0 from "@angular/core";
2
2
  import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DestroyRef, ElementRef, Injectable, InjectionToken, computed, effect, inject, input, makeEnvironmentProviders, output, provideAppInitializer, signal, untracked, viewChild } from "@angular/core";
3
3
  import { DOCUMENT } from "@angular/common";
4
4
  import { Subject, defer, firstValueFrom, from, of, throwError, timer } from "rxjs";
5
- import { ConfirmService, SidePanelComponent, ToastService, UiLabelsService, uiText } from "@forgeroute/ui";
5
+ import { ConfirmService, SidePanelComponent, ToastService, UI_NOTES_QUEUE, UiLabelsService, uiText } from "@forgeroute/ui";
6
6
  import { HttpClient, HttpContextToken, HttpErrorResponse, HttpHeaders, HttpParams, HttpResponse } from "@angular/common/http";
7
7
  import { catchError, finalize, retry, switchMap, tap, timeout } from "rxjs/operators";
8
8
  const IDEMPOTENCY_IN_PROGRESS_CODE = "IDEMPOTENCY_IN_PROGRESS";
@@ -1160,7 +1160,7 @@ function splitUrl(url) {
1160
1160
  hash
1161
1161
  };
1162
1162
  }
1163
- function decode(s) {
1163
+ function decode$1(s) {
1164
1164
  try {
1165
1165
  return decodeURIComponent(s);
1166
1166
  } catch {
@@ -1181,10 +1181,10 @@ function walkStrings(value, visit) {
1181
1181
  function referenceStrings(url, body) {
1182
1182
  const out = [];
1183
1183
  const parts = splitUrl(url);
1184
- for (const seg of parts.path.split("/")) if (seg !== "") out.push(decode(seg));
1184
+ for (const seg of parts.path.split("/")) if (seg !== "") out.push(decode$1(seg));
1185
1185
  if (parts.query !== null) for (const pair of parts.query.split("&")) {
1186
1186
  const eq = pair.indexOf("=");
1187
- if (eq >= 0) out.push(decode(pair.slice(eq + 1)));
1187
+ if (eq >= 0) out.push(decode$1(pair.slice(eq + 1)));
1188
1188
  }
1189
1189
  walkStrings(body, (s) => out.push(s));
1190
1190
  return out;
@@ -1217,14 +1217,14 @@ function rewriteTempIds(url, body, map) {
1217
1217
  };
1218
1218
  const parts = splitUrl(url);
1219
1219
  const path = parts.path.split("/").map((seg) => {
1220
- const d = decode(seg);
1220
+ const d = decode$1(seg);
1221
1221
  return map.has(d) ? encodeURIComponent(String(map.get(d))) : seg;
1222
1222
  }).join("/");
1223
1223
  let query = parts.query;
1224
1224
  if (query !== null) query = query.split("&").map((pair) => {
1225
1225
  const eq = pair.indexOf("=");
1226
1226
  if (eq < 0) return pair;
1227
- const d = decode(pair.slice(eq + 1));
1227
+ const d = decode$1(pair.slice(eq + 1));
1228
1228
  return map.has(d) ? `${pair.slice(0, eq)}=${encodeURIComponent(String(map.get(d)))}` : pair;
1229
1229
  }).join("&");
1230
1230
  return {
@@ -1409,9 +1409,9 @@ var OfflineReplayer = class OfflineReplayer {
1409
1409
  async discard(id) {
1410
1410
  await this.store.init();
1411
1411
  const userId = this.cfg.getUserId();
1412
- if (!userId) return;
1412
+ if (!userId) return false;
1413
1413
  const row = await this.store.get(id);
1414
- if (!row || row.userId !== userId) return;
1414
+ if (!row || row.userId !== userId) return false;
1415
1415
  await this.store.delete(id);
1416
1416
  await this.refreshCounts();
1417
1417
  if ((row.status === "failed" || row.status === "conflict") && row.entityType && row.entityId) try {
@@ -1422,6 +1422,7 @@ var OfflineReplayer = class OfflineReplayer {
1422
1422
  this.discardDrains.add(drain);
1423
1423
  }
1424
1424
  } catch {}
1425
+ return true;
1425
1426
  }
1426
1427
  async drainSettled() {
1427
1428
  while (this.discardDrains.size > 0) await Promise.all([...this.discardDrains]);
@@ -1927,6 +1928,7 @@ var OfflineSupportService = class OfflineSupportService {
1927
1928
  this.cleanups = [];
1928
1929
  this.retryTimer = null;
1929
1930
  this.destroyed = false;
1931
+ this.discardedIds = /* @__PURE__ */ new Map();
1930
1932
  }
1931
1933
  async start() {
1932
1934
  if (this.started) return;
@@ -2072,12 +2074,50 @@ var OfflineSupportService = class OfflineSupportService {
2072
2074
  if (!applied || !outcome) throw new KeepMineError("notConflict", this.labels.t("connection.conflict.error.notConflict"));
2073
2075
  return outcome;
2074
2076
  }
2077
+ static {
2078
+ this.DISCARD_MARK_MS = 6e4;
2079
+ }
2080
+ wasDiscarded(id) {
2081
+ const at = this.discardedIds.get(id);
2082
+ if (at === void 0) return false;
2083
+ if (Date.now() - at > OfflineSupportService.DISCARD_MARK_MS) {
2084
+ this.discardedIds.delete(id);
2085
+ return false;
2086
+ }
2087
+ return true;
2088
+ }
2089
+ markDiscarded(ids) {
2090
+ if (this.discardedIds.size > 500) this.discardedIds.clear();
2091
+ const now = Date.now();
2092
+ for (const id of ids) this.discardedIds.set(id, now);
2093
+ }
2075
2094
  async discard(id) {
2076
- await this.replayer.discard(id);
2095
+ const userId = this.cfg.getUserId();
2096
+ const existing = userId ? await this.store.get(id) : void 0;
2097
+ if (!existing || existing.userId !== userId) {
2098
+ await this.replayer.discard(id);
2099
+ await this.refresh();
2100
+ return;
2101
+ }
2102
+ this.markDiscarded([id]);
2103
+ try {
2104
+ if (!await this.replayer.discard(id)) this.discardedIds.delete(id);
2105
+ } catch (e) {
2106
+ this.discardedIds.delete(id);
2107
+ throw e;
2108
+ }
2077
2109
  await this.refresh();
2078
2110
  }
2079
2111
  async discardAll() {
2080
- await this.replayer.discardAll();
2112
+ const userId = this.cfg.getUserId();
2113
+ const ids = userId ? (await this.store.list(userId)).map((r) => r.id) : [];
2114
+ this.markDiscarded(ids);
2115
+ try {
2116
+ await this.replayer.discardAll();
2117
+ } catch (e) {
2118
+ for (const id of ids) this.discardedIds.delete(id);
2119
+ throw e;
2120
+ }
2081
2121
  await this.refresh();
2082
2122
  }
2083
2123
  async beforeSessionEnd() {
@@ -2350,18 +2390,135 @@ const offlineHttpInterceptors = [
2350
2390
  offlineInterceptor,
2351
2391
  connectionInterceptor
2352
2392
  ];
2353
- function provideOfflineSupport(config) {
2354
- return makeEnvironmentProviders([{
2355
- provide: OFFLINE_SUPPORT_CONFIG,
2356
- useValue: config
2357
- }, provideAppInitializer(() => {
2358
- const store = inject(OfflineStore);
2359
- const support = inject(OfflineSupportService);
2360
- inject(ConnectionStateService);
2361
- return store.init().then(() => {
2362
- support.start().catch(() => void 0);
2393
+ const STATE = {
2394
+ pending: "waiting",
2395
+ syncing: "sending",
2396
+ failed: "failed",
2397
+ conflict: "conflict"
2398
+ };
2399
+ function textOf(body) {
2400
+ let b = body;
2401
+ if (typeof b === "string") try {
2402
+ b = JSON.parse(b);
2403
+ } catch {
2404
+ return "";
2405
+ }
2406
+ if (b === null || typeof b !== "object") return "";
2407
+ const o = b;
2408
+ return (typeof o.body === "string" ? o.body : typeof o.content === "string" ? o.content : "").trim();
2409
+ }
2410
+ function decode(segment) {
2411
+ try {
2412
+ return decodeURIComponent(segment);
2413
+ } catch {
2414
+ return segment;
2415
+ }
2416
+ }
2417
+ function segments(url) {
2418
+ return pathOnly(url).split("/").filter((s) => s !== "").map(decode);
2419
+ }
2420
+ function startsWith(path, prefix) {
2421
+ return path.length >= prefix.length && prefix.every((s, i) => path[i] === s);
2422
+ }
2423
+ var OfflineNotesQueue = class OfflineNotesQueue {
2424
+ constructor() {
2425
+ this.support = inject(OfflineSupportService);
2426
+ this.connection = inject(ConnectionStateService);
2427
+ this.offline = this.connection.isOffline;
2428
+ }
2429
+ wasDiscarded(rowId) {
2430
+ return this.support.wasDiscarded(rowId);
2431
+ }
2432
+ queuedFor(listPath, itemPrefix) {
2433
+ const list = segments(listPath);
2434
+ const prefix = segments(itemPrefix);
2435
+ const out = [];
2436
+ for (const row of this.support.rows()) {
2437
+ const queued = this.toQueued(row, list, prefix);
2438
+ if (queued) out.push(queued);
2439
+ }
2440
+ return out;
2441
+ }
2442
+ toQueued(row, list, prefix) {
2443
+ const path = segments(row.url);
2444
+ const method = row.method.toUpperCase();
2445
+ const base = {
2446
+ rowId: row.id,
2447
+ createdAt: row.createdAt,
2448
+ state: STATE[row.status]
2449
+ };
2450
+ if (method === "POST" && path.length === list.length && startsWith(path, list)) return {
2451
+ ...base,
2452
+ kind: "add",
2453
+ noteId: "",
2454
+ text: textOf(row.body)
2455
+ };
2456
+ if ((method === "PUT" || method === "PATCH" || method === "DELETE") && startsWith(path, prefix)) {
2457
+ if (path.length !== prefix.length + 1) return null;
2458
+ const id = path[prefix.length];
2459
+ return method === "DELETE" ? {
2460
+ ...base,
2461
+ kind: "delete",
2462
+ noteId: id,
2463
+ text: ""
2464
+ } : {
2465
+ ...base,
2466
+ kind: "edit",
2467
+ noteId: id,
2468
+ text: textOf(row.body)
2469
+ };
2470
+ }
2471
+ return null;
2472
+ }
2473
+ static {
2474
+ this.ɵfac = i0.ɵɵngDeclareFactory({
2475
+ minVersion: "12.0.0",
2476
+ version: "22.2.2",
2477
+ ngImport: i0,
2478
+ type: OfflineNotesQueue,
2479
+ deps: [],
2480
+ target: i0.ɵɵFactoryTarget.Injectable
2363
2481
  });
2364
- })]);
2482
+ }
2483
+ static {
2484
+ this.ɵprov = i0.ɵɵngDeclareInjectable({
2485
+ minVersion: "12.0.0",
2486
+ version: "22.2.2",
2487
+ ngImport: i0,
2488
+ type: OfflineNotesQueue,
2489
+ providedIn: "root"
2490
+ });
2491
+ }
2492
+ };
2493
+ i0.ɵɵngDeclareClassMetadata({
2494
+ minVersion: "12.0.0",
2495
+ version: "22.2.2",
2496
+ ngImport: i0,
2497
+ type: OfflineNotesQueue,
2498
+ decorators: [{
2499
+ type: Injectable,
2500
+ args: [{ providedIn: "root" }]
2501
+ }]
2502
+ });
2503
+ function provideOfflineSupport(config) {
2504
+ return makeEnvironmentProviders([
2505
+ {
2506
+ provide: OFFLINE_SUPPORT_CONFIG,
2507
+ useValue: config
2508
+ },
2509
+ {
2510
+ provide: UI_NOTES_QUEUE,
2511
+ useExisting: OfflineNotesQueue
2512
+ },
2513
+ provideAppInitializer(() => {
2514
+ const store = inject(OfflineStore);
2515
+ const support = inject(OfflineSupportService);
2516
+ inject(ConnectionStateService);
2517
+ return store.init().then(() => {
2518
+ support.start().catch(() => void 0);
2519
+ });
2520
+ })
2521
+ ]);
2365
2522
  }
2366
2523
  const SYNCED_FLASH_MS = 3e3;
2367
2524
  let nextId = 0;
@@ -3286,6 +3443,6 @@ i0.ɵɵngDeclareClassMetadata({
3286
3443
  }]
3287
3444
  }
3288
3445
  });
3289
- export { ConnectionBannerComponent, ConnectionStateService, DEFAULT_DETAIL_TTL_MS, DEFAULT_LIST_TTL_MS, FROM_CACHE_HEADER, IDEMPOTENCY_HEADER, IDEMPOTENCY_IN_PROGRESS_CODE, IDMAP_TTL_MS, KeepMineError, LEASE_TTL_MS, OFFLINE_IDB_FACTORY, OFFLINE_LEASE_STORAGE, OFFLINE_LOCK_MANAGER, OFFLINE_REPLAY_HEADER, OFFLINE_SUPPORT_CONFIG, ONLINE_DEBOUNCE_MS, OfflineBlockedError, OfflineReplayer, OfflineStore, OfflineSupportService, PendingChangesDrawerComponent, QueueFullError, REQUEST_LONG, REQUEST_TIMEOUT_MS, RequestTimeoutError, STALE_CLAIM_MS, SYNCED_FLASH_MS, StaleDataChipComponent, StaleDataService, StillWorkingComponent, StorageUnavailableError, backoffMs, classifyReplay, connectionInterceptor, createChannel, defaultCreatedId, deriveEntity, generateUuid, hasStampField, idempotencyInterceptor, invalidationPrefix, isClaimable, isInScope, isMutationMethod, isOfflineBlockedError, isQuotaError, isRequestTimeoutError, lastSegmentIsId, matchesAny, matchesUrl, offlineHttpInterceptors, offlineInterceptor, pathAndQuery, pathOnly, provideOfflineSupport, resolveConfig, resolveConfigCached, stripStampFields };
3446
+ export { ConnectionBannerComponent, ConnectionStateService, DEFAULT_DETAIL_TTL_MS, DEFAULT_LIST_TTL_MS, FROM_CACHE_HEADER, IDEMPOTENCY_HEADER, IDEMPOTENCY_IN_PROGRESS_CODE, IDMAP_TTL_MS, KeepMineError, LEASE_TTL_MS, OFFLINE_IDB_FACTORY, OFFLINE_LEASE_STORAGE, OFFLINE_LOCK_MANAGER, OFFLINE_REPLAY_HEADER, OFFLINE_SUPPORT_CONFIG, ONLINE_DEBOUNCE_MS, OfflineBlockedError, OfflineNotesQueue, OfflineReplayer, OfflineStore, OfflineSupportService, PendingChangesDrawerComponent, QueueFullError, REQUEST_LONG, REQUEST_TIMEOUT_MS, RequestTimeoutError, STALE_CLAIM_MS, SYNCED_FLASH_MS, StaleDataChipComponent, StaleDataService, StillWorkingComponent, StorageUnavailableError, backoffMs, classifyReplay, connectionInterceptor, createChannel, defaultCreatedId, deriveEntity, generateUuid, hasStampField, idempotencyInterceptor, invalidationPrefix, isClaimable, isInScope, isMutationMethod, isOfflineBlockedError, isQuotaError, isRequestTimeoutError, lastSegmentIsId, matchesAny, matchesUrl, offlineHttpInterceptors, offlineInterceptor, pathAndQuery, pathOnly, provideOfflineSupport, resolveConfig, resolveConfigCached, stripStampFields };
3290
3447
 
3291
3448
  //# sourceMappingURL=forgeroute-ui-offline.mjs.map