Tutorial: Platform.init

Platform.init

Initializes a Platform and launches the windows described in its snapshot.

Must be called from the Provider when using a custom provider.

Basic Example

// From Provider context
await fin.Platform.init();
// Platform API is now hooked up and windows contained in the manifest snapshot are open.

Platform.init accepts an options object that can contain a callback function which can be used to extend or replace default Provider behavior. As an argument, this function will receive the Provider class, which is used to handle Platform actions. The function must return an object with methods to handle Platform API actions. The recommended approach is to extend the Provider class, overriding the methods you wish to alter, and return an instance of your subclass.

Example with API behavior override

const overrideCallback = async (PlatformProvider) => {
    // Actions can be performed before initialization.
    // e.g. we might authenticate a user, set up a Channel, etc before initializing the Platform.
    const { manifestUrl } = await fin.Application.getCurrentSync().getInfo();

    // Extend or replace default PlatformProvider behavior by extending the PlatformProvider class.
    class MyOverride extends PlatformProvider {
        // Default behavior can be changed by implementing methods with the same names as those used by the default PlatformProvider.
        async getSnapshot() {
            // Since we are extending the class, we can call `super` methods to access default behavior.
            const snapshot = await super.getSnapshot();
            // But we can modify return values.
            return { ...snapshot, answer: 42, manifestUrl };
        }
        async replaceLayout({ opts, target }) {
            // To disable an API method, overwrite with a noop function.
            return;
        }
    }
    // Return instance with methods to be consumed by Platform.
    // The returned object must implement all methods of the PlatformProvider class.
    // By extending the class, we can simply inherit methods we do not wish to alter.
    return new MyOverride();
};

fin.Platform.init({overrideCallback});