-
Notifications
You must be signed in to change notification settings - Fork 1
Container Client Documentation
Alexei Darmin edited this page Feb 26, 2022
·
2 revisions
import { ContainerClient } from "@statflo/widget-sdk";
const widgetContainerClient = new ContainerClient({ window });
Initialization mandatory properties:
-
windowthe window object that will be used to.postMessage()to the container client.- For server-side rendered JS, a synthetic window can be used to build successfully such as the domino NPM package.
setState(id, property, value) : Updates the state of the widget by setting the property to equal the value. Syncs local widget state with remote container state, triggering all downstream callbacks for the container's state subscribers as well.
post(id, eventName, value) : Posts an event to the container with eventName and value
.on(eventName, callback) : A method to subscribe a callback to the designated event name. Note that this function can also subscribe to event methods such as containerMethods.setState.
- A widget's state can be modified by invoking the
client.setState(). - One property can be changed at a time
- For example in order to store
"myValue"into the property"myProp"one would executeclient.setState("widgetId", "myProp", "myValue").
- For example in order to store
- Widget state updates propagate to the container, and subscribers to the container's state as well. See container receiving state updates for more info.
client.setState("widgetId", "header", "my cool widget");
import { ContainerMethods } from "@statflo/widget-sdk/dist/shared";
client.on(ContainerMethods.setState, (e) => {
const { property, value } = e.payload;
...
});
- Widgets can send events to the container that don't result in stateful updates.
- This can be accomplished via the
client.post()method.
client.post("widgetId", "myEventName", "myEventPayload")
This will trigger any functions within the container client that are subscribed to "myEventName"
client.on("containerEventName", (e) => { ... })