Skip to content

# Class: Singleton

Common.Util.Singleton

Copyright (c) 2021 OpenLens Authors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## Table of contents

### Constructors

- constructor

### Methods

- createInstance
- getInstance
- resetInstance

## Constructors

### constructor

• new Singleton()

## Methods

### createInstance

▸ `Static` createInstance<T, R>(...`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` | `R`: `any`[]

#### Parameters

Name | Type | Description
--- | --- | ---
`...args` | `R` | The constructor arguments for the child class

#### Returns

`T`

An instance of the child class

### getInstance

▸ `Static` getInstance<T, R>(`strict`): `T`

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

#### Type parameters

Name | Type
--- | ---
`T` | `T`
`R` | `R`: `any`[]

#### Parameters

Name | Type | Description
--- | --- | ---
`strict` | `boolean` | 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`