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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mobx-saga

v1.0.11

Published

mobx-saga

Readme

mobx-saga

mobx + saga,让 mobx 拥有强大的异步流的处理能力

开始

安装

npm install mobx-saga

创建 Saga (使用 Store 的计数器例子)

import { makeObservable, observable, action } from 'mobx'
import { namespace, effect } from 'mobx-saga'

const getCountUrl = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(1);
    }, 2000)
  })
}

@namespace('countStore')
class Store {
  constructor() {
    makeObservable(this)
  }

  @observable
  count: 0,

  @action
  changeCount(count) {
    this.count = count;
  }

  @effect()
  *getCount(paylod, { call }) {
    const count = yield call(getCountUrl);
    this.changeCount(count);
  }
}
const store = new Store();

export default store;

注册 saga

import React from 'react';
import { create, Container } from 'mobx-saga';
import App from './app';
import store from './store';
const app = create();
app.registeredEffects(store);

React.render(
  <Container stores={[store]}>
    <App />
  </Container>,
  document.getElementById('#root'),
);

注册 saga

import { create } from 'mobx-saga';
import store from './store';
const app = create();
app.registeredEffects(store);

使用 inject 注入 store,触发 effect 改变 count

import { observer, inject } from 'mobx-react';
import store from './store';

const App = observer(props: any) => {
  return <>
    <a javascript="void(0);" onClick={() => store.getCount()}>获取次数</a>
    <p>次数, { store.count }</p>
  </>
});

export default App;

内置异步流处理方案

takeLeading

上一个 getCount 处理结束后,才会再次执行 getCount

@effect('takeLeading')
*getCount(paylod, { call }) {
  const count = yield call(getCountUrl);
  this.changeCount(count);
}

takeLatest

接收 getCount 调用指令后,默认取消上次未完成的 getCount

@effect('takeLatest')
*getCount(paylod, { call }) {
  const count = yield call(getCountUrl);
  this.changeCount(count);
}

debounce

防抖,默认被触发时立即调用 getCount,配置 leading: false 时,延时后调用

@effect('debounce', {ms: 800})
*getCount(paylod, { call }) {
  const count = yield call(getCountUrl);
  this.changeCount(count);
}

throttle

截流

@effect('throttle', {ms: 800})
*getCount(paylod, { call }) {
  const count = yield call(getCountUrl);
  this.changeCount(count);
}

poll

接收开始指令后,间隔 800ms,自动调用一次 getCount,接受结束指令后,终止调用

@effect('poll', {delay: 800})
*getCount(paylod, { call }) {
  const count = yield call(getCountUrl);
  this.changeCount(count);
}

// 触发
// dispatch({type: 'countStore/getCount-start'});

// 结束
// // dispatch({type: 'countStore/getCount-end'});

loading 状态管理

开启

import createLoading from 'mobx-saga/plugins/loading';

app.use(createLoading());

使用

import { observer } from 'mobx-react';
import { inject } from 'mobx-saga';
import store from './store';

const App = () => {
  const loadingStore = props.loading;
  return (
    <>
      <a javascript="void(0);" onClick={() => store.getCount()}>
        获取次数
      </a>
      {loadingStore.effects['countStore/getCount'] ? (
        '获取中...'
      ) : (
        <p>次数, {store.count}</p>
      )}
    </>
  );
};

export default inject('loading')(observer(App));