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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@stackra/coordinator

v2.0.0

Published

Cross-tab coordination for browser apps — leader election, distributed locks, and event relay across tabs.

Downloads

346

Readme

@stackra/coordinator

Cross-tab coordination for browser apps — leader election, distributed locks, and event relay across every open tab. Zero server dependency; uses Web Locks API and BroadcastChannel.

Install

pnpm add @stackra/coordinator @stackra/container @stackra/contracts reflect-metadata

Quick start

import { CoordinatorModule } from "@stackra/coordinator";

@Module({
  imports: [
    CoordinatorModule.forRoot({
      channelName: "my-app",
      heartbeatMs: 1000,
      staleThresholdMs: 3000,
      broadcastEvents: true,
      broadcastPatterns: ["auth:**", "sync:**"],
      preferWebLocks: true,
      preferWebLocksElection: true,
      preferVisibleLeader: false,
    }),
  ],
})
export class AppModule {}

Public API

TabCoordinator

The elected-leader service. One tab per origin becomes the leader; others are followers.

import { Inject, Injectable } from "@stackra/container";
import { TAB_COORDINATOR } from "@stackra/contracts";
import { TabCoordinator } from "@stackra/coordinator";

@Injectable()
class SyncService {
  constructor(@Inject(TAB_COORDINATOR) private coord: TabCoordinator) {}

  onModuleInit() {
    // Only the leader tab performs the expensive sync
    this.coord.onRoleChange((role) => {
      if (role === "leader") this.startAutoSync();
      else this.stopAutoSync();
    });
  }
}

Runtime API:

coord.isLeader(); // boolean
coord.getRole(); // 'leader' | 'follower'
coord.getTabId(); // this tab's UUID
coord.getLeaderId(); // leader's UUID or null
coord.getActiveTabs(); // ITabInfo[]
coord.getTabCount(); // number
coord.onRoleChange(cb); // subscribe
coord.resign(); // voluntary step-down
coord.destroy(); // cleanup

LockManager

Cross-tab mutual exclusion. Uses Web Locks API when available with a localStorage-CAS fallback:

import { InjectLockManager, LockManager } from "@stackra/coordinator";

@Injectable()
class AuthService {
  constructor(@InjectLockManager() private locks: LockManager) {}

  async refreshToken() {
    // Only one tab at a time refreshes the token
    return this.locks.run(
      "token-refresh",
      async () => {
        const token = await api.refresh();
        await cache.set("token", token);
        return token;
      },
      { timeoutMs: 10000 },
    );
  }
}

// Check if a lock is held (Web Locks only)
const busy = await locks.isLocked("token-refresh");

CoordinatorTransport

Automatically bridges the @stackra/events bus across tabs. Events matching broadcastPatterns fire on every open tab as if they were emitted locally. No wiring needed beyond broadcastEvents: true in the module config.

Cross-tab events

import { COORDINATOR_EVENTS } from "@stackra/contracts";

events.on(COORDINATOR_EVENTS.LEADER_ELECTED, ({ tabId }) => {});
events.on(COORDINATOR_EVENTS.LEADER_RESIGNED, ({ tabId }) => {});
events.on(COORDINATOR_EVENTS.TAB_JOINED, ({ tabId }) => {});
events.on(COORDINATOR_EVENTS.TAB_LEFT, ({ tabId }) => {});

React bindings — @stackra/coordinator/react

import { useIsLeader, useTabCount } from "@stackra/coordinator/react";

function SyncIndicator() {
  const isLeader = useIsLeader();
  return isLeader ? <Badge>Syncing on this tab</Badge> : null;
}

function TabInfo() {
  const count = useTabCount();
  return <span>{count} tab(s) open</span>;
}

Options

| Option | Default | Purpose | | ------------------------ | ------------------------------------ | ---------------------------------------------------------------- | | channelName | 'stackra-coordinator' | BroadcastChannel name (isolates apps on the same origin) | | heartbeatMs | 1000 | Leader liveness ping interval | | staleThresholdMs | 3000 | Time after which the leader is considered dead | | broadcastEvents | true | Enable cross-tab event relay | | broadcastPatterns | ['sync:**', 'auth:**', 'state:**'] | Wildcard patterns to relay | | preferWebLocks | true | Use Web Locks API for distributed locks (fallback: localStorage) | | preferWebLocksElection | true | Use Web Locks for leader election (fallback: heartbeat protocol) | | preferVisibleLeader | false | Prefer the visible/focused tab as leader |

Election protocol

  1. Web Locks path (default in modern browsers) — every tab requests the same named lock via navigator.locks.request(). The lock holder is the leader. Failover is instant when the leader closes.
  2. Heartbeat path (fallback) — the leader broadcasts a heartbeat every heartbeatMs. If no heartbeat within staleThresholdMs, followers race to claim leadership. Tie-breaking is by tabId ordering.

Configuration

cp node_modules/@stackra/coordinator/config/coordinator.config.ts src/config/coordinator.config.ts

Testing helper — @stackra/coordinator/testing

import {
  createMockCoordinator,
  createMockLockManager,
} from "@stackra/coordinator/testing";

// Coordinator starts as leader — flip roles to exercise both branches
const coordinator = createMockCoordinator();
expect(coordinator.isLeader()).toBe(true);
service.subscribeToRoleChanges(coordinator); // wires onRoleChange

coordinator.simulateRole("follower");
service.$.assertCalled("teardown").once();

coordinator.simulateRole("leader");
service.$.assertCalled("bootstrap").once();

// Lock manager — real promise-based serialisation, no real Web Locks
const locks = createMockLockManager();
const result = await locks.run("token-refresh", async () => "new-token");
expect(result).toBe("new-token");
locks.$.assertCalled("run").with("token-refresh").once();

Neither mock touches BroadcastChannel, navigator.locks, or localStorage — every operation runs in-process, so tests are deterministic and fast.

Subpaths

| Import | Purpose | | ------------------------------ | ---------------------------------------------------------------- | | @stackra/coordinator | CoordinatorModule, TabCoordinator, LockManager, decorators | | @stackra/coordinator/react | useCoordinator, useIsLeader, useLock | | @stackra/coordinator/testing | createMockCoordinator(), createMockLockManager() |

License

MIT