The MessageCatcherWidget was introduced in tiddlywiki 5.2.0. I am wondering if I can use it to trap instances of the use of a given tm-message any where in the wiki?.
For example the save wiki button $:/core/ui/Buttons/save-wiki uses the message $message=“tm-save-wiki”.
I would like to trap every save and update a tiddler with the last saved date and time
Will this work for autosave?
Where in the wiki do I need to wrap it with <$messagecatcher> so no matter where the tm-save-message is triggered?
Can we have an action occur before the tm-save-wiki is actioned?
Will javascript modules use the same named messages, so they too can be trapped?
In other work can I do something similar to trigger additional actions when various buttons send a message eg; tm-close-tiddler or tm-navigate?
The anywhere part might be tricky. A $messagecatcher widget traps messages that originate from within its child content. The typical use case for the $messagecatcher widget is to wrap it around content from which you want to trap messages, not to use it to trap messages or behaviour in the entire wiki. The latter is probably more so in the realm of hooks, for which we do not yet have wikitext affordances but we are most likely moving in that direction.
Theoretically it is possible for JavaScript code to send messages from the root widget which is outside the page template, which would therefore be impossible to trap using a $messagecatcher widget. Whether this is a realistic problem in your use case I am not sure, you will need to test and figure that out.
Your best bet would be to wrap it around the contents of the entire page template.
There might be some nuances to account for, there is a separate tm-auto-save-wiki widget message.
Once you trap the message you decide what happens next. You can trap the message, run any actions you like, and then re-issue the message. With saving you might have to consider the possibility of ending up in a loop if your actions make changes to tiddlers which in turn trigger a save. Not all actions that modify tiddlers trigger autosave though so depending on what you need to this may not be an issue.
The MessageCatcher widget is an advanced widget that starts to blur the lines between wikitext and the internal JavaScript code of TiddlyWiki, and by doing so allows an extensive level of flexibility in customization that would not otherwise be possible from wikitext (compare it to $linkcatcher to better understand this). As such it can in some cases require an understanding of the message event objects and their structure. Fortunately our widget message documentation already contains this information.
I thought I would share this so anyone can quickly introduce the ability to trap any link navigation in the whole wiki. catch-navigate.json (2.4 KB)
I am yet to do a deep test.
One question remains for me;
The navigation event I am trapping is somewhere inside the navigator widget, I am replacing it with the following; <$action-navigate $to=<<event-navigateTo>>/>
Is this sufficient or should I be providing more to the action navigate widget, to correctly replace the functionality inside the navigator?