Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2170

Tilia is born... still early version, ideas welcome :-)

$
0
0

Great project! Like valtio but with Rescript bindings. I gave it a try because I needed to have observable data in React that could be changed outside of React. With React Context, one has to write a Provider and if you want to mutate the data, you are forced to do it from React, so any global API has to be wrapped in React component with useEffect which adds so much complexity. This allowed me to establish a global, observable state that can be mutated from anywhere. Brilliant.

The only problem I encounter initially is with nested objects. In your example

type clouds = {
  mutable morning: string,
  mutable evening: string,
}
type state = {
  mutable flowers: string,
  mutable clouds: clouds,
}

// Create a tracked object or array:
let tree = Tilia.make({
  flowers: "are beautiful",
  clouds: { morning: "can be pink", evening: "can be orange" },
})

If you mutate the tree by changing the clouds immutably, and some other part of the app subscribes to the clouds like use(tree.clouds) then since you are doing an immutable update of the tree the subscriber would not see any change, as the tree.clouds are replaced and the old reference is unchanged, but no longer referenced from the tree. This leaves the subscriber with stale reference. The solution is either

  • always do mutable changes even on nested objects, which seems like an anti-pattern in Rescript,
  • always subscribe to the whole tree which will notify subscribers about any change anywhere, but block fine-grained reactivity via subscribing only to certain tree branches.

My use-case involved an array in the tree, like

let tree = {
  messages: []
}

and obviously, I did an immutable update of the array as I’m trained to in React. I curious what tells you your instinct, should I do a mutable updates of an array? Or can the project be patched in a way, that nested objects keeps reference of the parent tree and can detect replacements of itself :thinking:


Viewing all articles
Browse latest Browse all 2170

Trending Articles