Function backed action invoked from Slate is one edit behind

Hi, I have the following setup:

  1. A TypeScript function that takes two arguments: An object ID and a property value. The function uses the ID to look up an object in the ontology then updates its property to the given value.
  2. An action type associated with the ontology object type that invokes the function
  3. A Slate application that contains a Foundry Action widget that invokes the action type

In the Slate application, I am doing the following to trigger the action:

  1. There is a Code Sandbox widget whose ‘State’ block has a property named “edit” and whose ‘Events’ block has an event named “submit_edit”
  2. An event-action pair where the event is the Code Sandbox widget’s “submit_edit” event and the action is the Foundry Action’s “submit” action
  3. When the relevant user interaction occurs, I call SlateFunction.setState to update “edit” then call SlateFunction.triggerEvent to trigger “submit_edit” (and by extension to invoke the Foundry Action’s “submit” action)

This all works great except for one problem: When I inspect the relevant ontology object, the edits are one behind. For example, if the user changes the value to “a” then to “b”, when I check the object, its property value is “a” rather than “b”. If the user then changes the value to “c”, the ontology object updates to “b”.

Is there anything obvious I’m doing that might be wrong?

Thanks,

David

Hi @sirstruck ,

For step 3, I’m going to assume you are doing setState and then immediately calling triggerEvent after. If that’s the case, I suspect the issue is that the event is being triggered before the state has had a chance to update which is causing your action to execute with the previous value.

There’s a few options you can try to resolve this:

  • When you trigger triggerEvent you can pass in the new value and use that to set some variable which is then used by the action e.g. triggerEvent(“submit_edit”, new_edit_state) - more on this in the docs: https://www.palantir.com/docs/foundry/slate/widgets-advanced#triggerevent
  • You can try add a short 1 second timeout before triggering the event
  • Recommended - can you refactor your flow so that you just update a variable with setState and then set up an event-action pair so that when that variable updates, it triggers the Foundry Action? This way the Foundry Action always runs as a response to the handlebar value updating.

Hi @zaini ,

Thank you for the suggestions!

After reading your post, I agree that using a variable is a better workflow. I updated my Slate application to no longer use the Code Sandbox widget’s state. Instead, I now have two event-action pairs:

  1. The action for the first pair updates the variable using the event payload
  2. The second pair triggers when the variable changes and invokes the Foundry Action’s ‘submit’ action

From the Code Sandbox’s JavaScript, I call SlateFunction.triggerEvent and pass the new edit value as the second argument.

While I agree this workflow is cleaner, it unfortunately still has the same problem.

I had another thought: put a printout in the TypeScript function so I can inspect what it receives from the action. But do you know where that printout can be viewed?

Thanks,

David

OK, we found the way!

  1. (Same as before) The action for the first pair updates the variable using the event payload
  2. The second pair triggers on the Foundry Action widget’s validationSuccess event and fires the same widget’s submit action.

Since the Foundry Action widget’s fields all use default parameters that use handlebars to refer to the variable, when we change the variable (in the first event-action pair), it automatically fires validationSuccess which in turn submits the property value at the appropriate time to the update function.

Thanks,

David

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.