API Reference

keyed-instance-factory

API entries in this module: 7

keyed-instance-factory overview

KeyedInstanceFactory is an abstract class that lets you manage one singleton instance per user-defined id. It keeps a reference count for each id: create(...) increments the count by one, and release(...) decrements it by one. When the reference count reaches zero, the instance is released.

Example: extend KeyedInstanceFactory

import { KeyedInstanceFactory } from '@rs-x/core';

type UserData = { id: string; name: string };

class MyKeyedInstanceFactory extends KeyedInstanceFactory<string, UserData, UserData, UserData> {
  public getId(data: UserData): string | undefined {
    return data.id;
  }

  protected createId(data: UserData): string {
    return data.id;
  }

  protected createInstance(data: UserData): UserData {
    return { ...data };
  }
}

const factory = new MyKeyedInstanceFactory();

// create(...) increases reference count
const first = factory.create({ id: 'u1', name: 'Ada' });

Module API entries