take named options in makeSessionHooks
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -425,11 +425,11 @@ describe('makeSessionHooks arm-latch + event mapping', () => {
|
|||||||
>()
|
>()
|
||||||
/* the hook only passes this through by identity; a stub bundle suffices */
|
/* the hook only passes this through by identity; a stub bundle suffices */
|
||||||
const bundle = {} as SessionBundle
|
const bundle = {} as SessionBundle
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
return {onSessionChange, bundle, hooks}
|
return {onSessionChange, bundle, hooks}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,11 +528,11 @@ describe('session-hook payload threading (pre-commit ordering)', () => {
|
|||||||
refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData)
|
refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => ({}) as SessionBundle,
|
getBundle: () => ({}) as SessionBundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
session = new PasswordSession(sessionAccountToSessionData(makeAccount()), {
|
session = new PasswordSession(sessionAccountToSessionData(makeAccount()), {
|
||||||
...hooks,
|
...hooks,
|
||||||
fetch: asFetch(fetchMock),
|
fetch: asFetch(fetchMock),
|
||||||
@@ -573,11 +573,11 @@ describe('session-hook payload threading (pre-commit ordering)', () => {
|
|||||||
refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData)
|
refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => ({}) as SessionBundle,
|
getBundle: () => ({}) as SessionBundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
const session = new PasswordSession(
|
const session = new PasswordSession(
|
||||||
sessionAccountToSessionData(makeAccount()),
|
sessionAccountToSessionData(makeAccount()),
|
||||||
{...hooks, fetch: asFetch(fetchMock)},
|
{...hooks, fetch: asFetch(fetchMock)},
|
||||||
@@ -606,11 +606,11 @@ describe('session-hook payload threading (pre-commit ordering)', () => {
|
|||||||
*/
|
*/
|
||||||
describe('disposeBundle kill-switch', () => {
|
describe('disposeBundle kill-switch', () => {
|
||||||
it('the injected fetch throws after disposeBundle', () => {
|
it('the injected fetch throws after disposeBundle', () => {
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
jest.fn(),
|
onSessionChange: jest.fn(),
|
||||||
() => ({}) as SessionBundle,
|
getBundle: () => ({}) as SessionBundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
/* the injected fetch is the kill-switch wrapper makeSessionHooks bakes in */
|
/* the injected fetch is the kill-switch wrapper makeSessionHooks bakes in */
|
||||||
const injectedFetch = hooks.fetch!
|
const injectedFetch = hooks.fetch!
|
||||||
|
|
||||||
@@ -734,11 +734,11 @@ describe('PasswordSession.refresh through armed hooks', () => {
|
|||||||
(bundle: SessionBundle, did: string, event: AtpSessionEvent) => void
|
(bundle: SessionBundle, did: string, event: AtpSessionEvent) => void
|
||||||
>()
|
>()
|
||||||
const bundle = {} as SessionBundle
|
const bundle = {} as SessionBundle
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
/*
|
/*
|
||||||
* makeSessionHooks bakes in networkAwareFetch (the real global fetch);
|
* makeSessionHooks bakes in networkAwareFetch (the real global fetch);
|
||||||
* override it with the mock while keeping the arm-latched callbacks (they
|
* override it with the mock while keeping the arm-latched callbacks (they
|
||||||
@@ -957,11 +957,11 @@ describe('a session destroyed or rejected during preparation', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('finishPreparation disposes and rethrows without running the snapshot', async () => {
|
it('finishPreparation disposes and rethrows without running the snapshot', async () => {
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
jest.fn(),
|
onSessionChange: jest.fn(),
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
const session = new PasswordSession(
|
const session = new PasswordSession(
|
||||||
sessionAccountToSessionData(makeAccount()),
|
sessionAccountToSessionData(makeAccount()),
|
||||||
{...hooks, fetch: asFetch(makeMockFetch())},
|
{...hooks, fetch: asFetch(makeMockFetch())},
|
||||||
@@ -997,11 +997,11 @@ describe('a throwing onSessionChange does not brick the session', () => {
|
|||||||
throw new Error('reducer side effect exploded')
|
throw new Error('reducer side effect exploded')
|
||||||
})
|
})
|
||||||
let bundle!: SessionBundle
|
let bundle!: SessionBundle
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => DID,
|
getDid: () => DID,
|
||||||
)
|
})
|
||||||
const session = new PasswordSession(
|
const session = new PasswordSession(
|
||||||
sessionAccountToSessionData(makeAccount()),
|
sessionAccountToSessionData(makeAccount()),
|
||||||
{...hooks, fetch: asFetch(fetchMock)},
|
{...hooks, fetch: asFetch(fetchMock)},
|
||||||
|
|||||||
@@ -58,11 +58,11 @@ export async function createSessionBundleAndCreateAccount(
|
|||||||
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
||||||
let bundle!: SessionBundle
|
let bundle!: SessionBundle
|
||||||
let accountDid = ''
|
let accountDid = ''
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => accountDid,
|
getDid: () => accountDid,
|
||||||
)
|
})
|
||||||
|
|
||||||
const session = await PasswordSession.createAccount(
|
const session = await PasswordSession.createAccount(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -114,13 +114,19 @@ export type OnSessionChange = (
|
|||||||
/**
|
/**
|
||||||
* Hooks stay inert during initial session preparation. `kill()` disarms them
|
* Hooks stay inert during initial session preparation. `kill()` disarms them
|
||||||
* and disables the injected fetch so a disposed session cannot refresh or
|
* and disables the injected fetch so a disposed session cannot refresh or
|
||||||
* dispatch. The bundle getters are deferred because hooks are created first.
|
* dispatch.
|
||||||
*/
|
*/
|
||||||
export function makeSessionHooks(
|
export function makeSessionHooks({
|
||||||
onSessionChange: OnSessionChange,
|
onSessionChange,
|
||||||
getBundle: () => SessionBundle,
|
getBundle,
|
||||||
getDid: () => string,
|
getDid,
|
||||||
) {
|
}: {
|
||||||
|
onSessionChange: OnSessionChange
|
||||||
|
/** Deferred: hooks are created before the bundle exists. */
|
||||||
|
getBundle: () => SessionBundle
|
||||||
|
/** Deferred: hooks are created before the bundle exists. */
|
||||||
|
getDid: () => string
|
||||||
|
}) {
|
||||||
let armed = false
|
let armed = false
|
||||||
let killed = false
|
let killed = false
|
||||||
const dispatch = (event: AtpSessionEvent, sessionData?: SessionData) => {
|
const dispatch = (event: AtpSessionEvent, sessionData?: SessionData) => {
|
||||||
@@ -247,11 +253,11 @@ export async function createSessionBundleAndResume(
|
|||||||
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
||||||
const gates = features.refresh({strategy: 'prefer-low-latency'})
|
const gates = features.refresh({strategy: 'prefer-low-latency'})
|
||||||
let bundle!: SessionBundle
|
let bundle!: SessionBundle
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => storedAccount.did,
|
getDid: () => storedAccount.did,
|
||||||
)
|
})
|
||||||
|
|
||||||
let session: PasswordSession
|
let session: PasswordSession
|
||||||
const sessionData = sessionAccountToSessionData(storedAccount)
|
const sessionData = sessionAccountToSessionData(storedAccount)
|
||||||
@@ -326,11 +332,11 @@ export async function createSessionBundleAndLogin(
|
|||||||
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
): Promise<{account: SessionAccount; bundle: SessionBundle}> {
|
||||||
let bundle!: SessionBundle
|
let bundle!: SessionBundle
|
||||||
let accountDid = ''
|
let accountDid = ''
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => accountDid,
|
getDid: () => accountDid,
|
||||||
)
|
})
|
||||||
|
|
||||||
const session = await PasswordSession.login({
|
const session = await PasswordSession.login({
|
||||||
...hooks,
|
...hooks,
|
||||||
@@ -376,11 +382,11 @@ export function createSessionBundleFromStoredAccount(
|
|||||||
) => boolean = () => true,
|
) => boolean = () => true,
|
||||||
): {account: SessionAccount; bundle: SessionBundle} | undefined {
|
): {account: SessionAccount; bundle: SessionBundle} | undefined {
|
||||||
let bundle!: SessionBundle
|
let bundle!: SessionBundle
|
||||||
const hooks = makeSessionHooks(
|
const hooks = makeSessionHooks({
|
||||||
onSessionChange,
|
onSessionChange,
|
||||||
() => bundle,
|
getBundle: () => bundle,
|
||||||
() => storedAccount.did,
|
getDid: () => storedAccount.did,
|
||||||
)
|
})
|
||||||
const session = new PasswordSession(
|
const session = new PasswordSession(
|
||||||
sessionAccountToSessionData(storedAccount),
|
sessionAccountToSessionData(storedAccount),
|
||||||
hooks,
|
hooks,
|
||||||
|
|||||||
Reference in New Issue
Block a user