services/settings/RemoteSettingsWorker.jsm
author Mathieu Leplatre <mathieu@mozilla.com>
Fri, 10 May 2019 22:57:40 +0000
changeset 532303 255211227da24911c8ce2112de7f9d26d2a13bbf
parent 531917 89ab5386f54bc1f39b5ea368cc3e6f0d2952e410
child 532889 5765e6384ff13d09dce8580df9abb4ed63178a31
permissions -rw-r--r--
Bug 1501214 - Reusable helper to download Remote Settings attachments r=nanj,glasserc,Gijs Reusable helper to download Remote Settings attachments Differential Revision: https://phabricator.services.mozilla.com/D28707

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * Interface to a dedicated thread handling for Remote Settings heavy operations.
 */

// ChromeUtils.import("resource://gre/modules/PromiseWorker.jsm", this);

var EXPORTED_SYMBOLS = ["RemoteSettingsWorker"];

class Worker {
  constructor(source) {
    this.worker = new ChromeWorker(source);
    this.worker.onmessage = this._onWorkerMessage.bind(this);

    this.callbacks = new Map();
    this.lastCallbackId = 0;
  }

  async _execute(method, args = []) {
    return new Promise(async (resolve, reject) => {
      const callbackId = ++this.lastCallbackId;
      this.callbacks.set(callbackId, [resolve, reject]);
      this.worker.postMessage({ callbackId, method, args });
    });
  }

  _onWorkerMessage(event) {
    const { callbackId, result, error } = event.data;
    const [resolve, reject] = this.callbacks.get(callbackId);
    if (error) {
      reject(new Error(error));
    } else {
      resolve(result);
    }
    this.callbacks.delete(callbackId);
  }

  async canonicalStringify(localRecords, remoteRecords, timestamp) {
    return this._execute("canonicalStringify", [localRecords, remoteRecords, timestamp]);
  }

  async importJSONDump(bucket, collection) {
    return this._execute("importJSONDump", [bucket, collection]);
  }

  async checkFileHash(filepath, size, hash) {
    return this._execute("checkFileHash", [filepath, size, hash]);
  }
}

var RemoteSettingsWorker = new Worker("resource://services-settings/RemoteSettingsWorker.js");