Data Sharing Between Apps

Retrieving Context Data

The Shared Contexts API is accessible through the glue.contexts object.

See the JavaScript Shared Contexts example on GitHub.

To get the names of all currently available shared contexts, use the all() method:

// Returns a string array with the available context names.
const availableContexts = glue.contexts.all();

To get the value of a specific context object, use the get() method:

const data = await glue.contexts.get("app-styling");

Updating a Context

Use the update() method to create a new shared context or update the properties of an existing shared context. New properties (context keys) will be added, existing ones will be updated, and you can also remove context keys by setting them to null.

const contextUpdate = {
    backgroundColor: "red",
    alternativeColor: "green"
};

await glue.contexts.update("app-styling", contextUpdate);

To remove keys, send a context update and set them to null:

const keysToRemove = { alternativeColor: null };

await glue.contexts.update("app-styling", keysToRemove);

Replacing a Context

Other than updating a context, you have the option to replace its value completely by using the set() method:

const newContext = { backgroundColor: "purple" };

// This will completely overwrite the existing context value.
await glue.contexts.set("app-styling", newContext);

The set() method overwrites the existing context object, as opposed to the update() method, which only updates the values of its properties.

Subscribing for Context Updates

To subscribe for context updates, use the subscribe() method. It accepts the name of the context as a first required parameter and a function that will handle the context updates as a second required parameter:

const handler = (context, delta, removed) => {
    const bgColor = context.backgroundColor;

    console.log(bgColor);
});

await glue.contexts.subscribe("app-styling", handler); 

Unsubscribing

The subscribe() method returns a Promise which resolves with a function you can use to unsubscribe from context updates:

const unsubscribe = await glue.contexts.subscribe("app-styling", handler);

unsubscribe();

Destroying a Context

To destroy a context object, use the destroy() method:

await glue.contexts.destroy("app-styling");

Reference

For a complete list of the available Shared Contexts API methods and properties, see the Shared Contexts API Reference Documentation.