Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

proxy-state-tree

2.0.1 • Public • Published

proxy-state-tree

An implementation of the Mobx/Vue state tracking approach, for library authors

npm install proxy-state-tree

Why

The proxy-state-tree project is created to stimulate innovation in state management. The introduction of Flux was followed by a big wave of libraries trying to improve on the idea. All these iterations helped moving the community forward and Redux was born a year later. It was frustrating to have all these variations of the same idea, but at the same time it made the core idea better. One factor I believe made this possible is that Flux state management is based on immutability. It is a difficult concept to understand, but when you understand it, it is easy to implement the concept of change. You literally just check if a value you depend on has changed. That said, immutability tends to put a lof effort on the hands of the consumer. You have to think really hard about how you structure state and expose state to components to avoid performance issues and prevent boilerplate.

vuejs and mobx has a different approach to change. They use getter/setter interception to track access to state and changes to state. This concept completely removes the consumers burden of how the state is structured and how it is exposed to the different parts of the app. You just expose state in any form and the usage is automatically tracked and optimized. The problem with this approach though is that it is difficult to implement as a library author. I want to change that!

proxy-state-tree is a low level implementation of the getter/setter interception with a single state tree to help library authors innovate. I hope to see innovations that removes the burden that immutability currently causes, but keeps the guarantees that was introduced in Flux. I invite you to make a mobx and redux baby! ;-)

Examples

  • Vue example with a simple implementation that allows you to define a state tree and expose a store to the components which can be mutated in the methods

  • Preact example with a simple implementation of a external state and actions passed on a Provider. Also includes a simple, but inspiring debugger

Create a tree

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({})
 
console.log(tree.get()) // {}

As a library author you would typically expose a mechanism to define the initial state of the application, which you would pass to the ProxyStateTree. You would also expose a way to access the state, hiding the tree.get() from the consumer of your library.

Track access

You can track access to the state by using the startPathsTracking and clearPathsTracking methods.

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: 'bar',
  bar: 'baz'
})
const state = tree.get()
 
const trackId = tree.startPathsTracking()
const foo = state.foo
const bar = state.bar
const paths = tree.clearPathsTracking(trackId)
 
console.log(paths) // Set { 'foo', 'bar' }

You would typically use this mechanism to track usage of state. For example rendering a component, calculating a a computed value etc. The returned paths set is stored for later usage. The paths structure is used internally by proxy-state-tree, but you can also consume it as a library author to for example showing components and what paths they depend on in a devtool. Nested paths uses dot notation, for example ['foo.bar']. Path tracking needs to be synchronous from the beginning of the tracking, but the clearing of tracking can be asynchronous.

Track mutations

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: 'bar',
  bar: []
})
const state = tree.get()
 
tree.startMutationTracking()
state.foo = 'bar2'
state.bar.push('baz')
const mutations = tree.clearMutationTracking()
 
console.log(mutations)
/*
  [{
    method: 'set',
    path: ['foo'],
    args: ['bar2']  
  }, {
    method: 'push',
    path: ['bar'],
    args: ['baz']
  }]
*/

You would use startMutationTracking and clearMutationTracking around logic that is allowed to do mutations, for example actions or similar. Trying to mutate without this tracking active results in an error. The returned array can be used in combination with a devtool.

Check need to update

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: 'bar',
  bar: 'baz'
})
const state = tree.get()
 
function render () {
  const trackId = tree.startPathsTracking()
  const foo = state.foo
  const bar = state.bar
 
  return tree.clearPathsTracking(trackId)
}
 
const listener = tree.addFlushListener(render(), (flushId) => {
  // Runs when mutations matches paths passed in
 
  // Whenever mutations affecting these paths occurs
  // we typically create the paths again due to possible
  // conditional logic, in "render" in this example
  listener.update(render()) 
})
 
tree.startMutationTracking()
state.foo = 'bar2'
state.bar.push('baz')
tree.clearMutationTracking()
 
// This command flushes out the current mutations and
// notifies any listeners
tree.flush()
 
// Remove listener
listener.dispose()

Here we combine the tracked paths with the mutations performed to see if this components, computed or whatever indeed needs to run again, doing a new startPathsTracking and clearPathsTracking.

You can optionally declare a global listener which will be informed by all mutation flushes:

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: 'bar',
  bar: 'baz'
})
const state = tree.get()
 
const listener = tree.addFlushListener((mutations, flushId) => {
  /*
    [{
      method: "set",
      path: "foo",
      args: ["bar2"] 
    }]
  */
})
 
tree.startMutationTracking()
state.foo = 'bar2'
tree.clearMutationTracking()
tree.flush()
listener.dispose()

In addition to this you can also listen to each individual mutation:

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: 'bar',
  bar: 'baz'
})
const state = tree.get()
 
const listener = tree.addMutationListener((mutation, paths, flushId) => {
  /*
    mutation: {
      method: "set",
      path: "foo",
      args: ["bar2"] 
    }
    paths: Set(["foo"])
    flushId: 1
  */
})
 
tree.startMutationTracking()
state.foo = 'bar2'
tree.clearMutationTracking()
listener.dispose()

It is important here to check the paths argument for a list of all paths possibly changed. Reason is that a single mutation might cause changes to multiple paths.

Dynamic state values

If you insert a function into the state tree it will be called when accessed. The function is passed the proxy-state-tree instance and the path of where the function lives in the tree.

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: (proxyStateTree, path) => {}
})

The allows you to easily extend functionality with for example a computed concept that lives in the tree, as you can see in this codesandbox.

You can inject a wrapper around this function by:

import ProxyStateTree from 'proxy-state-tree'
 
const tree = new ProxyStateTree({
  foo: (foo, proxyStateTree, path) => {}
}, {
  dynamicWrapper: (proxyStateTree, path, func) => func('foo', proxyStateTree, path)
})

This helps you expose library entities to these functions.

install

npm i proxy-state-tree

Downloadsweekly downloads

8,790

version

2.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability