@rdlabo/ionic-angular-kit 21.4.5 → 21.4.6
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/README.md +132 -18
- package/fesm2022/rdlabo-ionic-angular-kit-offline.mjs +152 -16
- package/fesm2022/rdlabo-ionic-angular-kit-offline.mjs.map +1 -1
- package/fesm2022/rdlabo-ionic-angular-kit.mjs +371 -39
- package/fesm2022/rdlabo-ionic-angular-kit.mjs.map +1 -1
- package/package.json +1 -1
- package/types/rdlabo-ionic-angular-kit-offline.d.ts +73 -24
- package/types/rdlabo-ionic-angular-kit.d.ts +147 -17
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A small ergonomic kit for Ionic Angular applications. It provides:
|
|
|
4
4
|
|
|
5
5
|
- **KitStorageService** — a typed, write-loss-safe wrapper around `@ionic/storage-angular`
|
|
6
6
|
- **KitOverlayController** — a unified presenter for Ionic Modal, Toast, and Alert
|
|
7
|
-
- **Auth guards** — functional `
|
|
7
|
+
- **Auth guards** — functional guards plus shared `none` / `local` / `remote` runtime access
|
|
8
8
|
- **HTTP interceptor** — a fleet-canonical auth + retry + error-hook interceptor
|
|
9
9
|
- **KitRealtimeConnection** — foreground/network-aware Hibernation WebSocket reconnect and resync
|
|
10
10
|
- **KitAuthInputDirective** — sign-in email remember/prefill + iOS autofill workaround for `ion-input`
|
|
@@ -59,6 +59,8 @@ authentication and the stable `KIT_REALTIME_CLIENT_ID` through WebSocket subprot
|
|
|
59
59
|
putting credentials in the URL.
|
|
60
60
|
|
|
61
61
|
Domain event types, authorization, room selection, and REST resync behavior remain in the app.
|
|
62
|
+
Offline-capable authenticated clients set `requireRemoteAccess: true` in `realtimeOptions`; sockets then close on
|
|
63
|
+
`local` / `none` and reopen only after `KitAuthAccessService` publishes `remote`.
|
|
62
64
|
|
|
63
65
|
---
|
|
64
66
|
|
|
@@ -227,16 +229,42 @@ This centralizes presentation options, keeps component props and dismiss data ty
|
|
|
227
229
|
|
|
228
230
|
### Auth guards + provideKitAuth
|
|
229
231
|
|
|
230
|
-
Functional `CanActivateFn` guards for a
|
|
231
|
-
|
|
232
|
-
| State
|
|
233
|
-
|
|
|
234
|
-
| `'user'`
|
|
235
|
-
| `'confirm'`
|
|
236
|
-
| `'required'`
|
|
237
|
-
| `'anonymous'`
|
|
238
|
-
|
|
239
|
-
|
|
232
|
+
Functional `CanActivateFn` guards for a five-state auth model:
|
|
233
|
+
|
|
234
|
+
| State | Meaning |
|
|
235
|
+
| --------------- | -------------------------------------------------------- |
|
|
236
|
+
| `'user'` | Fully authenticated |
|
|
237
|
+
| `'confirm'` | Authenticated but email confirmation pending |
|
|
238
|
+
| `'required'` | Not authenticated |
|
|
239
|
+
| `'anonymous'` | Anonymous login active (can be prompted to register) |
|
|
240
|
+
| `'unavailable'` | The authentication authority cannot currently be reached |
|
|
241
|
+
|
|
242
|
+
**Convention:** every redirect path is supplied via `provideKitAuth`; the kit does not hard-code any routes.
|
|
243
|
+
`authState` and `redirects` are required. The app-specific hooks `onAuthorized`, `onUnauthenticated`, and
|
|
244
|
+
`onUnavailable` are optional. An authenticated user is allowed by default; unauthenticated and unavailable states
|
|
245
|
+
redirect by default.
|
|
246
|
+
|
|
247
|
+
`'required'` is an authoritative signed-out result. It must never be converted into offline access.
|
|
248
|
+
`'unavailable'` means the authentication authority could not produce a result. Likewise,
|
|
249
|
+
`isUnavailableError` must classify transport failures only; HTTP 401/403 are explicit denials and must return
|
|
250
|
+
`false`. `onUnavailable` authorizes the route for local-replica use only—it does not create an HTTP or realtime
|
|
251
|
+
credential.
|
|
252
|
+
|
|
253
|
+
`KitAuthAccessService` is the authoritative capability state for the rest of the application:
|
|
254
|
+
|
|
255
|
+
| Access mode | Local replica / outbox | Authenticated HTTP / realtime / sync |
|
|
256
|
+
| ----------- | ---------------------- | ------------------------------------ |
|
|
257
|
+
| `none` | blocked | blocked |
|
|
258
|
+
| `local` | allowed | blocked |
|
|
259
|
+
| `remote` | allowed | allowed |
|
|
260
|
+
|
|
261
|
+
Remote activation has two ordered phases. `activate()` installs the remotely verified identity without starting
|
|
262
|
+
transport. The guard then publishes `remote`, and only then calls `resume()` to start pull, outbox replay, and
|
|
263
|
+
realtime work. Returning plain `true` remains supported for applications that do not need phased activation.
|
|
264
|
+
When a protected guard starts a new asynchronous decision, any previously published `remote` capability is
|
|
265
|
+
immediately suspended to `none`; it is granted again only after the current lease completes successfully.
|
|
266
|
+
Once the authority returns `required` or `confirm`, an existing `local` capability is also suspended before any
|
|
267
|
+
anonymous-sign-in fallback runs. Only the `unavailable` path may retain or re-grant verified local access.
|
|
240
268
|
|
|
241
269
|
**Setup**
|
|
242
270
|
|
|
@@ -257,10 +285,38 @@ export const appConfig: ApplicationConfig = {
|
|
|
257
285
|
whenUnauthorized: '/auth', // kitRequireAuthorizedGuard
|
|
258
286
|
},
|
|
259
287
|
// onAuthorized / onUnauthenticated omitted → defaults (allow / redirect).
|
|
260
|
-
// Supply onAuthorized only when 'user' needs extra work
|
|
261
|
-
//
|
|
288
|
+
// Supply onAuthorized only when 'user' needs extra work. A phased result is preferred when
|
|
289
|
+
// activating the offline runtime:
|
|
290
|
+
// onAuthorized: async () => {
|
|
291
|
+
// const session = await auth.exchangeCredential();
|
|
292
|
+
// return {
|
|
293
|
+
// activate: (lease) => offline.prepareRemoteSession(
|
|
294
|
+
// session.userId, session.groupIds, session.subject, lease,
|
|
295
|
+
// ),
|
|
296
|
+
// resume: () => offline.resumeRemoteSession(),
|
|
297
|
+
// };
|
|
298
|
+
// },
|
|
262
299
|
// Supply onUnauthenticated only for a fallback such as anonymous sign-in:
|
|
263
300
|
// onUnauthenticated: async () => { await auth.signInAnonymously(); return true; },
|
|
301
|
+
// Supply onUnavailable only for a previously verified local replica:
|
|
302
|
+
// onUnavailable: async (_state, _error, lease) =>
|
|
303
|
+
// (await offline.activateOfflineSession(auth.currentSubject(), lease)) !== null,
|
|
304
|
+
// isUnavailableError: (error) => isOfflineFallbackError(error),
|
|
305
|
+
// Optionally recover automatically after the authority is reachable again:
|
|
306
|
+
// remoteRecovery: {
|
|
307
|
+
// availability: () => auth.authorityAvailable$,
|
|
308
|
+
// reauthenticate: async () => {
|
|
309
|
+
// const session = await auth.tryExchangeCredential();
|
|
310
|
+
// return session
|
|
311
|
+
// ? {
|
|
312
|
+
// activate: (lease) => offline.prepareRemoteSession(
|
|
313
|
+
// session.userId, session.groupIds, session.subject, lease,
|
|
314
|
+
// ),
|
|
315
|
+
// resume: () => offline.resumeRemoteSession(),
|
|
316
|
+
// }
|
|
317
|
+
// : false;
|
|
318
|
+
// },
|
|
319
|
+
// },
|
|
264
320
|
};
|
|
265
321
|
}),
|
|
266
322
|
],
|
|
@@ -341,6 +397,54 @@ Mutations are queued explicitly with `OfflineSyncService.enqueue`, not through H
|
|
|
341
397
|
Web storage uses Ionic Storage; iOS and Android use encrypted `@capacitor-community/sqlite`. Importing either the
|
|
342
398
|
primary entry point or `/offline` does not pull the optional native SQLite plugin into web-only applications.
|
|
343
399
|
|
|
400
|
+
For cold-start offline route access, `OfflineCoordinatorService.activateOfflineSession()` restores only a manifest
|
|
401
|
+
that is bound to a non-null authentication-provider subject. Supplying a currently known subject also rejects a
|
|
402
|
+
different user on a shared device. It activates local replica writes and durable outbox enqueue, but remote pull and
|
|
403
|
+
command replay remain disabled until online authentication completes the ordered
|
|
404
|
+
`prepareRemoteSession(...)` → publish `remote` → `resumeRemoteSession()` transition. `activateSession(...)` remains
|
|
405
|
+
available as a backward-compatible one-step API for callers that do not enforce shared access mode.
|
|
406
|
+
Explicit sign-out must first call `KitAuthAccessService.clear()` and then await `clearActiveSession()`. The first
|
|
407
|
+
step immediately invalidates every in-flight auth lease; the second serializes cleanup after local persistence
|
|
408
|
+
already in progress and removes the manifest and user replica.
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
provideKitAuth(() => {
|
|
412
|
+
const offline = inject(OfflineCoordinatorService);
|
|
413
|
+
return {
|
|
414
|
+
authState: () => auth.state$,
|
|
415
|
+
onAuthorized: async () => {
|
|
416
|
+
const session = await auth.exchangeCredential();
|
|
417
|
+
return {
|
|
418
|
+
activate: (lease) => offline.prepareRemoteSession(session.userId, session.groupIds, session.subject, lease),
|
|
419
|
+
resume: () => offline.resumeRemoteSession(),
|
|
420
|
+
};
|
|
421
|
+
},
|
|
422
|
+
onUnavailable: async (_state, _error, lease) =>
|
|
423
|
+
(await offline.activateOfflineSession(auth.currentSubject(), lease)) !== null,
|
|
424
|
+
isUnavailableError: isOfflineFallbackError,
|
|
425
|
+
remoteRecovery: {
|
|
426
|
+
availability: () => auth.authorityAvailable$,
|
|
427
|
+
reauthenticate: async () => {
|
|
428
|
+
const session = await auth.tryExchangeCredential();
|
|
429
|
+
return session
|
|
430
|
+
? {
|
|
431
|
+
activate: (lease) =>
|
|
432
|
+
offline.prepareRemoteSession(session.userId, session.groupIds, session.subject, lease),
|
|
433
|
+
resume: () => offline.resumeRemoteSession(),
|
|
434
|
+
}
|
|
435
|
+
: false;
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
redirects,
|
|
439
|
+
};
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
Register `offlineInterceptor` before `kitAuthInterceptor`. In local mode the auth interceptor synthesizes a
|
|
444
|
+
transport-unavailable error before generating credentials or touching the network; the outer offline interceptor
|
|
445
|
+
may then serve a matched `GET` from the replica. In `none` mode the same request is rejected and no local data is
|
|
446
|
+
returned.
|
|
447
|
+
|
|
344
448
|
The offline interceptor observes real transport responses to update API reachability. For matched `GET`
|
|
345
449
|
requests only, a transport failure with `status=0` may return a local replica response tagged
|
|
346
450
|
`X-Offline-Response: local`. `POST` and other write methods always go to transport unchanged; outbox replay
|
|
@@ -541,6 +645,9 @@ export const appConfig: ApplicationConfig = {
|
|
|
541
645
|
const auth = inject(AuthService);
|
|
542
646
|
const reload = inject(KitReloadAlertController);
|
|
543
647
|
return {
|
|
648
|
+
// Required for the new offline auth boundary. Kept opt-in so existing applications retain
|
|
649
|
+
// their current interceptor behavior until they wire KitAuthAccessService.
|
|
650
|
+
enforceAuthAccessMode: true,
|
|
544
651
|
getAuthHeaders: async (req) => ({
|
|
545
652
|
Authorization: `Bearer ${await auth.getToken()}`,
|
|
546
653
|
}),
|
|
@@ -561,13 +668,20 @@ export const appConfig: ApplicationConfig = {
|
|
|
561
668
|
};
|
|
562
669
|
```
|
|
563
670
|
|
|
671
|
+
For an offline replica, use
|
|
672
|
+
`withInterceptors([offlineInterceptor, kitAuthInterceptor])` in that order. Authentication/bootstrap endpoints that
|
|
673
|
+
must run before `remote` is granted must be explicitly covered by `bypass`; do not globally relax
|
|
674
|
+
`enforceAuthAccessMode`.
|
|
675
|
+
|
|
564
676
|
**Error dispatch** (after retries, in `catchError`):
|
|
565
677
|
|
|
566
|
-
1. `
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
678
|
+
1. With `enforceAuthAccessMode`, `401` / `403` → revoke access, notify the matching hook, and reject
|
|
679
|
+
without consulting `offlineFallback`
|
|
680
|
+
2. Otherwise, `offlineFallback` non-null → return fallback observable (no further hooks called)
|
|
681
|
+
3. `401` → `onUnauthorized` · `403` → `onForbidden`
|
|
682
|
+
4. `0` (connected) → `onNetworkError` · `429` → `onRateLimited(retryAfter?)` · `502/503/504` → `onServerBusy(status, retryAfter?)`
|
|
683
|
+
5. `400/422/500` with `error.message` → `onServerError`
|
|
684
|
+
6. anything else (`404`, …) → not handled here; the caller decides
|
|
571
685
|
|
|
572
686
|
Plus: a `getAuthHeaders` rejection → `onAuthError(request, error)` (the request is never sent).
|
|
573
687
|
|
|
@@ -1328,13 +1328,15 @@ class OfflineSessionService {
|
|
|
1328
1328
|
this.#repository = inject(OFFLINE_REPOSITORY);
|
|
1329
1329
|
this.#activeManifest = signal(null, ...(ngDevMode ? [{ debugName: "#activeManifest" }] : /* istanbul ignore next */ []));
|
|
1330
1330
|
this.#initialized = false;
|
|
1331
|
-
this.#
|
|
1331
|
+
this.#localAccessThisRun = false;
|
|
1332
|
+
this.#remoteActivatedThisRun = false;
|
|
1332
1333
|
this.activeManifest = this.#activeManifest.asReadonly();
|
|
1333
1334
|
}
|
|
1334
1335
|
#repository;
|
|
1335
1336
|
#activeManifest;
|
|
1336
1337
|
#initialized;
|
|
1337
|
-
#
|
|
1338
|
+
#localAccessThisRun;
|
|
1339
|
+
#remoteActivatedThisRun;
|
|
1338
1340
|
async initialize() {
|
|
1339
1341
|
if (this.#initialized)
|
|
1340
1342
|
return;
|
|
@@ -1346,23 +1348,31 @@ class OfflineSessionService {
|
|
|
1346
1348
|
}
|
|
1347
1349
|
this.#initialized = true;
|
|
1348
1350
|
}
|
|
1349
|
-
async activateSession(userId, scopeIds, authSubject) {
|
|
1351
|
+
async activateSession(userId, scopeIds, authSubject, lease) {
|
|
1350
1352
|
await this.initialize();
|
|
1353
|
+
if (lease && !lease.isCurrent())
|
|
1354
|
+
return false;
|
|
1351
1355
|
const normalizedScopeIds = [...new Set(scopeIds)].filter((id) => id !== 0).sort((a, b) => a - b);
|
|
1352
1356
|
const previousUserId = await this.#repository.getLastUserId();
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1357
|
+
if (lease && !lease.isCurrent())
|
|
1358
|
+
return false;
|
|
1359
|
+
let previous = previousUserId === userId ? ((await this.#repository.getSessionManifest(userId)) ?? null) : null;
|
|
1360
|
+
if (lease && !lease.isCurrent())
|
|
1361
|
+
return false;
|
|
1356
1362
|
// A changed provider subject is a different person even when the product reuses its numeric id.
|
|
1357
1363
|
// This deliberately also clears legacy null -> known subject and known subject -> null transitions.
|
|
1358
1364
|
if (previousUserId !== null && (previousUserId !== userId || previous?.authSubject !== authSubject)) {
|
|
1359
1365
|
await this.#repository.clearUser(previousUserId);
|
|
1366
|
+
if (lease && !lease.isCurrent())
|
|
1367
|
+
return false;
|
|
1360
1368
|
previous = null;
|
|
1361
1369
|
}
|
|
1362
1370
|
const active = new Set(normalizedScopeIds);
|
|
1363
1371
|
await Promise.all((previous?.scopeIds ?? [])
|
|
1364
1372
|
.filter((groupId) => !active.has(groupId))
|
|
1365
1373
|
.map((groupId) => this.#repository.clearGroup({ userId, groupId })));
|
|
1374
|
+
if (lease && !lease.isCurrent())
|
|
1375
|
+
return false;
|
|
1366
1376
|
const manifest = {
|
|
1367
1377
|
userId,
|
|
1368
1378
|
scopeIds: normalizedScopeIds,
|
|
@@ -1370,9 +1380,15 @@ class OfflineSessionService {
|
|
|
1370
1380
|
updatedAt: Date.now(),
|
|
1371
1381
|
};
|
|
1372
1382
|
await this.#repository.setLastUserId(userId);
|
|
1383
|
+
if (lease && !lease.isCurrent())
|
|
1384
|
+
return false;
|
|
1373
1385
|
await this.#repository.putSessionManifest(userId, manifest);
|
|
1386
|
+
if (lease && !lease.isCurrent())
|
|
1387
|
+
return false;
|
|
1374
1388
|
this.#activeManifest.set(manifest);
|
|
1375
|
-
this.#
|
|
1389
|
+
this.#localAccessThisRun = true;
|
|
1390
|
+
this.#remoteActivatedThisRun = true;
|
|
1391
|
+
return lease ? true : undefined;
|
|
1376
1392
|
}
|
|
1377
1393
|
async clearActiveSession() {
|
|
1378
1394
|
await this.initialize();
|
|
@@ -1380,12 +1396,62 @@ class OfflineSessionService {
|
|
|
1380
1396
|
if (userId !== null)
|
|
1381
1397
|
await this.#repository.clearUser(userId);
|
|
1382
1398
|
this.#activeManifest.set(null);
|
|
1383
|
-
this.#
|
|
1399
|
+
this.#localAccessThisRun = false;
|
|
1400
|
+
this.#remoteActivatedThisRun = false;
|
|
1384
1401
|
}
|
|
1385
|
-
|
|
1402
|
+
/** Disable remote pull/replay eligibility while retaining the verified local manifest. */
|
|
1403
|
+
async suspendRemoteSession() {
|
|
1404
|
+
await this.initialize();
|
|
1405
|
+
this.#remoteActivatedThisRun = false;
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Returns the persisted identity boundary for local-only route access.
|
|
1409
|
+
*
|
|
1410
|
+
* @remarks
|
|
1411
|
+
* This does not activate the sync context. Call it only after the authentication authority has
|
|
1412
|
+
* been classified as unavailable, never after explicit sign-out or HTTP 401/403. Legacy
|
|
1413
|
+
* manifests without an authentication-provider subject are rejected.
|
|
1414
|
+
*
|
|
1415
|
+
* @param authSubject - A currently known provider subject. When supplied, it must match the
|
|
1416
|
+
* persisted subject.
|
|
1417
|
+
*/
|
|
1418
|
+
async getOfflineAccessManifest(authSubject) {
|
|
1386
1419
|
await this.initialize();
|
|
1387
|
-
|
|
1420
|
+
const manifest = this.#activeManifest();
|
|
1421
|
+
if (!manifest?.authSubject || (authSubject !== undefined && manifest.authSubject !== authSubject)) {
|
|
1388
1422
|
return null;
|
|
1423
|
+
}
|
|
1424
|
+
return { ...manifest, scopeIds: [...manifest.scopeIds] };
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Activates a previously verified manifest for local replica and outbox access only.
|
|
1428
|
+
*
|
|
1429
|
+
* @remarks
|
|
1430
|
+
* This never enables pull or command replay. A later successful remote authentication must call
|
|
1431
|
+
* {@link activateSession} before synchronization can use transport.
|
|
1432
|
+
*
|
|
1433
|
+
* @param authSubject - A currently known provider subject. When supplied, it must match the
|
|
1434
|
+
* persisted subject.
|
|
1435
|
+
*/
|
|
1436
|
+
async activateOfflineSession(authSubject, lease) {
|
|
1437
|
+
const manifest = await this.getOfflineAccessManifest(authSubject);
|
|
1438
|
+
if (lease && !lease.isCurrent())
|
|
1439
|
+
return null;
|
|
1440
|
+
this.#localAccessThisRun = manifest !== null;
|
|
1441
|
+
this.#remoteActivatedThisRun = false;
|
|
1442
|
+
return manifest;
|
|
1443
|
+
}
|
|
1444
|
+
/** Returns the session allowed to use the local replica and append outbox commands. */
|
|
1445
|
+
async getLocalSession() {
|
|
1446
|
+
await this.initialize();
|
|
1447
|
+
return this.#localAccessThisRun ? this.#sessionFromManifest() : null;
|
|
1448
|
+
}
|
|
1449
|
+
/** Returns the remotely authenticated session eligible for pull and command replay. */
|
|
1450
|
+
async getSession() {
|
|
1451
|
+
await this.initialize();
|
|
1452
|
+
return this.#remoteActivatedThisRun ? this.#sessionFromManifest() : null;
|
|
1453
|
+
}
|
|
1454
|
+
#sessionFromManifest() {
|
|
1389
1455
|
const manifest = this.#activeManifest();
|
|
1390
1456
|
return manifest
|
|
1391
1457
|
? { userId: manifest.userId, scopes: manifest.scopeIds.map((groupId) => ({ userId: manifest.userId, groupId })) }
|
|
@@ -1492,6 +1558,13 @@ class OfflineSyncService {
|
|
|
1492
1558
|
if (this.#network.connected())
|
|
1493
1559
|
this.#flushInBackground();
|
|
1494
1560
|
}
|
|
1561
|
+
/** Restores local outbox visibility without enabling pull or replay transport. */
|
|
1562
|
+
async refreshLocalSession() {
|
|
1563
|
+
await this.initialize();
|
|
1564
|
+
await this.#discoverLocalScopes();
|
|
1565
|
+
await this.#restoreInterruptedCommands();
|
|
1566
|
+
await this.#refreshState();
|
|
1567
|
+
}
|
|
1495
1568
|
async resetSession() {
|
|
1496
1569
|
this.#invalidateFlush();
|
|
1497
1570
|
await this.#waitForSendingTransitions();
|
|
@@ -1508,7 +1581,7 @@ class OfflineSyncService {
|
|
|
1508
1581
|
}
|
|
1509
1582
|
async #enqueue(request, options) {
|
|
1510
1583
|
await this.initialize();
|
|
1511
|
-
const session = await this.#
|
|
1584
|
+
const session = await this.#getLocalSession();
|
|
1512
1585
|
if (!session)
|
|
1513
1586
|
throw new Error('Cannot enqueue an offline command without an authenticated user');
|
|
1514
1587
|
const userId = session.userId;
|
|
@@ -1833,6 +1906,19 @@ class OfflineSyncService {
|
|
|
1833
1906
|
}
|
|
1834
1907
|
async #discoverScopes(generation = this.#generation) {
|
|
1835
1908
|
const session = await this.#context.getSession();
|
|
1909
|
+
if (!this.#isCurrent(generation))
|
|
1910
|
+
return false;
|
|
1911
|
+
if (!session) {
|
|
1912
|
+
return false;
|
|
1913
|
+
}
|
|
1914
|
+
this.#setActiveUser(session.userId);
|
|
1915
|
+
this.#knownScopes.clear();
|
|
1916
|
+
for (const scope of session.scopes)
|
|
1917
|
+
this.#knownScopes.set(this.#scopeKey(scope), scope);
|
|
1918
|
+
return true;
|
|
1919
|
+
}
|
|
1920
|
+
async #discoverLocalScopes(generation = this.#generation) {
|
|
1921
|
+
const session = await this.#getLocalSession();
|
|
1836
1922
|
if (!this.#isCurrent(generation))
|
|
1837
1923
|
return false;
|
|
1838
1924
|
if (!session) {
|
|
@@ -1846,6 +1932,9 @@ class OfflineSyncService {
|
|
|
1846
1932
|
this.#knownScopes.set(this.#scopeKey(scope), scope);
|
|
1847
1933
|
return true;
|
|
1848
1934
|
}
|
|
1935
|
+
#getLocalSession() {
|
|
1936
|
+
return this.#context.getLocalSession?.() ?? this.#context.getSession();
|
|
1937
|
+
}
|
|
1849
1938
|
#setActiveUser(userId) {
|
|
1850
1939
|
if (this.#activeUserId === userId)
|
|
1851
1940
|
return;
|
|
@@ -1964,6 +2053,8 @@ class OfflineCoordinatorService {
|
|
|
1964
2053
|
this.#network = inject(OfflineNetworkService);
|
|
1965
2054
|
this.#sync = inject(OfflineSyncService);
|
|
1966
2055
|
this.#session = inject(OfflineSessionService);
|
|
2056
|
+
this.#transitionRevision = 0;
|
|
2057
|
+
this.#transitionTail = Promise.resolve();
|
|
1967
2058
|
this.networkState = this.#network.state;
|
|
1968
2059
|
this.syncState = this.#sync.syncState;
|
|
1969
2060
|
this.pendingCount = this.#sync.pendingCount;
|
|
@@ -1973,19 +2064,56 @@ class OfflineCoordinatorService {
|
|
|
1973
2064
|
#network;
|
|
1974
2065
|
#sync;
|
|
1975
2066
|
#session;
|
|
2067
|
+
#transitionRevision;
|
|
2068
|
+
#transitionTail;
|
|
1976
2069
|
async initialize() {
|
|
1977
2070
|
await Promise.all([this.#repository.initialize(), this.#network.initialize()]);
|
|
1978
2071
|
await this.#session.initialize();
|
|
1979
2072
|
await this.#sync.initialize();
|
|
1980
2073
|
}
|
|
1981
2074
|
async activateSession(userId, scopeIds, authSubject) {
|
|
1982
|
-
await this
|
|
1983
|
-
|
|
2075
|
+
if (!(await this.prepareRemoteSession(userId, scopeIds, authSubject)))
|
|
2076
|
+
return;
|
|
2077
|
+
await this.resumeRemoteSession();
|
|
2078
|
+
}
|
|
2079
|
+
/** Installs a remotely verified identity without starting pull or outbox replay. */
|
|
2080
|
+
prepareRemoteSession(userId, scopeIds, authSubject, authLease) {
|
|
2081
|
+
const revision = ++this.#transitionRevision;
|
|
2082
|
+
const lease = this.#lease(revision, authLease);
|
|
2083
|
+
return this.#enqueueTransition(async () => {
|
|
2084
|
+
await this.#sync.resetSession();
|
|
2085
|
+
await this.#session.suspendRemoteSession();
|
|
2086
|
+
if (!lease.isCurrent())
|
|
2087
|
+
return false;
|
|
2088
|
+
return this.#session.activateSession(userId, scopeIds, authSubject, lease);
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
/** Starts pull and outbox replay after the caller has published remote access. */
|
|
2092
|
+
async resumeRemoteSession() {
|
|
1984
2093
|
await this.#sync.refreshSession();
|
|
1985
2094
|
}
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
2095
|
+
/**
|
|
2096
|
+
* Activates a restored identity for local replica/outbox use without enabling transport sync.
|
|
2097
|
+
*/
|
|
2098
|
+
activateOfflineSession(authSubject, authLease) {
|
|
2099
|
+
const revision = ++this.#transitionRevision;
|
|
2100
|
+
const lease = this.#lease(revision, authLease);
|
|
2101
|
+
return this.#enqueueTransition(async () => {
|
|
2102
|
+
await this.#sync.resetSession();
|
|
2103
|
+
if (!lease.isCurrent())
|
|
2104
|
+
return null;
|
|
2105
|
+
const manifest = await this.#session.activateOfflineSession(authSubject, lease);
|
|
2106
|
+
if (manifest && lease.isCurrent())
|
|
2107
|
+
await this.#sync.refreshLocalSession();
|
|
2108
|
+
return lease.isCurrent() ? manifest : null;
|
|
2109
|
+
});
|
|
2110
|
+
}
|
|
2111
|
+
clearActiveSession() {
|
|
2112
|
+
++this.#transitionRevision;
|
|
2113
|
+
return this.#enqueueTransition(async () => {
|
|
2114
|
+
await this.#sync.resetSession();
|
|
2115
|
+
await this.#session.clearActiveSession();
|
|
2116
|
+
});
|
|
1989
2117
|
}
|
|
1990
2118
|
async prepareLogout(action) {
|
|
1991
2119
|
if (action === 'cancel')
|
|
@@ -2000,6 +2128,14 @@ class OfflineCoordinatorService {
|
|
|
2000
2128
|
flush() {
|
|
2001
2129
|
return this.#sync.flush();
|
|
2002
2130
|
}
|
|
2131
|
+
#lease(revision, authLease) {
|
|
2132
|
+
return { isCurrent: () => revision === this.#transitionRevision && (authLease?.isCurrent() ?? true) };
|
|
2133
|
+
}
|
|
2134
|
+
#enqueueTransition(operation) {
|
|
2135
|
+
const transition = this.#transitionTail.then(operation, operation);
|
|
2136
|
+
this.#transitionTail = transition.then(() => undefined, () => undefined);
|
|
2137
|
+
return transition;
|
|
2138
|
+
}
|
|
2003
2139
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: OfflineCoordinatorService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
2004
2140
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: OfflineCoordinatorService, providedIn: 'root' }); }
|
|
2005
2141
|
}
|