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

@jstoolkit/mobx-utils

v2.10.5

Published

MobX utils

Downloads

5

Readme

MobX Utils

npm package

Base classes and utilities of mobx stores and validable mobx models.

Example

RootStore

import { action } from 'mobx';
import { BaseRootStore, NotificationsStore, WorkerStore } from '@vlazh/mobx-utils/stores';
import { JSONModel, JSONSerializable } from '@vlazh/mobx-utils/serialization';
import AuthStore from './AuthStore';
import AppStore from './AppStore';
import SignUpStore from './SignUpStore';
import SignInStore from './SignInStore';

export type RootStoreState = Pick<Partial<RootStore>, 'appStore' | 'authStore'>;

export default class RootStore extends BaseRootStore implements JSONSerializable<RootStoreState> {
  readonly _serializable = this;

  readonly authStore: AuthStore;

  readonly appStore: AppStore;

  readonly signInStore: SignInStore;

  readonly signUpStore: SignUpStore;

  // ...

  constructor(initialState: Partial<JSONModel<RootStoreState>> = {}) {
    super();
    const appNotificationsStore = this.createNotificationsStore();
    const appWorkerStore = this.createWorkerStore();

    this.authStore = new AuthStore(
      this,
      appNotificationsStore,
      appWorkerStore,
      initialState.authStore
    );
    this.appStore = new AppStore(
      this,
      appNotificationsStore,
      appWorkerStore,
      initialState.appStore
    );

    this.signInStore = new SignInStore(this, appNotificationsStore, appWorkerStore);
    this.signUpStore = new SignUpStore(this, appNotificationsStore, appWorkerStore);
  }

  createNotificationsStore(): NotificationsStore<this, AppNotification> {
    return new NotificationsStore<this, AppNotification>(this, 10000);
  }

  createWorkerStore<TaskKeys extends string = never>(): WorkerStore<this, TaskKeys> {
    return new WorkerStore(this);
  }

  toJSON(): JSONModel<RootStoreState> {
    return {
      appStore: this.appStore.toJSON(),
      authStore: this.authStore.toJSON(),
    };
  }
}

AppStore

import { RequestableStore, NotificationsStore, WorkerStore } from '@vlazh/mobx-utils/stores';
import RootStore from './RootStore';

export default class AppStore
  extends RequestableStore<
    RootStore,
    NotificationsStore<RootStore, AppNotification>,
    WorkerStore<RootStore>
  >
  implements JSONSerializable<AppStore> {
  readonly _serializable = this;

  // ...

  constructor(
    rootStore: RootStore,
    notifications: NotificationsStore<RootStore, AppNotification>,
    worker: WorkerStore<RootStore>,
    initialState?: JSONModel<AppStore>
  ) {
    super(rootStore, notifications, worker);
    if (initialState) {
      // ...
    }
  }

  @withRequest<AppStore>({
    // Keep live last result of call of loadData for 10min or while authStore.sessionId is changed.
    memo: { lifetime: 60 * 10, inputs: (self) => [self.rootStore.authStore.sessionId] },
  })
  loadData = flow(function* loadData(this: AppStore) {
    const { authStore } = this.rootStore;

    if (authStore.isLoggedIn) {
      const data = yield api.fetchSomeData();
      // ...
    } else {
      const data = yield api.fetchOtherData();
      // ...
    }
  });

  toJSON(): JSONModel<AppStore> {
    return {
      // ...
    };
  }
}

AppView

import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';

function AppView({ rootStore }: AppViewProps): JSX.Element {
  const {
    authStore,
    appStore,
    appStore: { notifications, worker },
  } = rootStore;

  useEffect(() => {
    appStore.loadData();
  }, [appStore, authStore.isLoggedIn]);

  return (
    <RootStoreContext.Provider value={rootStore}>
      <AppLoader loading={worker.isPending()} />
      <AppNotifications notifications={notifications} position="window-top" />
    </RootStoreContext.Provider>
  );
}

export default observer(AppView);