Question on javascript macro being invoked excessively in my webDAV file picker

As a learning exercise, I’m working on a javascript macro to select and pick filenames from a webDAV server. I may have bitten off more than I can chew :frowning: Functionally, the macro seems to work, but for each attempt read of a webDAV directory, the js macro is invoked three times, apparently as part of the “TW refresh processing”. I found a reference in TW documentation:

“Macros are just used to return a chunk of wikitext for further processing. They should not make modifications to tiddlers in the wiki store. The reason is that you cannot control when the macro is called; it may be called repeatedly as part of refresh processing. So it is important that macros do not have any other side effects beyond generating their text.”

I tried to pass a flag (below) to the js macro so it can return immediately if it’s not being deliberately invoked, to no avail. Those extra calls were made while the proper macro call is being made (from within a conditional statement) and while the flag was set. OTOH, this is already an improvement over an earlier iteration where the JS macro is spontaneously invoked for every file in the directory within a $LIST loop!
The nature of this ‘refreshing’ is mystifying to me.

<$action-deletefield  $tiddler="@Art_datadict" ButtonActive/>        <!--- @Art_readDir will run only if ButtonActive FIELD don't exist and flag != 'No' ; testing  --->
<$set name="var_ButtonActive" filter="[[@Art_datadict]field:ButtonActive[]]" value='1' emptyValue="No" >
<$set name="var_readDir" filter="[[@Art_datadict]field:ButtonActive[]]"  emptyValue="{}" value='<$macrocall $name="@Art_readDir" webdavurl={{@Art_datadict!!webdavurl}} webdavdir={{@Art_datadict!!webdavdir}} flag=<<var_ButtonActive>> />' >
<$wikify name=wiki_Dir text=<<var_readDir>> >
<$action-setfield $tiddler="@Art_datadict" text=<<wiki_Dir>> />
<$action-setfield $tiddler="@Art_datadict" ButtonActive="SET"/>

On Android (using RCX) and Windows (using Hacdias webdav server), the response time from webdav server is still ok, but on a Chromebook (using Hacdias webdav server) the response time is slow enough to trigger a not responding message from Tiddywiki.

My question is is there anything that can be done at the Wikitext level so the js macro is called only once, or minimally ?
Also, is a javascript macro the wrong approach for this purpose of selecting files from a webdav server ?

Oh, I know Saqimtiaz has a better webDAV solution if i really need to use webDAV :slight_smile: So this is really just a learning experience for me. I do seem to be tantalizingly close to getting it done (js macro in json attached), so would like to see if I can get pass this hurdle.

JArt_webdav_filepicker_trial.json (17.0 KB)

JacN

Using an action widget would be a considerable improvement upon using a macro. However, if you have a refresh cycle while an HTTP request is in progress the results will be unreliable.

The correct approach for this is neither macros nor widgets, but rather messages. Any widgets in the widget tree are vulnerable to a refresh cycle. So the best approach is to send a Widget Message up the widget tree which is then handled by a custom listener on the root widget, which is outside the refresh cycle. The message is handled, an HTTP request made, and the response data provided to actions for further processing.

Jeremy and I have both worked with a generalized means of making HTTP requests from wikitext in this vein, and hopefully we will be able to propose a version thereof as a candidate for the core in the near future. This opens the door for playing with a lot of interesting APIs via HTTP requests and can be used for WebDAV as well.

1 Like

I see. That’s helpful ! I think this widget message approach is similar to what you discussed here: Time consuming JS macro - #3 by saqimtiaz. That is beyond what i want to go into now though. Will stick to Wikitext where there is still much that I have to learn and do. Thanks!