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”:

https://en.wikipedia.org/wiki/Pure_function#:~:text=In%20computer%20programming%2C%20a%20pure,from%20I%2FO%20devices).

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.



> On 12 Dec 2020, at 20:57, Leung arvin <arvindf...@gmail.com> wrote:
> 
> I see , thanks! that is a much better method to implement the idea
> 
> Mark S. 在 2020年12月12日 星期六下午8:53:02 [UTC] 的信中寫道:
> 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.
> 
> 
> 
> On Saturday, December 12, 2020 at 11:29:44 AM UTC-8 Leung arvin wrote:
> 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.
> 
> 
> -- 
> 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+unsubscr...@googlegroups.com 
> <mailto:tiddlywiki+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/tiddlywiki/d967d530-bea7-4019-9b70-024a517c9c88n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/tiddlywiki/d967d530-bea7-4019-9b70-024a517c9c88n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/8FBA8968-DEE0-4BE7-9DEF-883063DC8EF5%40gmail.com.

Reply via email to