MCPcopy Create free account
hub / github.com/Effect-TS/effect / stateCodecOf

Function stateCodecOf

packages/effect/src/ui/internal/stateCodec.ts:81–120  ·  view source on GitHub ↗
(component: Component.Mountable.Any)

Source from the content-addressed store, hash-verified

79}
80/** @internal */
81export const stateCodecOf = (component: Component.Mountable.Any): StateCodec => {
82 let found = stateCodecs.get(component)
83 if (found === undefined) {
84 const schema = (component as Component.Component<any, any, any, any, any, any, any>)
85 .model
86 const bound = queryCore.boundOf(component)
87 if (
88 holdsRedacted(schema.ast) ||
89 (bound !== undefined && holdsRedacted(bound.schema.ast))
90 ) {
91 found = redactedState
92 } else if (bound === undefined) {
93 const codec = jsonCodecOf(schema)
94 found = {
95 redacted: false,
96 encode: (model) => codec.encode(model),
97 decode: (encoded) =>
98 Effect.mapEager(
99 codec.decode(encoded),
100 (model) => ({ model, results: undefined })
101 )
102 }
103 } else {
104 const codec = jsonCodecOf(
105 Schema.Struct({ model: schema, queries: bound.schema })
106 )
107 found = {
108 redacted: false,
109 encode: (model, results) => codec.encode({ model, queries: results ?? bound.empty }),
110 decode: (encoded) =>
111 Effect.mapEager(
112 codec.decode(encoded),
113 (state: any) => ({ model: state.model, results: state.queries })
114 )
115 }
116 }
117 stateCodecs.set(component, found)
118 }
119 return found
120}
121
122/** @internal */
123export const forgetStateCodec = (component: Component.Any): void => {

Callers 4

writeFunction · 0.90
keepHeldFunction · 0.90
encodePageFunction · 0.85
decodePageFunction · 0.85

Calls 5

jsonCodecOfFunction · 0.85
encodeMethod · 0.80
getMethod · 0.65
setMethod · 0.65
decodeMethod · 0.45

Tested by

no test coverage detected