Observable

Observable is the core of Legend State. An observable function takes any value and returns a proxied version of it

Any observable is a Node with following properties

  1. id a unique value to identify the Node
  2. parent It’s a Node which is the direct parent value
  3. children it’s a Map of key with values as Nodes
  4. proxy it’s a Proxy around the Node
  5. key it’s the name/type for example key can be arr or the primitive value like 1 or hello
  6. root The original value along with isPrimitive and isSafe
  7. listeners it’s a List of Listener functions

getProxy

  1. It accepts two arguments
  2. node value
  3. prop (optional)
    Case 1: Only node value is passed
    It creates a proxy on node if it’s not available.

It returns the proxy, if it’s available

Case 2: node and prop is passed

Tries to get child value from the node with prop,

Creates a proxy if not available and returns it

getChildNode

Accpets Node and key as arguments.

Modifies existing node to add children prop which is a Map, if Node already has children, it adds the key to it with value as child

getNodeValue

It Accepts a node and returns the original value from it.

set

-> set —> setProp —> updateNodes
-> set —> notify

Accessing

  1. Accessing any property on observable triggers get trap from proxyHandler

tags: reactive-programming

sources: