Skip to content

Instantly share code, notes, and snippets.

@freaktechnik
Created April 20, 2018 18:48
Show Gist options
  • Select an option

  • Save freaktechnik/80dda043b233190ed5cfd0dfc0d5bd5f to your computer and use it in GitHub Desktop.

Select an option

Save freaktechnik/80dda043b233190ed5cfd0dfc0d5bd5f to your computer and use it in GitHub Desktop.
/**
* A Promise that centralizes initialization of ExtensionStorageSync.
*
* This centralizes the use of the Sqlite database, to which there is
* only one connection which is shared by all threads.
*
* Fields in the object returned by this Promise:
*
* - connection: a Sqlite connection. Meant for internal use only.
* - kinto: a KintoBase object, suitable for using in Firefox. All
* collections in this database will use the same Sqlite connection.
* @returns {Promise<Object>}
*/
async function storageSyncInit() {
// Memoize the result to share the connection.
if (storageSyncInit.promise === undefined) {
const path = "storage-sync.sqlite";
/**
* @static
* @type {Promise<Object>}
*/
storageSyncInit.promise = FirefoxAdapter.openConnection({path})
.then(connection => {
return {
connection,
kinto: new Kinto({
adapter: FirefoxAdapter,
adapterOptions: {sqliteHandle: connection},
timeout: KINTO_REQUEST_TIMEOUT,
retry: 0,
}),
};
}).catch(e => {
// Ensure one failure doesn't break us forever.
Cu.reportError(e);
storageSyncInit.promise = undefined;
throw e;
});
}
return storageSyncInit.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment