the-dr-lazy / purescript-monarch

A simple but powerful PureScript library for declaring pure UIs
Mozilla Public License 2.0
9 stars 0 forks source link

2x `update` function call on each message #12

Closed the-dr-lazy closed 3 years ago

the-dr-lazy commented 3 years ago

Description

Every time when a message triggers. The update function applies twice but because of distinctUntilRefChanged operator the second model doesn't emit to the downstream.

Minimal, Reproducible Example

The example in the repository is prone to this issue.

Steps to Reproduce

Just add a trace to the update function to reveal the function call.

update :: Message -> Model -> Model
update = case _ of
  Increase -> trace "increase" (\_ -> (_ + 1))
  Decrease -> (_ - 1)

Expected Behavior

On each message the update function should just be called once.

Actual Behavior

2x call of update function on each message.

Browsers Affected

Versions

Etiology

The issue is because the eModel is an Event not a Queue (BTW we should choose a better name for it :|). Each time when we subscribe to the eModel event it adds a subscriber to qMessage queue and each subscriber will be called separately.

https://github.com/the-dr-lazy/purescript-monarch/blob/403bcfa4030ff4a9f707ca15c824b196c2f298d1/src/Monarch/Platform.purs#L126-L127

Solution

The eModel event should be convert to a queue.