From 75a67b61c2bcfa9d9d3d0cca4d5e9ec58345712d Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Wed, 7 Nov 2018 08:10:26 +0100 Subject: [PATCH] Simplify promises --- src/components/touch-entropy/touch-entropy.ts | 46 +++++++++---------- src/providers/audio/audio.browser.service.ts | 8 ++-- src/providers/audio/audio.native.service.ts | 30 +++++------- src/providers/camera/camera.native.service.ts | 31 ++++++------- src/providers/entropy/entropy.service.ts | 28 +++++------ .../gyroscope/gyroscope.native.service.ts | 25 +++++----- 6 files changed, 72 insertions(+), 96 deletions(-) diff --git a/src/components/touch-entropy/touch-entropy.ts b/src/components/touch-entropy/touch-entropy.ts index 41815513..0ecc3b5d 100644 --- a/src/components/touch-entropy/touch-entropy.ts +++ b/src/components/touch-entropy/touch-entropy.ts @@ -71,40 +71,36 @@ export class TouchEntropyComponent implements OnInit, IEntropyGenerator { start(): Promise { this.collectedEntropyPercentage = 0 - return new Promise((resolve, reject) => { - this.renderer.listen(this.canvas, 'mousedown', (e) => { - this.isDrawing = true - }) - - this.renderer.listen(this.canvas, 'touchstart', (e) => { - this.isDrawing = true - }) + this.renderer.listen(this.canvas, 'mousedown', (e) => { + this.isDrawing = true + }) - this.renderer.listen(this.canvas, 'mouseup', (e) => { - this.isDrawing = false - }) + this.renderer.listen(this.canvas, 'touchstart', (e) => { + this.isDrawing = true + }) - this.renderer.listen(this.canvas, 'touchend', (e) => { - this.isDrawing = false - }) + this.renderer.listen(this.canvas, 'mouseup', (e) => { + this.isDrawing = false + }) - this.renderer.listen(this.canvas, 'mousemove', (e) => { - if (this.isDrawing) this.collectEntropy(e) - }) + this.renderer.listen(this.canvas, 'touchend', (e) => { + this.isDrawing = false + }) - this.renderer.listen(this.canvas, 'touchmove', (e) => { - if (this.isDrawing) this.collectEntropy(e) - }) + this.renderer.listen(this.canvas, 'mousemove', (e) => { + if (this.isDrawing) this.collectEntropy(e) + }) - resolve() + this.renderer.listen(this.canvas, 'touchmove', (e) => { + if (this.isDrawing) this.collectEntropy(e) }) + + return Promise.resolve() } stop(): Promise { - return new Promise((resolve, reject) => { - this.isDrawing = false - resolve() - }) + this.isDrawing = false + return Promise.resolve() } collectEntropy(e) { diff --git a/src/providers/audio/audio.browser.service.ts b/src/providers/audio/audio.browser.service.ts index eed4ae26..8a6a877f 100644 --- a/src/providers/audio/audio.browser.service.ts +++ b/src/providers/audio/audio.browser.service.ts @@ -63,11 +63,9 @@ export class AudioBrowserService implements IEntropyGenerator { } stop(): Promise { - return new Promise((resolve, reject) => { - this.microphoneStreamSource.disconnect() - this.scriptProcessor.disconnect() - resolve() - }) + this.microphoneStreamSource.disconnect() + this.scriptProcessor.disconnect() + return Promise.resolve() } getEntropyUpdateObservable(): Observable { diff --git a/src/providers/audio/audio.native.service.ts b/src/providers/audio/audio.native.service.ts index 9792615c..5c318c9f 100644 --- a/src/providers/audio/audio.native.service.ts +++ b/src/providers/audio/audio.native.service.ts @@ -39,31 +39,25 @@ export class AudioNativeService implements IEntropyGenerator { start(): Promise { this.collectedEntropyPercentage = 0 - return new Promise((resolve, reject) => { - this.platform.ready().then(() => { + return this.platform.ready().then(() => { - window.audioinput.start({ - bufferSize: this.ENTROPY_SIZE - }) - - setTimeout(() => { - window.addEventListener('audioinput', this.handler) - }, 1000) + window.audioinput.start({ + bufferSize: this.ENTROPY_SIZE + }) - console.log('audioinput created.') + setTimeout(() => { + window.addEventListener('audioinput', this.handler) + }, 1000) - resolve() - }) + console.log('audioinput created.') }) } stop(): Promise { - return new Promise((resolve, reject) => { - console.log('removed audioinput listener') - window.audioinput.stop() - window.removeEventListener('audioinput', this.handler) - resolve() - }) + console.log('removed audioinput listener') + window.audioinput.stop() + window.removeEventListener('audioinput', this.handler) + return Promise.resolve() } getEntropyUpdateObservable(): Observable { diff --git a/src/providers/camera/camera.native.service.ts b/src/providers/camera/camera.native.service.ts index acb5151c..22eb0785 100644 --- a/src/providers/camera/camera.native.service.ts +++ b/src/providers/camera/camera.native.service.ts @@ -69,10 +69,8 @@ export class CameraNativeService implements IEntropyGenerator { start(): Promise { this.disabled = false this.collectedEntropyPercentage = 0 - return new Promise((resolve, reject) => { - this.platform.ready().then(() => { - resolve(this.initCamera()) - }) + return this.platform.ready().then(() => { + return this.initCamera() }) } @@ -151,25 +149,22 @@ export class CameraNativeService implements IEntropyGenerator { // if it is called while taking a photo if (this.cameraIsTakingPhoto) { this.uninjectCSS() - return new Promise((resolve, reject) => { - setTimeout(() => { - console.log('CAMERA IS TAKING PHOTO, DELAYING') - return this.stop() - }, 200) - }) + setTimeout(() => { + console.log('CAMERA IS TAKING PHOTO, DELAYING') + this.stop() + }, 200) + return new Promise(() => {}) } this.uninjectCSS() if (this.cameraInterval) { clearInterval(this.cameraInterval) } - return new Promise((resolve, reject) => { - this.cameraPreview.stopCamera().then(() => { - this.cameraIsRunning = false - console.log('camera stopped.') - }, error => { - console.log('camera could not be stopped.') - reject(error) - }) + return this.cameraPreview.stopCamera().then(() => { + this.cameraIsRunning = false + console.log('camera stopped.') + }, error => { + console.log('camera could not be stopped.') + return Promise.reject(error) }) } diff --git a/src/providers/entropy/entropy.service.ts b/src/providers/entropy/entropy.service.ts index 6f2bb2d3..2e768cb5 100644 --- a/src/providers/entropy/entropy.service.ts +++ b/src/providers/entropy/entropy.service.ts @@ -60,26 +60,22 @@ export class EntropyService { stopEntropyCollection(): Promise { let promises = [] - return new Promise((resolve, reject) => { - // clear collection interval - for (let i = 0; i < this.entropySubscriptions.length; i++) { - this.entropySubscriptions[i].unsubscribe() - } + // clear collection interval + for (let i = 0; i < this.entropySubscriptions.length; i++) { + this.entropySubscriptions[i].unsubscribe() + } - this.entropySubscriptions = [] + this.entropySubscriptions = [] - // stop entropy sources - for (let i = 0; i < this.entropyGenerators.length; i++) { - console.log('stopping entropy source...') - promises.push(this.entropyGenerators[i].stop()) - } + // stop entropy sources + for (let i = 0; i < this.entropyGenerators.length; i++) { + console.log('stopping entropy source...') + promises.push(this.entropyGenerators[i].stop()) + } - this.entropyGenerators = [] + this.entropyGenerators = [] - Promise.all(promises).then(() => { - resolve() - }) - }) + return Promise.all(promises).then(() => {}) } getEntropyAsHex(): Promise { diff --git a/src/providers/gyroscope/gyroscope.native.service.ts b/src/providers/gyroscope/gyroscope.native.service.ts index 998b30b1..634f2fb5 100644 --- a/src/providers/gyroscope/gyroscope.native.service.ts +++ b/src/providers/gyroscope/gyroscope.native.service.ts @@ -24,21 +24,18 @@ export class GyroscopeNativeService implements GyroscopeService, IEntropyGenerat public start(): Promise { this.collectedEntropyPercentage = 0 - return new Promise((resolve, reject) => { - this.platform.ready().then(() => { - this.entropyObservable = new Observable(observer => { - this.gyroSubscription = this.deviceMotion.watchAcceleration({ frequency: 500 }).subscribe((acceleration: DeviceMotionAccelerationData) => { - const entropyBuffer = this.arrayBufferFromIntArray([acceleration.x, acceleration.y, acceleration.z]) - entropyCalculatorWorker.onmessage = (event) => { - this.collectedEntropyPercentage += event.data.entropyMeasure - observer.next({ - entropyHex: event.data.entropyHex - }) - } - entropyCalculatorWorker.postMessage({ entropyBuffer: entropyBuffer }, [entropyBuffer]) - }) + return this.platform.ready().then(() => { + this.entropyObservable = new Observable(observer => { + this.gyroSubscription = this.deviceMotion.watchAcceleration({ frequency: 500 }).subscribe((acceleration: DeviceMotionAccelerationData) => { + const entropyBuffer = this.arrayBufferFromIntArray([acceleration.x, acceleration.y, acceleration.z]) + entropyCalculatorWorker.onmessage = (event) => { + this.collectedEntropyPercentage += event.data.entropyMeasure + observer.next({ + entropyHex: event.data.entropyHex + }) + } + entropyCalculatorWorker.postMessage({ entropyBuffer: entropyBuffer }, [entropyBuffer]) }) - resolve() }) }) }