Data Sharing Between Apps

Shared Contexts

JavaScript

Updating a Context

The Shared Contexts API can be accessed through the glue.contexts object.

See our JavaScript Shared Contexts example on GitHub.

Use the glue.contexts.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.

glue.contexts.update("app-styling",
    {
        backgroundColor: "red",
        alternativeColor: "green"
    });

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

glue.contexts.update("app-styling",
    {
        alternativeColor: null
    });

Signature of the glue.contexts.update() method:

glue.contexts.update(name: string, delta: Object)
  • name - the name of the context to update;
  • delta - an object that will be merged with the existing context object;

Replacing a Context

Other than updating a context, you have the option to replace its object value completely by using the glue.contexts.set() method, which has the following signature:

glue.contexts.set(name: string, newContextValue: Object)
  • name - the name of the context to update;
  • newContextValue - the object that will be the new context object value;

This wipes all previous properties of the context object, as opposed to the glue.contexts.update() method, which only updates them.

Example:

// removes completely the current context object, containing the backgroundColor property
// and sets an entirely new context object value with a new backgroundColor property 
glue.contexts.set("app-styling", { backgroundColor: "red" });

Subscribing for Context Updates

To subscribe for context updates, use the glue.contexts.subscribe() method, which has the following signature:

subscribe(name: string, callback: Function)
  • name - the name of the context you want your app to subscribe to;
  • callback: (context: Object, delta: Object, removed: string[]) - a callback function which will be invoked when the context is updated:
    • delta - a subset of the context, containing the properties which were updated;
    • removed - an array of context keys which were removed;

Example:

glue.contexts.subscribe("app-styling",
    (context, delta, removed) => {
        // use context here
    });

Unsubscribing

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

let unsubscribe;

glue.contexts
    .subscribe("app-styling", handleContextUpdate)
    .then(unsubscribeFunction => unsubscribe = unsubscribeFunction);

unsubscribe();

Listing All Available Contexts

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

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

Reference

Reference