Skip to content

Instantly share code, notes, and snippets.

@developit
Last active November 13, 2023 08:39
Show Gist options
  • Save developit/a0430c500f5559b715c2dddf9c40948d to your computer and use it in GitHub Desktop.
Save developit/a0430c500f5559b715c2dddf9c40948d to your computer and use it in GitHub Desktop.

Revisions

  1. developit revised this gist Jul 14, 2018. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1,14 @@
    {"name":"valoo","version":"2.0.0","module":"valoo.mjs","main":"dist/valoo.js","types":"./valoo.d.ts","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md;microbundle valoo.mjs"},"devDependencies":{"microbundle":"^0.5.1"}}
    {
    "name": "valoo",
    "version": "2.1.0",
    "module": "valoo.mjs",
    "main": "dist/valoo.js",
    "types": "./valoo.d.ts",
    "license": "Apache-2.0",
    "scripts": {
    "prepublishOnly": "cp *valoo.md README.md;microbundle valoo.mjs"
    },
    "devDependencies": {
    "microbundle": "^0.5.1"
    }
    }
  2. developit revised this gist Jul 14, 2018. 2 changed files with 14 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    {"name":"valoo","version":"2.0.0","module":"valoo.mjs","main":"dist/valoo.js","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md;microbundle valoo.mjs"},"devDependencies":{"microbundle":"^0.5.1"}}
    {"name":"valoo","version":"2.0.0","module":"valoo.mjs","main":"dist/valoo.js","types":"./valoo.d.ts","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md;microbundle valoo.mjs"},"devDependencies":{"microbundle":"^0.5.1"}}
    13 changes: 13 additions & 0 deletions valoo.d.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    declare module "valoo" {
    namespace valoo {
    type Disposer = () => void;
    interface Observable<T> {
    (): T;
    (value: T): void;
    on(fn: (value: T) => void): Disposer;
    }
    }

    function valoo<T = any>(value: T): valoo.Observable<T>;
    export = valoo;
    }
  3. developit revised this gist Jul 7, 2018. No changes.
  4. developit revised this gist Jul 6, 2018. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions *valoo.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <h1 align=center>🐻 valoo</h1>
    <p align=center><em>just the bare necessities of state management.</em></p>
    <p align=center><img src="https://i.imgur.com/bRPpZrT.png" width="572"></p>
    <p align=center><img src="https://i.imgur.com/Ke0BCSf.jpg" width="572"></p>

    ## Usage

    @@ -15,10 +15,10 @@ import valoo from 'https://unpkg.com/valoo'
    const num = valoo(42)

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

    // unsubscribe that listener:
    unsub()
    off()

    // set the value, invoking any listeners:
    num(43)
    @@ -27,9 +27,10 @@ num(43)
    num() // 43
    ```

    ## valoo-lite
    ## Other Versions

    The light version is smaller but doesn't support unsubscribing.
    - `valoo-lite.mjs`: lighter 120b version, but doesn't support unsubscribing.
    - `valoo-original.mjs`: v1-compatible, with subscribe handled via overloading.

    ## Credit

  5. developit revised this gist Jul 6, 2018. No changes.
  6. developit revised this gist Jul 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    {"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"}}
    {"name":"valoo","version":"2.0.0","module":"valoo.mjs","main":"dist/valoo.js","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md;microbundle valoo.mjs"},"devDependencies":{"microbundle":"^0.5.1"}}
  7. developit revised this gist Jul 6, 2018. 2 changed files with 16 additions and 4 deletions.
    5 changes: 5 additions & 0 deletions valoo-original.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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)
    }
    15 changes: 11 additions & 4 deletions valoo.mjs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    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)
    export default function valoo(v) {
    const cb = [];
    function value(c) {
    if (arguments.length) cb.map(f => { f && f(v=c); });
    return v;
    }
    value.on = c => {
    const i = cb.push(c)-1;
    return () => { cb[i] = 0; };
    };
    return value;
    }
  8. developit revised this gist Jul 6, 2018. No changes.
  9. developit revised this gist Jul 6, 2018. No changes.
  10. developit revised this gist Jul 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    {"name":"valoo","version":"1.0.1","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"}}
    {"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"}}
  11. developit revised this gist Jul 6, 2018. 2 changed files with 5 additions and 1 deletion.
    4 changes: 4 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    node_modules
    package-lock.json
    dist
    README.md
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    {"name":"valoo","version":"1.0.1","main":"valoo.mjs","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md","postpublish": "rm README.md"}}
    {"name":"valoo","version":"1.0.1","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"}}
  12. developit revised this gist Jul 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions *valoo.md
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@

    ## Usage

    Hotlink it from `https://rawgit.com/developit/a0430c500f5559b715c2dddf9c40948d/raw/valoo.mjs`.
    Hotlink it from `https://unpkg.com/valoo`.

    See [Interactive Codepen Demo](https://codepen.io/developit/pen/NzZoVp?editors=1010).

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

    // create an observable value:
    const num = valoo(42)
  13. developit revised this gist Jul 6, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    {"name":"valoo","version":"1.0.0","main":"valoo.mjs","author":"developit","license":"Apache-2.0"}
    {"name":"valoo","version":"1.0.1","main":"valoo.mjs","license":"Apache-2.0","scripts":{"prepublishOnly":"cp *valoo.md README.md","postpublish": "rm README.md"}}
  14. developit revised this gist Jul 6, 2018. No changes.
  15. developit revised this gist Jul 6, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions *valoo.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@

    ## Usage

    Hotlink it from `https://rawgit.com/developit/a0430c500f5559b715c2dddf9c40948d/raw/valoo.mjs`.

    See [Interactive Codepen Demo](https://codepen.io/developit/pen/NzZoVp?editors=1010).

    ```js
  16. developit revised this gist Jul 6, 2018. No changes.
  17. developit revised this gist Jul 6, 2018. No changes.
  18. developit revised this gist Jul 6, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {"name":"valoo","version":"1.0.0","main":"valoo.mjs","author":"developit","license":"Apache-2.0"}
  19. developit revised this gist Jul 6, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions *valoo.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,11 @@ num() // 43

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

    ## Credit

    The idea here was first implemented in [Mithril](https://mithril.js.org).
    I believe the subscription mechanism is new though.

    ## License

    Apache-2.0. Copyright 2018 Google LLC.
  20. developit revised this gist Jul 6, 2018. No changes.
  21. developit created this gist Jul 6, 2018.
    34 changes: 34 additions & 0 deletions *valoo.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <h1 align=center>🐻 valoo</h1>
    <p align=center><em>just the bare necessities of state management.</em></p>
    <p align=center><img src="https://i.imgur.com/bRPpZrT.png" width="572"></p>

    ## Usage

    See [Interactive Codepen Demo](https://codepen.io/developit/pen/NzZoVp?editors=1010).

    ```js
    import valoo from '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.

    ## License

    Apache-2.0. Copyright 2018 Google LLC.
    1 change: 1 addition & 0 deletions valoo-lite.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    export default (v, cb=[]) => c => c===void 0 ? v : c.call ? cb.push(c) : cb.map(f=>f(v=c)) && v
    5 changes: 5 additions & 0 deletions valoo.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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)
    }