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

createMemo should respect the equals value on its initial execution #2362

Open
snnsnn opened this issue Nov 12, 2024 · 5 comments
Open

createMemo should respect the equals value on its initial execution #2362

snnsnn opened this issue Nov 12, 2024 · 5 comments

Comments

@snnsnn
Copy link

snnsnn commented Nov 12, 2024

A memo accept an initial value and the equals property via the options object to exert finer control over its update logic. However it does not respect the equals property on its initial execution but it should for consistency.

For the following example, 'some value' should never be assigned to the memo as it is not a valid value.

import { createEffect, createSignal, createMemo } from 'solid-js';

const [preferences, setPreferences] = createSignal({
  lang: 'en', theme: 'some value', fontSize: 'large',
});

const theme = createMemo(() => preferences().theme, 'light', {
  equals: (prev, curr) => {
    if (prev === curr) return true;
    if (prev === 'dark' && curr === 'light') return false;
    if (prev === 'light' && curr === 'dark') return false;
    return true;
  }
});

createEffect(() => console.log(theme()));
@atk
Copy link
Contributor

atk commented Nov 13, 2024

But equals by definition compares the last value to the next one. As there is no last value initially, you would have to guard your comparison against it being undefined.

In your case, a filter for invalid values should be done within the derivation, not the equals function.

@snnsnn
Copy link
Author

snnsnn commented Nov 13, 2024

I know how it works but what is the point of having an initial value if you don't utilize it, if you think about it, it is only logical to use the custom comparator function for the initial execution too. It better aligns with the behavior of a signal and it will reduce clutter as we can rely on the equals function to validate the next value, avoiding unnecessary conditionals.

@atk
Copy link
Contributor

atk commented Nov 13, 2024

createMemo cannot know if you are intending to use the initial value or not.

@snnsnn
Copy link
Author

snnsnn commented Nov 13, 2024

createMemo cannot know if you are intending to use the initial value or not.

Please can you elaborate because I am not sure if I understand what do you mean by this sentence?

If current behaviour is OK, then what is the purpose of the initial value? More importantly what difference does it make if createMemo uses the equals value for the initial execution? Memo will be executed any way, equals controls if the returned value should replace the existing one, providing a consistent behavior.

@atk
Copy link
Contributor

atk commented Nov 13, 2024

This was merely an answer to your statement that having an initial value was pointless if you're not using it. But that's merely your preference based on a single case.

In Solid, Ryan usually aims for the most helpful, predictable and performant behavior. Since an initial equality comparison without a previous value will be unhelpful except for very few edge cases, totally unexpected and easily provoking errors and provides the worse performance, he chose not to apply it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants