Shared Contexts
1.6.5A Shared Context is a named object (holding a map
of key
/value
pairs) that stores cross application data. The context object can hold any desktop-wide or cross-application data. Any application can update a context or subscribe for update notifications (by using the name of the context). Apps can also react to context changes (by subscribing for context updates) or update the context at runtime.
The Shared Contexts API offers a simple and effective solution for sharing data between your applications. Imagine you have an application showing a list of clients and an application showing client portfolios. What you need, is your "Portfolio" app to show the portfolio of a specific client that the user has selected from the "Clients" app. You can easily achieve this in a few simple steps by using the Shared Contexts API:
- instruct the "Clients" app to publish updates to a context object, holding the
id
of the currently selected client; - instruct the "Portfolio" app to subscribe for updates of that same context object and specify how the "Portfolio" app should handle the received data in order to update its current state;
The Shared Contexts API can be accessed through the glue.contexts
object.
See also the Shared Contexts documentation for more details.
APIobject
Methods
allmethod
Signature
() => string[]
Description
Returns all existing context names. Using the context name you can subscribe for context changes, updates or set context values.
getmethod
Signature
(name: string, resolveImmediately?: boolean) => Promise<any>
Description
Return the context data immediately or asynchronously as soon as any data becomes available.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context from which you want to get data. |
|
resolveImmediately | boolean | If |
setmethod
Signature
(name: string, data: any) => Promise<void>
Description
Replaces a context. All properties of the specified context object will be removed and replaced with the ones supplied in the data
parameter.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be replaced. |
|
data | any | The object that will be applied to the context. |
subscribemethod
Signature
(name: string, callback: (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void) => Promise<() => void>
Description
Subscribes for context events. Returns an unsubscribe function which you can use to stop receiving context updates.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to which you want to subscribe. |
|
callback | (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void | Function that will handle the updates. |
updatemethod
Signature
(name: string, data: any) => Promise<void>
Description
Updates a context with the supplied object. This method updates only the specified context properties. Any other existing context properties will remain intact.
If the context does not exist, the update()
method will create it.
Parameters
Name | Type | Required | Description |
---|---|---|---|
name | string | Name of the context to be updated. |
|
data | any | The object that will be applied to the context. |
Example
glue.contexts.update("app-styling", {
backgroundColor: "red",
alternativeColor: "green"
});
ContextDeltaobject
Description
Context delta when updating or replacing a context.
Properties
Property | Type | Default | Required | Description |
---|---|---|---|---|
added | ContextEntries | Context properties that were added. |
||
removed | string[] | Context properties that were removed. |
||
reset | ContextEntries | Context properties that were reset. |
||
updated | ContextEntries | Context properties that were updated. |