Skip to content
Aldo Bucchi edited this page Jul 31, 2014 · 4 revisions

A Cell is a supercharged javascript variable.

Javascript variable

// declaring
name = "Aldo"
// reading
console.log name
// writing
name = "Bob"

Javascript Cell

// declaring
name = reactivity.cell "Aldo"
// reading
console.log name()
// writing
name "Aldo"

From the above example you can see that:

  • A cell is some sort of function that keeps an internal value
  • If you call it with no arguments, it returns the value
  • If you call it with one argument it replaces the stored value with the sole argument

Why is this useful, you say?

Cells are natively reactive. You don't have to do anything to enable this. In fact, most developers need only know how to use cells to create fully reactive apps. They are the glue that ties everything together.

name = reactivity.cell "Aldo"
reactivity.subscribe name, (e,r) -> console.log "name = " + r
// console.log --> 'name = Aldo'
name "Bob"
// console.log --> 'name = Bob'

But a cell is just a convention really. You don't need to use reactivity.cell() to create one. This is just a way to create a cell that knows how to be reactive. A cell should behave according to the following spec:

interface Cell<T> {

    // retrieve the current value
    // cells are initialized to undefined by default
    ( ): T

    // store a value
    // MUST return undefined/void
    ( v: T ): void

    // store an error
    ( v: Error ): void

    // store a value or an error
    // this allows you to pass a cell as
    // a node.js style callback directly
    ( e?: Error, v?: T ): void

}

Notes

  • Cells provide referential transparency. You can use simple non-reactive functions and swap them later on