diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-04-27 23:21:10 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-04-28 20:11:46 +0800 |
commit | 53537824f179038d227c9a340302d039ab151ede (patch) | |
tree | c25aed7d2fad00204f869be933941d5f5dfe9602 /mascara/src | |
parent | e7e0919d7c76c818590df4435db0152298298bd9 (diff) | |
download | tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar.gz tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar.bz2 tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar.lz tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar.xz tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.tar.zst tangerine-wallet-browser-53537824f179038d227c9a340302d039ab151ede.zip |
break out IndexDbController && Run the first time test on mascara
Diffstat (limited to 'mascara/src')
-rw-r--r-- | mascara/src/background.js | 15 | ||||
-rw-r--r-- | mascara/src/lib/index-db-controller.js | 71 | ||||
-rw-r--r-- | mascara/src/ui.js | 20 |
3 files changed, 33 insertions, 73 deletions
diff --git a/mascara/src/background.js b/mascara/src/background.js index 746c479f9..a6703b291 100644 --- a/mascara/src/background.js +++ b/mascara/src/background.js @@ -22,6 +22,7 @@ const STORAGE_KEY = 'metamask-config' // const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' const METAMASK_DEBUG = true let popupIsOpen = false +let connectedClientCount = 0 const log = require('loglevel') global.log = log @@ -32,6 +33,11 @@ self.addEventListener('install', function(event) { }) self.addEventListener('activate', function(event) { event.waitUntil(self.clients.claim()) + self.clients.matchAll() + .then((clients) => { + if (connectedClientCount < clients.length) sendMessageToAllClients('reconnect') + }) + }) console.log('inside:open') @@ -108,6 +114,7 @@ function setupController (initState, client) { connectionListener.on('remote', (portStream, messageEvent) => { console.log('REMOTE CONECTION FOUND***********') + connectedClientCount += 1 connectRemote(portStream, messageEvent.data.context) }) @@ -143,4 +150,12 @@ function setupController (initState, client) { return Promise.resolve() } + +function sendMessageToAllClients (message) { + self.clients.matchAll().then(function(clients) { + clients.forEach(function(client) { + client.postMessage(message) + }) + }) +} function noop () {} diff --git a/mascara/src/lib/index-db-controller.js b/mascara/src/lib/index-db-controller.js deleted file mode 100644 index 5aded1cbe..000000000 --- a/mascara/src/lib/index-db-controller.js +++ /dev/null @@ -1,71 +0,0 @@ -const EventEmitter = require('events') -module.exports = class IndexDbController extends EventEmitter { - - constructor (opts) { - super() - global.IDBTransaction = global.IDBTransaction || global.webkitIDBTransaction || global.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers - global.IDBKeyRange = global.IDBKeyRange || global.webkitIDBKeyRange || global.msIDBKeyRange - this.migrations = opts.migrations - this.key = opts.key - this.version = opts.version - this.initialState = opts.initialState - } - - // Opens the database connection and returns a promise - open () { - return this.get('dataStore') - .then((data) => { - if (!data) { - return this._add('dataStore', this.initialState) - .then(() => this.get('dataStore')) - .then((versionedData) => Promise.resolve(versionedData)) - } - return Promise.resolve(data) - }) - } - - - get (key = 'dataStore') { - return this._request('get', key) - } - put (state) { - return this._request('put', state, 'dataStore') - } - - _add (key = 'dataStore', objStore) { - return this._request('add', objStore, key) - } - - _request (call, ...args) { - return new Promise((resolve, reject) => { - const self = this - const dbOpenRequest = global.indexedDB.open(this.key, this.version) - - dbOpenRequest.addEventListener('upgradeneeded', (event) => { - this.db = event.target.result - this.db.createObjectStore('dataStore') - }) - - dbOpenRequest.onsuccess = (event) => { - this.db = dbOpenRequest.result - this.emit('success') - const dbTransaction = this.db.transaction('dataStore', 'readwrite') - const request = dbTransaction.objectStore('dataStore') - const objRequest = request[call](...args) - objRequest.addEventListener('success', (event) => { - return resolve(objRequest.result) - }) - objRequest.addEventListener('error', (err) => { - return reject(`IndexDBController - ${call} failed to excute on indexedDB`) - }) - dbTransaction.addEventListener('complete', (event) => { - this.emit('complete') - }) - } - - dbOpenRequest.addEventListener('error', (event) => { - return reject({message: `IndexDBController - open:@${call} failed to excute on indexedDB`, errorEvent: event}) - }) - }) - } -} diff --git a/mascara/src/ui.js b/mascara/src/ui.js index cdf985ebd..07763cc5e 100644 --- a/mascara/src/ui.js +++ b/mascara/src/ui.js @@ -27,7 +27,7 @@ const background = new SWcontroller({ wakeUpInterval: 20000 }) // Setup listener for when the service worker is read -background.on('ready', (readSw) => { +const connectApp = function (readSw) { let connectionStream = SwStream({ serviceWorker: background.controller, context: name, @@ -37,8 +37,24 @@ background.on('ready', (readSw) => { store.subscribe(() => { const state = store.getState() if (state.appState.shouldClose) window.close() + console.log('IN the things?') }) }) +} + +background.on('ready', (sw) => { + background.removeListener('updatefound', connectApp) + connectApp(sw) +}) +background.on('updatefound', () => background.serviceWorkerApi.ready + .then((sw) =>{ + background.removeListener('ready', connectApp) + connectApp(sw.active) + }) +) +background.on('message', (messageEvent) => { + console.log(messageEvent) }) -background.startWorker() +window.addEventListener('load', () => background.startWorker()) +// background.startWorker() console.log('hello from MetaMascara ui!') |