Typescript runtime
You can extend the runtime of ROOC by defining your own functions.
In the project editor, click the "Show runtime" button, and a typescript editor will appear below the model
editor.
You can define a new function by calling the "register" function and passing the name of the function, the types
of the parameters, the return type and the function itself.
As the runtime is not executed in javascript itself, but rather a WASM compiled rust code, you need to
serialize/deserialize the data to/from the runtime as a JSON object.
Utility constants are provided to help you with this task. One example is the "Primitive" type, which you can
use to define the type of the parameters and the return type of the function.
Here is an example on how to register a function that will sum all the elements of an array:
If you need more advanced usecases, the "returns" field can either be a constant value or a function that will calculate what the return type will be. If there is an error in this function, the "Any" type will be used. Here is an example of using the "returns" field as a function:
The serialized data is a JSON like object that has a "type" field which tells you the type of the data, and a "value" field which is the actual value of the data. Here are the type definitions for it:
Useful functions
Inside of the typescript runtime there are some utility functions defined in the "Rooc" namespace.
Using the global GET_FILES()
function, you can get the contents (as strings) of the files that
are in the project (you can select them with the "attach files" button).
In the Rooc.parseCsvTable
and Rooc.parseCsvObject
you can pass a CSV string and it
will return an array of arrays or an array of objects respectively, of the parsed CSV string.
Internally it uses the csv library to parse
the
CSV string, so look at the documentation of that library for more information.
Other useful functions is the integration of graphlib library
to work with graphs. You can use the Rooc.Graph
class to create a graph manually, or using the Rooc.fromSerializedGraph
function to create a graph from a rooc serialized graph.
You can also find algorithms to use over graphs in the Rooc.GraphAlgorithms
, look at the graphlib
library for more information.