aboutsummaryrefslogtreecommitdiffstats
path: root/alv/result/evt.moon
blob: ee44f7635124b49dd0a080ec7926bad2f179dc54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
----
-- Stream of momentary events.
--
-- @classmod EvtStream
import Result from require 'alv.result.base'

class EvtStream extends Result
--- Result interface
--
-- `EvtStream` implements the `Result` interface.
-- @section result

  --- return whether this Result was changed in the current tick.
  -- @treturn bool
  dirty: => @updated == (COPILOT and COPILOT.T)

  --- get the sequence of current events (if any).
  --
  -- Returns `value` if `dirty`, or `nil` otherwise.
  -- Asserts `@type == type` if `type` is given.
  --
  -- @tparam[opt] type.Type type the type to check for
  -- @tparam[optchain] string msg message to throw if type don't match
  -- @treturn ?any `value`
  unwrap: (type, msg) =>
    assert type == @type, msg or "#{@} is not a #{type}" if type
    if @dirty! then @value

  --- create a mutable copy of this stream.
  --
  -- Used to wrap insulate eval-cycles from each other.
  --
  -- @treturn EvtStream
  fork: => @@ @type

  --- alias for `unwrap`.
  __call: (...) => @unwrap ...

  __tostring: =>
    value = if @dirty! then @type\pp @value else 'nil'
    "<#{@type}#{@metatype} #{value}>"

  --- the type of this Result's value.
  -- @tfield type.Type type

  --- the metatype string for this Result.
  -- @tfield string metatype (`!`)
  metatype: '!'

  --- documentation metadata.
  --
  -- an optional table containing metadata for error messages and
  -- documentation. The following keys are recognized:
  --
  -- - `name`: optional name
  -- - `summary`: single-line description (markdown)
  -- - `examples`: optional list of single-line code examples
  -- - `description`: optional full-text description (markdown)
  --
  -- @tfield ?table meta

--- members
-- @section members

  --- push an event value into the stream.
  --
  -- Marks this stream as dirty for the remainder of the current tick.
  -- If `event` is nil, does nothing.
  set: (event, force=false) =>
    return if event == nil
    assert force or not @dirty!, "#{@} is already dirty!"
    @updated = COPILOT.T
    @value = event

  --- the wrapped Lua value.
  -- @tfield any value

--- static functions
-- @section static

  --- construct a new EvtStream.
  --
  -- @classmethod
  -- @tparam type.Type type the type
  new: (type) =>
    super type

{
  :EvtStream
}