Skip to content

Commit

Permalink
Singleton protection done
Browse files Browse the repository at this point in the history
  • Loading branch information
RNEvok committed Jul 19, 2024
1 parent 600b520 commit 4a01472
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
9 changes: 3 additions & 6 deletions Connector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as T from './types';
import { CONFIG } from './config';
import { Singleton } from './helpers/classes';
import { EventHandleError, ScenarioHandleError } from './Errors';
import { SOCKET_EVENTS_LISTEN, SOCKET_EVENTS_EMIT } from './api/api';
import { SPECIAL_INSTRUCTIONS_TABLE, SPECIAL_INSTRUCTIONS } from './constants/events';
Expand All @@ -15,8 +16,7 @@ import { asyncStoragePlugin } from './asyncStorage/asyncStorage';
import { localStoragePlugin } from './localStorage/localStorage';
import moment from 'moment';

class Connector {
private static _hasInstance = false;
class Connector extends Singleton {
private _eventListenersTable: T.EventListenersTable = {};
private _connectorInitiated: boolean = false;
private _apiKey: string = "";
Expand Down Expand Up @@ -70,10 +70,7 @@ class Connector {
public lastEvent: T.RemoteEvent | null = null;

constructor() {
if (Connector._hasInstance)
throw new Error("Attempted to create second instance of a Singleton class!");

Connector._hasInstance = true;
super("Connector");
};

public addEventListener(key: string, handler: (event: T.RemoteEvent) => any) {
Expand Down
15 changes: 15 additions & 0 deletions helpers/classes/Singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ObjectT } from "../../types";

export abstract class Singleton {
private static _classHasInstanceTable: ObjectT<boolean> = {};

constructor(classId: string) {
if (classId.length < 1)
throw new Error("Non-empty classId required for Singleton class protection!");

if (Singleton._classHasInstanceTable[classId])
throw new Error("Attempted to create second instance of a Singleton class!");

Singleton._classHasInstanceTable[classId] = true;
}
};
1 change: 1 addition & 0 deletions helpers/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Singleton } from "./Singleton";
10 changes: 4 additions & 6 deletions services/idService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class IdService {
private static _hasInstance = false;
import { Singleton } from "./../helpers/classes";

class IdService extends Singleton {
private _currentInterceptedReduxActionId = 0;
private _currentInterceptedStorageActionId = 0;
private _currentCapturedEventId = 0;
Expand All @@ -8,10 +9,7 @@ class IdService {
private _currentInterceptedMobxEventId = 0;

constructor() {
if (IdService._hasInstance)
throw new Error("Attempted to create second instance of a Singleton class!");

IdService._hasInstance = true;
super("IdService");
}

public get currentInterceptedReduxActionId() {
Expand Down
9 changes: 3 additions & 6 deletions services/remoteSettingsService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Singleton } from "./../helpers/classes";
import { validateHexId24Symbols } from "../constants/regex";
import { codebudConsoleWarn } from "../helpers/helperFunctions";
import { PersonalProjectsSetting, RefreshPersonalProjectsSettingCallback, RefreshRemoteSettingsCallback, RemoteSettings, RemoteSettingsListenersTable } from "../types/types";
import { api } from './../api/api';
import { classicApiResponseValidator } from './../helpers/apiResponseValidators';

class RemoteSettingsService {
private static _hasInstance = false;
class RemoteSettingsService extends Singleton {
private _projectId: string = "";
private _apiKey: string = "";
private _isInit: boolean = false;
Expand All @@ -23,10 +23,7 @@ class RemoteSettingsService {
};

constructor() {
if (RemoteSettingsService._hasInstance)
throw new Error("Attempted to create second instance of a Singleton class!");

RemoteSettingsService._hasInstance = true;
super("RemoteSettingsService");
}

public onGotNewRemoteSettings(r: RemoteSettings) {
Expand Down

0 comments on commit 4a01472

Please # to comment.