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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jafish/m-store

v1.0.4

Published

简易的状态管理库

Downloads

2

Readme

@jafish/m-store

npm

简易的状态管理库,仅有 get set subscribe

可以针对不同的功能分别实现各自的状态,分布式管理

使用

import MStore, { UseStorage } from '@jafish/m-store'

// 初始化
export const store = new MStore({
    test: 1
}, [
    new UseStorage('namespace', ['test'])
])

// 设置值
store.set({ test: 2 })
store.set(state => ({ test: state.test + 1 }))

// 获取值
console.log(store.get().test)

// 发起订阅,拿到变更的值
const subscriber = store.subscribe((updates) => {
    console.log(updates)
})

// 取消订阅
subscriber()

API

get(): state

返回最新的全部状态

set(newState | state => newState)

设置新的状态,接受两种参数,对象或函数

设置值是同步设置的,通知订阅者是异步通知的

store.set({ 
    test: 1,
    test2: 2,
})

store.set(state => ({
    test: state.test + 1,
}))

subscribe(updates => {}): unsubscribe()

发起订阅,当数据改变时,触发更新。返回一个取消订阅的方法

const subscriber = store.subscribe(updates => {
    updates.forEach(item => {
        item.key
        item.newValue
        item.oldValue
    })
    
    console.log(updates)
})

// 取消订阅
subscriber()

插件

UseStorage

使用 storage 进行缓存

new MStore({
    test: 1
}, [
    new UseStorage(
        'namespace', // 命名空间,唯一值 
        ['test'], // 需要缓存的key,与传入的状态对应
        { // 配置项,可选
            // 为 true 时使用 sessionStorage ,默认使用 localStorage 进行缓存
            useShort: false, 
        }
    )
])

工具

easySet

能够更加容易的进行赋值

import MStore, { easySet } from '@jafish/m-store'

// 假如有复杂类型的值
export const store = new MStore({
    obj: {
        a: 1,
        b: 2,
    },
    arr: [
        {
            c: 3
        }
    ]
})

// 在常见情况,想要仅修改一个值,会显得尤为复杂
store.set(state => ({
    obj: {
        ...state.obj,
        b: 4,
    },
}))
store.set(state => {
    const arr = state.arr.slice()

    arr.splice(0, 1, {
        c: 5
    })

    return {
        arr
    }
})

// 针对复杂结构赋值,可以使用 easySet
// 写法参照微信小程序的 this.setData({ 'obj.b': 4 })
store.set(easySet({
    'obj.b': 4,
    'arr[0].c': 5,
}))

// ps: 相同顶级key一次只能赋值一个
// 如:该情况后面的会覆盖前面的
store.set(easySet({
    'arr[0].c': 5,
    'arr[1].c': 5,
}))

合理使用 easySet 可以大大的简化重复代码,更加直观

实践

import MStore, { UseStorage } from '@jafish/m-store'

// 初始化
export const store = new MStore({
    test: 1
}, [
    new UseStorage('namespace', ['test'])
])

// 修改 test
export const updateTest = (num) => {
    ... // 其他操作

    store.set({ test: num })
} 

// 异步修改 test
export const syncUpdateTest = async (num) => {
    const { test } = store.get()

    const newTest = await axios.post('xxx', { test })

    store.set(state => ({
        test: state.test + newTest
    }))
}