(def: QueryDef, label: string, decoding: boolean)
| 3572 | |
| 3573 | /** Checks a snapshot against the query's Schemas, the type parameters. */ |
| 3574 | const snapshotRun = (def: QueryDef, label: string, decoding: boolean) => { |
| 3575 | const is = isSnapshotOf(def, label) |
| 3576 | return (typeParameters: ReadonlyArray<SchemaAST.AST>) => { |
| 3577 | const [args, success, error] = typeParameters.map((ast) => |
| 3578 | SchemaParser.decodeUnknownEffect( |
| 3579 | Schema.make<Schema.Codec<unknown, unknown>>(ast) |
| 3580 | ) |
| 3581 | ) |
| 3582 | return ( |
| 3583 | input: unknown, |
| 3584 | ast: SchemaAST.Declaration, |
| 3585 | options: SchemaAST.ParseOptions |
| 3586 | ): Effect.Effect<SnapshotImpl, SchemaIssue.Issue> => { |
| 3587 | if (!is(input)) { |
| 3588 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 3589 | } |
| 3590 | const data = input[SnapshotTypeId] |
| 3591 | if (data.result === undefined) return Effect.succeed(input) |
| 3592 | const result = data.result |
| 3593 | // The key stays as it is: it is the snapshot's identity. A decoded snapshot is accepted only when its |
| 3594 | // args, decoded all the way, key back to it, so it never names an entry other than the one it was taken from. |
| 3595 | return Effect.flatMap( |
| 3596 | Effect.all([ |
| 3597 | args(data.args, options), |
| 3598 | // A patched value is used as it is: a patch that fails `success` is reported as `PatchInvalid`, and a |
| 3599 | // check of a query result mustn't throw on it. The value `~query` ships keeps its identity, so it can |
| 3600 | // be referenced and is shared across readers. |
| 3601 | settledMap( |
| 3602 | result, |
| 3603 | (u) => |
| 3604 | data.patched ? Effect.succeed(u) : Effect.flatMap( |
| 3605 | Effect.context(), |
| 3606 | (ctx) => |
| 3607 | isShipped(ctx, def, data.argsKey!, result) |
| 3608 | ? Effect.succeed(u) |
| 3609 | : success(u, options) |
| 3610 | ), |
| 3611 | (u) => error(u, options) |
| 3612 | ), |
| 3613 | // A failed load more holds a typed error too, which the message log's form carries. |
| 3614 | data.more?._tag === "Failed" |
| 3615 | ? Effect.map( |
| 3616 | error(data.more.error, options), |
| 3617 | (error): More<unknown> => ({ _tag: "Failed", error }) |
| 3618 | ) |
| 3619 | : Effect.succeed(data.more) |
| 3620 | ]), |
| 3621 | ([args, result, more]) => { |
| 3622 | if (decoding) { |
| 3623 | let back: string | undefined |
| 3624 | try { |
| 3625 | back = def.argsKey(args) |
| 3626 | } catch { |
| 3627 | back = undefined |
| 3628 | } |
| 3629 | if (back !== data.argsKey) { |
| 3630 | return Effect.fail( |
| 3631 | snapshotIssue(def, "the args key isn't the key of its args") |
no test coverage detected