( previous: ObservedShape | null, value: unknown, now: number, )
| 198 | |
| 199 | /** Fold a new observation into an existing record, keeping the result bounded. */ |
| 200 | export const observeShape = ( |
| 201 | previous: ObservedShape | null, |
| 202 | value: unknown, |
| 203 | now: number, |
| 204 | ): ObservedShape => { |
| 205 | const observed = inferShape(value); |
| 206 | let schema = previous === null ? observed : mergeShapes(previous.schema, observed); |
| 207 | for ( |
| 208 | let depth = MAX_DEPTH; |
| 209 | depth > 0 && JSON.stringify(schema).length > MAX_SHAPE_JSON_CHARS; |
| 210 | depth-- |
| 211 | ) { |
| 212 | schema = shrink(schema, depth); |
| 213 | } |
| 214 | return { |
| 215 | schema, |
| 216 | observations: (previous?.observations ?? 0) + 1, |
| 217 | updatedAt: now, |
| 218 | }; |
| 219 | }; |
no test coverage detected