How can I detect new contracts using daml-react?

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?

1 Like

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)

1 Like

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

1 Like

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.

3 Likes