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

@huang-julien/history-state

v1.0.1

Published

A simple state history manager, written in TS

Downloads

2

Readme

A simple library for state history management

How it works

this library provide two classes:

  • HistoryState This class handle the history of whatever you want and can be extended.
  • HistoryManager This class handle traces of multiple HistoryState changes. If you are using a HistoryManager on a HistoryState, avoid using undo() or redo() from the HistoryState instance but instead call it from the HistoryManager

Methods

HistoryState

/**
 * method to override if you are extending HistoryState
 */
__onStateChange(value: S, states: S[], type: 'redo'|'undo'): void

/**
 * method to call to commit a change
 * you have to call this method everytime you want to commit a change
 */
commitChange(value: S): void

/**
 *  method to call to redo changes
 */
redo(): void

/**
 * method to call to undo changes
 */
undo(): void 

HistoryManager


/**
 *  method to call to redo changes
 */
redo(): void

/**
 * method to call to undo changes
 */
undo(): void 

Basic usage

let data = ''

const dataHistory = new HistoryState<string>({
    onStateChange: (value: string, states: string[], type: 'redo'|'undo') => {
        data = value
    },
    originalState = ''
})

data = 'hello'

dataHistory.registerChange(data)

dataHistory.undo() // data === ''
data.redo() // data === 'hello'

Manage multiple HistoryState

if you need to manage multiple HistoryState, pass a HistoryManager instance as the second param in the constructor

let hello = 'hello'
let world = 'world'

const statesHistory = new HistoryManager()

const helloStateHistory = new HistoryState({
    onStateChange: (value: string, states: string[], type: 'redo'|'undo') => {
        data = value
    },
    originalState: hello
}, statesHistory)

const worldStateHistory = new HistoryState({
    onStateChange: (value: string, states: string[], type: 'redo'|'undo') => {
        data = value
    },
    originalState: world
}, statesHistory)

hello = 'test'
helloStateHistory.registerChange(hello)

/* call the redo and undo from HistoryManager */
statesHistory.undo() // hello === 'hello'; world === 'world';
statesHistory.redo() // hello === 'test'; world === 'world';

In this case, don't call redo() and undo() on the HistoryState instances but call theses methods from the HistoryManager instance

Extend HistoryState

A class can extends the HistoryState class. Instead of passing onStateChange, these methods can be overriden.

type THistoryState = {hello: string, world: string}

class SomeData extends HistoryState<THistoryState> {

    notSaved = 'data not kept in history'
    #hello = 'hello'
    #world = 'world'

    constructor() {
        super({ hello: 'hello', world: 'world' })
    }

    get hello() {
        return this.#hello;
    }

    set hello(v: string) {
        this.#hello = v
        this.onChange()
    }

    get hello() {
        return this.#world;
    }

    set hello(v: string) {
        this.#world = v
        this.onChange()
    }
    
    onChange() {
        this.registerChange({
            hello: this.#hello,
            world: this.#world
        })
    }

    __onStateChange(s: THistoryState) {
        const { hello, world } = s
        this.#hello = hello
        this.#world = world
    }
}

Note: Feel free to fork it and improve it