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

@cohook/core

v1.3.0-beta.0

Published

Framework-independent, lightweight, progressive, immutable data management

Downloads

91

Readme

@cohook/core

框架无关、轻量级、渐进式的不可变数据管理方案

✨特性

  • 读写分离,自定义Action去处理数据,灵活化组装实现逻辑复用
  • 轻量,API简单化,类型提示友好
  • 数据不可变, 使用immer管理数据
  • 支持插件化拓展功能

📦 安装

yarn add @cohook/core immer
npm i @cohook/core immer

💡API

createContainer(initialData [, option])

createContainer作为容器工厂函数,接收初始数据initialData和可选的插件选项pluginsOption和共享选项shareOption等配置,返回一个容器对象container

pluginsOption

插件选项是一个对象,key为插件名,value为插件方法具体实现。

⚠️ 为了插件的更简便实现,改写了插件方法的this参数,因此插件方法不能用箭头函数

  • this: PluginContext<T>

| 属性 | 说明 | 类型(T为容器数据的类型) | | -- | -- | -- | | getData | 容器对象container的getData方法 | () => T | | commit | 容器对象container的commit方法 | (updater: Updater) => T | | tryToTrackEffect | 追踪对应的数据,注入副作用钩子effectHook | (option: TrackOption)) => { trackId: number } | null | | cleanUpEffect | 清除副作用钩子 | (trackId: number) => void |

🌰

  • loggerPlugin
function loggerPlugin() {
  this.tryToTrackEffect({
    effectHook: (option) => {
      logToMyService(option)
    }
  })
}

shareOption

共享选项配置常用于跨应用(或窗口)共享容器,包括提供者shareProvider和消费者shareConsumer两个配置:

| 属性 | 说明 | 类型(T为容器数据的类型) | | -- | -- | -- | | shareProvider | 共享容器的提供者,常用于缓存容器 | (container: Container) => void | | shareConsumer | 共享容器的消费者,用于查找缓存的容器 | () => Container |

🌰

// 跨iframe共享容器
const getSharedOption: () => ShareOption<T> = () => {
  const scopeName = Symbol.for('__scopeName__')

  return window.top === window ? {
    shareProvider: container => window[scopeName] = container
  } : {
    shareConsumer: () => window.top[scopeName]
  }
}

const container = createContainer(initialData,  getSharedOption())

container

container.getData()

用于获取当前的容器数据,就像是一个内置的action,可在任何地方使用。

container.commit(updater)

commit 接受一个函数updater(immer中的produce的第二个参数保持一致)作为参数,用来创建修改数据的Action,可灵活组合容器对象提供的方法来组装Action。

const dec = () => {
  const count = container.getData()
  if(count < 0) return
  container.commit((draft) => void (draft.current -= 1)
}