Tutorial: Platform.applySnapshot

Platform.applySnapshot

Adds a snapshot to a running Platform.

Example

Pass in a snapshot. It will create any windows and views that are not running but are passed in the snapshot object. Any View specified in the snapshot is assigned a randomly generated name to avoid collisions.

// Get a wrapped layout platform instance
const platform = await fin.Platform.getCurrent();

const snapshot = {
    windows: [
        {
            layout: {
                content: [
                    {
                        type: 'stack',
                        content: [
                            {
                                type: 'component',
                                componentName: 'view',
                                componentState: {
                                    name: 'component_X',
                                    url: 'https://www.openfin.co'
                                }
                            },
                            {
                                type: 'component',
                                componentName: 'view',
                                componentState: {
                                    name: 'component_Y',
                                    url: 'https://cdn.openfin.co/embed-web/chart.html'
                                }
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

platform.applySnapshot(snapshot);

Example

In place of a snapshot object, applySnapshot can take a url or filepath and to retrieve a JSON snapshot.

const platform = await fin.Platform.getCurrent();
platform.applySnapshot('https://api.jsonbin.io/b/5e6f903ef4331e681fc1231d/1');

Example

Optionally, applySnapshot can close existing windows and restore a Platform to a previously saved state. This is accomplished by providing { closeExistingWindows: true } as an option.

// Get a wrapped layout platform instance
const platform = await fin.Platform.getCurrent();

async function addViewToWindow(winId) {
    return await platform.createView({
        name: 'test_view_3',
        url: 'https://openfin.co'
    }, winId);
}

async function createWindowWithTwoViews() {
    const platform = await fin.Platform.getCurrent();

    return platform.createWindow({
        layout: {
            content: [
                {
                    type: 'stack',
                    content: [
                        {
                            type: 'component',
                            componentName: 'view',
                            componentState: {
                                name: 'test_view_1',
                                url: 'https://example.com'
                            }
                        },
                        {
                            type: 'component',
                            componentName: 'view',
                            componentState: {
                                name: 'test_view_2',
                                url: 'https://yahoo.com'
                            }
                        }
                    ]
                }
            ]
        }
    });
}

const win = await createWindowWithTwoViews();
// ... you will now see a new window with two views in it

// we take a snapshot of the current state of the app, before changing it
const snapshotOfInitialAppState = await platform.getSnapshot();

// now let's change the state of the app:
await addViewToWindow(win.identity);
// ... the window now has three views in it

await platform.applySnapshot(snapshotOfInitialAppState, { closeExistingWindows: true });
// ... the window will revert to previous state, with just two views