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

Array indexing #59

Closed
Toubat opened this issue Apr 21, 2022 · 3 comments
Closed

Array indexing #59

Toubat opened this issue Apr 21, 2022 · 3 comments

Comments

@Toubat
Copy link

Toubat commented Apr 21, 2022

It seems like array assignment by index is not supported currently. For example, if I have a store

export const store = syncedStore({
  todos: [] as Todo[],
  myText: {} as { text?: any },
  arr: [],
  fragment: "xml"
});

Then I type:

store.arr.push(1);
store.arr[0] = 1;

It gives me an error says array assignment is not supported. Is there any other way I can do to accomplish equivalent operation?

@ofabel
Copy link

ofabel commented Apr 29, 2022

Change your array of numbers to an array of objects with one single attribute. Then modify that attribute.

export const store = syncedStore({
  todos: [] as Todo[],
  myText: {} as { text?: any },
  arr: [] as { value: number }[],
  fragment: "xml"
});

store.arr.push({value:1});
store.arr[0].value = 42;

@YousefED
Copy link
Owner

Hi @Toubat,

The reason this is not supported, is that "assigning elements in an array" is most of the time not multi-user proof.

Assume we have an array ["green", "red", "blue"];

Let's say Alice and Bob both change array[1] at the same time, while being "offline":

  • Alice does array[1] = "purple"
  • Bob does array[1] = "orange"

When these changes sync, would you expect ["green", "purple", "blue"], ["green", "orange", "blue"] or ["green", "purple", "orange", "blue"]?

When dealing with lists / arrays, I think often you'd want the latter ["green", "purple", "orange", "blue"]. An assignment doesn't seem "idiomatic" for this, as it's better represented by a delete and insert operation.

I think adding support for assignment in arrays will lead to a lot of confusion in multi-user scenarios, I think it's cleaner to recommend users to use alternative operations (inserting and removing elements separately)

@Toubat
Copy link
Author

Toubat commented Apr 29, 2022

That's really helpful! Thank you for the detailed explanation!

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

3 participants