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

trace-state

v1.0.0-alpha.2

Published

Trace a state of objects using event streams

Downloads

3

Readme

Traceable

Build Status

Inspired by @ngrx/platform for Angular applications, this library allows you to decorate any prototype / class with a functionality to track object states created from it as an event stream using Reactive Extension observables, see ReactiveX/rxjs. It's also possible to create side effects in the prototype level that runs automatically when the state of an object has changed or we're trying to make some actions on it.

Setup

Please make sure you know what reactive programming means and you are familiar with the rxjs library first in order to use the Observable interface:

Install our library:

npm i trace-state

Usage

Check out our pre maid Examples to get sense of how to use this library properly.

using Traceable decorator: the Traceable decorator adds functionality to a class / prototype (check type Traceable<STATE, ACTIONS>) so when an object of this prototype is created, you can track its changes.

in order to use decorators in Typescript you should enable experimentalDecorators compiler option in your tsconfig.json file like so:

{
    "compilerOptions": {
        "target": "ES5",
        "experimentalDecorators": true
    }
}

Create a person object and track it's state:

// declare what state you wish to trace
interface PersonData {
    age: string;
}

// declare which actions you can do to change that state
interface PersonOlderAction extends Action {
    type = 'older';

    constructor() {
    }
}

type PersonActions = PersonOlderAction; // You can add as much actions as you like with the operator pipe "|"

// declare function that change a state by a given action
function reducer(action: PersonActions, previous_state: PersonData) {
    // We have new incoming action, and we decide which next state the object should have
    if (action.type === 'older') {
        return {
            age: previous_state.age + 1
        };
    }

    return previous_state;
}

// declare initialization function so every person will have an initial state
function initialize() {
    return {
        age: 0
    }
}

// Here we bind the state and actions to the Person prototype
@Traceable<PersonData, PersonActions>({
    reduce: reducer,
    initialize: initialize
})
class Person {
    ....
    older() {
        // Basically we can trigger actions where we want to
        this.act(new PersonOlderAction());
    }
}

let person = new Person();

// Traceable decorator adds functions that were not exist in the original Person prototype
// Check the interface @Traceable<STATE, ACTIONS>
// Here we are listening to be notified when the 'age' field of the person is changed
let personAge = person.state$('age');

personAge.subscribe(console.log); // when the age of the person is changed, it will be printed out

person.older(); // Will cause the age field in person to be increased

// We can also run action on person directly
person.act(new PersonOlderAction());