Closed jrista closed 3 years ago
Example of the issue:
Usage should only be called unsafe if it is indeed unsafe. By globally denying use of first within effects (which is what is happening now), it can hamper the ability of developers to implement the most effective code that will solve the problems at hand.
Should be fixed in 4.0.3.
I have encountered a number of use cases where the
rxjs/no-unsafe-first
rule seems to catch safe usage offirst
within effects. Whenever an inner stream from aswitch|exhaust|concat|merge|flatMap
is piped andfirst
is used within that nested pipe, the usage should be save. Not only that, in some cases it is necessary to ensure that the inner streams complete in an appropriate manner, when completion is required. Without usage offirst
ortake
within nested streams, particularly withconcatMap
,mergeMap
orflatMap
, some inner streams that do not complete can either hang up the queue (concatMap
) indefinitely, or result in leaking inner streams (merge|flatMap
).A quick example of some code that interacts with FireStore via AngularFirestore. When a save event is dispatched, one effect either calls
set
orupdate
, which dispatches another action that allows another effect to wait for eithervalueChanges
orstateChanges
to feed the changed data back into the app. The second effect correlates the saved entity when it comes back through, and dispatches a final action representing successful save:The inner usage of
first
here is indeed safe, and has been verified as so (I've just tested several use cases, none of which caused completion of the effect, only of observables nested withinmergeMap
,flatMap
, orconcatMap
). When a save occurs and the updated entity data is fed back into the app, the inner stream completes, because of thefirst
filter, after it dispatches the success action. Withoutfirst
, ortake
, and instead iffilter
is used on the inner stream, themergeMap
here will leak the inner streams, and every time a save action comes through, all prior inner streams plus an additional one will all handle theentityAdded
andentityModified
streams. This continues indefinitely, or until the web app is reloaded.Unsafe usage of
first
would primarily be within the OUTER stream, not inner streams. So if I usedfirst
ortake
anywhere at the same level as themergeMap
operator is used, that would indeed complete the effect, and would be an unsafe usage. Right now, it appears as though the rule is catching ANY and ALL usage offirst
and considering it "unsafe", when in fact there are indeed safe use cases forfirst
,take
, etc. within effects. Not only that, in some use cases, their usage is essential!