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

@ngify/store

v1.5.3

Published

A simple state manager based on decorators and rxjs.

Downloads

13

Readme

@ngify/store

version Node.js CI License CodeFactor English

一个基于 装饰器rxjs 的简易状态管理器。

概念

应用程序中有一个仓库(Store),存储着一些全局状态(State)。 每一个状态都是一个对象。对象拥有属性,这些属性便是这个状态的值;对象拥有方法,这些方法可以修改对象的值。 按照约定,只有状态类对象的方法才可以修改状态值。

API

有关完整的 API 定义,请访问 https://ngify.github.io/ngify

安装

npm i @ngify/store

用法

定义状态:

import { Action, State } from '@ngify/store';

// 使用 State 将 User 装饰为一个状态类
@State()
export class User {
  constructor(
    public name: string,
    public age: number,
    public weight: number
  ) { }

  // 使用 Action 装饰涉及修改状态的方法
  @Action()
  changeName(name: string) {
    this.name = name;
  }

  // Action 有一个可选属性(Action Name),默认为方法名
  @Action()
  growUp() {
    this.age++;
  }

  // 可以自己定义 Action Name
  @Action('减肥')
  loseWeight() {
    this.weight -= 5;
  }

  // 当需要异步修改状态时,需要使用 async/await 或者返回一个 Promise / Observable
  @Action()
  async asyncLoseWeight() {
    await new Promise(resolve => {
      this.weight -= 10;
      resolve();
    });
  }

  log() {
    console.log(this);
  }
}

在应用程序入口处创建状态:

import { User } from './xxx';

...
new User('小明', 15, 100);
...

侦听状态变更:

import { getStore } from '@ngify/store';
import { map } from 'rxjs';
import { User } from './xxx';

const store = getStore();

store.on(User).subscribe(o => {
  console.log('User:变更了', o);
});

store.on(User, 'changeName').pipe(
  map(o => o.name)
).subscribe(name => {
  console.log('User:改名了', name);
});

store.on(User, '减肥').subscribe(o => {
  console.log('User:减肥成功', o);
});

获取状态对象:

import { getStore } from '@ngify/store';
import { User } from './xxx';

const user = getStore().get(User);
user.changeName('小红');