Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dinh/c147f43a088a4b99e82f531e1ad498cc to your computer and use it in GitHub Desktop.
Save dinh/c147f43a088a4b99e82f531e1ad498cc to your computer and use it in GitHub Desktop.

Revisions

  1. @ssrdjan-fadv ssrdjan-fadv revised this gist Jun 14, 2020. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions 208.hawk.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    import React from 'react';
    import { hawk, hawkeye, useHawkState, useHawkSetState } from 'react-hawk';

    const counterState = hawk({
    key: 'counter',
    default: 0
    });

    const isEvenState = hawkeye({
    key: 'isEven',
    get: ({ get }) => {
    const count = get(counterState)
    return Boolean(count % 2) ? 'odd' : 'even'
    }
    })

    const useIncrease = () => {
    const setCounter = useHawkSetState(counterState)
    const increase = (n = 1) => {
    setCounter(count => count + n)
    }
    return increase
    }

    const useDecrease = () => {
    const setCounter = useHawkSetState(counterState)
    const decrease = (n = 1) => {
    setCounter(count => count - n)
    }
    return decrease
    }

    export const Counter = () => {
    const count = useHawkState(counterState)
    const even = useHawkState(isEvenState)
    const decrease = useDecrease()
    const increase = useIncrease()
    return (
    <>
    <button onClick={() => decrease()}>-</button>
    {count} is {even}
    <button onClick={() => increase()}>+</button>
    </>
    )
    }
  2. @ssrdjan-fadv ssrdjan-fadv revised this gist Jun 9, 2020. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions 207-solid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import { createState } from "solid-js";

    const Counter = () => {
    const [state, setState] = createState({ counter: 0 });
    return <div>
    {state.counter}
    <button type="button" onClick={() => setState({ counter: state.counter + 1 }}>+1</button>
    <button type="button" onClick={() => setState({ counter: state.counter - 1 }}>-1</button>
    </div>;
    };

    render(() => createComponent(Counter, document.getElementById("app"));
  3. @ssrdjan-fadv ssrdjan-fadv revised this gist Jun 7, 2020. 2 changed files with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions 206-use-atom.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import React from 'react';
    import ReactDOM from 'react-dom';

    import { Provider, createAtom, useAtom } from 'use-atom';

    const countAtom = createAtom({ default: 0 });

    const Counter = () => {
    const [count, setCount] = useAtom(countAtom);
    return (
    <div>
    <span>Count: {count}</span>
    <button type="button" onClick={() => setCount(count + 1)}>+1</button>
    <button type="button" onClick={() => setCount((c) => c - 1)}>-1</button>
    </div>
    );
    };

    const App = () => (
    <Provider>
    <h1>Counter</h1>
    <Counter />
    </Provider>
    );
  4. @ssrdjan-fadv ssrdjan-fadv renamed this gist Jun 6, 2020. 1 changed file with 0 additions and 0 deletions.
  5. @ssrdjan-fadv ssrdjan-fadv renamed this gist Jun 6, 2020. 1 changed file with 0 additions and 0 deletions.
  6. @ssrdjan-fadv ssrdjan-fadv revised this gist Jun 6, 2020. 106 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  7. @ssrdjan-fadv ssrdjan-fadv renamed this gist Jun 6, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @ssrdjan-fadv ssrdjan-fadv revised this gist Jun 6, 2020. 2 changed files with 22 additions and 0 deletions.
    File renamed without changes.
    22 changes: 22 additions & 0 deletions 104-purescript-web.purs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    module Main where

    import Prelude
    import Effect (Effect)

    import Concur.Core (Widget)
    import Concur.React (HTML)
    import Concur.React.DOM as D
    import Concur.React.Props as P
    import Concur.React.Run (runWidgetInDom)

    counterWidget :: forall a. Int -> Widget HTML a
    counterWidget count = do
    n <- D.div'
    [ D.p' [D.text ("State: " <> show count)]
    , D.button [P.onClick] [D.text "Increment"] $> count+1
    , D.button [P.onClick] [D.text "Decrement"] $> count-1
    ]
    counterWidget n

    main :: Effect Unit
    main = runWidgetInDom "main" (counterWidget 0)
  9. @ssrdjan-fadv ssrdjan-fadv revised this gist May 26, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 000
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    More than 100 different counter app implementations...
  10. @ssrdjan-fadv ssrdjan-fadv revised this gist May 26, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 103 different counter app examples...
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ... using diff libraries and frameworks
  11. @ssrdjan-fadv ssrdjan-fadv revised this gist May 26, 2020. 2 changed files with 1 addition and 1 deletion.
    1 change: 0 additions & 1 deletion 0...100 Different counter apps examples...
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ... using many diff libraries and frameworks
    1 change: 1 addition & 0 deletions 103 different counter app examples...
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ... using diff libraries and frameworks
  12. @ssrdjan-fadv ssrdjan-fadv revised this gist May 26, 2020. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions 103-capsid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import { wired, on, component } from "capsid";

    @component("counter")
    export class Counter {
    count: number = 0;

    @wired(".label")
    label: HTMLElement;

    __mount__() {
    this.update();
    }

    @on.click.at(".plus")
    plus() {
    this.count++;
    this.update();
    }

    @on.click.at(".minus")
    minus() {
    this.count--;
    this.update();
    }

    /** Updates the label. */
    update() {
    this.label.textContent = `${this.count}`;
    }
    }
  13. @ssrdjan-fadv ssrdjan-fadv revised this gist May 22, 2020. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions 102-feliz.fs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    open Feliz
    open Feliz.UseElmish
    open Elmish

    type Msg =
    | Increment
    | Decrement

    type State = { Count : int }

    let init() = { Count = 0 }, Cmd.none

    let update msg state =
    match msg with
    | Increment -> { state with Count = state.Count + 1 }, Cmd.none
    | Decrement -> { state with Count = state.Count - 1 }, Cmd.none

    let counter = React.functionComponent(fun () ->
    let state, dispatch = React.useElmish(init, update, [| |])
    Html.div [
    Html.h1 state.Count
    Html.button [
    prop.text "Increment"
    prop.onClick (fun _ -> dispatch Increment)
    ]

    Html.button [
    prop.text "Decrement"
    prop.onClick (fun _ -> dispatch Decrement)
    ]
    ]
    )
  14. @ssrdjan-fadv ssrdjan-fadv revised this gist May 15, 2020. 2 changed files with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions 101-statlake.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import React from "react";
    import { StateLake } from "statelake";

    const store = new StateLake({ count: ""});

    export default function App() {
    const [count, setCount] = store.useState("count")();

    return (
    <div className="App">
    <button onClick={() => setCount(count + 1)}>+</button>
    <button onClick={() => setCount(count - 1)}>-</button>
    <div>{count}</div>
    </div>
    );
    }
  15. @ssrdjan-fadv ssrdjan-fadv revised this gist May 9, 2020. 2 changed files with 1 addition and 1 deletion.
    1 change: 1 addition & 0 deletions 0 100 Different counter apps examples...
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ... using many diff libraries and frameworks
    1 change: 0 additions & 1 deletion 0_100 Different counter apps examples...
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ... using diff frameworks
  16. @ssrdjan-fadv ssrdjan-fadv renamed this gist May 9, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. @ssrdjan-fadv ssrdjan-fadv revised this gist May 9, 2020. 2 changed files with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions 100-uhtml.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    import { define } from 'uce';
    import { render, html } from 'uhtml';

    define("my-counter", {
    init() {
    this.count = 0;
    this.dec = () => {
    this.count--;
    this.render();
    };
    this.inc = () => {
    this.count++;
    this.render();
    };
    this.render();
    },
    render() {
    this.html`
    <button onclick="${this.dec}">-</button>
    <span>${this.count}</span>
    <button onclick="${this.inc}">+</button>
    `;
    }
    });

    addEventListener(
    'DOMContentLoaded',
    () => {

    // populate the body
    render(document.body, html`
    <my-counter />
    <footer></footer>
    `);

    // fetch project details and populate the footer
    fetch('package.json')
    .then(_ => _.json())
    .then(({description, version}) => {
    render(document.querySelector('footer'), html`
    ${description} v${version}
    `);
    });
    },
    {once: true}
    );
  18. @ssrdjan-fadv ssrdjan-fadv renamed this gist May 9, 2020. 1 changed file with 0 additions and 0 deletions.
  19. @ssrdjan-fadv ssrdjan-fadv renamed this gist May 9, 2020. 1 changed file with 0 additions and 0 deletions.
  20. @ssrdjan-fadv ssrdjan-fadv revised this gist May 9, 2020. 3 changed files with 2 additions and 2 deletions.
    3 changes: 2 additions & 1 deletion 002-react-values.js
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ const Counter = () => (
    <span>{value}</span>
    <button onClick={() => decrement()}>-1</button>
    )}
    </NumberValue>)
    </NumberValue>
    )
    1 change: 0 additions & 1 deletion 005-elmar.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    import React from 'react'

    export const init = count => count

    const Action = {
  21. @ssrdjan-fadv ssrdjan-fadv revised this gist May 9, 2020. 2 changed files with 1 addition and 1 deletion.
    1 change: 0 additions & 1 deletion 0->100 counter apps
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ...
    1 change: 1 addition & 0 deletions 100 counter apps examples...
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ... using diff frameworks
  22. @ssrdjan-fadv ssrdjan-fadv revised this gist May 9, 2020. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions 099-flame.purs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    module Examples.NoEffects.Counter.Main where
    import Prelude
    import Effect (Effect)
    import Flame (QuerySelector(..), Html)
    import Flame.Application.NoEffects as FAN
    import Flame.HTML.Element as HE
    import Flame.HTML.Attribute as HA

    type Model = Int
    data Message = Increment | Decrement

    init :: Model
    init = 0

    update :: Model -> Message -> Model
    update model = case _ of
    Increment -> model + 1
    Decrement -> model - 1

    view :: Model -> Html Message
    view model = HE.main "main" [
    HE.button [HA.onClick Decrement] "-",
    HE.text $ show model,
    HE.button [HA.onClick Increment] "+"
    ]

    main :: Effect Unit
    main = FAN.mount_ (QuerySelector "main") {
    init,
    update,
    view
    }
  23. @ssrdjan-fadv ssrdjan-fadv revised this gist May 5, 2020. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions 098-solid-with-storeon.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import { useStoreon } from 'storeon-solidjs'

    let counter = store => {
    store.on('@init', () => ({ count: 0 }))
    store.on('inc', ({ count }) => ({ count: count + 1 }))
    store.on('dec', ({ count }) => ({ count: count - 1 }))
    }

    export default function Counter() {
    const [state, dispatch] = useStoreon()

    return (
    <div>
    {state.count}
    <button onClick={() => dispatch('inc')}>inc</button>
    <button onClick={() => dispatch('dec')}>dec</button>
    </div>
    )
    }
  24. @ssrdjan-fadv ssrdjan-fadv revised this gist May 3, 2020. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions 097-flowponent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import { render } from "preact";
    import flowponent from "flowponent";

    const App = flowponent(function*() {
    let count = 0;

    for (;;) {
    count += yield resolve => (
    <div>
    <div>current value: {count}</div>
    <button onClick={() => resolve(1)}>+1</button>
    <button onClick={() => resolve(-1)}>-1</button>
    </div>
    );
    }
    });

    render(<App />, document.getElementById("root"));
  25. @ssrdjan-fadv ssrdjan-fadv revised this gist May 2, 2020. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  26. @ssrdjan-fadv ssrdjan-fadv renamed this gist May 2, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  27. @ssrdjan-fadv ssrdjan-fadv revised this gist May 2, 2020. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions 096-vue.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,8 @@
    <template>
    <div>
    <button @click="this.dec">
    -
    </button>
    <button @click="this.dec">-</button>
    <span>{{count}}</span>
    <button @click="this.inc">
    +
    </button>
    <button @click="this.inc">+</button>
    </div>
    </template>

  28. @ssrdjan-fadv ssrdjan-fadv revised this gist May 2, 2020. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions 047-hybrids-with-redux.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,14 @@
    import { html } from 'hybrids';
    import store, { increment, decrement } from './redux';

    // This is a factory for connecting redux store.
    // It can be taken out from here and use in other elements.
    const connect = (store, mapState) => ({
    get: mapState ? () => mapState(store.getState()) : () => store.getState(),
    connect: (host, key, invalidate) => store.subscribe(invalidate),
    });

    // Event callbacks
    const onInc = ({ offset }) => store.dispatch(increment(offset));
    const onDec = ({ offset }) => store.dispatch(decrement(offset));

    // redux-counter definition
    export default {
    count: connect(store, (state) => state.count),
    offset: 1,
  29. @ssrdjan-fadv ssrdjan-fadv revised this gist May 2, 2020. No changes.
  30. @ssrdjan-fadv ssrdjan-fadv revised this gist May 2, 2020. No changes.