aboutsummaryrefslogtreecommitdiffstats
path: root/core/stream/event.moon
blob: f7b49c47de72a5dcf878811cd4c21a2854aea346 (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
91
92
93
94
95
96
97
98
99
100
----
-- Stream of momentary events.
--
-- @classmod EventStream
import Stream from require 'core.stream.base'
import Result from require 'core.result'
import Error from require 'core.error'
import scope, base, registry from require 'core.cycle'

class EventStream extends Stream
--- members
-- @section members

  --- return whether this stream was changed in the current tick.
  --
  -- @treturn bool
  dirty: => @updated == registry.Registry.active!.tick

  --- push an event value into the stream.
  --
  -- Marks this stream as dirty for the remainder of the current tick.
  --
  -- @tparam any event
  add: (event) =>
    if not @dirty!
      @events = {}

    @updated = registry.Registry.active!.tick
    table.insert @events, event

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

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

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

  __tostring: =>
    "<#{@@__name} #{@type}>"

  --- Stream metatype.
  --
  -- @tfield string metatype
  metatype: 'event'

  --- the type name of the stream.
  --
  -- the following builtin typenames are used:
  --
  -- - `str` - strings, `value` is a Lua string
  -- - `sym` - symbols, `value` is a Lua string
  -- - `num` - numbers, `value` is a Lua number
  -- - `bool` - booleans, `value` is a Lua boolean
  -- - `bang` - trigger signals, `value` is a Lua boolean
  -- - `opdef` - `value` is an `Op` subclass
  -- - `builtin` - `value` is an `Action` subclass
  -- - `fndef` - `value` is a `FnDef` instance
  -- - `scope` - `value` is a `Scope` instance
  --
  -- @tfield string type

  --- 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

--- static functions
-- @section static

  --- construct a new EventStream.
  --
  -- @classmethod
  -- @tparam string type the type name
  new: (type) => super type

{
  :EventStream
}