From 9331fa1856166f3ab34057a019dff48110a6a1ad Mon Sep 17 00:00:00 2001 From: Kim Joar Bekkelund Date: Fri, 7 Jul 2017 12:57:54 +0200 Subject: [PATCH 1/2] Cleaner Pid observable code --- platform/plugins/pid/PidFile.ts | 2 +- platform/plugins/pid/PidService.ts | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/platform/plugins/pid/PidFile.ts b/platform/plugins/pid/PidFile.ts index 3eb6f4a86ba0d6..9ecee42ac2e20f 100644 --- a/platform/plugins/pid/PidFile.ts +++ b/platform/plugins/pid/PidFile.ts @@ -47,4 +47,4 @@ export class PidFile { this.log.debug(`deleting pid file [${path}]`); unlinkSync(path); } -} \ No newline at end of file +} diff --git a/platform/plugins/pid/PidService.ts b/platform/plugins/pid/PidService.ts index f8e22ac4c4ec93..868b24797d7c31 100644 --- a/platform/plugins/pid/PidService.ts +++ b/platform/plugins/pid/PidService.ts @@ -6,17 +6,14 @@ import { PidFile } from './PidFile'; import { LoggerFactory } from '../../logger'; export class PidService { - private readonly pid$: Observable; + private readonly pid$: Observable; private subscription?: Subscription; - constructor(pidConfig$: Observable, logger: LoggerFactory) { + constructor( + pidConfig$: Observable, + logger: LoggerFactory + ) { this.pid$ = pidConfig$ - .map( - config => - config !== undefined - ? new PidFile(process.pid, config, logger) - : undefined - ) // Explanation of `switchMap`: // It's kinda like a normal `flatMap`, except it's producing observables // and you _only_ care about the latest observable it produced. It's @@ -24,17 +21,19 @@ export class PidService { // and when you're done with an observable, like here where we want to // write the pid file we receive a pid config, and delete it when we // receive new config values (or when we stop the pid service). - .switchMap(pid => { - if (pid === undefined) { - // If pid is not specified, we return an observable that does nothing - return new Observable(noop); + .switchMap(config => { + if (config === undefined) { + // If there is no pid config we return an observable that does nothing + return new Observable(noop); } // Otherwise we return an observable that writes the pid when // subscribed to and deletes it when unsubscribed (e.g. if new config // is received or if `stop` is called below.) - return new Observable(observable => { + return new Observable(observable => { + const pid = new PidFile(process.pid, config, logger); + pid.writeFile(); return function unsubscribe() { From feee61a9fb2a17d52186d767ce7f32324450c4a0 Mon Sep 17 00:00:00 2001 From: Kim Joar Bekkelund Date: Fri, 7 Jul 2017 18:12:28 +0200 Subject: [PATCH 2/2] remove unused arg --- platform/plugins/pid/PidService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/plugins/pid/PidService.ts b/platform/plugins/pid/PidService.ts index 868b24797d7c31..3ec4622bab77df 100644 --- a/platform/plugins/pid/PidService.ts +++ b/platform/plugins/pid/PidService.ts @@ -31,7 +31,7 @@ export class PidService { // subscribed to and deletes it when unsubscribed (e.g. if new config // is received or if `stop` is called below.) - return new Observable(observable => { + return new Observable(() => { const pid = new PidFile(process.pid, config, logger); pid.writeFile();