All files / layers / L2W coordinatorResolver.ts

97.05% Statements 33/34
87.5% Branches 7/8
100% Functions 8/8
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55        1690x 1690x 394x   1296x   69x 16x 1690x 1690x 1690x 146x 146x 146x   54x   146x   16x 16x 16x   146x 146x   1x 1690x 2x       1x           1x 1x                 16x    
import { createCoordinator } from './coordinator'
import {
  createDefaultBehaviorContract,
  createFieldPolicyFromConfig,
} from './behaviorContract'
import { hooksByPath } from '../L1/entityResolver'
import type { EntityPath } from '../L1/contracts'
import type { RouteContext, EntitySelectSource } from '../interfaces'
 
function cacheKey(entityPath: EntityPath, fieldSet: string[], entitySelectSources?: Record<string, EntitySelectSource>): string {
  const base = `${entityPath}:${fieldSet.join(',')}`
  Eif (entitySelectSources) {
    return `${base}:es:${JSON.stringify(entitySelectSources)}`
  }
  return base
}
 
const coordinatorCache = new Map<
  string,
  any
>()
 
function getCoordinator(entityPath: EntityPath, fieldSet: string[], entitySelectSources?: Record<string, EntitySelectSource>) {
  const key = cacheKey(entityPath, fieldSet, entitySelectSources)
  const existing = coordinatorCache.get(key)
  if (existing) return existing
 
  const hooks = hooksByPath[entityPath]
  const base = createDefaultBehaviorContract()
 
  const behavior = {
    ...base,
    fieldPolicy: (mode: 'add' | 'edit') =>
      createFieldPolicyFromConfig(fieldSet, mode),
  }
 
  const coord = createCoordinator({
    hooks: hooks as any,
    behavior: behavior as typeof base,
    owner: entityPath,
    entitySelectSources,
  })
  coordinatorCache.set(key, coord)
  return coord
}
 
export function useEntityCoordinator(
  entityPath: EntityPath,
  fieldSet: string[],
  routeContext: RouteContext,
  entitySelectSources?: Record<string, EntitySelectSource>,
) {
  return getCoordinator(entityPath, fieldSet, entitySelectSources).useCoordinator(routeContext)
}