Skip to content

Commit

Permalink
Use ReplaySubject instead of BehaviorSubject in RawConfigService.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Aug 28, 2018
1 parent 2b15fac commit d21d693
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/core/server/config/raw_config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,21 @@
*/

import { cloneDeep, isEqual, isPlainObject } from 'lodash';
import { BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged, filter, map } from 'rxjs/operators';
import { Observable, ReplaySubject } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import typeDetect from 'type-detect';

import { Config } from './config';
import { ObjectToConfigAdapter } from './object_to_config_adapter';
import { getConfigFromFiles } from './read_config';

// Used to indicate that no config has been received yet
const notRead = Symbol('config not yet read');

export class RawConfigService {
/**
* The stream of configs read from the config file. Will be the symbol
* `notRead` before the config is initially read, and after that it can
* potentially be `null` for an empty yaml file.
* The stream of configs read from the config file.
*
* This is the _raw_ config before any overrides are applied.
*
* As we have a notion of a _current_ config we rely on a BehaviorSubject so
* every new subscription will immediately receive the current config.
*/
private readonly rawConfigFromFile$ = new BehaviorSubject<any>(notRead);
private readonly rawConfigFromFile$ = new ReplaySubject<number>(1);

This comment has been minimized.

Copy link
@spalger

spalger Aug 28, 2018

Contributor

Kind of surprised TypeScript isn’t complaint about this being a ReplaySubject of numbers that is receiving rawConfig objects.

This comment has been minimized.

Copy link
@azasypkin

azasypkin Aug 28, 2018

Author Member

Haha, I was playing with that and tried to figure out why it doesn't complain and forgot to remove :/ Bummer, 1:40h for another CI run :/

This comment has been minimized.

Copy link
@azasypkin

azasypkin Aug 28, 2018

Author Member

We have very "relaxed" ensureDeepObject that accepts any and returns any, I'll try to make it more typed in a separate PR.


private readonly config$: Observable<Config>;

Expand All @@ -50,7 +42,6 @@ export class RawConfigService {
new ObjectToConfigAdapter(rawConfig)
) {
this.config$ = this.rawConfigFromFile$.pipe(
filter(rawConfig => rawConfig !== notRead),
// We only want to update the config if there are changes to it.
distinctUntilChanged(isEqual),
map(rawConfig => {
Expand Down

0 comments on commit d21d693

Please sign in to comment.