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

@smt-lib/swanx

v2.0.3

Published

轻量级状态管理工具

Downloads

12

Readme


title: @smt-lib/swanx header: develop nav: extensions sidebar: @smt-lib/swanx

smt-swanx

解释: 在小程序开发中,总会遇到多页面需要使用同一数据的情况,swanx是个轻量级数据管理工具,可以帮助开发者解决数据监听,多页面共用数据,子组件轻松获得父组件数据等功能。

npm 引入 swanx

小程序种使用三方npm包方法,见npm使用说明

npm install @smt-lib/swanx

方法 createStore

创建store,可多页面共用一个,也可以每个页面独立使用自己的store

|属性名 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |state |Object | 是 | | 数据状态 | |fields |Object/Array | 是 | |需要同步到小程序data上的数据。当类型为Object时,可以自定义挂载到data上的属性名。fields支持制定对象中的子元素,例如: 'a.b'| |mutations|Object|否| |keycommit时提交的事件名,value为执行的修改store.state状态的function,第一个参数为state,第二个参数为commit时传入的参数payload| |actions|Object|否| |与mutation类似,keydispatch时提交的事件名,value为执行的事件function,第一个参数为contextcontext包含commit方法和state属性,第二个参数为dispatch传入的参数payload|

返回值

|名称|类型 |说明| |---- | ---- | ---- | |subscribe|Function | 订阅方法 | |getState|Function| 获取state值| |dispatch|Function|更改store上数据状态,并触发有依赖的监听函数| |unsubscribeAll|Function|清空所有订阅,无参数传入|

** subscribe参数说明 **

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |Array/Object | 否 | * | 会变化的数据,默认fields中所有数据变化都会触发回调函数| |第二个参数 |Function | 是 | |数据变化时的回调函数|

** getState参数说明 **

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |String | 是 | | 要获取数据的key,例如:'a' , 'a.b.c'|

** commit参数说明 **

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |String | 是 | | mutation方法名| |第二个参数 |Object/Array/String | 否 | |传入mutation的参数|

** dispatch参数说明 **

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |String | 是 | | action方法名| |第二个参数 |Object/Array/String | 否 | |传入action的参数|

方法 storeBinding

在onLoad或者attached时,将创建的store和当前上下文绑定

storeBinding参数说明

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |Object | 是 | | 当前上下文 | |第二个参数 |Object | 是 | |创建后的store|

返回值

|类型 |说明| |---- |----| |Function | 清空所有storeBinding |

方法 connect

使用connect装饰过的组件或者页面,可以更方便的使用父级的store,避免逐层传递公用数据。

connect参数说明

|参数 |类型 |必填 | 默认值 |说明| |---- | ---- | ---- | ----|----| |第一个参数 |Object | 是 | | 页面或组件原型 | |第二个参数 |Object | 否 | |创建后的store,如果没填则默认使用当前组件的父页面的store|

返回值

|类型 |说明| |---- | ---- | |Function | 装饰后的组件或页面原型 |

各部分代码示例

** store示例 **

// store.js
import { createStore } from "@smt-lib/swanx";
export const store = createStore({
    state: {
        a: 1,
        b: {
            d: 4,
            e: 5
        },
        c: 3
    },
    fields: ['a', 'b.d'],
    mutations: {
        changeA(state, num) {
            state.a = num;
        },
        addD(state) {
            state.b.d++;
        }
    }
    actions: {
        changeA(context, num) {
            context.commit('changeA', num);
        },
        addD({commit}) {
            setTimeout(() => {
                commit('addD');
            }, 300);
        }
    }
});

// a页面
import { store } from "./store";
import { bindStore } from "@smt-lib/swanx";
Page({
    data: {},
    onLoad() {
        this.unbindStore = storeBinding(this, store);
        console.log(this.store);
    },

    myChangeA() {
        store.dispatch('changeA', 1);
    },

    myAddD() {
        store.commit('addD');
    }

    unOnload() {
        this.unbindStore();
    }
});


// a页面的b组件
import { connect } from "@smt-lib/swanx";
const newComponent = connect(Component);
newComponent({
    properties: {},
    pageLifetimes: {
        attached() {
            console.log(this.data.a);
            store.subscribe(['b.d'], state => {
                console.log(state.b);
            });
        }
    },
    method: {
        myChangeA() {
            this.store.dispatch('changeA', 1);
        },

        myAddD() {
            this.store.commit('addD');
        }
    }
});