Tutorial: system.downloadRuntime

system.downloadRuntime

Downloads the given OpenFin Runtime.

Example downloading immediately

const downloadOptions = {
    //Specific version number required, if given a release channel the call will produce an error.
    version: '6.49.21.9'
};

function onProgress(progress) {
    const downloadedPercent = Math.floor((progress.downloadedBytes / progress.totalBytes) * 100);
    console.log(`Downloaded ${downloadedPercent}%`);
}

function onComplete() {
    console.log('Runtime now downloaded');
}

function onError(reason ,err) {
    console.log(`Error downloading the runtime: ${err.message}, stack: ${err.stack}`);
}

fin.desktop.System.downloadRuntime(downloadOptions, onProgress, onComplete , onError);

Example scheduling a download

Here we will demonstrate how we can schedule a download for 5 minutes.

function downloadRuntime(delayInMinutes) {

    const downloadOptions = {
		//Specific version number required, if given a release channel the call will produce an error.
		version: '6.49.21.9'
    };

    setTimeout(() => {
		fin.desktop.System.downloadRuntime(downloadOptions, progress => {
			document.open();
			document.write(`${Math.floor((progress.downloadedBytes / progress.totalBytes) * 100)}%`);
			document.close();	
		}, () => {
			document.open();
			document.write('Download complete');
			document.close();
		}, (r, e) => {
			document.open();
			document.write(`Download Failed, we could retry: ${e.message}`);
			document.close();
			console.log(e);
		});
    }, delayInMinutes * 60000);
}

//We create a new Application that will download the Runtime version given.
const app = new fin.desktop.Application({
    url: 'about:blank',
    uuid: `my_download_app_${ Math.floor(Math.random() * 10000) }`,
    name: "Application Name",
    mainWindowOptions: {
        autoShow: true
    }
}, () => {
    console.log("Application successfully created");
    app.run(() => {
	//We inject the downloadRuntime function in the new application and call it with a delay of 5 minutes.
	app.getWindow().executeJavaScript(`${ downloadRuntime.toString() }; downloadRuntime(5);`);
    });
}, (r, e) => {
    console.log("Error creating application:", e.message);
});