Skip to main content

State

The sim.State is a simple key/value in-memory store for the simulator with support for lazy tokens.

Consider a use case where there is an attribute of a simulated service that only gets resolved during initialization (e.g. the exposed port of a container). In order to create such resources, we need a way to obtain a lazy token that gets resolved during simulator initialization. It just so happens that we already have a mechanism like this, but it was not exposed as a public API.

Use the preflight method state.token(key) to obtain a token that can be used to reference the value of the state at runtime.

During simulator app initialization (i.e. cloud.OnDeploy or cloud.Service startup), you must call the inflight method state.set(key, value) to set the runtime value. The value will be available at runtime through the inflight method state.get(key), or via a resolved token.

Usage

Let's say I want to create a simulated service that has property called startTime which returns the time the service was started. This information is also known when the service is actually initialized.

bring cloud;
bring sim;

class MyService {
startTime: str;

new() {
let state = new sim.State();

new cloud.Service(inflight () => {
state.set("start", std.Datetime.utcNow().toIso());
});

this.startTime = state.token("start");
}
}

In this example, the startTime property will resolve to a simulator token (something like ${root/MyService/State#attrs.start)}) which can be referenced ("lifted") into inflight context naturally:

let s = new MyService();

new cloud.Function(inflight () => {
log("service start time is {s}");
});

This creates an implicit dependency between the cloud.Function and MyService such that only when the state start is set, the cloud.Function will be initialized with the actual value.

API Reference

Resources

State

Key/value in-memory state for the simulator.

Use the preflight method token(key) to obtain a token that can be used to reference the value of the state at runtime.

During deployment (i.e. cloud.OnDeploy or cloud.Service startup), you must call the inflight method set(key, value) to set the runtime value. The value will be available at runtime through the inflight method get(key) (or resolved as a token).

See tests for examples.

Initializers

bring sim;

new sim.State();
NameTypeDescription

Methods

Preflight Methods
NameDescription
token
Returns a token that can be used to retrieve the value of the state after the simulation has run.
toSimulator
Convert this resource to a resource schema for the simulator.
Inflight Methods
NameDescription
get
Gets the runtime state of this object.
set
Sets the state of runtime a runtime object.
tryGet
Checks if runtime state exists for this object and returns it's value.

token
token(key: str): str

Returns a token that can be used to retrieve the value of the state after the simulation has run.

keyRequired
  • Type: str

The object key retrieved through the inflight state.get().


toSimulator
toSimulator(): ToSimulatorOutput

Convert this resource to a resource schema for the simulator.

get
inflight get(key: str): Json

Gets the runtime state of this object.

Throws if there is no value for the given key.

keyRequired
  • Type: str

The object's key.


set
inflight set(key: str, value: Json): void

Sets the state of runtime a runtime object.

keyRequired
  • Type: str

The object's key.


valueRequired

The object's value.


tryGet
inflight tryGet(key: str): Json?

Checks if runtime state exists for this object and returns it's value.

If no value exists, returns nil.

keyRequired
  • Type: str

The object's key.


Static Functions

NameDescription
onLiftType
A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.
toInflight
Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource.

onLiftType
bring sim;

sim.State.onLiftType(host: IInflightHost, ops: MutArray<str>);

A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.

The list of requested inflight methods needed by the inflight host are given by ops.

This method is commonly used for adding permissions, environment variables, or other capabilities to the inflight host.

hostRequired

opsRequired
  • Type: MutArray<str>

toInflight
bring sim;

sim.State.toInflight(obj: IResource);

Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource.

NOTE: This statement must be executed within an async context.

objRequired

Properties

NameTypeDescription
node
constructs.NodeThe tree node.

nodeRequired
node: Node;
  • Type: constructs.Node

The tree node.