Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Often we feel a need to call a function which is shared by a ruleset installed in a different pico. There are two ways to do this.

Warning
titleDo not use these techniques within a single pico

Picos are single-threaded, and the techniques shown here use a synchronous operation. If, while your pico is evaluating, it makes a synchronous request to itself, the request will just be added to the pico's incoming queue. The current thread will wait forever (before it continues and evaluates the next thing on its incoming queue).

If this happens, you will have to stop your pico engine (Ctrl-C). You can then restart your engine and no state will be lost.

Use the skyQuery() function in Wrangler

If we are using io.picolabs.wrangler as a module in our ruleset, we can take advantage of its general purpose function skyQuery to invoke a function shared by a different pico.

...

Wrangler's skyQuery function will send an HTTP GET request to the other pico, which includes the RID and function name, and the named argument values which the remote function expects. It will check the HTTP return code, and decode the returned content, finally returning it to our code as KRL data. If an error is detected, the return value will instead be a Map with keys errorhttpStatusskyQueryErrorskyQueryErrorMsg, and skyQueryReturnValue. On the other hand if the response to the underlying HTTP request does not indicate an error, the return value is the KRL data computed by the remote function.

Use http:get() directly

Since each pico is a first-class Internet citizen, it has an API, so we can use techniques that are applicable to any external API, as discussed in the Modules and External APIs Lesson. The same example might be written this way:

...