diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-03-24 11:39:25 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-03-24 11:39:25 +0000 |
| commit | 677c0d2f01e14bbeca1583ec7878d80c71c3aa68 (patch) | |
| tree | cc20e04697da590b546de370599eeaaef647d20c /core/stream/event.moon | |
| parent | internals/plugin-guide: first draft (diff) | |
| download | alive-677c0d2f01e14bbeca1583ec7878d80c71c3aa68.tar.gz alive-677c0d2f01e14bbeca1583ec7878d80c71c3aa68.zip | |
Value -> Value/Event/IO-Stream
Close 12
Diffstat (limited to 'core/stream/event.moon')
| -rw-r--r-- | core/stream/event.moon | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/core/stream/event.moon b/core/stream/event.moon new file mode 100644 index 0000000..5dbf846 --- /dev/null +++ b/core/stream/event.moon @@ -0,0 +1,92 @@ +---- +-- 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, {} + + --- 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: => EventStream @type + + --- alias for `unwrap`. + __call: (...) => @unwrap ... + + --- 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 +} |
