Skip to content

Class: Singleton#

Common.Util.Singleton

Copyright (c) OpenLens Authors. All rights reserved. Licensed under MIT License. See LICENSE in root directory for more information.

Table of contents#

Constructors#

Methods#

Constructors#

constructor#

new Singleton()

Methods#

createInstance#

Static createInstance<T, R>(this, ...args): T

Creates the single instance of the child class if one was not already created.

Multiple calls will return the same instance. Essentially throwing away the arguments to the subsequent calls.

Note: this is a racy function, if two (or more) calls are racing to call this function only the first's arguments will be used.

Type parameters#

Name Type
T T
R extends any[]

Parameters#

Name Type Description
this StaticThis<T, R> Implicit argument that is the child class type
...args R The constructor arguments for the child class

Returns#

T

An instance of the child class


getInstance#

Static getInstance<T, R>(this, strict?): T

Get the instance of the child class that was previously created.

Type parameters#

Name Type
T T
R extends any[]

Parameters#

Name Type Default value Description
this StaticThis<T, R> undefined Implicit argument that is the child class type
strict boolean true If false will return undefined instead of throwing when an instance doesn't exist. Default: true

Returns#

T

An instance of the child class


resetInstance#

Static resetInstance(): void

Delete the instance of the child class.

Note: this doesn't prevent callers of getInstance from storing the result in a global.

There is no way in JS or TS to prevent globals like that.

Returns#

void