Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Last active February 5, 2019 13:23
Show Gist options
  • Select an option

  • Save adrianmcli/c28e936414c27b5f1dc9efe30cd3b2b2 to your computer and use it in GitHub Desktop.

Select an option

Save adrianmcli/c28e936414c27b5f1dc9efe30cd3b2b2 to your computer and use it in GitHub Desktop.

Revisions

  1. adrianmcli revised this gist Jan 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion counter-composition.js
    Original file line number Diff line number Diff line change
    @@ -28,4 +28,4 @@ myCounter.inc();
    console.log(myCounter.get()); // 2
    myCounter.dec();
    myCounter.dec();
    console.log(myCounter.get()); // 0
    console.log(myCounter.get()); // 0
  2. adrianmcli revised this gist Jan 7, 2019. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions counter-composition.js
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,31 @@
    const useState = (initVal) => {
    // state container definition
    const useState = initVal => {
    let val = initVal;

    const get = () => val;
    const set = x => (val = x);
    return { get, set };

    return Object.freeze({ get, set });
    };

    // make a counter by using the state container
    const makeCounter = () => {
    let { get, set } = useState(0);
    const { get, set } = useState(0);

    const inc = () => set(get() + 1);
    const dec = () => set(get() - 1);

    return Object.freeze({ get, set, inc, dec });
    return Object.freeze({ get, inc, dec });
    };

    // create the counter object
    const myCounter = makeCounter();

    // let's test our counter out
    console.log(myCounter.get()); // 0
    myCounter.set(2);
    console.log(myCounter.get()); // 2
    myCounter.inc();
    console.log(myCounter.get()); // 3
    myCounter.inc();
    console.log(myCounter.get()); // 2
    myCounter.dec();
    myCounter.dec();
    console.log(myCounter.get()); // 1
    console.log(myCounter.get()); // 0
  3. adrianmcli revised this gist Jan 7, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions counter-composition.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    const useCount = (initVal = 0) => {
    let count = initVal;
    const get = () => count;
    const set = x => (count = x);
    const useState = (initVal) => {
    let val = initVal;
    const get = () => val;
    const set = x => (val = x);
    return { get, set };
    };

    const makeCounter = () => {
    let { get, set } = useCount();
    let { get, set } = useState(0);

    const inc = () => set(get() + 1);
    const dec = () => set(get() - 1);
  4. adrianmcli created this gist Jan 6, 2019.
    28 changes: 28 additions & 0 deletions counter-composition.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    const useCount = (initVal = 0) => {
    let count = initVal;
    const get = () => count;
    const set = x => (count = x);
    return { get, set };
    };

    const makeCounter = () => {
    let { get, set } = useCount();

    const inc = () => set(get() + 1);
    const dec = () => set(get() - 1);

    return Object.freeze({ get, set, inc, dec });
    };

    // create the counter object
    const myCounter = makeCounter();

    // let's test our counter out
    console.log(myCounter.get()); // 0
    myCounter.set(2);
    console.log(myCounter.get()); // 2
    myCounter.inc();
    console.log(myCounter.get()); // 3
    myCounter.dec();
    myCounter.dec();
    console.log(myCounter.get()); // 1