Skip to content

Instantly share code, notes, and snippets.

@rossipedia
Forked from developit/*valoo.md
Created July 6, 2018 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossipedia/f649d9eab820da99a67392b1128e9a1f to your computer and use it in GitHub Desktop.
Save rossipedia/f649d9eab820da99a67392b1128e9a1f to your computer and use it in GitHub Desktop.
🐻 Valoo: just the bare necessities of state management. 160b / 120b. https://npm.im/valoo

🐻 valoo

just the bare necessities of state management.

Usage

Hotlink it from https://unpkg.com/valoo.

See Interactive Codepen Demo.

import valoo from 'https://unpkg.com/valoo'

// create an observable value:
const num = valoo(42)

// subscribe to value changes:
const unsub = num( v => console.log(v) )

// unsubscribe that listener:
unsub()

// set the value, invoking any listeners:
num(43)

// get the current value:
num()  // 43

valoo-lite

The light version is smaller but doesn't support unsubscribing.

Credit

The idea here was first implemented in Mithril. I believe the subscription mechanism is new though.

License

Apache-2.0. Copyright 2018 Google LLC.

node_modules
package-lock.json
dist
README.md
{"name":"valoo","version":"1.0.2","module":"valoo.mjs","main":"dist/valoo.umd.js","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md;microbundle valoo.mjs"},"devDependencies":{"microbundle":"^0.5.1"}}
export default (v, cb=[]) => c => c===void 0 ? v : c.call ? cb.push(c) : cb.map(f=>f(v=c)) && v
export default (v, cb=[]) => c => {
if (c===void 0) return v
if (c.call) return cb.splice.bind(cb, cb.push(c)-1, 1, 0)
v = c; for (c of cb) c && c(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment