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

@travetto/registry

v5.0.13

Published

Patterns and utilities for handling registration of metadata and functionality for run-time use

Downloads

1,803

Readme

Registry

Patterns and utilities for handling registration of metadata and functionality for run-time use

Install: @travetto/registry

npm install @travetto/registry

# or

yarn add @travetto/registry

This module is the backbone for all "discovered" and "registered" behaviors within the framework. This is primarily used for building modules within the framework and not directly useful for application development.

Flows

Registration, within the framework flows throw two main use cases:

Initial Flows

The primary flow occurs on initialization of the application. At that point, the module will:

  1. Initialize RootRegistry and will automatically register/load all relevant files
  2. As files are imported, decorators within the files will record various metadata relevant to the respective registries
  3. When all files are processed, the RootRegistry is finished, and it will signal to anything waiting on registered data that its free to use it. This flow ensures all files are loaded and processed before application starts. A sample registry could like:

Code: Sample Registry

import { Class } from '@travetto/runtime';
import { MetadataRegistry } from '@travetto/registry';

interface Group {
  class: Class;
  name: string;
}

interface Child {
  name: string;
  method: Function;
}

function isComplete(o: Partial<Group>): o is Group {
  return !!o;
}

export class SampleRegistry extends MetadataRegistry<Group, Child> {
  /**
   * Finalize class after all metadata is collected
   */
  onInstallFinalize<T>(cls: Class<T>): Group {
    const pending: Partial<Group> = this.getOrCreatePending(cls);
    if (isComplete(pending)) {
      return pending;
    } else {
      throw new Error('Invalid Group');
    }
  }

  /**
   * Create scaffolding on first encounter of a class
   */
  createPending(cls: Class): Partial<Group> {
    return {
      class: cls,
      name: cls.name
    };
  }
}

The registry is a MetadataRegistry that similar to the Schema's Schema registry and Dependency Injection's Dependency registry.

Live Flow

At runtime, the registry is designed to listen for changes and to propagate the changes as necessary. In many cases the same file is handled by multiple registries.

As the DynamicFileLoader notifies that a file has been changed, the RootRegistry will pick it up, and process it accordingly.

Supporting Metadata

As mentioned in Manifest's readme, the framework produces hashes of methods, classes, and functions, to allow for detecting changes to individual parts of the codebase. During the live flow, various registries will inspect this information to determine if action should be taken.

Code: Sample Class Diffing


#handleFileChanges(importFile: string, classes: Class[] = []): number {
    const next = new Map<string, Class>(classes.map(cls => [cls.Ⲑid, cls] as const));

    let prev = new Map<string, Class>();
    if (this.#classes.has(importFile)) {
      prev = new Map(this.#classes.get(importFile)!.entries());
    }

    const keys = new Set([...Array.from(prev.keys()), ...Array.from(next.keys())]);

    if (!this.#classes.has(importFile)) {
      this.#classes.set(importFile, new Map());
    }

    let changes = 0;

    // Determine delta based on the various classes (if being added, removed or updated)
    for (const k of keys) {
      if (!next.has(k)) {
        changes += 1;
        this.emit({ type: 'removing', prev: prev.get(k)! });
        this.#classes.get(importFile)!.delete(k);
      } else {
        this.#classes.get(importFile)!.set(k, next.get(k)!);
        if (!prev.has(k)) {
          changes += 1;
          this.emit({ type: 'added', curr: next.get(k)! });
        } else {
          const prevHash = describeFunction(prev.get(k)!)?.hash;
          const nextHash = describeFunction(next.get(k)!)?.hash;
          if (prevHash !== nextHash) {
            changes += 1;
            this.emit({ type: 'changed', curr: next.get(k)!, prev: prev.get(k) });
          }
        }
      }
    }
    return changes;
  }