@fun-land/observable-fun-state

Use FunState with zen-observable!

Usage no npm install needed!

<script type="module">
  import funLandObservableFunState from 'https://cdn.skypack.dev/@fun-land/observable-fun-state';
</script>

README

@fun-land/observable

Use FunState with zen-observable!

API

observableFunState

<State>(initialState: State) => Observable<FunState<State>>

Create an Observable of a FunState which calls Observable.next() when subscribers modify the state using the FunState API.

import observableFunState from "@fun-land/observable";

const observableState = observableFunState({ counter: 0 });

observableState.subscribe((funState) => {
    // can read from funState like usual
    const currentCount = funState.prop("counter").get();
    console.log(currentCount);

    if (currentCount < 10) {
      // writing to the state causes another event
      funState.prop("counter").mod((v) => v + 1);
    }
  }, 100);
});

Why?

This allows usage of FunState for doing reactive programming with accessors without requiring use-fun-state or React.js.