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

@open-game-system/app-bridge-types

v0.20250411.3

Published

Core type definitions for the app-bridge ecosystem

Downloads

19

Readme

@open-game-system/app-bridge-types

Core type definitions for the app-bridge ecosystem.

Installation

npm install @open-game-system/app-bridge-types
# or
yarn add @open-game-system/app-bridge-types
# or
pnpm add @open-game-system/app-bridge-types

API Reference

Core Types

/**
 * Represents a generic state type that can be used in stores
 */
export type State = object;

/**
 * Represents a generic event type that can be dispatched to stores
 * Events are discriminated unions with a type field and optional additional properties
 */
export type Event = { type: string };

/**
 * Producer function type for handling events
 * Similar to Immer's produce function
 */
export type Producer<S extends State, E extends Event> = (draft: S, event: E) => void;

/**
 * Store configuration for creating new stores
 */
export interface StoreConfig<S extends State, E extends Event> {
  initialState: S;
  producer?: Producer<S, E>;
}

/**
 * Creates a new store with the given configuration
 */
export type CreateStore = <S extends State, E extends Event>(
  config: StoreConfig<S, E>
) => Store<S, E>;

/**
 * Represents a store instance with state management capabilities
 */
export interface Store<S extends State = State, E extends Event = Event> {
  /** Get the current state */
  getSnapshot(): S;
  /** Subscribe to state changes */
  subscribe(listener: (state: S) => void): () => void;
  /**
   * Dispatch an event to the store. Synchronously updates state and triggers listeners.
   */
  dispatch(event: E): void;
  /** Reset store to initial state */
  reset(): void;
  /**
   * Add a listener for specific dispatched events.
   * Listeners can be async and receive the event and store instance.
   * @param eventType The type of the dispatched event (E['type']).
   * @param listener The callback function.
   * @returns An unsubscribe function.
   */
  on(eventType: E['type'], listener: (event: E, store: Store<S, E>) => void): () => void;
}

/**
 * Represents a collection of store definitions
 */
export type BridgeStores<
  T extends Record<string, { state: State; events: Event }> = Record<
    string,
    { state: State; events: Event }
  >
> = {
  [K in keyof T]: {
    state: T[K]["state"];
    events: T[K]["events"];
  };
};

/**
 * Base bridge interface that all implementations extend
 */
export interface Bridge<TStores extends BridgeStores> {
  /**
   * Check if the bridge is supported in the current environment
   */
  isSupported: () => boolean;

  /**
   * Get a store by its key
   * Returns undefined if the store doesn't exist
   */
  getStore: <K extends keyof TStores>(
    storeKey: K
  ) => Store<TStores[K]["state"], TStores[K]["events"]> | undefined;

  /**
   * Set or remove a store for a given key
   */
  setStore: <K extends keyof TStores>(
    key: K,
    store: Store<TStores[K]["state"], TStores[K]["events"]> | undefined
  ) => void;

  /**
   * Subscribe to store availability changes
   */
  subscribe: (listener: () => void) => () => void;
}

/**
 * Utility type to extract store types from any Bridge implementation
 */
export type ExtractStoresType<T> = T extends {
  getStore: <K extends keyof (infer U)>(key: K) => any;
}
  ? U
  : never;

/**
 * Defines the configuration for declarative, potentially async event listeners
 * within a store, triggered *after* state updates for a given dispatched event type.
 * Listeners receive the event and the store instance.
 */
export type StoreOnConfig<S extends State, E extends Event> = Partial<{
  [K in E['type']]: (event: Extract<E, { type: K }>, store: Store<S, E>) => void;
}>;

Usage Examples

Creating a Store Type

// Define your state type
interface CounterState extends State {
  value: number;
}

// Define your events
type CounterEvents =
  | { type: "INCREMENT" }
  | { type: "DECREMENT" }
  | { type: "SET"; value: number };

// Create store configuration
const config: StoreConfig<CounterState, CounterEvents> = {
  initialState: { value: 0 },
  producer: (draft, event) => {
    switch (event.type) {
      case "INCREMENT":
        draft.value += 1;
        break;
      case "DECREMENT":
        draft.value -= 1;
        break;
      case "SET":
        draft.value = event.value;
        break;
    }
  }
};

Defining Bridge Stores

// Define your application's stores
type AppStores = {
  counter: {
    state: CounterState;
    events: CounterEvents;
  };
  // Add more stores as needed
};

// Use with a bridge implementation
const bridge: Bridge<AppStores> = createBridge();

Using Store Methods

// Get a store
const store = bridge.getStore('counter');
if (store) {
  // Subscribe to state changes
  const unsubscribe = store.subscribe(state => {
    console.log('Counter value:', state.value);
  });

  // Dispatch events
  store.dispatch({ type: "INCREMENT" });
  store.dispatch({ type: "SET", value: 42 });

  // Reset to initial state
  store.reset();

  // Clean up subscription
  unsubscribe();
}