When using a button to trigger a recalculate variable event, nothing seems to happen. I tried setting the recompute value to “when triggered by an event”, but the function seems to be displaying an outdated value.
Typescript functions are cached. Sometimes it is desirable to execute a function with the same inputs multiple times.
There is currently no setting to not use that cache.
The way you can force a re-computation is by using an input that has no material outcome on the function output, but does change as an input.
An example would be a counter. i.e. moving your function from:
@Function()
public generateAnswer1(name: string): String {
return "name: " + name + Timestamp.now().toISOString()
}
using:
@Function()
public generateAnswer2(name: string, _dummerCounter: Integer): String {
return "name: " + name + Timestamp.now().toISOString()
}
If you call generateAnswer1("palantir") you will always get the timestamp of the first run.
Calling generateAnswer2("palantir", 0) will get the timestamp of the first run with an input of 0.
Calling generateAnswer2("palantir", 0) will return that first timestamp again, but calling generateAnswer2("palantir", 1) will return a new timestamp.
So if you have a timestamp or a counter or something similar in your application, use that as an input to your function to avoid hitting a cache when you don’t want to.
Indeed, currently, some “entropy” needs to be passed to the function, so that it forces a cache-miss and so a recalculation.
As an example, here’s some function’s code:
import { Function, Integer, Timestamp } from "@foundry/functions-api";
import { Objects } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";
export class MyFunctions {
/**
* Gets the current time as a timezone object in UTC time.
* @returns Current time as a timezone object in UTC time.
*/
@Function()
public getCurrentTimeStamp() : Timestamp {
return Timestamp.now()
}
@Function()
public generateUUID(): string {
return Uuid.random()
}
@Function()
public getCurrentTimeStampWithEntropy(entropy: Integer) : Timestamp {
return Timestamp.now()
}
@Function()
public generateUUIDWithEntropy(entropy: Integer): string {
return Uuid.random()
}
}
In Workshop I need to have 2 helper variables:
Increment - Just a static numerical variable, default value = 1
IncrementFuture - defined as a Variable Transformation, which is equivalent to Increment + 1
Now, when we back a variable by a function (that would return something useful, e.g. the current timestamp or the output of some calculation), it is possible to pass the increment variable to act as some entropy, that will force a recalculation.
Note: the variable needs to be set on recompute variable value automatically as, otherwise, the entropy value updating won’t be enough to trigger a recomputation, and you will also need to explicitly trigger a “variable recalculation” event.
Now you can set an event - where you see fit, for example on click of a button - to bump the Increment value, and hence trigger recomputations.
Final result: Functions taking “some entropy” as input are recomputing.