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

Deprecate derived units in target #563

Closed
AlexandrHoroshih opened this issue Nov 24, 2021 · 9 comments
Closed

Deprecate derived units in target #563

AlexandrHoroshih opened this issue Nov 24, 2021 · 9 comments
Assignees
Labels
bug Something isn't working core effector package

Comments

@AlexandrHoroshih
Copy link
Member

AlexandrHoroshih commented Nov 24, 2021

What is the current behavior:
Usage of .on method is deprecated on derived stores and to be removed in the next release - this is very good news and, i think, makes everything a lot more consistent and predictable 👍

But now usage of derived stores as a target in operators is not warned or error by effector, so the problem of unexpected changes to derived stores still persists 😢

$readOnlyStore = $store.map(v => v)

// yay! error in console!
$readOnlyStore.on(ev, () => {})

// no error :(
sample({
 clock: $store,
 target: $readOnlyStore,
})

Also i think that stores created by restore from an event or effect should be considered as derived too, since they also have explicit dependency at the moment of creation: restore(changed, null) 🤔
UPD: Since restored event state still can be used by app and can't be derived from, for e.g., state in serialized scope during SSR, it is definetly not derived (at least, in a sense similiar to mapped stores)

Please provide the steps to reproduce and if possible a minimal demo of the problem via https://share.effector.dev, https://codesandbox.io or similar

https://share.effector.dev/yqvd359F

What is the expected behavior:
Any attempt of adding a new source of updates to dervied store is forbidden and warning/error is shown.
If such behaviour is needed - it is better to create classic (not derived) store and subscribe it to changes of original derived store.

Which versions of effector packages, and which browser and OS are affected by this issue? Did this work in previous versions of effector?:
effector@22.0.0^

@AlexandrHoroshih AlexandrHoroshih added bug Something isn't working needs triage labels Nov 24, 2021
@zerobias zerobias added core effector package and removed needs triage labels Nov 24, 2021
@zerobias zerobias self-assigned this Nov 24, 2021
@zerobias zerobias added this to the effector 22.2.0 milestone Nov 24, 2021
@zerobias
Copy link
Member

So we need to add such validations to:

  • forward to
  • sample target
  • guard target
  • split cases

Anything else?

@sergeysova
Copy link
Collaborator

Maybe add validation on the kernel level? To add support for patronum

@zerobias
Copy link
Member

zerobias commented Nov 25, 2021

This is added to the metadata of the graph node anyway, assume usage in that way:

if (unit.graphite.meta.derived) throw Error('derived unit cannot be used as a target')

Even more, this is already implemented like that

@AlexandrHoroshih AlexandrHoroshih changed the title Deprecate derived stores in target Deprecate derived units in target Nov 26, 2021
@AlexandrHoroshih
Copy link
Member Author

AlexandrHoroshih commented Nov 26, 2021

I updated a REPL example a bit:

  • dervied events in target are also not warned yet
  • merge returns an not-derived unit and its direct call is not warned

https://share.effector.dev/yqvd359F

@zerobias
Copy link
Member

@AlexandrHoroshih thanks, I wrote more tests based on your findings

zerobias added a commit that referenced this issue Nov 26, 2021
tests with direct calls of derived units returned by methods:
- merge
- split
- sample
- guard

tests with derived units in targets of methods:
- split
- sample
- guard
- forward

each `undefined` snapshot is a place where should be a warning

related issue #563
zerobias added a commit that referenced this issue Nov 26, 2021
affected methods:
- merge
- split
- sample
- guard

related issue #563
zerobias added a commit that referenced this issue Nov 26, 2021
affected methods:
- sample target
- guard target
- split cases
- forward to

related issue #563
@sergeysova
Copy link
Collaborator

Also i think that stores created by restore from an event or effect should be considered as derived too, since they also have explicit dependency at the moment of creation: restore(changed, null) 🤔

I cannot agree. restore(event, default) invented to be a shortcut for createStore(default).on(event, (_,i) => i). So it is should create a NEW store. Example use case:

const nameChanged = createEvent<string>();
const $name = restore(nameChanged, '');

But in real life you want to reset form and load data to form fields:

$name.reset(formCleanup);

spread({
  source: formLoadFx.doneData,
  targets: {
    name: $name,
  },
})

As we see it is a common store, not derived.

@AlexandrHoroshih
Copy link
Member Author

AlexandrHoroshih commented Dec 1, 2021

I cannot agree. restore(event, default) invented to be a shortcut for createStore(default).on(event, (_,i) => i). So it is should create a NEW store

It is how it works know, yes, but same works for mapped stores now and this behavoir is still deprecated for a reason

But yeah, restored event is probably not a derived store, because its state definitely should be transferred over the network during SSR - otherwise app rendered on the client will be different from the render at server

@sergeysova
Copy link
Collaborator

but same works for mapped stores now

Not the same semantics.

Mapped store it the explicit contract with the single source of truth (single source of updates).

Here we proof store has no updates source except $a:

$b = $a.map(fn)

Here the same, but source of updates is $c AND $d:

$e = combine($c, $d, (c, d) => c + d)

@zerobias
Copy link
Member

zerobias commented Dec 8, 2021

Deprecation warning will be published in the next minor release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core effector package
Projects
None yet
Development

No branches or pull requests

3 participants