Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify promises #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions src/components/touch-entropy/touch-entropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,36 @@ export class TouchEntropyComponent implements OnInit, IEntropyGenerator {

start(): Promise<void> {
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<void> {
return new Promise((resolve, reject) => {
this.isDrawing = false
resolve()
})
this.isDrawing = false
return Promise.resolve()
}

collectEntropy(e) {
Expand Down
8 changes: 3 additions & 5 deletions src/providers/audio/audio.browser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ export class AudioBrowserService implements IEntropyGenerator {
}

stop(): Promise<any> {
return new Promise((resolve, reject) => {
this.microphoneStreamSource.disconnect()
this.scriptProcessor.disconnect()
resolve()
})
this.microphoneStreamSource.disconnect()
this.scriptProcessor.disconnect()
return Promise.resolve()
}

getEntropyUpdateObservable(): Observable<Entropy> {
Expand Down
30 changes: 12 additions & 18 deletions src/providers/audio/audio.native.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,25 @@ export class AudioNativeService implements IEntropyGenerator {

start(): Promise<void> {
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<void> {
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<Entropy> {
Expand Down
31 changes: 13 additions & 18 deletions src/providers/camera/camera.native.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ export class CameraNativeService implements IEntropyGenerator {
start(): Promise<void> {
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()
})
}

Expand Down Expand Up @@ -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)
})
}

Expand Down
28 changes: 12 additions & 16 deletions src/providers/entropy/entropy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,22 @@ export class EntropyService {

stopEntropyCollection(): Promise<void> {
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<string> {
Expand Down
25 changes: 11 additions & 14 deletions src/providers/gyroscope/gyroscope.native.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ export class GyroscopeNativeService implements GyroscopeService, IEntropyGenerat

public start(): Promise<void> {
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()
})
})
}
Expand Down