Skip to content

RxJS should prevent Subject values to be modified by anyone but a "next" push. #6046

@vicmarcal

Description

@vicmarcal

Bug Report

Current Behavior
Subjects values can be easily messed up via getValue() or subject.value since they are returning references. Imho, by design, the only way a Subject value should be able to be modified is through Subject.next(). Otherwise the Subject.value can be modified by a function1() and hence a function2() can obtain inconsistent values.

Expected behavior
RxJS should prevent Subject.values to be modified by anone but a "next" push.

Reproduction
This reproduction example shows 2 Test Cases.

First test case is just reading the subject.value, manipulating the returned value and as a side effect modifying subject value (and probably totally unnoticed for the developer due the "sticky" references Javascript has).
Worst thing is that a second function, which could live in a different file/method, now is seeing a wrong Subject value and outputting a wrong result.

Second test case is showing how subject.getValue() is really returning also a reference, so the behavior is exactly the same. (Just checking if it was making any difference really)

RxJS should protect the value/s being stored in the Subjects. If a value was pushed to a Subject, no one should be able to modify such value. This is imho critical in a language, as Javascript is, where references are so sticky.

Environment
Latest RXJS, provided in the stackblitz.

Possible Solution
If, for retrocompatibility, subject.value and getValue() are decided to keep returning references, then I would suggest to create a new getDeepValue() method which returns a deep clone to avoid the mess created in the example and heavily recommend to use getDeepValue() in documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions