-
Notifications
You must be signed in to change notification settings - Fork 8
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
Difficult to copy over parts of an observable #182
Comments
If we do decide to go with the "exposing the raw() function" plan, we could choose to make it part of the interface for the stores, or part of the Tram-One library. The most trivial way to implement this would be through Tram-One imports: import { useStore, raw } from 'tram-one'
const useNumbers = () => {
const workingStore = useStore({ numbers: [1,2,3,4] });
const savedStore = useStore({ numbers: [1,2,3] });
const onSave = () => {
savedStore.numbers = raw(workingStore).numbers
}
} But we could add a import { useStore } from 'tram-one'
const useNumbers = () => {
const workingStore = useStore({ numbers: [1,2,3,4] });
const savedStore = useStore({ numbers: [1,2,3] });
const onSave = () => {
savedStore.numbers = workingStore.raw().numbers
}
} |
Conversely, we could expose an interface that is specifically for "copying over parts-of or entire stores to other stores". This could allow us to deliver a purposeful |
This branch exposes a |
Summary
As a side-effect of how stores are made (proxy objects), when you set a property of a store to another store's property (for example, when managing a saved state), you get the reference to the other store, not it's value:
This can be worked around by either of the following ways:
[...workingStore.numbers]
raw()
function that the observable library providesThe first solution can be cumbersome an error prone, as the store structure gets larger or more complicated.
The second solution could lead to misuse / confusion on how stores work - up until this point they are treated as black-box objects that "just work", exposing a
raw()
function could lead to people interacting with that more than they should, in ways they really shouldn't / don't need to.Conversely, exposing the
raw()
function could also be really really useful for debugging purposes, right now it is somewhat non-trivial to inspect a the proxy-observable store, but this would make it much easier.The text was updated successfully, but these errors were encountered: