|
| 1 | +import type Observer from './Observer.ts'; |
| 2 | +import { |
| 3 | + createObserver, |
| 4 | +} from './ObserverRuntime.ts'; |
| 5 | +import LegacyReading, { |
| 6 | + type NeighborhoodReadingFields, |
| 7 | + type ReadingDirection, |
| 8 | +} from './Reading.ts'; |
| 9 | +import type { |
| 10 | + ReadingValue, |
| 11 | +} from './ObservedReading.ts'; |
| 12 | +import WarpError from '../errors/WarpError.ts'; |
| 13 | + |
| 14 | +export type GraphNeighborhoodOptions = { |
| 15 | + readonly around: string; |
| 16 | + readonly direction?: ReadingDirection; |
| 17 | + readonly labels?: readonly string[]; |
| 18 | + readonly limit?: number; |
| 19 | + readonly cursor?: string; |
| 20 | +}; |
| 21 | + |
| 22 | +export type GraphNeighborhoodEdge = Readonly<{ |
| 23 | + readonly direction: 'out' | 'in'; |
| 24 | + readonly neighborId: string; |
| 25 | + readonly label: string; |
| 26 | +}> & Readonly<Record<string, ReadingValue>>; |
| 27 | + |
| 28 | +export type GraphNeighborhoodChart = Readonly<{ |
| 29 | + readonly subject: string; |
| 30 | + readonly direction: ReadingDirection; |
| 31 | + readonly edges: readonly GraphNeighborhoodEdge[]; |
| 32 | + readonly completeness: 'complete' | 'truncated'; |
| 33 | + readonly cursor: string | null; |
| 34 | +}> & Readonly<Record<string, ReadingValue>>; |
| 35 | + |
| 36 | +export type GraphChartObservers = Readonly<{ |
| 37 | + neighborhood(options: GraphNeighborhoodOptions): Observer<GraphNeighborhoodChart>; |
| 38 | +}>; |
| 39 | + |
| 40 | +/** Bounded, graph-shaped derived observers. */ |
| 41 | +export const graph: GraphChartObservers = Object.freeze({ |
| 42 | + neighborhood(options: GraphNeighborhoodOptions): Observer<GraphNeighborhoodChart> { |
| 43 | + return createObserver<GraphNeighborhoodChart>( |
| 44 | + 'charts.graph.neighborhood', |
| 45 | + neighborhoodReading(options), |
| 46 | + decodeNeighborhoodChart, |
| 47 | + ); |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +function neighborhoodReading(options: GraphNeighborhoodOptions): LegacyReading { |
| 52 | + if (options === null || options === undefined) { |
| 53 | + throw chartError('graph.neighborhood options are required', 'E_CHART_OPTIONS'); |
| 54 | + } |
| 55 | + const { around: subject, ...settings } = options; |
| 56 | + const fields: NeighborhoodReadingFields = { subject, ...settings }; |
| 57 | + return LegacyReading.neighborhood(fields); |
| 58 | +} |
| 59 | + |
| 60 | +function decodeNeighborhoodChart(value: ReadingValue): GraphNeighborhoodChart { |
| 61 | + const { |
| 62 | + subject: rawSubject, |
| 63 | + direction: rawDirection, |
| 64 | + edges: rawEdges, |
| 65 | + completeness: rawCompleteness, |
| 66 | + cursor: rawCursor, |
| 67 | + } = requireRecord(value, 'chart value'); |
| 68 | + const subject = requireString(rawSubject, 'chart subject'); |
| 69 | + const direction = requireReadingDirection(rawDirection); |
| 70 | + const edges = requireArray(rawEdges, 'chart edges').map(decodeNeighborhoodEdge); |
| 71 | + const completeness = requireCompleteness(rawCompleteness); |
| 72 | + const cursor = requireNullableString(rawCursor, 'chart cursor'); |
| 73 | + return Object.freeze({ |
| 74 | + subject, |
| 75 | + direction, |
| 76 | + edges: Object.freeze(edges), |
| 77 | + completeness, |
| 78 | + cursor, |
| 79 | + }) as GraphNeighborhoodChart; |
| 80 | +} |
| 81 | + |
| 82 | +function decodeNeighborhoodEdge(value: ReadingValue): GraphNeighborhoodEdge { |
| 83 | + const { |
| 84 | + direction: rawDirection, |
| 85 | + neighborId: rawNeighborId, |
| 86 | + label: rawLabel, |
| 87 | + } = requireRecord(value, 'chart edge'); |
| 88 | + const direction = requireEdgeDirection(rawDirection); |
| 89 | + const neighborId = requireString(rawNeighborId, 'chart edge neighborId'); |
| 90 | + const label = requireString(rawLabel, 'chart edge label'); |
| 91 | + return Object.freeze({ |
| 92 | + direction, |
| 93 | + neighborId, |
| 94 | + label, |
| 95 | + }) as GraphNeighborhoodEdge; |
| 96 | +} |
| 97 | + |
| 98 | +function requireRecord( |
| 99 | + value: ReadingValue | undefined, |
| 100 | + field: string, |
| 101 | +): Readonly<Record<string, ReadingValue>> { |
| 102 | + if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 103 | + throw chartError(`graph.neighborhood received an invalid ${field}`, 'E_CHART_VALUE'); |
| 104 | + } |
| 105 | + return value as Readonly<Record<string, ReadingValue>>; |
| 106 | +} |
| 107 | + |
| 108 | +function requireArray( |
| 109 | + value: ReadingValue | undefined, |
| 110 | + field: string, |
| 111 | +): readonly ReadingValue[] { |
| 112 | + if (!Array.isArray(value)) { |
| 113 | + throw chartError(`graph.neighborhood received invalid ${field}`, 'E_CHART_VALUE'); |
| 114 | + } |
| 115 | + return value as readonly ReadingValue[]; |
| 116 | +} |
| 117 | + |
| 118 | +function requireString(value: ReadingValue | undefined, field: string): string { |
| 119 | + if (typeof value !== 'string') { |
| 120 | + throw chartError(`graph.neighborhood received invalid ${field}`, 'E_CHART_VALUE'); |
| 121 | + } |
| 122 | + return value; |
| 123 | +} |
| 124 | + |
| 125 | +function requireNullableString( |
| 126 | + value: ReadingValue | undefined, |
| 127 | + field: string, |
| 128 | +): string | null { |
| 129 | + if (value !== null && typeof value !== 'string') { |
| 130 | + throw chartError(`graph.neighborhood received invalid ${field}`, 'E_CHART_VALUE'); |
| 131 | + } |
| 132 | + return value; |
| 133 | +} |
| 134 | + |
| 135 | +function requireReadingDirection(value: ReadingValue | undefined): ReadingDirection { |
| 136 | + if (value !== 'out' && value !== 'in' && value !== 'both') { |
| 137 | + throw chartError('graph.neighborhood received invalid chart direction', 'E_CHART_VALUE'); |
| 138 | + } |
| 139 | + return value; |
| 140 | +} |
| 141 | + |
| 142 | +function requireEdgeDirection(value: ReadingValue | undefined): 'out' | 'in' { |
| 143 | + if (value !== 'out' && value !== 'in') { |
| 144 | + throw chartError('graph.neighborhood received an invalid chart edge', 'E_CHART_VALUE'); |
| 145 | + } |
| 146 | + return value; |
| 147 | +} |
| 148 | + |
| 149 | +function requireCompleteness( |
| 150 | + value: ReadingValue | undefined, |
| 151 | +): 'complete' | 'truncated' { |
| 152 | + if (value !== 'complete' && value !== 'truncated') { |
| 153 | + throw chartError('graph.neighborhood received invalid chart completeness', 'E_CHART_VALUE'); |
| 154 | + } |
| 155 | + return value; |
| 156 | +} |
| 157 | + |
| 158 | +function chartError(message: string, code: string): WarpError { |
| 159 | + return new WarpError(message, code); |
| 160 | +} |
0 commit comments