( def: QueryDef, label: string, [args, success, error]: ReadonlyArray<Schema.Top>, log: boolean )
| 3646 | * tuple Schema and the two steps between a tuple and a snapshot. |
| 3647 | */ |
| 3648 | const snapshotWire = ( |
| 3649 | def: QueryDef, |
| 3650 | label: string, |
| 3651 | [args, success, error]: ReadonlyArray<Schema.Top>, |
| 3652 | log: boolean |
| 3653 | ) => { |
| 3654 | const invalid = (message: string) => snapshotIssue(def, message) |
| 3655 | const argsJson = Schema.toCodecJson(args as Schema.Codec<unknown, unknown>) |
| 3656 | const encodeArgs = SchemaParser.encodeUnknownEffect(argsJson) |
| 3657 | const decodeArgs = SchemaParser.decodeUnknownEffect(argsJson) |
| 3658 | // `args` here is the encoded side of the args Schema, so these see the args as the declaration passes them. |
| 3659 | const toKey = ( |
| 3660 | args: unknown, |
| 3661 | options: SchemaAST.ParseOptions |
| 3662 | ): Effect.Effect<string, SchemaIssue.Issue> => |
| 3663 | Effect.flatMap(encodeArgs(args, options), (encoded) => { |
| 3664 | try { |
| 3665 | return Effect.succeed(canonical(encoded)) |
| 3666 | } catch { |
| 3667 | return Effect.fail( |
| 3668 | invalid("the encoded args hold a value JSON can't carry") |
| 3669 | ) |
| 3670 | } |
| 3671 | }) |
| 3672 | const fromKey = ( |
| 3673 | key: string, |
| 3674 | options: SchemaAST.ParseOptions |
| 3675 | ): Effect.Effect<unknown, SchemaIssue.Issue> => { |
| 3676 | let parsed: unknown |
| 3677 | try { |
| 3678 | parsed = JSON.parse(key) |
| 3679 | } catch { |
| 3680 | return Effect.fail(invalid("the args key isn't JSON")) |
| 3681 | } |
| 3682 | // The declaration checks the key once the args are decoded all the way. |
| 3683 | return decodeArgs(parsed, options) |
| 3684 | } |
| 3685 | // The log's form adds what page state drops: whether a fetch is in flight, and a list's exact more. |
| 3686 | const transient = Schema.Tuple([ |
| 3687 | Schema.Boolean, |
| 3688 | Schema.NullOr( |
| 3689 | Schema.Union([ |
| 3690 | Schema.Literals(["End", "Available", "Loading"]), |
| 3691 | Schema.Struct({ Failed: error }) |
| 3692 | ]) |
| 3693 | ) |
| 3694 | ]) |
| 3695 | const settled = (elements: ReadonlyArray<Schema.Top>) => Schema.Tuple(log ? [...elements, transient] : elements) |
| 3696 | const decodeSettled = ( |
| 3697 | tuple: any, |
| 3698 | options: SchemaAST.ParseOptions |
| 3699 | ): Effect.Effect<SnapshotImpl, SchemaIssue.Issue> => { |
| 3700 | if (!log) return decodeState(tuple, options) |
| 3701 | const [waiting, more] = tuple[tuple.length - 1] |
| 3702 | return Effect.map(decodeState(tuple.slice(0, -1), options), (snapshot) => { |
| 3703 | const data = snapshot[SnapshotTypeId] |
| 3704 | const result = data.result! |
| 3705 | return new SnapshotImpl({ |
no test coverage detected