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

rxjs6-wx

v2.0.8

Published

Based on Rx.js for Weixin miniprogram

Downloads

19

Readme

rxwx

_克隆自仓库 https://github.com/yalishizhude/RxWX 然后升级 rxjs 到 6.5.3 umd 版本. 升级了一下打包工具, 更新了一下 readme.md 里这些示例. 重新发包到 npmjs.org

封装了RxJS对象微信小程序 API,让你写出更优雅更简介的代码。

RxWX 模块支持所有微信小程序中 wx 对象的属性和函数,例如getUserInfo等。 RxWX 模块的Rx属性为 RxJS 对象,支持 RxJS 对象所有属性,例如Observable等。

安装

  1. 安装依赖

npm i rxjs6-wx

  1. 引用文件

import rxwx from 'rxjs6-wx'

也可将 operatorsrxjs 单独引入 import { operators } from 'rxjs6-wx/rx', 不单独引入的话就这样 rxwx.Rx.operators.map(...) 用.

不要直接 npm install rxjs, 那样小程序就太大了

同步函数

// 原写法
try {
  let result = wx.removeStorageSync('xx')
  console.log(result)
} catch(e) {
  console.error('小程序API发现错误')
}

// 使用RxWX,rxwx对象具有wx对象的所有函数和属性,函数返回Observable对象
// 下面示例中省略这行
import rxwx from 'rxjs6-wx'

rxwx.removeStorageSync('xx').pipe(
  operators.catchError(e => console.error('rxwx发现错误'))
  // 或独立引入 operators: import { operators } from '../rxjs6-wx/rx'
  // rxwx.Rx.operators.catchError(e => console.error('rxwx发现错误'))
  ).subscribe(
    resp => console.log(resp)
  )

异步函数

// 原写法
wx.removeStorage({
  key: 'xx',
  success: function(res) {
    console.log(res)
  },
  error: function(e) {
    console.error('小程序API发现错误')
  }
})

// 引用RxWX,rxwx对象函数参数与wx同名函数一致
rxwx.removeStorage({key: 'xx'}).pipe(
    operators.catchError(e => console.error('rxwx发现错误'))
    ).subscribe(
      resp => console.log(resp)
    )

异步嵌套

// 调用小程序原生API
wx.login({
  success(res) {
    wx.getUserInfo({
      success(res) {
        console.log(res.userInfo)
      },
      fail(e) {
        console.error(e)
      }
    })
  },
  fail(e) {
    console.error(e)
  }
})

// 调用RxWX
rxwx.login().switchMap(
    () => rxwx.getUserInfo()
  ).pipe(
    operators.catchError(e => console.log(e))
  ).subscribe(
    res => console.log(res.userInfo)
  )

异步合并

// 调用小程序API
let getUser =  new Promise((success, fail) => {
  wx.getUserInfo({
    success,
    fail
  })
})
let getSystem =  new Promise((success, fail) => {
  wx.getSystemInfo({
    success,
    fail
  })
})
Promise.all([getUser, getSystem])
.then((resp) => console.log(resp), e => console.error(e))

// 调用RxWX中的Rx对象,包含RxJS所有操作符和函数
// 使用zip操作符
Rx.Observable.zip(
    rxwx.getUserInfo(),
    rxwx.getSystemInfo()
  ).pipe(
    operators.catchError(e => console.log(e))
  ).subscribe(
    resp => console.log(resp)
  )

网络请求

http 请求

let handlerA = (res) => console.log('handler a:', res)
let handlerB = (res) => console.log('handler b:', res)
// 调用小程序API
let url = 'http://localhost:3456'
wx.request({
  url,
  success(res) {
    // 逻辑与请求的紧耦合
    handlerA(res)
    handlerB(res)
  }
})

// 调用RxWX
let req = rxwx.request({
  url
})
// 轻轻松松将业务逻辑与请求分离
req.subscribe(handlerA)
req.subscribe(handlerB)

websocket

let url = 'ws://localhost:34567'
// 调用微信小程序API
let ws = wx.connectSocket({
  url
})
ws.onOpen(() => {
  ws.send({ data: new Date })
  ws.onMessage(msg => console.log(msg.data))
  ws.close()
  ws.onClose(msg => console.log('Websocket closed.'))
})
// 调用RxWX
rxwx.connectSocket({
  url
})
.subscribe(ws => {
  ws.onOpen(() => {
    ws.send({ data: new Date })
    ws.onMessage(msg => console.log(msg.data))
    ws.close()
    ws.onClose(msg => console.log('Websocket closed.'))
  })
})

事件绑定

import rxwx from '../../utils/RxWX.js'
// 页面对象
let page ={
  data: {
    // 页面数据...
  },
  onLoad: function () {
    // 页面初始化
  },
}
// 使用fromEvent绑定事件,返回Observable对象
rxwx.fromEvent(page, 'bindViewTap')
  .debounceTime(1000)  // 防抖
  .switchMap(() => rxwx.navigateTo({
    url: '../logs/logs'
  }))  // 页面跳转
  .subscribe()
// 初始化页面逻辑
Page(page)