aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-04-12 17:36:34 +0000
committers-ol <s-ol@users.noreply.github.com>2020-04-12 17:36:48 +0000
commit0c14567501831d5e772edb0d778f46aed4ba8cff (patch)
treeefb8cc5a66e8e1cbe718111168b9db9af22f5b1f
parentbuiltin/print: support string events (diff)
downloadalive-0c14567501831d5e772edb0d778f46aed4ba8cff.tar.gz
alive-0c14567501831d5e772edb0d778f46aed4ba8cff.zip
time/every: allow specifying event
e.g. (print (every 0.5 "hello")) (print (every 0.5 (switch (tick 1) "hello" "world")))
-rw-r--r--lib/time.moon16
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/time.moon b/lib/time.moon
index 5380657..344b5fa 100644
--- a/lib/time.moon
+++ b/lib/time.moon
@@ -187,24 +187,28 @@ tick = ValueStream.meta
every = ValueStream.meta
meta:
name: 'every'
- description: "Emits a bang once every `period` seconds.
+ summary: "Emit events regularly."
+ examples: { '(every [clock] period [evt])' }
+ description: "Emits `evt` as an event once every `period` seconds.
- `clock` should be a `time/clock` event stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `period` should be a num-value.
-- the return type will be a stream of bang-events."
+- `evt` can be a value of any type. It defaults to `bang`.
+- the return type will be an event stream with the same type as `evt`."
value: class extends Op
new: (...) =>
super ...
@state or= 0
- @out or= EventStream 'bang'
- pattern = -evt.clock + val.num
+ pattern = -evt.clock + val.num + -val!
setup: (inputs, scope) =>
- { clock, period } = pattern\match inputs
+ { clock, period, evt } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
period: Input.cold period
+ evt: Input.cold evt or ValueStream 'bang', true
+ @out = EventStream @inputs.evt\type!
tick: =>
for tick in *@inputs.clock!
@@ -212,7 +216,7 @@ every = ValueStream.meta
while @state >= 1
@state -= 1
- @out\add true
+ @out\add @inputs.evt!
sequence = ValueStream.meta
meta: