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

mana-observable

v0.3.2

Published

Simple class property decorator that makes the class properties observable and can be easily used with React

Downloads

31

Readme

mana-observable

Simple class property decorator that makes the class properties observable and can be easily used with React.

NPM version NPM downloads

功能

  1. 对基于类的数据管理系统提供变更追踪机制。可独立使用,
  2. 配合依赖注入容器使用,为 React 组件提供管理数据。

安装

npm i mana-observable --save

用法

数据 API

数据 api 可以独立使用,不需要在依赖注入的环境中

@prop

将类中的基础类型属性转化为可追踪属性。对于基础类型 (当前支持数值、字符、布尔) 以及一般的引用类型,当值或引用发生变化时,触发数据的变更。对于部分内置类型 (数组、map、plainObject) 除引用变更外,其管理的内部值或引用发生变化时,也会触发数据变更。

class Ninja {
  @prop name: string = '';
}

watch

监听一个带有可追踪属性对象的属性变更

// 监听某个可追踪属性的变化
watch(ninja, 'name', (obj, prop) => {
  // ninja name changed
});
// 监听对象所有可追踪属性的变化
watch(ninja, (obj, prop) => {
  // any observable property on ninja changed
});

observable

经过 babel 处理的类可能会在实例上定义属性,而属性装饰器作用在原型上,因而无法进行属性转换。所以这里引入新的 API 解决相关的问题,后续希望提供免除该 API 调用的使用方式。

class Ninja {
  @prop name: string = '';
}

React API

当前仅提供基于 React hooks 的 API。

useInject

在 react 组件中,如果希望使用依赖注入容器中带有可追踪属性的对象,那么可以使用 useInject 来获取他们。

@singleton()
class Ninja {
  @prop name: string = '';
}

container.register(Ninja);

export function NinjaRender() {
  const ninja = useInject(Ninja);
  return <div>{ninja.name}</div>;
}

useObserve

为了精确控制每个 React 组件只因为自己访问的可追踪属性变更而进行 update,也为了在子组件内提供 hook 的创建时机,通过除 useInject 外方式获取到的对象,应当通过 useObserve 进行处理,重置其作用组件更新的范围。

export function NinjaName(props: { ninja: Ninja }) {
  const ninja = useObserve(props.ninja);
  return <div>{ninja.name}</div>;
}

getOrigin

在 React 组件中,我们访问的组件并不是原始实例,而是实例的代理,如果在 API 调用等环节需要获取原始对象(例如作为参数传递给其他 API),需要通过调用 getOrigin 方法获得。