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

ractor

v1.1.7

Published

actor like state management.inspired by actor, redux, mobx.

Downloads

9

Readme

安装

npm i ractor

快速了解 Ractor

Ractor 仅仅包含三个基本概念,您只需要熟悉这三样东西就能彻底掌握 Ractor 的用法。

System

和其他库有所区别, Ractor 的事件系统和逻辑系统是分开的,需要单独创建。一般情况下,您只需要为你的 App 创建一个事件系统。

import { System } from "ractor";
export const system = new System("counter");

Action

类似 Redux 的 action,不过 Ractor 需要用 class 创建它。

export class Increment {}

Store

通俗的说,你可以把 Ractor 的 Store 理解为 Redux 的 Store。唯一的区别是,Redux 的 Store 内置了一套事件分发机制,Ractor 的事件分发由 System 处理。

Store 是个抽象类,你的业务 Store 需要继承它并实现 createReceive 方法。Store 里有个很方便的工具类可以帮您构建用来处理事件的 Receive 对象。

import { Increment } from "./Increment";
import { Decrement } from "./Decrement";
import { Store } from "ractor";

export class CounterStore extends Store<{ value: number }> {
  public state = { value: 1 };
  public createReceive() {
    return this.receiveBuilder()
      .match(Increment, async () => this.setState({ value: this.state.value + 1 }))
      .match(Decrement, async () => this.setState({ value: this.state.value - 1 }))
      .build();
    }
}

除了 class 创建 Store 之外,Ractor 还提供了比较简单的创建方式 createStore

import { ReceiveBuilder } from "ractor"

const CounterStore = createStore((getState, setState) => {
  return ReceiveBuilder
    .create()
    .match(Increment, () => setState(getState + 1))
    .match(Decrement, () => setState(getState - 1))
    .build()
}, 0)

React

我们可以用 ractor-react 也可以用 ractor-hooks 搭配 React 使用。这里主要介绍 ractor-hooks

Provider

在这里注入 system

  import { Provider } from "ractor-hooks"
  import { System } from "ractor"

  function App() {
    return <Provider system={new System("app")}><Counter /></Provider>
  }

useStore

输入 Ractor Store 的子类作为参数,输出实例化之后的状态。

function Counter() {
  const counter = useStore(CounterStore)
  return jsx
}

useSystem

输出 Provider 注入的 system。建议直接倒入 system,不太建议使用这种方式获取 system。

function Counter() {
  const system = useSystem(CounterStore)
  system.dispatch(new Increment)
  return jsx
}

Examples

交流群

QQ群:683288508