Subjects - apply changes on every next value [BehaviorSubject.onBeforeNext?]
<p>Say you have a simple BehaviorSubject</p>

<pre><code>this.countryOfBirth$ = new BehaviorSubject&lt;CountryEnum&gt;(null);

get countryOfBirth() {
    return this.countryOfBirth$.value;
};

set countryOfBirth(val: CountryEnum) {
    this.countryOfBirth$.next(val);
};
</code></pre>

<p>So doing <code>instance.countryOfBirth</code> returns the last value and <code>instance.countryOfBirth = CountryEnum.US</code> pushes the new value to the Subject.</p>

<p>The problem I'm having is that I pass this subject instance to a custom model-driven form module which by default would wrongly pass a string instead of an instance of my <code>CountryEnum</code> class.</p>

<p>I can fix this in the forms module but I would like to keep that as decoupled as possible from any app-specific logic so it would make more sense to implement the fix in the BehaviorSubject instance itself.</p>

<p>My question is: is there hook or any other way to apply some changes to each and every "next" value before it triggers its subscribers? In other words, after my code does</p>

<pre><code>instance.countryOfBirth = CountryEnum.US;
</code></pre>

<p>Before any of the subscribers are triggered I would like to check whether the value is a string (ex: US) and if so - I would like to get the corresponding CountryEnum instance and pass that to the "next" call instead of the original "US" string.</p>

<p>In code it would look something like</p>

<pre><code>this.countryOfBirth$.onBeforeNext((val) =&gt; {
    if (typeof val == "string") {
        return CountryEnum.getInstance(val);
    } else if (val instanceof CountryEnum) {
        return val;
    } else {
        throw new Error("Unsupported value type");
    }
});
</code></pre>

<p>but obviously <code>onBeforeNext</code> doesn't exist and I can't seem to find anything in the dox that would do what I want.</p>

<p>Your help would be much appreciated!</p>