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

tms.js

v0.0.1

Published

Using ES6 class to write state management

Downloads

3

Readme

前言

其实我不太喜欢将TMS称之为一种状态管理模式,我更喜欢将它称之为一种开发模式。为了满足业务的需要,同时使用了VueReact的相关技术栈,而且未来也会开发微信小程序快应用,不同框架、不同平台之间的代码复用,以及降低它们的学习成本是TMS出现的根本原因。现在前端主流的开发方式,都是将业务写在业务组件之中,在需求快速变更的情况下,业务的变化导致组件的拆分,这种开发方式就稍显笨拙了。

对比

实现一个计数功能

vuex


    import Vuex from 'vuex';

    const store = new Vuex.Store({
        state: {
            count: 0
        },
        mutations: {
            increment(state) {
                state.count++;
            }
        }
    });

    store.commit('increment');

    console.log(store.state.count); // -> 1

redux


    import { createStore } from 'redux';


    const reducer = (state = { count: 0 }, action) => {
        switch (action.type) {
            case 'increment': return { count: state.count + 1 };
            default: return state;
        }
    };


    const store = createStore(reducer);

    store.dispatch({ type: 'increment' });

    console.log(store.getState().count); // -> 1

TMS

    import Tms from 'tms.js';

    class Store extends Tms {
        count = 0;
        $increment() {
            this.count++;
        }
    }

    const store = new Store();

    store.$increment();

    console.log(store.count); // -> 1

通过上述对比,我们知道TMS是一个以class形式的开发模式,你只需要把extends Tms的相关代码删除掉,它仍然能够很好的运行,所以说TMS本身是没有入侵性的,它能很好的运行在JavaScript的各种运行环境中。

工作原理

在实例初始化的时候,TMS会查找原型链上所有$开头的方法,然后劫持当前的实例对应的$开头的方法 例子:


    class Store extends Tms {
        count = 0;
        constructor() {
            super();
            // 错误的,原型链上不会存在$reduce方法
            this.$reduce = () => {
                this.count--;
            };
        }
        // 错误的,原型链上不会存在$plus方法
        $plus = () => {
            this.count++;
        }
        // 正确的,原型链上会存在$setCount方法
        $setCount(count) {
            this.count = count;
        }
    }