Extract session identity helper

master
dev 5 months ago
parent aa8a7403a5
commit 8a68e93e52

@ -16,6 +16,7 @@ Estado al cierre:
- `arts/timr/engine-timers.ts` delega `TimerHandle` cancel/reschedule en `timer-handle.ts`.
- `arts/stor/engine-storage.ts` delega IDs de adapter, bus keys y suscripciones cross-tab en `adapter-registry.ts`.
- `arts/stor/engine-storage.ts` delega el registro de defaults conflictivos en `defaults-registry.ts`.
- `arts/sess/engine-session.ts` delega la clasificacion `none/anonymous/identified` en `session-identity.ts`.
- Integracion total ampliada: `Auth.signOut()` valida anonimizacion, invalidacion de `Permissions` y evento `Cache.invalidate`.
- Tanda focalizada verde: `npx vitest run src/arts/conn src/libs/cach src/arts/cach src/svrs/cach src/arts/auth src/svrs/auth src/libs/auth src/arts/aapp/test/ecosystem.integration.test.ts` -> 19 archivos, 82 tests.
- `/test/ecosystem` revisado en navegador: carga sin errores de consola, `ar` cambia a `rtl`, Formats se actualiza por locale, Perm cambia con rol `viewer`, Cach re-scopea por locale y Conn loopback publica/recibe.

@ -19,9 +19,6 @@ import {
FIELD_CREDENTIAL,
FIELD_DATA,
FIELD_USER,
IDENTITY_ANONYMOUS,
IDENTITY_IDENTIFIED,
IDENTITY_NONE,
REFRESH_STATUS_EXPIRED,
REFRESH_STATUS_FAILED,
REFRESH_STATUS_REFRESHED,
@ -52,6 +49,7 @@ import {
hydrateSessionStorage,
wireSessionStorageSync
} from './session-sync.ts';
import { computeSessionIdentity } from './session-identity.ts';
import { checkSessionInvariants, validateSessionCandidate } from './session-validation.ts';
import type {
AdoptResultField,
@ -63,7 +61,6 @@ import type {
Session,
SessionActor,
SessionChange,
SessionIdentityState,
SessionListener
} from './types.ts';
@ -149,11 +146,6 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
// ── State helpers ───────────────────────────────────────────────────────
function computeIdentity(s: Snap | null): SessionIdentityState {
if (s === null) return IDENTITY_NONE;
return s.user === null ? IDENTITY_ANONYMOUS : IDENTITY_IDENTIFIED;
}
/**
* Apply a snapshot that came from outside this engine (storage event,
* BroadcastChannel). Dedups against the current snapshot so a storage
@ -164,10 +156,10 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
function applyExternalChange(next: Snap | null): void {
const prev = snapshot;
if (sameSnapshot(prev, next)) return;
const fromIdentity = computeIdentity(prev);
const fromIdentity = computeSessionIdentity(prev);
generation += 1;
snapshot = next;
const toIdentity = computeIdentity(next);
const toIdentity = computeSessionIdentity(next);
dispatch({
event: EVENT_EXTERNAL_CHANGED,
current: next,
@ -210,11 +202,11 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
event: typeof EVENT_ADOPTED | typeof EVENT_ADOPTED_SERVER | typeof EVENT_REFRESHED
): void {
const prev = snapshot;
const fromIdentity = computeIdentity(prev);
const fromIdentity = computeSessionIdentity(prev);
generation += 1;
snapshot = next;
persist(next);
const toIdentity = computeIdentity(next);
const toIdentity = computeSessionIdentity(next);
dispatch({
event,
current: next,
@ -230,7 +222,7 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
revoke?: RevokeResult
): void {
const prev = snapshot;
const fromIdentity = computeIdentity(prev);
const fromIdentity = computeSessionIdentity(prev);
generation += 1;
snapshot = null;
persist(null);
@ -239,7 +231,7 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
current: null,
previous: prev,
generation,
identity: { from: fromIdentity, to: IDENTITY_NONE },
identity: { from: fromIdentity, to: computeSessionIdentity(null) },
revoke
});
broadcast(event);
@ -265,7 +257,7 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
return generation;
},
get identity() {
return computeIdentity(snapshot);
return computeSessionIdentity(snapshot);
},
async adopt(input) {
@ -396,8 +388,8 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
previous: snapshot,
generation,
identity: {
from: computeIdentity(snapshot),
to: computeIdentity(snapshot)
from: computeSessionIdentity(snapshot),
to: computeSessionIdentity(snapshot)
},
error: err
});
@ -511,7 +503,7 @@ export function createEngineSession<TUser, TCredential = undefined, TData = unde
if (disposed) return () => {};
listeners.add(listener);
try {
const identity = computeIdentity(snapshot);
const identity = computeSessionIdentity(snapshot);
listener({
event: EVENT_INITIAL_SESSION,
current: snapshot,

@ -0,0 +1,9 @@
import { IDENTITY_ANONYMOUS, IDENTITY_IDENTIFIED, IDENTITY_NONE } from './consts.ts';
import type { Session, SessionIdentityState } from './types.ts';
export function computeSessionIdentity(
session: Pick<Session<unknown, unknown, unknown>, 'user'> | null
): SessionIdentityState {
if (session === null) return IDENTITY_NONE;
return session.user === null ? IDENTITY_ANONYMOUS : IDENTITY_IDENTIFIED;
}
Loading…
Cancel
Save

Powered by TurnKey Linux.