'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.setupWorker = undefined;
















var _workerActions = require('./workerActions');

var runtime = null; // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.
// FIXME: This should be move to dapp-develop
var PromiseWorker = null;var WebWorker = null;try {PromiseWorker = require('promise-worker');WebWorker = require('worker-loader!../../webWorker.js'); // eslint-disable-line
  runtime = require('serviceworker-webpack-plugin/lib/runtime');} catch (error) {}function setupServiceWorker() {if (!('serviceWorker' in navigator) || !runtime) {return Promise.reject(new Error('ServiceWorker is not available in your browser.'));}

  var getServiceWorker = function getServiceWorker() {
    return navigator.serviceWorker.ready.
    then(function (registration) {
      var worker = registration.active;

      worker.controller = registration.active;

      return new PromiseWorker(worker);
    });
  };

  return new Promise(function (resolve, reject) {
    // Safe guard for registration bugs (happens in Chrome sometimes)
    var timeoutId = window.setTimeout(function () {
      console.warn('could not register ServiceWorker after 2.5s');
      getServiceWorker().then(resolve).catch(reject);
    }, 2500);

    // Setup the Service Worker
    runtime.
    register().
    then(function () {
      window.clearTimeout(timeoutId);
      return getServiceWorker();
    }).
    then(resolve).catch(reject);
  });
}

function getWorker() {
  try {
    var worker = new PromiseWorker(new WebWorker());

    return Promise.resolve(worker);
  } catch (error) {
    return Promise.reject(error);
  }
}

var setupWorker = exports.setupWorker = function setupWorker(store) {var
  dispatch = store.dispatch,getState = store.getState;

  var state = getState();
  var stateWorker = state.worker.worker;

  if (stateWorker !== undefined) {
    return;
  }

  getWorker().
  then(function (worker) {
    dispatch((0, _workerActions.setWorker)(worker));
  }).
  catch(function (error) {
    console.warn('setupWorker', error);
    dispatch((0, _workerActions.setWorker)(null));
  });
};

// Setup the Service Worker
setupServiceWorker().
then(function () {return console.log('ServiceWorker is setup');}).
catch(function (error) {return console.warn('ServiceWorker error', error);});