Tutorial: Platform.setWindowContext

Platform.setWindowContext

Set a window's customContext. This context will be accessible from the window itself and from any child views via getWindowContext. It will be saved in any platform snapshots and available upon application of those snapshots. The context data must be serializable. This can only be called from a window or view that has been launched into a platform. This method can be called from the window itself, or from any child view. Context data is shared by all entities within a window.

Example 1: Setting own context

const platform = fin.Platform.getCurrentSync();
const contextData = {
    security: 'STOCK',
    currentView: 'detailed'
}

await platform.setWindowContext(contextData);
// Context of current window is now set to `contextData`

Example 2: Setting the context of another window or view

const platform = fin.Platform.getCurrentSync();
const contextData = {
    security: 'STOCK',
    currentView: 'detailed'
}

const windowOrViewIdentity = { uuid: fin.me.uuid, name: 'nameOfWindowOrView' };
await platform.setWindowContext(contextData, windowOrViewIdentity);
// Context of the target window or view is now set to `contextData`

Example 3: Listening to context updates

A view can listen to changes to its host window's context by listening to the host-context-changed event. This event will fire when a host window's context is updated or when the view is reparented to a new window.

// From a view
const contextChangeHandler = ({ context }) => {
    console.log('Host window\'s context has changed. New context data:', context);
    // react to new context data here
}
await fin.me.on('host-context-changed', contextChangeHandler);

const platform = await fin.Platform.getCurrentSync();
const contextData = {
    security: 'STOCK',
    currentView: 'detailed'
}
platform.setWindowContext(contextData) // contextChangeHandler will log the new context

To listen to a window's context updates, use the context-changed devent.

// From a window
const contextChangeHandler = ({ context }) => {
    console.log('This window\'s context has changed. New context data:', context);
    // react to new context data here
}
await fin.me.on('context-changed', contextChangeHandler);

const platform = await fin.Platform.getCurrentSync();
const contextData = {
    security: 'STOCK',
    currentView: 'detailed'
}
platform.setWindowContext(contextData) // contextChangeHandler will log the new context