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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@appboypov/veto-mvvm

v1.0.0

Published

MVVM pattern with BaseViewModel class, ViewModelBuilder component, and DI container for React - Flutter inspired

Readme

@appboypov/veto-mvvm

MVVM pattern implementation and lightweight dependency injection container for React, inspired by Flutter.

Installation

npm install @appboypov/veto-mvvm

Features

  • BaseViewModel class - Abstract class with lifecycle management and notifyListeners()
  • ViewModelBuilder component - Declarative ViewModel creation and lifecycle management
  • Lightweight DI container - Inspired by Flutter's get_it
  • React 18 concurrent mode support via useSyncExternalStore
  • Zero dependencies (peer dependency on React 18+)

Quick Start

BaseViewModel + ViewModelBuilder

import { BaseViewModel, ViewModelBuilder } from '@appboypov/veto-mvvm';

class CounterViewModel extends BaseViewModel {
  private _count = 0;

  get count() {
    return this._count;
  }

  increment() {
    this._count++;
    this.notifyListeners(); // Triggers rebuild
  }

  async initialise(): Promise<void> {
    // Load initial data here
    await super.initialise(); // Call LAST
  }

  dispose(): void {
    // Cleanup here
    super.dispose(); // Call LAST
  }
}

function CounterView() {
  return (
    <ViewModelBuilder
      viewModelBuilder={() => new CounterViewModel()}
      builder={(vm, isInitialised) => (
        <div>
          {isInitialised ? (
            <>
              <p>Count: {vm.count}</p>
              <button onClick={() => vm.increment()}>+</button>
            </>
          ) : (
            <p>Loading...</p>
          )}
        </div>
      )}
    />
  );
}

API - BaseViewModel

BaseViewModel<A>

Abstract base class for ViewModels with lifecycle management.

class MyViewModel extends BaseViewModel<MyArgs> {
  // Your ViewModel implementation
}

Properties

| Property | Type | Description | |----------|------|-------------| | isInitialised | boolean | Whether initialise() has completed | | isMounted | boolean | Whether component is mounted | | arguments | A \| undefined | Arguments passed via argumentBuilder |

Methods

| Method | Description | |--------|-------------| | initialise() | Async initialization. Call super.initialise() LAST | | dispose() | Cleanup. Call super.dispose() LAST | | notifyListeners() | Trigger rebuild of subscribed components |

Example

class UserViewModel extends BaseViewModel<{ userId: string }> {
  user: User | null = null;
  isLoading = false;

  async initialise(): Promise<void> {
    this.isLoading = true;
    this.notifyListeners();

    this.user = await fetchUser(this.arguments!.userId);
    this.isLoading = false;

    await super.initialise(); // Sets isInitialised = true
  }

  dispose(): void {
    // Cleanup subscriptions, etc.
    super.dispose();
  }
}

API - ViewModelBuilder

ViewModelBuilder<T, A>

Component that creates, manages, and provides a BaseViewModel to its children.

<ViewModelBuilder
  viewModelBuilder={() => new MyViewModel()}
  argumentBuilder={() => ({ userId: '123' })}
  builder={(vm, isInitialised, child) => (
    // Your UI here
  )}
  child={<StaticContent />}
  shouldDispose={true}
  onDispose={(vm) => console.log('Disposed')}
/>

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | viewModelBuilder | () => T | required | Factory to create ViewModel | | argumentBuilder | () => A | - | Factory to provide arguments | | builder | (vm, isInit, child?) => ReactNode | required | Render function | | child | ReactNode | - | Non-rebuilding child | | shouldDispose | boolean | true | Dispose ViewModel on unmount | | onDispose | (vm) => void | - | Callback before dispose |

Example with Arguments

function UserProfile({ userId }: { userId: string }) {
  return (
    <ViewModelBuilder
      viewModelBuilder={() => new UserViewModel()}
      argumentBuilder={() => ({ userId })}
      builder={(vm, isInitialised) => (
        <div>
          {!isInitialised ? (
            <p>Loading...</p>
          ) : (
            <div>
              <h1>{vm.user?.name}</h1>
              <p>{vm.user?.email}</p>
            </div>
          )}
        </div>
      )}
    />
  );
}

Example with Non-Rebuilding Child

<ViewModelBuilder
  viewModelBuilder={() => new ListViewModel()}
  child={<ExpensiveHeader />}
  builder={(vm, isInitialised, child) => (
    <div>
      {child}
      <ul>
        {vm.items.map(item => <li key={item.id}>{item.name}</li>)}
      </ul>
    </div>
  )}
/>

API - Dependency Injection

LocatorService

The DI container class. Access via locator singleton.

Registration Methods

import { locator, locate } from '@appboypov/veto-mvvm';

// Pre-created instance (eager)
locator.registerSingleton('api', new UsersApi());

// Lazy instantiation (created on first access)
locator.registerLazySingleton('service', () => new UsersService(locate('api')));

// New instance every time
locator.registerFactory('dto', () => new UserDto());

Retrieval Methods

// Direct access
const service = locator.get<UsersService>('service');

// Shorthand
const service = locate<UsersService>('service');

// React hook (memoized)
function MyComponent() {
  const service = useService<UsersService>('service');
}

Utility Methods

locator.isRegistered('key');  // Check if registered
locator.unregister('key');    // Remove registration
locator.reset();              // Clear all (for testing)

Patterns

Service Registration at Startup

// services/setup.ts
export function setupServices() {
  locator.registerLazySingleton('usersApi', () => new UsersApi());
  locator.registerLazySingleton('usersService', () =>
    new UsersService(locate('usersApi'))
  );
}

// main.tsx
setupServices();
ReactDOM.createRoot(root).render(<App />);

ViewModel with Service Injection

class UsersViewModel extends BaseViewModel {
  private usersService = locate<UsersService>('usersService');
  users: User[] = [];

  async initialise(): Promise<void> {
    this.users = await this.usersService.getUsers();
    await super.initialise();
  }
}

Testing with Mocks

describe('UsersViewModel', () => {
  beforeEach(() => {
    locator.reset();
    locator.registerSingleton('usersService', new MockUsersService());
  });

  it('loads users on init', async () => {
    const vm = new UsersViewModel();
    await vm.initialise();
    expect(vm.users).toHaveLength(2);
  });
});

License

MIT