Files
tasks/chasm/field_internal.go
Alan WuandGitHub 18223df619 Implement detached component (#9086)
## What changed?
Implement detached component as a Field option and Registrable Component
option.

Add detached boolean value to ComponentAttributes persistence proto
definition.

## Why?
Allow detached components to continue updates and task execution even if
parent node lifecycle is closed.

## How did you test it?
- [X] built
- [X] run locally and tested manually
- [X] covered by existing tests
- [X] added new unit test(s)
- [ ] added new functional test(s)
2026-01-28 17:43:01 -05:00

57 lines
1.5 KiB
Go

package chasm
type fieldInternal struct {
// These 2 fields are used when node is not set yet (i.e., node==nil).
// Don't access them directly outside of this file. Use corresponding getters instead.
ft fieldType
v any // Component | Data | Pointer
// Pointer to the corresponding tree node. Can be nil for the just created fields.
node *Node
// Detached field option. When true, the node created from this field
// will be detached regardless of the component type's registration.
detached bool
}
func newFieldInternalWithValue(ft fieldType, v any) fieldInternal {
return fieldInternal{
ft: ft,
v: v,
}
}
func newFieldInternalWithNode(node *Node) fieldInternal {
return fieldInternal{
node: node,
}
}
func (fi fieldInternal) isEmpty() bool {
return fi.v == nil && fi.node == nil
}
func (fi fieldInternal) value() any {
// Deferred pointers are special-cased, since their serialized nodes are
// initialized as regular persistable pointers.
//
// Deferred pointers may have a non-nil node after syncSubComponents, but before
// resolution.
if fi.node == nil || fi.ft == fieldTypeDeferredPointer {
return fi.v
}
return fi.node.value
}
func (fi fieldInternal) fieldType() fieldType {
// Deferred pointers are special-cased, since their serialized nodes are
// initialized as regular persistable pointers.
//
// Deferred pointers may have a non-nil node after syncSubComponents, but before
// resolution.
if fi.node == nil || fi.ft == fieldTypeDeferredPointer {
return fi.ft
}
return fi.node.fieldType()
}