Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treaty: subscribe for treaties to prevent race condition #268

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions desk/app/treaty.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
++ default-ally ~dister-dozzod-dozzod
::
+$ card card:agent:gall
+$ state-1 [%1 state-0]
+$ state-0
$: treaties=(map [=ship =desk] treaty)
sovereign=(map desk treaty)
Expand All @@ -15,7 +16,7 @@
^- agent:gall
%+ verb |
%- agent:dbug
=| state-0
=| state-1
=* state -
=<
|_ =bowl:gall
Expand All @@ -30,8 +31,19 @@
++ on-save !>(state)
++ on-load
|= =vase
?: =(%1 -.q.vase)
=+ !<(old=state-1 vase)
`this(state old)
=+ !<(old=state-0 vase)
`this(state old)
:_ this(state [%1 old])
%- zing
^- (list (list card))
%+ turn ~(tap by allies.old)
|= [=ship s=(set *)]
^- (list card)
=* al ~(. al:cc ship)
?^ s ~
~[leave:al watch:al]
::
++ on-poke
|= [=mark =vase]
Expand Down Expand Up @@ -59,7 +71,7 @@
?< =(ship our.bowl)
=* al ~(. al:cc ship.update)
?- -.update
%add [~[watch:al] this(allies (~(put by allies) ship *alliance))]
%add [~[leave:al watch:al] this(allies (~(put by allies) ship *alliance))]
%del [~[leave:al] this(allies (~(del by allies) ship))]
==
::
Expand Down Expand Up @@ -111,6 +123,17 @@
::NOTE this assumes that all treaties in sovereign are also
:: present in the treaties map
(fact-init:io (treaty-update:cg:cc %ini treaties))^~
::
[%treaties @ ~]
:_ this
=/ =ship (slav %p i.t.path)
=/ alliance (~(get ju allies) ship)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the case for the scry too, but I'm kind of surprised this isn't doing a (~(put in alliance) ship), to make sure the ship we're actually explicitly asking about is also in there, always.

=/ allied
%- ~(gas by *(map [^ship desk] treaty))
%+ skim ~(tap by treaties)
|= [ref=[^ship desk] =treaty]
(~(has in alliance) ref)
(fact-init:io (treaty-update:cg:ca:cc %ini allied))^~
::
[%alliance ~]
:_ this
Expand Down Expand Up @@ -307,13 +330,14 @@
++ leave (leave:pass dock)
++ gone
^- (list card)
:~ (fact:io (treaty-update:cg %del ship desk) /treaties ~)
:~ (fact:io (treaty-update:cg %del ship desk) /treaties /treaties/[(scot %p ship)] ~)
(kick-only:io our.bowl path ~)
==
++ give
^- (list card)
=/ t=treaty (~(got by treaties) ship desk)
:~ (fact:io (treaty-update:cg %add t) /treaties ~)
(fact:io (treaty-update:cg %add t) /treaties/[(scot %p ship)] ~)
(fact:io (treaty:cg t) path ~)
==
--
Expand Down
8 changes: 7 additions & 1 deletion ui/src/state/docket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,15 @@ export function useAllyTreaties(ship: string) {
// [ship]
// )
// );
const { data: treatyData, isLoading } = useReactQueryScry<TreatyUpdateIni>({

const { data: treatyData, isLoading } = useReactQuerySubscription<
TreatyUpdateIni,
TreatyUpdate
>({
queryKey: ['treaty', 'treaties', ship],
app: 'treaty',
path: `/treaties/${ship}`,
scry: `/treaties/${ship}`,
options: {
enabled: isAllied,
},
Expand All @@ -238,6 +243,7 @@ export function useAllyTreaties(ship: string) {
}
return normalizeDockets(treatyData.ini);
}, [treatyData]);

const status = getAllyTreatyStatus(
treaties,
isLoading,
Expand Down
Loading