urbit/pkg/arvo/app/thread-watch-hook.hoon

185 lines
4.2 KiB
Plaintext
Raw Normal View History

2020-10-28 03:16:00 +03:00
:: thread-watch-hook:
::
:: watch an app at a particular path and forward all facts to a
:: particular thread
::
/+ default-agent, dbug
::
|%
+$ card card:agent:gall
+$ versioned-state
$% state-0
==
+$ observer [app=term =path input=mark thread=term]
+$ state-0 [%0 observers=(map time observer)]
+$ action
$% [%watch =observer]
[%ignore =observer]
==
::
++ got-by-val
|= [a=(map time observer) b=observer]
^- time
%- need
%+ roll ~(tap by a)
|= [[key=time val=observer] output=(unit time)]
?:(=(val b) `key output)
--
::
%- agent:dbug
=| state-0
=* state -
::
^- agent:gall
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
::
++ on-init on-init:def
++ on-save !>([%0 ~])
++ on-load
|= old-vase=vase
^- (quip card _this)
`this(state !<(state-0 old-vase))
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
?> (team:title our.bowl src.bowl)
?. ?=(%thread-watch-action mark)
(on-poke:def mark vase)
=/ =action !<(action vase)
2020-10-29 23:02:09 +03:00
=* observer observer.action
2020-10-28 03:16:00 +03:00
=/ vals (silt ~(val by observers))
?- -.action
%watch
2020-10-29 23:02:09 +03:00
?: ?|(=(app.observer %spider) =(app.observer %thread-watch-hook))
2020-10-28 03:16:00 +03:00
~|('we avoid infinite loops' !!)
2020-10-29 23:02:09 +03:00
?: (~(has in vals) observer)
2020-10-28 03:16:00 +03:00
~|('duplicate observer' !!)
2020-10-29 23:02:09 +03:00
:_ this(observers (~(put by observers) now.bowl observer))
2020-10-28 03:16:00 +03:00
:_ ~
:* %pass
/observer/(scot %da now.bowl)
%agent
2020-10-29 23:02:09 +03:00
[our.bowl app.observer]
2020-10-28 03:16:00 +03:00
%watch
2020-10-29 23:02:09 +03:00
path.observer
2020-10-28 03:16:00 +03:00
==
::
%ignore
2020-10-29 23:02:09 +03:00
?. (~(has in vals) observer)
2020-10-28 03:16:00 +03:00
~|('cannot remove nonexistent observer' !!)
2020-10-29 23:02:09 +03:00
=/ key (got-by-val observers observer)
:_ this(observers (~(del by observers) key))
2020-10-28 03:16:00 +03:00
:_ ~
:* %pass
/observer/(scot %da key)
%agent
2020-10-29 23:02:09 +03:00
[our.bowl app.observer]
2020-10-28 03:16:00 +03:00
%leave
~
==
==
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
2020-10-29 23:02:09 +03:00
?- -.sign
2020-10-28 03:16:00 +03:00
%kick
2020-10-29 23:02:09 +03:00
?+ wire (on-agent:def wire sign)
[%thread-result @ ~] [~ this]
[%observer @ ~]
=/ =time (slav %da i.t.wire)
=/ =observer (~(got by observers) time)
:_ this
:_ ~
:* %pass
wire
%agent
[our.bowl app.observer]
%watch
path.observer
==
==
2020-10-28 03:16:00 +03:00
::
%poke-ack
2020-10-29 23:02:09 +03:00
?. ?=([%thread-start @ @ ~] wire)
(on-agent:def wire sign)
?~ p.sign [~ this]
=/ =time (slav %da i.t.wire)
=/ tid (slav %uv i.t.t.wire)
=/ =observer (~(got by observers) time)
:_ this(observers (~(del by observers) time))
:~ :* %pass
[%observer i.t.wire ~]
%agent
[our.bowl app.observer]
%leave
~
==
:* %pass
wire
%agent
[our.bowl app.observer]
%leave
~
==
==
2020-10-28 03:16:00 +03:00
::
%watch-ack
2020-10-29 23:02:09 +03:00
?~ p.sign [~ this]
?+ wire (on-agent:def wire sign)
[%thread-result @ ~] [~ this]
[%observer @ ~]
=/ =time (slav %da i.t.wire)
[~ this(observers (~(del by observers) time))]
==
2020-10-28 03:16:00 +03:00
::
%fact
2020-10-29 23:02:09 +03:00
?+ wire (on-agent:def wire sign)
2020-10-28 03:16:00 +03:00
[%thread-result @ ~]
2020-10-29 23:02:09 +03:00
?. =(p.cage.sign %thread-fail)
[~ this]
=/ =time (slav %da i.t.wire)
=/ =observer (~(got by observers) time)
:_ this(observers (~(del by observers) time))
:_ ~
:* %pass
[%observer i.t.wire ~]
%agent
[our.bowl app.observer]
%leave
~
==
2020-10-28 03:16:00 +03:00
::
[%observer @ ~]
2020-10-29 23:02:09 +03:00
=/ =time (slav %da i.t.wire)
=/ =observer (~(got by observers) time)
=/ tid (scot %uv (sham eny.bowl))
:_ this
:~ :* %pass
[%thread-start i.t.wire tid ~]
%agent
[our.bowl %spider]
%poke
%spider-start
!>([~ `tid thread.observer q.cage.sign])
==
:* %pass
[%thread-result tid ~]
%agent
[our.bowl %spider]
%watch
[%thread-result tid ~]
== ==
2020-10-28 03:16:00 +03:00
==
==
::
++ on-watch on-watch:def
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--