How can I detect new contracts using daml-react?
- georgOP 1
Let’s say I have a page that uses
useStreamQueriesto read and live-update contracts in a table. If I want to display a notification every time a new contract arrives, is there anything indaml-reactthat 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? - Alex_Matson 1
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)
- Alex_Matson 1
I would also be curious to know if this can be done without any Daml model changes, though
- Gary_Verhaegen 3
The underlying
streamQuerieson the Ledger object actually gives you the diff on eachchangeevent, in addition to the new state. This is currently lost in the React bindings, though, by only using the first argument of thechangehandler.@cocreature Is that something we should fix? Perhaps add a
setDataargument to theuseStream*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.