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

@7pound/westore

v1.0.1

Published

miniprogram-westore

Downloads

10

Readme

westore

基于发布订阅简单实现的状态管理,支持多模块,计算属性。

小程序状态管理

  • API简单
  • 压缩后仅有1kb, 无其它依赖
  • 可按模块划分

使用

  1. 安装
npm install --save @7pound/westore
  1. 构建npm

在微信开发工具中 菜单 - 工具 - 构建npm

  1. 定义store
import WeStore from '@7pound/westore'

const fetchUserScore = () => new Promise((resolve) => {
  setTimeout(() => {
    const score = Math.floor(Math.random() * 101)
    resolve({code: 0, message: 'ok', data: {score}})
  }, 500)
})
class UserStore extends WeStore {
  data = {
    name: 'xigua',
    age: 10,
    score: 0,
    say() {
      // the this is UserStore instance
      return this.data.name + ' say: my score is ' + this.data.score + '!'
    }
  }

  getUserScore = async () => {
    const res = await fetchUserScore()
    this.data.score = res.data.score
    this.update()
  }

  setName = (name) => {
    this.data.name = name
    this.update()
  }
}

const userStore = new UserStore()

export default userStore
  1. 使用

在page中使用

Page({
  data: {

  },
  onLoad() {
    userStore.bind(this, '$user')
  },
  onUnload() {
    userStore.unBing(this)
  },

  handleSetName() {
    const name = userStore.data.name.split('').reverse().join('')
    userStore.setName(name)
  },
  handleNavMine() {
    wx.navigateTo({url: '/pages/mine/mine'})
  }
})
<view class="container">
    <!-- <view class="page-card">
      <UserInfo />
    </view> -->


    <view class="p">
      <text>userStore.name: </text>
      <text>{{ $user.name }}</text>
    </view>

    <button type="primary" bindtap="handleSetName" class="btn">更新name</button>
    <button type="primary" bindtap="handleNavMine" class="btn">跳转个人中心页</button>
</view>

在Component中使用

Component({
  lifetimes: {
    ready() {
      userStore.bind(this, '$user')
    },
    detached() {
      userStore.unBind(this)
    }
  },
  methods: {
    handleGetScore() {
      wx.showLoading({title: '加载中'})
      userStore.getUserScore().then(() => {
        wx.hideLoading()
      }).catch(() => {})
    }
  }
})
<view class="contaienr">

  <view class="title">UserInfo组件</view>
  <view class="p">
    <text>userStore.name: </text>
    <text>{{ $user.name }}</text>
  </view>
   <view class="p">
    <text>userStore.score: </text>
    <text>{{ $user.score }}</text>
  </view>
  <view class="p">
    <text>userStore.say: </text>
    <text>{{ $user.say }}</text>
  </view>

  <button class="btn" type="primary" size="mini" bindtap="handleGetScore" >更新score</button>
</view>

DEMO示例

  1. 下载仓库
git clone https://github.com/7pou/westore.git
  1. 安装依赖
npm install or yarn
  1. 执行编译
npm run dev or yarn dev
  1. 导入项目

打开微信开发工具,导入项目、目录在 /miniprogram_dev

  1. 编译

API说明

store.data

store中元数据

store.bind(instance, store_name)

在页面中加载(onLoad)、组件挂载(lifetimes.ready)中调用。

绑定store到 page 或 component 的data 中,data中的键值为store_name

| 参数 | 说明 | 必填 | | ------------ | ------------- | --------------- | | instance | page实例 or component实例 | 是 | | store_name | 绑定到page实例 or component实例上data的名称 | 是 |

store.unBind(instance)

在页面卸载(onUnload)、组件销毁(lifetimes.detached)调用。

取消页面或组件中store绑定

| 参数 | 说明 | 必填 | | ------------ | ------------- | --------------- | | instance | page实例 or component实例 | 是 |

store.update()

更新store的值到页面或组件的 data 中(修改store.data 后调用)

基于小程序npm模板 开发

架构实现参考Tencent/westore

下期计划

  • [ ] 加入diff算法,优化update方法中setData一把梭的性能问题
  • [ ] 加入action log
  • [ ] 支持class 和 defineStore() 两种store定义方法