diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-04-24 09:41:23 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-04-24 10:38:23 +0000 |
| commit | 767dd35e61541f7d5655f0eb449606ac0412f138 (patch) | |
| tree | 98c4ba99d5ada759b3b33169cd744025fbe61823 | |
| parent | better logging interface (diff) | |
| download | alive-767dd35e61541f7d5655f0eb449606ac0412f138.tar.gz alive-767dd35e61541f7d5655f0eb449606ac0412f138.zip | |
move tick-counting into COPILOT
| -rw-r--r-- | alv/copilot.moon | 50 | ||||
| -rw-r--r-- | alv/registry.moon | 27 | ||||
| -rw-r--r-- | alv/stream/event.moon | 6 | ||||
| -rw-r--r-- | alv/stream/value.moon | 6 |
4 files changed, 41 insertions, 48 deletions
diff --git a/alv/copilot.moon b/alv/copilot.moon index 9d573c0..d4612c8 100644 --- a/alv/copilot.moon +++ b/alv/copilot.moon @@ -13,6 +13,8 @@ spit = (file, str) -> file\write str file\close! +export COPILOT + class Copilot --- static functions -- @section static @@ -21,9 +23,18 @@ class Copilot -- @classmethod -- @tparam string file name/path of the alive file to watch and execute new: (file) => + @T = 0 @registry = Registry! @open file if file +--- members +-- @section members + + --- current tick + -- @tfield number T + + --- change the running script. + -- @tparam string file open: (file) => mode = lfs.attributes file, 'mode' if mode != 'file' @@ -32,37 +43,29 @@ class Copilot @last_modification = 0 @file = file ---- members --- @section members - --- poll for changes and tick. tick: => - return unless @file + assert not COPILOT, "another Copilot is already running!" + COPILOT = @ + @T += 1 + return unless @file @poll! if @root L\set_time 'run' - @registry\begin_tick! ok, error = Error.try "updating", -> @root\tick_io! @root\tick! if not ok L\print error - @registry\end_tick! - eval: => - @registry\begin_eval! - ok, root, ast = Error.try "running '#{@file}'", loadfile, @file - if not ok - L\print root - @registry\rollback_eval! - return - - @registry\end_eval! - @root = root - spit @file, ast\stringify! + COPILOT = nil + --- poll all loaded modules for changes. + -- + -- Call `eval` if there are any, and write changed and newly added modules + -- back to disk. poll: => { :mode, :modification } = (lfs.attributes @file) or {} if mode != 'file' @@ -74,6 +77,19 @@ class Copilot @eval! @last_modification = os.time! + --- perform an eval-cycle. + eval: => + @registry\begin_eval! + ok, root, ast = Error.try "running '#{@file}'", loadfile, @file + if not ok + L\print root + @registry\rollback_eval! + return + + @registry\end_eval! + @root = root + spit @file, ast\stringify! + { :Copilot } diff --git a/alv/registry.moon b/alv/registry.moon index 21f57aa..4bd03f6 100644 --- a/alv/registry.moon +++ b/alv/registry.moon @@ -39,13 +39,12 @@ class Registry --- begin an evaluation cycle. -- - -- Begin an evaltime cycle (and tick). -- Set the active Registry and clear out pending registrations. -- -- All calls go `begin_eval` must be matched with either a call to -- `end_eval` or `rollback_eval`. begin_eval: => - @begin_tick! + @grab! @map, @pending = {}, {} --- end an evaluation cycle. @@ -67,31 +66,14 @@ class Registry @map[tag\index!] = expr @last_map = @map - @end_tick! + @release! --- abort an evaluation cycle. -- -- Unset the active Registry. rollback_eval: => - @end_tick! - - --- begin a run cycle. - -- - -- Increment the tick index and set the active Registry. - begin_tick: => - @grab! - @next_tick! - - --- end a run cycle. - -- - -- Unset the active Registry. - end_tick: => @release! - --- manually increment the tick index (for testing). - next_tick: => - @tick += 1 - --- set the active Registry. grab: => assert not @prev, "already have a previous registry? #{@prev}" @@ -109,7 +91,6 @@ class Registry -- @classmethod new: => @last_map, @map = {}, {} - @tick = 0 --- get the active Registry. -- @@ -121,10 +102,6 @@ class Registry class SimpleRegistry extends Registry new: => @cnt = 1 - @tick = 0 - - next_tick: => - @tick += 1 init: (tag, expr) => tag\set @cnt diff --git a/alv/stream/event.moon b/alv/stream/event.moon index 97570a2..16716be 100644 --- a/alv/stream/event.moon +++ b/alv/stream/event.moon @@ -5,7 +5,7 @@ import Stream from require 'alv.stream.base' import Result from require 'alv.result' import Error from require 'alv.error' -import scope, base, registry from require 'alv.cycle' +import scope, base from require 'alv.cycle' class EventStream extends Stream --- members @@ -14,7 +14,7 @@ class EventStream extends Stream --- return whether this stream was changed in the current tick. -- -- @treturn bool - dirty: => @updated == registry.Registry.active!.tick + dirty: => @updated == COPILOT.T --- push an event value into the stream. -- @@ -25,7 +25,7 @@ class EventStream extends Stream if not @dirty! @events = {} - @updated = registry.Registry.active!.tick + @updated = COPILOT.T table.insert @events, event --- get the sequence of current events (if any). diff --git a/alv/stream/value.moon b/alv/stream/value.moon index 213a26c..98720c9 100644 --- a/alv/stream/value.moon +++ b/alv/stream/value.moon @@ -7,7 +7,7 @@ import Stream from require 'alv.stream.base' import Result from require 'alv.result' import Error from require 'alv.error' -import scope, base, registry from require 'alv.cycle' +import scope, base from require 'alv.cycle' ancestor = (klass) -> assert klass, "cant find the ancestor of nil" @@ -22,12 +22,12 @@ class ValueStream extends Stream --- return whether this stream was changed in the current tick. -- -- @treturn bool - dirty: => @updated == registry.Registry.active!.tick + dirty: => @updated == COPILOT.T --- update this stream's value. -- -- Marks this stream as dirty for the remainder of the current tick. - set: (@value) => @updated = registry.Registry.active!.tick + set: (@value) => @updated = COPILOT.T --- unwrap to the Lua type. -- |
