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

veto-mvvm

v0.1.1

Published

View/ViewModel pattern implementation and DI container for React

Downloads

240

Readme

veto-mvvm

View/ViewModel pattern implementation and lightweight dependency injection container for React.

Installation

npm install veto-mvvm

Features

  • ViewModel pattern for separating UI from business logic
  • Lightweight DI container inspired by Flutter's get_it
  • Lifecycle management with init/dispose hooks
  • Three registration types: singleton, lazy singleton, factory
  • TypeScript-first with full type inference
  • Zero dependencies (peer dependency on React 18+)

Quick Start

import { locator, locate, useService, useViewModel, ViewModel } from 'veto-mvvm';

// Register services at app startup
locator.registerLazySingleton('usersApi', () => new UsersApi());
locator.registerLazySingleton('usersService', () => new UsersService(locate('usersApi')));

// Use in components
function UsersPage() {
  const usersService = useService<UsersService>('usersService');
  // ...
}

API - Dependency Injection

LocatorService

The DI container class. Access via locator singleton or LocatorService.I.

locator

Singleton instance of the DI container.

import { locator } from 'veto-mvvm';

Registration Methods

registerSingleton<T>(key: string, instance: T): T

Register a pre-created instance. Same instance returned on every get().

const api = new UsersApi();
locator.registerSingleton('usersApi', api);

registerLazySingleton<T>(key: string, factory: () => T): void

Register a factory for lazy instantiation. Instance created on first access, then cached.

locator.registerLazySingleton('usersApi', () => new UsersApi());
locator.registerLazySingleton('usersService', () =>
  new UsersService(locate('usersApi'))
);

registerFactory<T>(key: string, factory: () => T): void

Register a factory. New instance created on every get().

locator.registerFactory('userDto', () => new UserDto());

Retrieval Methods

get<T>(key: string): T

Get an instance by key.

const service = locator.get<UsersService>('usersService');

locate<T>(key: string): T

Shorthand function for locator.get().

const service = locate<UsersService>('usersService');

useService<T>(key: string): T

React hook for accessing services. Memoized to prevent re-lookups.

function MyComponent() {
  const usersService = useService<UsersService>('usersService');
  // ...
}

Utility Methods

isRegistered(key: string): boolean

Check if a key is registered.

unregister(key: string): void

Remove a registration.

reset(): void

Clear all registrations. Useful for testing.

beforeEach(() => {
  locator.reset();
  locator.registerSingleton('mockApi', new MockUsersApi());
});

API - ViewModel Pattern

ViewModel<A> Interface

Base interface for ViewModels.

interface ViewModel<A = void> {
  readonly arguments: A;
  readonly isInitialised: boolean;
  readonly isMounted: boolean;
}

useViewModel<T, A>(options): T & { rebuild: () => void }

Hook for implementing the ViewModel pattern.

Options

| Property | Type | Description | |----------|------|-------------| | create | (args: A) => T | Factory function to create the ViewModel | | arguments | A | Arguments passed to the ViewModel | | onInitialise | (vm: T) => void \| Promise<void> | Called after creation | | onDispose | (vm: T) => void | Called on unmount |

Example

interface UserListViewModel extends ViewModel<{ teamId: string }> {
  users: User[];
  isLoading: boolean;
  loadUsers: () => Promise<void>;
}

function UserListView({ teamId }: { teamId: string }) {
  const vm = useViewModel<UserListViewModel, { teamId: string }>({
    arguments: { teamId },
    create: (args) => {
      const [users, setUsers] = useState<User[]>([]);
      const [isLoading, setIsLoading] = useState(false);

      const loadUsers = async () => {
        setIsLoading(true);
        const data = await fetchTeamUsers(args.teamId);
        setUsers(data);
        setIsLoading(false);
      };

      return {
        arguments: args,
        isInitialised: false,
        isMounted: true,
        users,
        isLoading,
        loadUsers,
      };
    },
    onInitialise: async (vm) => {
      await vm.loadUsers();
    },
    onDispose: (vm) => {
      console.log('Cleanup');
    },
  });

  if (!vm.isInitialised) return <Loading />;

  return (
    <ul>
      {vm.users.map(user => <li key={user.id}>{user.name}</li>)}
    </ul>
  );
}

ViewModelProvider<T, A>

Component wrapper alternative to useViewModel.

function App() {
  return (
    <ViewModelProvider
      create={() => createUserListViewModel()}
      onInitialise={async (vm) => await vm.loadUsers()}
      onDispose={(vm) => vm.cleanup()}
    >
      {(vm, isInitialised) => (
        isInitialised ? <UserList users={vm.users} /> : <Loading />
      )}
    </ViewModelProvider>
  );
}

Utility Hooks

useIsMounted(): () => boolean

Track component mount state.

const isMounted = useIsMounted();

const loadData = async () => {
  const data = await fetchData();
  if (isMounted()) {
    setState(data);
  }
};

useForceUpdate(): () => void

Get a function to force re-render.

const forceUpdate = useForceUpdate();
// ... later
forceUpdate(); // Trigger re-render

useInitialised(): [boolean, (value: boolean) => void]

Track initialization state.

const [isInitialised, setInitialised] = useInitialised();

useEffect(() => {
  loadData().then(() => setInitialised(true));
}, []);

Patterns

Service Registration at Startup

// services/setup.ts
export function setupServices() {
  // APIs
  locator.registerLazySingleton('usersApi', () => new UsersApi(firestore));
  locator.registerLazySingleton('ordersApi', () => new OrdersApi(firestore));

  // Services (depend on APIs)
  locator.registerLazySingleton('usersService', () =>
    new UsersService(locate('usersApi'))
  );
  locator.registerLazySingleton('ordersService', () =>
    new OrdersService(locate('ordersApi'), locate('usersService'))
  );
}

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

Testing with Mock Services

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

  it('renders users', () => {
    render(<UserList />);
    // ...
  });
});

License

MIT