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

Function host

packages/effect/src/ui/Component.ts:4551–4647  ·  view source on GitHub ↗
(
  logic: Any,
  { message, model }: {
    readonly message?: Schema.Codec<any, any> | undefined
    readonly model?: Schema.Struct<Schema.Struct.Fields> | undefined
  },
  elements: View.Vocabulary<any>,
  rest: LogicOptions
)

Source from the content-addressed store, hash-verified

4549
4550// A logic finished by a host: the logic's pieces run first, and the host's add to them.
4551const host = (
4552 logic: Any,
4553 { message, model }: {
4554 readonly message?: Schema.Codec<any, any> | undefined
4555 readonly model?: Schema.Struct<Schema.Struct.Fields> | undefined
4556 },
4557 elements: View.Vocabulary<any>,
4558 rest: LogicOptions
4559): Any => {
4560 const definition = logic.definition
4561 const shared: LogicOptions = (logic as any)["~logic"]
4562 const own = rest.update as AnyUpdate | Record<string, AnyUpdate> | undefined
4563 // Reflect.ownKeys, so a symbol field counts too.
4564 const keys = model ? Reflect.ownKeys(model.fields) : []
4565 const fields = keys.length > 0
4566 if (fields && !(isStruct(definition.model) && isStruct(model))) {
4567 throw new Error(
4568 `${definition.name}: a host's Model fields need a Schema.Struct Model`
4569 )
4570 }
4571 // Restore only host fields from `source`, leaving dropped shared fields absent.
4572 // Preserve `next` by reference when no host field changed.
4573 const keep = (source: any, next: any) => {
4574 let out = next
4575 for (const key of keys) {
4576 const has = Object.hasOwn(source, key)
4577 if (
4578 !Object.is(out[key], source[key]) || Object.hasOwn(out, key) !== has
4579 ) {
4580 if (out === next) out = { ...next }
4581 if (has) out[key] = source[key]
4582 else delete out[key]
4583 }
4584 }
4585 return out
4586 }
4587 const sharedUpdate = toUpdateFn(shared.update)
4588 const kept: AnyUpdate = fields
4589 ? ($, msg) => {
4590 const next = sharedUpdate($, msg)
4591 if (!isStep(next)) return keep($.model, next)
4592 const model = keep($.model, next.model)
4593 return model === next.model ? next : next.set(model)
4594 }
4595 : sharedUpdate
4596 // The logic sees every message its Schema accepts first, even one the host's Schema accepts too.
4597 const isShared = message && Schema.is(definition.message)
4598 const init = rest.init
4599 const restore = definition.restore
4600 return finish({
4601 ...definition,
4602 elements,
4603 message: message
4604 ? Schema.Union([definition.message, message])
4605 : definition.message,
4606 model: fields
4607 ? merge(definition.model as any, model!) as any
4608 : definition.model,

Callers 2

withElementsFunction · 0.70
gateway.test.tsFile · 0.50

Calls 15

toUpdateFnFunction · 0.85
isStepFunction · 0.85
toNextFunction · 0.85
makeStepFunction · 0.85
isStructFunction · 0.70
keepFunction · 0.70
finishFunction · 0.70
mergeFunction · 0.70
initFunction · 0.70
bothFunction · 0.70
setMethod · 0.65
isMethod · 0.65

Tested by

no test coverage detected