Tutorial: Platform.applySnapshot

Platform.applySnapshot

Adds a snapshot to a running Platform.

Example

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

const snapshot = {
    windows: [
        {
            layout: {
                content: [
                    {
                        type: "stack",
                        id: "no-drop-target",
                        content: [
                            {
                                type: "component",
                                componentName: "view",
                                componentState: {
                                    name: "component_X",
                                    showDevTools: true,
                                    url: "https://www.openfin.co"
                                }
                            },
                            {
                                type: "component",
                                componentName: "view",
                                componentState: {
                                    name: "component_Y",
                                    showDevTools: true,
                                    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://www.my-url.com/snapshot.json');



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://bing.com'
    }, winId);
}

async function createWindowWithTwoViews() {
    const identity = {
        name: 'test window',
        uuid: 'OpenFin POC'
    };

    await platform.createWindow({
        ...identity,
        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"
                            }
                        }
                    ]
                }
            ]
        }
    });

    return fin.Window.wrapSync(identity);
}

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