(predicate: (s: SessionState) => boolean)
| 463 | * persisted store (listed in pages of 100). |
| 464 | */ |
| 465 | export async function findSessionMatching(predicate: (s: SessionState) => boolean): Promise<SessionState | null> { |
| 466 | const ctx = requestCtx.getStore(); |
| 467 | if (ctx) { |
| 468 | for (const session of ctx.sessions.values()) { |
| 469 | if (predicate(session)) return session; |
| 470 | } |
| 471 | } |
| 472 | const store = storeInstance; |
| 473 | if (!store) return null; |
| 474 | try { |
| 475 | const page = await store.list<Record<string, unknown>>(SESSIONS_COLLECTION, { limit: 100 }); |
| 476 | for (const row of page.items ?? []) { |
| 477 | const sessionKey = typeof row.__sessionKey === 'string' ? row.__sessionKey : undefined; |
| 478 | const session = deserializeSession(row); |
| 479 | if (predicate(session)) { |
| 480 | if (ctx && sessionKey) { |
| 481 | ctx.sessions.set(sessionKey, session); |
| 482 | ctx.snapshots.set(sessionKey, stableStringify(serializeSession(session, sessionKey))); |
| 483 | } |
| 484 | return session; |
| 485 | } |
| 486 | } |
| 487 | } catch (err) { |
| 488 | logger.warn({ err }, 'findSessionMatching: store list failed'); |
| 489 | } |
| 490 | return null; |
| 491 | } |
| 492 | |
| 493 | export function findMediaBuyAcrossSessions(mediaBuyId: string): Promise<SessionState | null> { |
| 494 | return findSessionMatching(s => s.mediaBuys.has(mediaBuyId)); |
no test coverage detected