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

mvc-state

v0.0.4

Published

## The MVC patter There are many definitions of what is MVC out there, here is the one this project follows:

Downloads

6

Readme

MVC State

The MVC patter

There are many definitions of what is MVC out there, here is the one this project follows:

MVC

Storage

A storage is a way of sharing data.

import { Storage } from 'mvc-state';

export class MyData {
    foo: number = 0;
    bar: number = 0;
}

export enum MyDataEvt {
    FOO = 'foo',
    BAR = 'bar',
    DEC_BAR = 'DEC_BAR',
}

export const MyDataStorage = new Storage<MyData>(new MyData());

We start by creating a class with all data to be shared called MyData in this example. Don't forget to give an initial value to EVERY property otherwise, it won't work properly later. MyDataEvt will be responsible for providing you with the right events to listen.

Model

Models hold all the business logic of your application.

import { Model, IView } from 'mvc-state';
import { MyData } from '../../storage/MyData';

export class InputModel extends Model<MyData> {

    constructor(view: IView, storage: MyData) {
        super(view, storage);
    }

    public incFoo(value: number) {
        this.storage.foo += value;
        this.updateView({ foo: this.storage.foo });
    }

    public incBar(value: number) {
        // setState updates both storage and view
        this.setState({ foo: this.storage.foo });
    }
}

By default a model's constructor takes 2 parameters:

  • view - That's the react component with all its properties
  • storage - That's the object we created earlier

The IntFoo function shows the basic usage of these 2 objects. When this.storage.foo is set, an event is sent out to every watcher listening to the MyDataEvt.FOO event. And this.updateView works like the setState used by React.

Be aware that the view will only be updated when you call updateView.

Controller

Controllers are responsible for listening to the view, storage events and talk to the model.

import { Controller, Listener } from 'mvc-state';
import { InputModel } from './InputModel';
import { MyDataEvt } from './MyData';

export class InputCtrl extends Controller {
    private model: InputModel;
    private emit: Function,
    private listener: Listener,

    constructor(model: InputModel, emit: Function, listener: Listener) {
        super();
        this.model = model;
        this.emit = emit;
        this.listener = listener;

        // This will be triggered when some controller calls `this.emit(MyDataEvt.FOO, value)`
        this.listener.watch(MyDataEvt.FOO, (inc: number) => this.model.incFoo(inc));

        // This will be triggered when some controller calls `this.emit(MyDataEvt.DEC_BAR, value)`
        this.listener.watch(MyDataEvt.DEC_BAR, (dec: number) => this.model.incFoo(dec));
    }

    public onIncBar = () => {
        // Here we are calling the model
        this.model.incBar(1);
    }

    public onDecBar = () => {
        // Here we are emiting an event
        this.emit(MyDataEvt.DEC_BAR, -1);
    }
}

View

The view is the presentation layer.

import * as React from "react";
import { MyData, MyDataStorage } from "../../storage/MyData";
import { InputCtrl } from "./InputCtrl";
import { InputModel } from "./InputModel";

export class Input extends React.Component {
    public state: MyData;
    private ctrl: InputCtrl;

    constructor(props: any) {
        super(props);
        const dataModule = new InputModel(this, MyDataStorage.state);

        // The way to get a listener is to pass a unique id to the function MyDataStorage.getListner
        this.ctrl = new InputCtrl(dataModule, MyDataStorage.emit, MyDataStorage.getListner('InputCtrl'));
        this.state = new MyData();
    }

    render() {
        return (
            <div >
                <span >
                    <button onClick={this.ctrl.onIncBar}>+</button>
                    <p>Bar: {this.state.bar}</p>
                    <button onClick={this.ctrl.onDecBar}>-</button>
                </span>
            </div>
        );
    }
}

Not much to say here. It is just react. Every time you call viewUpdate in a model, the state will be updated, any event should be handled by the controller.

API

  • Controller - Object responsible for manage events, validate data coming from the view and talk to the model.
  • GetWatcher - Function that takes an argument ID and returns an event watcher.
  • Listener - Object with the functions to watch and unWatch events for a storage.
  • Storage - Object responsible for storing data, emission, and listening of events.
  • Unwatch - Function that takes an argument event ID and removes the watcher associated with it.
  • Watch - Function that takes an event ID and a callback as arguments and link the event with the callback
  • Model - Object that holds the business layer, update the view and storage.