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

badland

v0.2.4

Published

package for state management

Downloads

12

Readme

badland

  • 쉽습니다.
  • 의존이 없습니다.

사용방법

스토어 생성

badland에서 제공하는 스토어를 상속받아서 스토어를 구현합니다.

// store/auth.ts

import Store from 'badland'

interface AuthStoreState {
    isLogin: boolean;
    username: string;
}

const INIT_STATE = {
    isLogin: false,
    username: '',
}

class AuthStore extends Store<AuthStoreState> {
    constructor() {
        super()
        this.state = INIT_STATE
    }
}

export const authStore = new AuthStore()

또는 아래 함수를 사용하여 생성할 수 있습니다.

import { createStore } from 'badland'

export const authStore = createStore({
    isLogin: false,
    username: '',
})

스토어 구독

subscribe 메서드로 상태의 변경을 감시할 수 있습니다. 이 메서드는 key 값을 리턴하며 이 값은 구독 해제시 사용합니다.

import { authStore } from '~/store/auth'

const key = authStore.subscribe((state) => {
    document.getElementById('root').innerHTML = !state.isLogin
        ? `<div>로그인이 필요합니다.</div>`
        : `<div>${state.username}님 환영합니다!</div>`
})

// authStore.unsubscribe(key)

스토어 상태 변경

객체를 전달하여 값을 변경합니다.

authStore.set({
    isLogin: true,
    username: 'baealex1',
})

함수로 전달하면 이전 상태를 받을 수 있습니다.

authStore.set((prevState) => ({
    ...prevState,
    username: 'baealex2',
}))

변경이 필요한 객체만 전달해도 됩니다.

authStore.set((prevState) => ({
    username: 'baealex3',
}))

스토어 상태 변경 감지

beforeStateChange, afterStateChange 메서드를 오버라이딩하여 상태 변경시 동작할 로직을 작성할 수 있습니다.

import Store from 'badland'

export interface AuthStoreState {
    isLogin: boolean
    username: string
}

class AuthStore extends Store<AuthStoreState> {
    constructor() {
        super()
        this.state = {
            isLogin: false,
            username: '',
        }
    }

    beforeStateChange() {
        alert('start set, before state change')
    }

    afterStateChange() {
        alert('end set, after state change')
    }
}

export const authStore = new AuthStore()

스토어 디버깅

스토어의 debug 변수를 true로 변경합니다.

import Store from 'badland'

export interface AuthStoreState {
    isLogin: boolean
    username: string
}

class AuthStore extends Store<AuthStoreState> {
    constructor() {
        super()
        this.debug = true,
        this.state = {
            isLogin: false,
            username: '',
        }
    }
}

export const authStore = new AuthStore()

이후 상태 변경시 로그가 아래와 같이 출력됩니다.

authStore > Array(1) [ {…} ]
authStore > Array(2) [ {…}, {…} ]
authStore > Array(3) [ {…}, {…}, {…} ]

debugger 변수를 오버라이딩하여 수정할 수 있습니다. name은 스토어의 이름 states는 변경 기록이 누적된 값입니다.

import Store from 'badland'

export interface AuthStoreState {
    isLogin: boolean
    username: string
}

class AuthStore extends Store<AuthStoreState> {
    constructor() {
        super()
        this.debug = true,
        this.debugger = (name, states) => {
            // console.log(name, states.slice(-5))
        }
        this.state = {
            isLogin: false,
            username: '',
        }
    }
}

export const authStore = new AuthStore()