A function that returns a boolean and receives multiple arguments:
Optional
opt: Partial<ControllerOptions>Optional configuration.
The property decorator function.
In the following example, the @While
decorator is used to reads
variable-length array from binary stream until a the condition based on the
object currently read is no longer met.
class BinObject {
@Relation(PrimitiveSymbol.u8)
type: number
@Relation(PrimitiveSymbol.u8)
len: number
@Count('len')
@Relation(PrimitiveSymbol.u8)
blob: number[]
}
class Protocol {
@While((obj) => obj.type !== 0x00)
@Relation(BinObject)
objs: BinObject[]
}
You can also use the peek
option to exclude the elements that don't meet
the condition and prevent them from being included in the result.
With this option the cursor will then be set back before the element was
read.
class Protocol {
@While((elem) => elem !== 0x00, { peek: true })
@Relation(PrimitiveSymbol.u8)
array: number[]
@Match(0x00)
@Relation(PrimitiveSymbol.u8)
end_elem: number
}
Don't use this decorator to compare the current value to EOF. Use Until instead.
@While
decorator continues the execution flow while the condition passed as a parameter is met.By default, if the condition is not met by a value, it will be included in the result, and the cursor will move forward. This is the default behavior because it's the most common use case. However, you can modify this behavior using the
peek
option.