Is it safe to make idempotent macros?

79 views
Skip to first unread message

Leung arvin

unread,
Dec 12, 2020, 2:29:44 PM12/12/20
to TiddlyWiki
I wish to implement the following functionality, using JS macro, defining the macro with name M. 

Whenever I use a macro <<M tiddler_name : "New Tiddler">> , then it creates a Tiddler with title "New Tiddler". Then the macro presents this link [[New Tiddler]]. The point is that the macro will create the Tiddler for me (with some starting content , customisable in JS) if none have already been created. 

Example code would be: 

function run(tiddler_name){
- Check if wiki has tiddler with title tiddler_name
- If not, create a tiddler of title tiddler_name and content "default content" 
- Return [[tiddler_name]]
}

I would like to know if this is safe. The documentation states: 

"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 cannott 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."

So this would break the no side effect rule. However, the idempotent design means that subsequent calls to this macro should not have any side effects. 

Will this potentially break stuff in unknown ways? I would also like to know if there are other methods to achieve the same tasks in JS.

Mark S.

unread,
Dec 12, 2020, 3:53:02 PM12/12/20
to TiddlyWiki
I suspect your usage would break the spirit of the JS rules, since it's using a macro to change TW content.

But you could write a non-JS (i.e.wikitext) macro that would basically do the same thing. If the tiddler doesn't exist, it would show a button which, when clicked on, would create the tiddler. A button can be disguised as a link, if that's important to you.

Leung arvin

unread,
Dec 12, 2020, 5:03:27 PM12/12/20
to TiddlyWiki
I see , thanks! that is a much better method to implement the idea

Mark S. 在 2020年12月12日 星期六下午8:53:02 [UTC] 的信中寫道:

Jeremy Ruston

unread,
Dec 13, 2020, 8:18:23 AM12/13/20
to TiddlyWiki Group
Hi Leung,

As Mark points out, it sounds like you can achieve what you want in wikitext, without requiring JavaScript.

Just to add a bit of background for subsequent searchers:

The motivation for JavaScript macros was to provide the simplest possible way integrate a custom JS module with TiddlyWiki. They stand in contrast to “widgets” which are the JS components that make up the heart of TiddlyWiki’s rendering process. They have a relatively complex interface because they need to be able to participate in TiddlyWiki’s refresh mechanism, which is the process by which changes to the tiddler store trigger updates to the DOM.

So, without the ability to participate in the refresh process, JavaScript macros are constrained to be functions that take an arbitrary number of arguments and return a single string. The refresh process expects that rendering a macro with the same parameters will always return the same text; if it returns different text it may not be displayed immediately, requiring the refresh of a parent widget to trigger the rerender. In computer science terms, JS macros are expected to be "pure functions”:


Having said that, there are lots of situations where macros are useful but no refreshing occurs, notably creating static HTML renderings, or within filters. So, with care, it is possible to break the “pure function” rule.

The requirement for idempotency applies throughout TiddlyWiki’s rendering and refreshing cycle. TiddlyWiki is a reactive system that seeks to automatically update the DOM as the tiddler store changes. If the rendering or refresh process does make changes to the tiddler store then the system risks getting locked into an infinite loop of changing the store, triggering a re-rendering, and then changing the store again, and back round the loop.

Best wishes

Jeremy.



-- 
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/d967d530-bea7-4019-9b70-024a517c9c88n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages