'use strict';

// Reload client for Chrome Apps & Extensions.
// The reload client has a compatibility with livereload.
// WARNING: only supports reload command.

const LIVERELOAD_HOST = 'localhost:';
const LIVERELOAD_PORT = 35729;
const connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload');

connection.onerror = (error) => {
  console.log('reload connection got error:', error);
};

connection.onmessage = (e) => {
  if (e.data) {
    const data = JSON.parse(e.data);
    if (data && data.command === 'reload') {
      chrome.runtime.reload();
    }
  }
};

'use strict';

// Only attach to the reload server if we're in development!
chrome.management.getSelf((r)=>{
  let debug = r.installType === 'development';
  if (!debug) return;

  attachReloader();
});

// Reload client for Chrome Apps & Extensions.
// The reload client has a compatibility with livereload.
// WARNING: only supports reload command.
function attachReloader() {

  const LIVERELOAD_HOST = 'localhost:';
  const LIVERELOAD_PORT = 35729;
  const connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload');

  connection.onerror = (error) => {
    console.log('reload connection got error:', error);
  };

  connection.onmessage = (e) => {
    if (e.data) {
      const data = JSON.parse(e.data);
      if (data && data.command === 'reload') {
        chrome.runtime.reload();
      }
    }
  };

}
load chrome reload client only when we're in development