npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@muzikanto/observable

v5.0.1

Published

The state manager

Downloads

26

Readme

Observable

npm version downloads dependencies Status size Code style

Linked projects

| name | version | downloads | | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | | observable-form | npm version | downloads |

Introduction

  • create store
  • listen store changes
  • listen store object part change
  • create event and subscribe store to event
  • create effect and subscribe events (done, fail, loading)
  • create cacheable effect
  • async store parts with combineAsync
  • override Event, Effect, Store if you need
  • and more..

Installation

npm i @muzikanto/observable
# or
yarn add @muzikanto/observable

Migrate 3.x.x > 4.x.x

remove to @muzikanto/observable-global

  • createGlobalStore
  • GlobalStoreCtx
  • GlobalStoreProvider
  • useGlobal

Examples

More examples

example createStore

const store = createStore<number>({ value: 1 });

function Component() {
   const state = useStore(store); // {value: 1};
   // const value = useStore(store, s => s.value);

   return <span>{state.value}</span>;
}

example createEvent

const store = createStore<number>(1);
const append = createEvent<number>();
const change = createEvent<number>();

store.on(append, (state, payload) => state + payload);
store.on(change, (state, payload) => payload);

append(2); // 3
change(-2); // -2
store.reset(); // 1

Run in CodeBox

example createEffect

type Request = { param: number };

// create cachable effect
const effect = createEffect<Request, Response, Error>(
   async (params: Request) => {
      try {
         const response = await axios.get('https://example.com', { params });

         return response;
      } catch (e) {
         throw e;
      }
   },
   { cache: true, cacheTime: 60000 },
);

// subscribe in store
storeDone.on(effect.done, (_, payload) => payload);
storeFail.on(effect.fail, (_, payload) => payload);
storeLoading.on(effect.loading, (_, payload) => payload);

// call
effect({ param: 1 })
   .then((response) => console.log(response))
   .catch((err) => console.log(err));

example combine

const one = createStore('Hello ');
const two = createStore('World');

const combinedObjStore = combine({ one, two });

combinedObjStore.get(); // { one: 'Hello ', two: 'World' }

const combinedStringStore = combine({ one, two }, ({ one, two }) => {
   return one + ' ' + two;
});

combinedStringStore.get(); // Hello World

example combineAsync

const one = createStore(1);
const two = createStore(2);
const three = createStore(3);

const combinedStore = combineAsync({ one, two });

combinedStore.get(); // { one: 1, two: 2 }

combinedStore.injectStore('three', three);

combinedStringStore.get(); // { one: 1, two: 2, three: 3 }

example timer

// change on timeout
(async () => {
   const store = createStore(1);
   const ev = createEvent<number>();
   store.on(ev, (state, payload) => state + payload);

   const runTimer = timer(ev, 200);

   runTimer(2);
   await wait(200); // wait timeout change to 3

   store.get(); // 3
})()(
   // change on interval
   async () => {
      const store = createStore(1);
      const ev = createEvent<number>();
      store.on(ev, (state, payload) => state + payload);

      const runTimer = timer(ev, 200, 100);

      runTimer(2);
      await wait(200); // wait timeout
      await wait(100); // wait interval 1, change to 3
      await wait(100); // wait interval 2, change to 5

      store.get(); // 5
   },
)();

Api

createStore

function createStore<T>(initialState: T): Store<T>

interface Store<T> {
  get: () => T;
  set: (v: T) => void;
  subscribe: (listener: Listener<any>, selector?: (state: T) => any) =>() => void;
  reset: () => void;
  on: <P>(event: IEvent<P>, handler: (state: T, payload: P) => T) => () => void;
  watch: (handler: (state: T, prev: T) => void): () => void;
}

createEvent

function createEvent<P = void>(): IEvent<P>;

type IEvent<P = void> = {
   (payload: P): void;
   watch: (watcher: Listener<P>) => () => void;
};

createEffect

function createEffect<Req, Res, Err = Error>(
   handler: (params: Req) => Promise<Res>,
   options?: {
      done?: IEvent<Res>;
      fail?: IEvent<Err>;
      loading?: IEvent<boolean>;
      cache?: boolean;
      cacheTime?: number;
   },
): IEffect<Req, Res, Err>;

type IEffect<Req, Res, Err = Error> = {
   (request: Req): Promise<Res>;
   done: IEvent<Res>;
   fail: IEvent<Err>;
   loading: IEvent<boolean>;
};

createApi

function createApi<S, A extends { [key: string]: (state: S, payload: any) => S }>(
   state: S,
   api: A,
): Api<S, A>;

type Api<S, A extends { [key: string]: (store: Store<S>, payload: any) => S }> = ApiEvents<S, A> & {
   store: Store<S>;
};

type ApiEvents<S, A> = {
   [K in keyof A]: A[K] extends (store: S, e: infer E) => S ? IEvent<E> : any;
};

combine

function combine<Map extends { [key: string]: any }, S = Map>(
   map: { [k in keyof Map]: Store<Map[k]> },
   func?: (map: Map) => S,
): CombineStore<S>;

combineAsync

function combineAsync<Map extends { [key: string]: any }, S = Map>(
   map: { [k in keyof Map]: Store<Map[k]> },
   func?: (map: Map) => S,
): CombineAsyncStore<S>;

forward

function forward<P>(
   from: IEvent<P>,
   to: IEvent<P> | Array<IEvent<P>>,
): (() => void) | Array<() => void>;

timer

export type Timer<R> = IEvent<R> & {
   disable: () => void;
};

function timer<R>(event: IEvent<R>, timeout: number, interval?: number): Timer<R>;

useStore

function useStore<T, V>(observable: Store<T>, selector?: (state: T) => V): V;

License

MIT