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

@xieyezi/genji

v0.0.1

Published

a vue state management framewok by vue3 reactivity

Downloads

33

Readme

Language: EN | 中文简体

visitors bundle size npm-version coverage

genji is A small vue state management framewok by vue3 reactivity.

Why calls genji ?

It's inspired by Overwatch.

Genji flings precise and deadly Shuriken at his targets, and uses his technologically-advanced katana to deflect projectiles or deliver a Swift Strike that cuts down enemies.

So genji is fast, agile and accurate!

pnpm install @xieyezi/genji
yarn add @xieyezi/genji

Create Store

Your store is a hook base on compostion-api! You can put anything in it: primitives, objects, functions. The set function merges state.

import { create } from '@xieyezi/genji'

const useStore = create((set, get) => ({
  count: 0,
  increase: () => set(state => ({ count: state.count + 1 })),
  resetCount: () => set({ count: 0 })
}))

Then use it in your vue components, and that's it!

Use the hook in your components, Select your state and the component will re-render on changes.

<template>
  <p>count is: {{ count }}</p>
  <button @click="increase">count++</button>
</template>
....
setup() {
  const { count, increase } = useStore(state => ({
   count: state.count,
   increase: state.increase
  }))

  return {
    count,
    increase,
  }
}

Selecting multiple state slices

You can get state sliece in the way you like.

If you want to pick it out one by one:

const count = useStore(state => state.count)
const genji = useStore(state => state.genji)

If you want pick it by Object, like vuex mapState:

// Object pick, re-renders the component when either state.count or state.genji change
const { count, genji } = useStore((state) => ({
  count: state.count,
  genji: state.genji
}))

If you want pick it lick by Array, like react hooks:

// Array pick, re-renders the component when either state.count or state.genji change
const [count, genji] = useStore(state => [state.count, state.genji])

Even you can pick it without args:

// uses the store with no args
const { count, increase } = useStore()

All pick is so random and simple! It's all up to you.

Fetching from multiple stores

Since you can create as many stores as you like, forwarding results to succeeding selectors is as natural as it gets.

import useUserStore from '../store/user'
import useOrder from '../store/order'

const name = useUserStore(state => state.name)
const orders = useOrder(state => state.orders)

Memoizing selectors

It is generally recommended to memoize selectors with computed.

But you need to pay attention, the value pick from the state needs to be wrapped with unref, because the value of the state is proxied by the Proxy.

const countDouble = useStore(state =>computed(()=>unref( state.count) * 2))

If a selector doesn't in components to reactivity, you can define it outside the components. But when you to use value of pick from the state, you need to be wrapped with unref too.

const selector = state => state.hero
const hero = useStore(selector)

// warpped with unref()
console.log(unref(hero))

// or you can use like this:
console.log(hero.value)

Overwriting state

genji provide set function to update state. just like this:

const useStore = create((set, get) => ({
  count: 0,
  increase: () => set(state => ({ count: state.count + 1 })),
}))

const { count, increase } = useStore(state => ({
  count: state.count,
  increase: state.increase
}))

then you can use increase function to change state.

Async actions

const useStore = create((set, get) => ({
   userInfo: {},
   getUserInfo: async () => {
      const res = await fetch(pond)
      set({ userInfo: res })
   }
}))

Read from state in actions

set allows fn-updates set(state => result), but you still have access to state outside of it through get.

const useStore = create((set, get) => ({
  hero: 'genji',
  action: () => {
    const hero = get().hero
    // ...
  }
})

TypeScript

// You can use `type`
type State = {
  count: number
  increase: (by: number) => void
}

// Or `interface`
interface State {
  count: number
  increase: (by: number) => void
}

// And it is going to work for both
const useStore = create<State>(set => ({
  count: 0,
  increase: (by) => set(state => ({ count: state.count + by })),
}))

Hope you enjoy it!

api usage inspired by zustand, thanks a lot!