Skip to main content

Singleton

The Singleton class is designed to manage singleton instances, where each instance is uniquely identified by a key. It provides mechanisms for creating and retrieving singleton instances and ensures that each key can only have one corresponding instance.

create(key: string, createFn: CreateFunction): any

This method is used to create a new singleton instance or retrieve an existing one associated with a unique key.

Parameters:

  • key: The unique identifier for the singleton instance.
  • createFn: A function that creates and returns a new instance.

Returns:

  • The singleton instance corresponding to the provided key.

Example:

Singleton.shared.create('player', () => new Player());

static get shared(): Singleton

Ensures that only one Singleton instance is used globally, following the singleton pattern.

Example:

const player = Singleton.shared['player'];
console.log(player);