Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base-controller] ControllerMessenger selectors skip initial value #3649

Closed
Gudahtt opened this issue Dec 11, 2023 · 0 comments · Fixed by #3697
Closed

[base-controller] ControllerMessenger selectors skip initial value #3649

Gudahtt opened this issue Dec 11, 2023 · 0 comments · Fixed by #3697
Assignees
Labels
bug Something isn't working team-wallet-framework

Comments

@Gudahtt
Copy link
Member

Gudahtt commented Dec 11, 2023

The ControllerMessenger subscribe function accepts an optional "selector" which can act like a Redux selector in that it narrows the state to just the subset that is of interest to that subscriber. The subscriber isn't invoked unless that specific piece of state is updated.

However, these selectors don't consider the initial state. They will always run upon the first state change, regardless what has changed.

We should update the ControllerMessenger to cache the initial state as well, so that we can use that for a point of comparison even on the first update.

@Gudahtt Gudahtt added the bug Something isn't working label Dec 11, 2023
@Gudahtt Gudahtt self-assigned this Dec 22, 2023
Gudahtt added a commit that referenced this issue Dec 22, 2023
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Dec 22, 2023
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Dec 22, 2023
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Jan 3, 2024
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Jan 8, 2024
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Jan 9, 2024
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
Gudahtt added a commit that referenced this issue Jan 9, 2024
The "selectors" feature of the `ControllerMessenger` did not work
correctly for the first event published after subscribing, because the
original value of the payload (at time of subscribing) was not
available for comparison.

To address this, a `registerInitialEventPayload` method has been added
to the `ControllerMessenger` and `RestrictedControllerMessenger`. This
allows registering a function that returns the "event payload state" at
a given time, which is used internally by the `subscribe` method to
correctly initialize the payload cache when a subscriber with a
selector is added. This ensures that the selector workds correctly even
for the initial state change, in that it will return the correct
previous value, and not fire at all if the selected piece of state is
unchanged.

In a later PR, `BaseControllerV2` can be updated to register an event
payload for the `stateChange` event. This will fix this flaw for the
`stateChange` event of each controller.

Fixes #3649
@Gudahtt Gudahtt closed this as completed in 580f07e Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-wallet-framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants