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

evg_event_history

v1.0.3

Published

The purpose of the project is to control the events of the object under study.

Downloads

15

Readme

What is EVG Event history?

EVG Event history - is a small library for control the events of the object under study.

Installation

Node.js

EVG Event history is available on npm. To install it, type:

$ npm install evg_event_history

Usage

Event history simple usage

I propose to consider an example of using the library. The written code is just an attempt to show how the library works. I apologize in advance for some logical and stylistic errors.

The example describes how a user receives money from an ATM. Everything is simplified as much as possible.

Create Environment Variables

file: env.ts

export enum Evt {
    INIT = "INIT",
    READY = "READY",
    ALLOWED = "ALLOWED",
    DISALLOWED = "DISALLOWED",
    REQUEST_FOR_MONEY = "REQUEST_FOR_MONEY",
    GET_MONEY = "GET_MONEY",
    LIMIT_ERROR = "LIMIT_ERROR",
    UNKNOWN_ERROR = "UNKNOWN_ERROR",
}

export type CreditCard = {
    creditLimit: number;
}

export type Cash = {
    coins: number;
    description: string;
}

Create cash machine

file: cashMachine.ts

import {History} from "evg_event_history/src/outLib/history";
import {Cash, CreditCard, Evt} from "./env";

export class CashMachine extends History<Evt> {
    userCard: CreditCard;

    constructor() {
        super(Evt.INIT);
        this.state = Evt.READY;
    }

    getMoney(card: CreditCard): Cash {
        this.userCard = card;
        this.state = Evt.REQUEST_FOR_MONEY;

        return this.handleBankReaction();
    }

    private handleBankReaction(): Cash {
        switch (this.state) {
            case Evt.ALLOWED:
                const userCash = this.userCard.creditLimit;
                this.state = Evt.GET_MONEY;

                return {
                    coins: userCash,
                    description: "Here is your money",
                };
            case Evt.DISALLOWED:
                this.state = Evt.LIMIT_ERROR;

                return {
                    coins: 0,
                    description: "Credit limit exceeded",
                };
        }

        this.state = Evt.UNKNOWN_ERROR;
        return {
            coins: 0,
            description: "Unknown error",
        }
    }

    allowOperation(): void {
        this.state = Evt.ALLOWED;
    }

    disallowOperation(): void {
        this.state = Evt.DISALLOWED;
    }
}

Create bank

file: bank.ts

import {ISubscriptionLike} from "evg_observable/src/outLib/Types";
import {Evt} from "./env";
import {CashMachine} from "./cashMachine";

export class Bank {
    subscriber: ISubscriptionLike<Evt>;

    connectTo(cashMachine: CashMachine): void {
        this.subscriber = cashMachine.eventSubscribe$(evt => {
            switch (evt) {
                case Evt.REQUEST_FOR_MONEY:
                    if (cashMachine.userCard.creditLimit > 0) {
                        cashMachine.allowOperation();
                        break;
                    }
                    cashMachine.disallowOperation();
                    break;
                case Evt.GET_MONEY:
                    cashMachine.userCard.creditLimit = 0;
            }
        })
    }

    communicationBreak(): void {
        this.subscriber && this.subscriber.unsubscribe();
        this.subscriber = null;
    }
}

Collect and launch

file: index.ts

import {CashMachine} from "./cashMachine";
import {Bank} from "./bank";

// The user has a card with a limit of 100 coins
const userCard = {creditLimit: 100};
// The bank services cards
const bank = new Bank();
// User wants to withdraw cash from this ATM
const cashMachine = new CashMachine();

// ATM connected to the bank
bank.connectTo(cashMachine);

// The user received his money
// "The user is trying to get money 
// (there are 100 coins in the card)"
console.log(cashMachine.getMoney(userCard));

// Alas, the money is no longer on the card
// "The user is trying to get money 
// (but he got everything last time)"
console.log(cashMachine.getMoney(userCard));

// Something happened and the ATM lost connection
bank.communicationBreak();

// Sorry something is wrong
// "The user is trying to get money again 
// (but the connection failed)"
console.log(cashMachine.getMoney(userCard));

// The support team checked the history of ATM events
// "Cash machine events history"
console.log(cashMachine.getHistory());

/**
 Print to console:

 { coins: 100, description: 'Here is your money' }
 { coins: 0, description: 'Credit limit exceeded' }
 { coins: 0, description: 'Unknown error' }
 Cash machine events history: [
 'INIT',
 'READY',
 'REQUEST_FOR_MONEY',
 'ALLOWED',
 'GET_MONEY',
 'REQUEST_FOR_MONEY',
 'DISALLOWED',
 'LIMIT_ERROR',
 'REQUEST_FOR_MONEY',
 'UNKNOWN_ERROR'
 ]
 */

Methods

class History

| methods and fields | will return | description | |:--------------------------------------------|:------------|:--------------------------------------------------------------------| | fields | | | | state | T | set or get state | | maxHistorySize | number | | | historySize | number | | | isDestroyed | boolean | | | | | | | methods | | | | constructor(startState: T, size?: number) | | startState - initial state, size - history size (50 by default) | | eventSubscribe$(subscriber: IListener<T>) | subscriber | subscribe listener to history | | stateForHistory(state: T) | void | set T to event history without state change | | isHistoryIncludes(state: T) | boolean | check T from event history | | getHistory() | T[] | get saved history | | destroy() | void | destroy history |