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

micro-reactive-cli

v1.2.2

Published

## 简介

Downloads

10

Readme

MICRO REACTIVE CLI

简介

micro-reactive cli

本地快速开始

# install micro-reactive-cli globally
pnpm i -g micro-reactive-cli
# create project
micro create my-app
# enter project
cd my-app
# install dependencies
pnpm i
# run dev
pnpm dev

模板

用于全局状态管理 (microx store)

  • options
/* options */
export const store = defineStore({
  id: "user",
  state: {
    count: 1,
  },
  getters: {
    double() {
      return this.count() * 2;
    },
    message() {
      return `double const is ${this.double()}`;
    },
  },
  actions: {
    increase(value: number = 1) {
      this.count(this.count() + value);
    },
  },
});
  • setup
/* setup */
import { defineStore } from "micro-reactive";

export const store = defineStore((ctx) => ({
  id: "user",
  state: {
    count: 1,
  },
  getters: {
    double: () => ctx.count() * 2,
    message: () => `double const is ${ctx.double()}`,
  },
  actions: {
    increase: (value: number = 1) => {
      ctx.count(ctx.count() + value);
    },
  },
}));
  • module
/* module */
import { useComputed, useMemo, useReactive } from "micro-reactive";

export const count = useReactive(0);
export const double = useMemo(() => count() * 2);
export function increase() {
  count(count() + 1);
}

solid js 中运行 (with solid js)

/* micro-counter.tsx */
import { useReactive } from "micro-reactive";

export default function Counter() {
  const count = useReactive(0);
  // 自增
  const increase = () => count(count() + 1);
  return (
    <button id="counter" type="button" onClick={increase}>
      count is {count()}
    </button>
  );
}
/* vite.config.ts */
import { defineConfig } from "vite";
import solidPlugin from "vite-plugin-solid";
// 自定义插件,在代码中嵌入 useEffect,收集依赖
import TrackEffect from "./plugins/vite-plugin-track-effect";

export default defineConfig({
  plugins: [solidPlugin(), TrackEffect()],
});
/* vite-plugin-track-effect.ts */
// 使用 useEffect 收集组件更新方法
const fileRegex = /\.(cjs|mjs|js|ts)/;
const funcRegex = /function updateComputation/;
// 导入
const importUseEffect = `import { useEffect } from "micro-reactive";`;
// 替换
const hackUpdateComputation = `
const update = updateComputation
updateComputation = function (node) {
  useEffect(() => update.call(this, node));
}`;
export default function TrackEffect() {
  return {
    name: "track-effect",
    transform(src: string, id: string) {
      if (fileRegex.test(id) && funcRegex.test(src)) {
        return { code: importUseEffect + src + hackUpdateComputation };
      }
    },
  };
}

vue3 中运行 (with vue3)

/* MicroCounter.vue */
<script setup lang="ts">
import { useReactive } from "micro-reactive";

const count = useReactive(0);
</script>

<template>
  <button type="button" @click="count(count() + 1)">
    count is {{ count() }}
  </button>
</template>
/* main.ts 中添加以下代码 */
import { ReactiveEffect } from "vue";
import { useEffect } from "micro-reactive";

// hack ReactiveEffect
const hackRun = ReactiveEffect.prototype.run;
ReactiveEffect.prototype.run = function () {
  return useEffect(() => hackRun.call(this));
};

react 中运行 (with react)

import { useComputed, useReactive } from "micro-reactive";
// 引入 hack
import { defineState } from "../hacks/defineState";

export default function Counter() {
  // useReactive 声明变量需写在 defineState 中
  const [count] = defineState(() => [useReactive(0)]);
  // 只读计算属性
  const double = () => count() * 2;

  return (
    <button onClick={() => count(count() + 1)}>
      double count is {double()}
    </button>
  );
}
/* defineState.ts */
import { useEffect } from "micro-reactive";
import { useState } from "react";

const stateMap = new WeakMap();

export function defineState<T extends {} | []>(defineReactive: () => T): T {
  const setter = useState({})[1];
  if (!setter) throw new Error("setter is empty");
  if (stateMap.has(setter)) {
    return stateMap.get(setter);
  }
  const state = useEffect(() => {
    setter({});
    if (stateMap.has(setter)) {
      return stateMap.get(setter);
    }
    const ret = defineReactive();
    for (const key in ret) {
      if (typeof ret[key] === "function") {
        (ret[key] as Function)();
      }
    }
    return ret;
  });
  stateMap.set(setter, state);
  return state;
}