To make our directive update asynchronously, we'll need to start a timer that re-renders the time elapsed.
Add a function that starts a periodic 3 second timer if it's not already started. Since directives are stateful, we can use a class field to store the timer's handle, and use that to start the timer only once:
The default implementation of update simply calls render, but since update
is not called during
SSR,
update is the right spot to start/subscribe to asynchronous tasks we don't
want running on the server.
Add the update callback. Call ensureTimerStarted() and then return the result of render. Note that we only want the timer running if the directive is currently connected, so check isConnected before starting the timer: