Resolving ~~race condition~~ something else?

hi all,
i believe i have encountered a race condition where values aren’t being set properly due to subsequent changes. my code is roughly this:

<$button>
Do The Thing
<$action-createtiddler $basetitle="new name" tags="mytag">
<$action-setfield $tiddler=<<createtiddler-title>> $field=thefield $value={{!!valueholder}}/>
</$action-createtiddler>
<$action-setfield valueholder=""/>
</$button>

ideally, what is supposed to happen is this:

  1. the new tiddler is created with a title and tag.
  2. the new tiddler’s thefield is set to the value in {{!!valueholder}}.
  3. the current tiddler’s valueholder is set to an empty string (blank).

what i think is actually happening is that step (1) is taking too long and so step (3) ends up happening before step (2).
the actual result is that the new tiddler’s thefield ends up blank.
this is replicable by pasting the above code into a new tiddler on tiddlywiki.com and setting that tiddler’s valueholder field to anything. after pressing the button, check the newly created tiddler new name's thefield field (or lack thereof).

does anyone have some insight on how i can fix this, making sure the tiddler creation is done before the valueholder field is wiped?

~scribs

There is a little trick that will achieve the desired timing by using the “inline” button action to create the tiddler and then using the $button action="..." to perform the “clear the input”. The inline action is performed first, followed by the action parameter.

You can also simplify your code by just setting thefield as a parameter directly in the $action-createtiddler widget, without needing to use a separate $action-setfield.

Try this:

<$edit-text field="valueholder"/>
<$button actions="<$action-setfield valueholder=''/>">
Do The Thing
<$action-createtiddler $basetitle="new name" tags="mytag" thefield={{!!valueholder}}/>
</$button>

Also, you can make your code easier to read/maintain by using \procedure definitions, like this:

\procedure create() <$action-createtiddler $basetitle="new name" tags="mytag" thefield={{!!valueholder}}/>
\procedure reset()  <$action-setfield valueholder=""/>

<$edit-text field="valueholder"/><$button actions=<<reset>>>Do The Thing <<create>></$button>

enjoy,
-e

1 Like

this is exactly what i needed! i’m simplifying from the actual code, the inner setfield has some conditionals so it can’t go in the createtiddler parameters. thanks much for the help as always :slight_smile:

update: it looks like your suggestion of moving the field into the createtiddler action was more than just a simplification… as i mentioned i can’t do that (the inner setfield is actually within a list in my code). both of the following snippets have the same issue as the OP:

basic case

<$edit-text field="valueholder"/>
<$button actions="<$action-setfield valueholder=''/>">
Do The Thing
<$action-createtiddler $basetitle="new name" tags="mytag">
<$action-setfield $tiddler=<<createtiddler-title>> $field=thefield $value={{!!valueholder}}/>
</$action-createtiddler>
</$button>

a simplified list filter, closer to my actual use case:

<$edit-text field="valueholder"/>
<$button actions="<$action-setfield valueholder=''/>">
Do The Thing
<$action-createtiddler $basetitle="new name" tags="mytag">
<$list filter="[[thefield]]">
<$action-setfield $tiddler=<<createtiddler-title>> $field=<<currentTiddler>> $value={{!!valueholder}}/>
</$list>
</$action-createtiddler>
</$button>

ETA:
at this point i don’t think it is actually a race condition. i did some more testing and it looks like the setfield inside the action-createtiddler is just not working, nothing i enter is actually applied to the new tiddler.
see also:

<$button>
Do The Thing
<$action-createtiddler $basetitle="new name">
<$action-setfield $tiddler=<<createtiddler-title>> testfield=testvalue/>
</$action-createtiddler>
</$button>

is this a bug, or am i missing something else?

oh! (you’re gonna facepalm!)…

The problem is the reference to <<createtiddler-title>>… it’s supposed to be <<createTiddler-title>>

If you fix the capitalization, then ALL the solutions, including your first one, start to work.

-e

D: facepalm indeed…
my lack of capitalization finally came back to bite me!
thanks for the catch!!

ok, so FYI:
the capitalization was correct in my actual code, but i was able to assess the true issue which was a function i had put in the $overwrite attribute of action-createtiddler. i was missing a !match[] in the filter run, so for some reason even though $overwrite=yes and $overwrite="" worked as expected, with the function it was changing the tiddler’s name (myfield in my example was actually title) with “New Tiddler”. no clue why.

i’ll still concede the capitalization was the problem with the provided code which did muddy the troubleshooting substantially, lol. thanks again for your help.

1 Like