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

@proxies/core

v1.0.0-beta

Published

A deeply responsive framework based on subscription and interception - the core part

Downloads

2

Readme

@proxies/core

一个基于订阅和拦截的深度响应式框架 - 核心部分

简单示例/主要特性

  • 订阅 Proxy 的变化,即便是深层次的
import {proxy, subscribe} from '@proxies/core';
const proxy = proxy({a:1, b:{c:2}});

// 监听 a 的变化
subscribe(proxy, 'a', ({value, oldValue}) => {
  console.log(`property 'a' changed from ${oldValue} to ${value}`);
});

proxy.a = 2;
// print -> property 'a' changed from 1 to 2
import {proxy, subscribe} from '@proxies/core';
const proxy = proxy({a:1, b:{c:2}});
// 监听 b.c 的变化
subscribe(proxy, 'b.c', ({value, oldValue}) => {
  console.log(`property 'b.c' changed from ${oldValue} to ${value}`);
});

proxy.b.c = 3;
// print -> property 'b.c' changed from 2 to 3
import {proxy, subscribe} from '@proxies/core';
const proxy = proxy({a:1, b:{c:2}});
// 监听整个对象内部的所有变化
subscribe(proxy, (value, oldValue, property) => {
  console.log(`[all] property '${property}' changed from ${oldValue} to ${value}`);
});

proxy.a = 3;
// print -> [all] property 'a' changed from 2 to 3
proxy.b.c = 4;
// print -> [all] property 'b.c' changed from 3 to 4
  • 拦截 Proxy 的变化,即便是深层次的
import {proxy, intercept} from '@proxies/core';
const proxy = proxy({b:{c:2}});
intercept(proxy, 'b.c', (value, preventDefault, directTarget, directProperty)=>{
    preventDefault() // 阻止默认操作
    // 设置新值
    directTarget[directProperty] = value + 1;
    // 返回是否允许修改,这里的逻辑和 Proxy 中的 Setter 一致
    return true;
})

a.b.c = 3;
console.log(a.b.c); // > 4
  • 除了对象属性的变化,我们还可以订阅/拦截更多的操作,比如 getter
import {proxy, subscribe, intercept} from '@proxies/core';
const proxy = proxy({a:1, b:{c:2}});

subscribe(proxy, 'a', {
    get({getterValue}){
        console.log(`Value to getter: ${getterValue}`);
    }
});

intercept(proxy, 'b.c', {
    get({getterValue, preventDefault}){
        preventDefault();
        return getterValue + 1;
    }
});

console.log(proxy.a); // > 1
// print -> Value to getter: 1

console.log(proxy.b.c); // > 3

文档

Documentation: https://github.com/canguser/proxies/blob/master/modules/core/docs/modules.md

其他

Intercept 思路

拦截原则:

  1. 先于默认方法执行
  2. 调用优先级:随着父级层数增加而降低,同一对象,会根据先后顺序影响优先级,先注册者,优先级较低
  3. 任一子级通过 preventDefault 可取消默认动作,且只有通过调用该方法后,才能影响返回值,该次调用称为有效调用
  4. 采用优先级最低的返回值,低优先级可以获取到上次有效调用的返回值

Subscribe 思路

订阅原则

  1. 于方法完成后调用,无法对原方法产生任何影响
  2. 调用优先级:随着父级层数增加而降低,同一对象,会根据先后顺序影响优先级,这里和拦截相反,先注册者,优先级高
  3. 订阅皆无返回值