Skip to content
Discussions/App Development/How can I detect new contracts using daml-react?Forum ↗

How can I detect new contracts using daml-react?

App Development4 posts435 views6 likesLast activity Mar 2021
GE
georgOP
Mar 2021

Let’s say I have a page that uses useStreamQueries to read and live-update contracts in a table. If I want to display a notification every time a new contract arrives, is there anything in daml-react that allows me to hook into this event elegantly?

The only alternative I can think of is to keep parent state in my component of all the contracts that have been loaded previously, and diff that on every re-render with the current list of contracts returned from useStreamQueries. But this seems heavy-handed and cumbersome for a seemingly simple task. Any ideas on how to best approach this?

AL
Alex_Matson
Mar 2021

The only other workaround I know of (for the JSON API) is to add a timestamp field to the contract on which you want notifications for, and then stream them. On the UI side, display a notification component if the current time is within some delta of the timestamp listed on the latest contract (i.e., the last 5 mins)

AL
Alex_Matson
Mar 2021

I would also be curious to know if this can be done without any Daml model changes, though

GA
Gary_Verhaegen
Mar 2021

The underlying streamQueries on the Ledger object actually gives you the diff on each change event, in addition to the new state. This is currently lost in the React bindings, though, by only using the first argument of the change handler.

@cocreature Is that something we should fix? Perhaps add a setData argument to the useStream* functions so users can get access to that diff if they want to? (Obviously with current behaviour as the default, so that would be backwards compatible.) My main concern is that these functions are starting to have quite a long queue of optional arguments.

← Back to Discussions