diff options
-rw-r--r-- | app/scripts/metamask-controller.js | 1 | ||||
-rw-r--r-- | app/scripts/popup-core.js | 1 | ||||
-rw-r--r-- | library/controller.js | 30 | ||||
-rw-r--r-- | library/lib/setup-iframe.js | 1 | ||||
-rw-r--r-- | library/popup.js | 18 | ||||
-rw-r--r-- | library/server.js | 2 | ||||
-rw-r--r-- | library/sw-controller.js | 67 | ||||
-rw-r--r-- | library/sw-core.js | 24 | ||||
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | ui/index.js | 2 |
10 files changed, 96 insertions, 54 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 536891dc6..a26c0e45d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -204,7 +204,6 @@ module.exports = class MetamaskController extends EventEmitter { // getState () { - const wallet = this.configManager.getWallet() const vault = this.keyringController.store.getState().vault const isInitialized = (!!wallet || !!vault) diff --git a/app/scripts/popup-core.js b/app/scripts/popup-core.js index b1e521a7a..7ade66922 100644 --- a/app/scripts/popup-core.js +++ b/app/scripts/popup-core.js @@ -11,6 +11,7 @@ module.exports = initializePopup function initializePopup (connectionStream) { // setup app + debugger connectToAccountManager(connectionStream, setupApp) } diff --git a/library/controller.js b/library/controller.js index bad8d33cd..f0aa7172c 100644 --- a/library/controller.js +++ b/library/controller.js @@ -1,19 +1,13 @@ -const SWcontroller = require('./sw-controller') -console.log('outside:open') -const background = new SWcontroller({ - fileName: 'sw-build.js', - registerOpts: { - scope: './', - } -}) +// const SWcontroller = require('./sw-controller') +// const SwStream = require('sw-stream/lib/sw-stream.js') +// const startPopup = require('../app/scripts/popup-core') -background.startWorker() -.then(registerdWorker => { - return background.sendMessage('connect') -}) -.then((port) => { - debugger -}) -.catch(err => { - console.error(`SW Controller: ${err}`) -}) +// console.log('outside:open') +// const background = new SWcontroller({ +// fileName: 'sw-build.js', +// }) +// background.on('ready', (readSw) => { +// startPopup(SwStream(background.controller)) +// }) +// background.startWorker() +console.log('hello from controller') diff --git a/library/lib/setup-iframe.js b/library/lib/setup-iframe.js index db67163df..0aa001dcf 100644 --- a/library/lib/setup-iframe.js +++ b/library/lib/setup-iframe.js @@ -5,6 +5,7 @@ module.exports = setupIframe function setupIframe(opts) { + debugger opts = opts || {} var frame = Iframe({ src: opts.zeroClientProvider || 'https://zero.metamask.io/', diff --git a/library/popup.js b/library/popup.js index 59b70edbb..3825661cf 100644 --- a/library/popup.js +++ b/library/popup.js @@ -1,7 +1,10 @@ const injectCss = require('inject-css') const MetaMaskUiCss = require('../ui/css') -const startPopup = require('../app/scripts/popup-core') const setupIframe = require('./lib/setup-iframe.js') +const MetamaskInpageProvider = require('../app/scripts/lib/inpage-provider.js') +const SWcontroller = require('./sw-controller') +const SwStream = require('sw-stream/lib/sw-stream.js') +const startPopup = require('../app/scripts/popup-core') var css = MetaMaskUiCss() @@ -15,5 +18,14 @@ var iframeStream = setupIframe({ sandboxAttributes: ['allow-scripts', 'allow-popups', 'allow-same-origin'], container: document.body, }) - -startPopup(iframeStream) +console.log('outside:open') +const background = new SWcontroller({ + fileName: '/popup/sw-build.js', +}) +background.on('ready', (readSw) => { + // var inpageProvider = new MetamaskInpageProvider(SwStream(background.controller)) + // startPopup(inpageProvider) + startPopup(SwStream(background.controller)) +}) +background.startWorker() +console.log('hello from /library/popup.js') diff --git a/library/server.js b/library/server.js index b9f59c2a1..a284d3e05 100644 --- a/library/server.js +++ b/library/server.js @@ -25,7 +25,7 @@ iframeServer.use('/popup', express.static('../dist/chrome')) iframeServer.get('/controller.js', function(req, res){ res.send(controllerBundle.latest) }) -iframeServer.get('/sw-build.js', function(req, res){ +iframeServer.get('/popup/sw-build.js', function(req, res){ console.log('/sw-build.js') res.setHeader('Content-Type', 'application/javascript') res.send(swBuild.latest) diff --git a/library/sw-controller.js b/library/sw-controller.js index d5065a933..23d67026e 100644 --- a/library/sw-controller.js +++ b/library/sw-controller.js @@ -1,34 +1,63 @@ const EventEmitter = require('events') -module.exports = class serviceWorkerController extends EventEmitter{ +module.exports = class ClientSideServiceWorker extends EventEmitter{ constructor (opts) { super() this.fileName = opts.fileName - this.registerOpts = opts.registerOpts - this.serviceWorker = navigator.serviceWorker - } + this.startDelay = opts.startDelay + this.serviceWorkerApi = navigator.serviceWorker + this.serviceWorkerApi.onmessage = (event) => this.emit('message', event) + this.serviceWorkerApi.onerror = (event) => this.emit('error') - startWorker () { - // check to see if their is a preregistered service worker - if (!this.serviceWorker.controller) { - return Promise.resolve(this.registerWorker()) - } else { - return Promise.resolve(this.serviceWorker.ready) + // temporary function + this.askForId = (message) => { + this.sendMessage('check-in') + .then((data) => console.log(`${message}----${data}`)) } + + // if (!!this.serviceWorkerApi) this.askForId('before') + + if (opts.initStart) this.startWorker() + + this.on('ready', (sw) => { + this.askForId('ready') + }) } - registerWorker () { - return this.serviceWorker.register(this.fileName, this.registerOpts) - .then(sw => { - return sw + get controller () { + return this.sw || this.serviceWorkerApi.controller + } + + + startWorker () { + return this.registerWorker() + .then((sw) => { + this.sw = sw + this.askForId('after register:') + this.sw.onerror = (err) => this.emit('error', err) + this.sw = sw + this.emit('ready', this.sw) }) + .catch((err) => this.emit('error', err)) } - syncSW (registeredSW) { - return registeredSW.sync.register('sync') - .then(() => { - console.log('sync') + registerWorker () { + return this.serviceWorkerApi.register(this.fileName) + .then((registerdWorker) => { + return new Promise((resolve, reject) => { + this.askForId('after') + let timeOutId = setTimeout(() => { + if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller) + if (registerdWorker.active) return resolve(registerdWorker.active) + return reject(new Error('ClientSideServiceWorker: No controller found and onupdatefound timed out')) + }, this.startDelay || 1000 ) + + registerdWorker.onupdatefound = (event) => { + this.emit('updatefound') + registerdWorker.update() + } + }) }) } @@ -43,7 +72,7 @@ module.exports = class serviceWorkerController extends EventEmitter{ resolve(event.data.data) } } - self.serviceWorker.controller.postMessage(message, [messageChannel.port2]) + this.controller.postMessage(message, [messageChannel.port2]) }) } } diff --git a/library/sw-core.js b/library/sw-core.js index 41fce637f..9ceaf1dc5 100644 --- a/library/sw-core.js +++ b/library/sw-core.js @@ -25,6 +25,10 @@ const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' let popupIsOpen = false +const log = require('loglevel') +global.log = log +log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn') + self.addEventListener('install', function(event) { event.waitUntil(self.skipWaiting()) }) @@ -132,12 +136,16 @@ function setupController (initState, client) { global.metamaskController = controller // setup state persistence - pipe( - controller.store, - storeTransform(versionifyData), - diskStore - ) - + // pipe( + // controller.store, + // storeTransform(versionifyData), + // diskStore + // ) + controller.store.subscribe((store) => { + dbController.put('dataStore', store) + // .then((event) => {debugger}) + // .catch((err) => {debugger}) + }) function versionifyData(state) { let versionedData = diskStore.getState() versionedData.data = state @@ -150,10 +158,8 @@ function setupController (initState, client) { /* need to write a service worker stream for this */ - // var connectionStream = new ParentStream() connectionListener.on('remote', (portStream, messageEvent) => { - debugger - connectRemote(connectionStream, messageEvent.origin) + connectRemote(portStream, messageEvent.origin) }) function connectRemote (connectionStream, originDomain) { diff --git a/package.json b/package.json index 30e89356f..1d19d5e75 100644 --- a/package.json +++ b/package.json @@ -104,14 +104,14 @@ "request-promise": "^4.1.1", "sandwich-expando": "^1.0.5", "semaphore": "^1.0.5", - "sw-stream": "^1.0.1", + "sw-stream": "^1.0.2", "textarea-caret": "^3.0.1", "three.js": "^0.73.2", "through2": "^2.0.1", "valid-url": "^1.0.9", "vreme": "^3.0.2", "web3": "0.18.2", - "web3-provider-engine": "^10.0.1", + "web3-provider-engine": "^11.0.0", "web3-stream-provider": "^2.0.6", "xtend": "^4.0.1" }, diff --git a/ui/index.js b/ui/index.js index 1a65f813c..932054027 100644 --- a/ui/index.js +++ b/ui/index.js @@ -14,7 +14,7 @@ log.setLevel(debugMode ? 'debug' : 'warn') function launchApp (opts) { var accountManager = opts.accountManager actions._setBackgroundConnection(accountManager) - + debugger // check if we are unlocked first accountManager.getState(function (err, metamaskState) { if (err) throw err |