diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-03-24 02:06:38 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-03-24 02:06:38 +0800 |
commit | 45ab81fb45ec1043d617f66c308e2e7911c8e0ad (patch) | |
tree | 335c7abdb8491b39184f07c9426ae30792546385 /library | |
parent | d4e8ff188bc82a974fd0a767c2676d4a4b9747b4 (diff) | |
download | tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar.gz tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar.bz2 tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar.lz tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar.xz tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.tar.zst tangerine-wallet-browser-45ab81fb45ec1043d617f66c308e2e7911c8e0ad.zip |
WIP: streams->indexdb
Diffstat (limited to 'library')
-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 |
6 files changed, 92 insertions, 50 deletions
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) { |