Skip to content

Commit

Permalink
refact(Singleton): rename the singleton methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelscr committed Jul 30, 2024
1 parent 7cbe250 commit 17c0ab0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ Additionally, the Incognia API instance can be stored in a global instance as fo
sure that a single Incognia API client is created and used.

```js
IncogniaApi.setSingletonInstance(
IncogniaApi.setInstance(
new IncogniaApi({ clientId: 'clientId', clientSecret: 'clientSecret' })
)

// Retrieves the stored instance
const incogniaApi = IncogniaApi.getSingletonInstance()
const incogniaApi = IncogniaApi.instance()
```

## API methods
Expand Down
18 changes: 9 additions & 9 deletions src/incogniaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ export class IncogniaApi {
** Singleton support
*/

private static instance?: IncogniaApi
private static apiInstance?: IncogniaApi

public static setSingletonInstance(instance: IncogniaApi): void {
if (!IncogniaApi.instance) {
public static setInstance(instance: IncogniaApi): void {
if (!IncogniaApi.apiInstance) {
if (!instance) {
throw new IncogniaError(
'No instance provided to set as singleton instance'
)
}
IncogniaApi.instance = instance
IncogniaApi.apiInstance = instance
}
}

public static getSingletonInstance(): IncogniaApi {
if (!IncogniaApi.instance) {
public static instance(): IncogniaApi {
if (!IncogniaApi.apiInstance) {
throw new IncogniaError('No singleton instance set')
}
return IncogniaApi.instance
return IncogniaApi.apiInstance
}

public static resetSingletonInstance(): void {
IncogniaApi.instance = undefined
public static resetInstance(): void {
IncogniaApi.apiInstance = undefined
}

/*
Expand Down
12 changes: 6 additions & 6 deletions test/incogniaApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,28 +404,28 @@ describe('IncogniaApi', () => {
})

describe('Singleton support', () => {
beforeEach(() => IncogniaApi.resetSingletonInstance())
beforeEach(() => IncogniaApi.resetInstance())

it('sets and gets the singleton instance', () => {
IncogniaApi.setSingletonInstance(incogniaApi)
IncogniaApi.setInstance(incogniaApi)

const newClient = new IncogniaApi(credentials)
IncogniaApi.setSingletonInstance(newClient)
IncogniaApi.setInstance(newClient)

expect(IncogniaApi.getSingletonInstance()).toEqual(incogniaApi)
expect(IncogniaApi.instance()).toEqual(incogniaApi)
})

it('throws an error when no singleton instance is set', () => {
const getSingletonInstance = () => {
IncogniaApi.getSingletonInstance()
IncogniaApi.instance()
}
expect(getSingletonInstance).toThrow('No singleton instance set')
expect(getSingletonInstance).toThrowError(IncogniaError)
})

it('throws an error when no instance is provided', () => {
const setSingletonInstance = () => {
IncogniaApi.setSingletonInstance(null as any)
IncogniaApi.setInstance(null as any)
}
expect(setSingletonInstance).toThrow(
'No instance provided to set as singleton instance'
Expand Down

0 comments on commit 17c0ab0

Please sign in to comment.