mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 00:32:35 +10:00
Firebase Stub: resolving ESLint errors
This commit is contained in:
@ -7,7 +7,7 @@ import {
|
|||||||
|
|
||||||
class FirebaseStub {
|
class FirebaseStub {
|
||||||
static auth() {
|
static auth() {
|
||||||
return new Auth();
|
return Auth.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static database() {
|
static database() {
|
||||||
|
|||||||
@ -1,48 +1,54 @@
|
|||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import { AuthConstants } from '../constants';
|
import Constants from '../constants/auth';
|
||||||
|
|
||||||
|
const singleton = Symbol('');
|
||||||
|
const singletonEnforcer = Symbol('');
|
||||||
|
|
||||||
class Auth {
|
class Auth {
|
||||||
static #instance = undefined;
|
constructor(enforcer) {
|
||||||
#uuid = '';
|
if (enforcer !== singletonEnforcer) {
|
||||||
#onAuthStateChangedObservers = [];
|
throw new Error('Cannot construct singleton');
|
||||||
|
|
||||||
constructor() {
|
|
||||||
if (Auth.#instance) {
|
|
||||||
return Auth.#instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Auth.#instance = this;
|
this.uuidField = uuidv4();
|
||||||
|
this.onAuthStateChangedObserversField = [];
|
||||||
|
}
|
||||||
|
|
||||||
this.#uuid = uuidv4();
|
static get instance() {
|
||||||
|
if (!this[singleton]) {
|
||||||
|
this[singleton] = new Auth(singletonEnforcer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this[singleton];
|
||||||
}
|
}
|
||||||
|
|
||||||
get uuid() {
|
get uuid() {
|
||||||
return this.#uuid;
|
return this.uuidField;
|
||||||
}
|
}
|
||||||
|
|
||||||
get onAuthStateChangedObservers() {
|
get onAuthStateChangedObservers() {
|
||||||
return this.#onAuthStateChangedObservers;
|
return this.onAuthStateChangedObserversField;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
this.#onAuthStateChangedObservers = [];
|
this.onAuthStateChangedObserversField = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
onAuthStateChanged(observer) {
|
onAuthStateChanged(observer) {
|
||||||
this.#onAuthStateChangedObservers.push(observer);
|
this.onAuthStateChangedObservers.push(observer);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter(
|
this.onAuthStateChangedObserversField = this.onAuthStateChangedObservers.filter(
|
||||||
(observer) => observer !== observer,
|
(obs) => obs !== observer,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async signInAnonymously() {
|
async signInAnonymously() {
|
||||||
const user = AuthConstants.anonymousUser1;
|
const user = Constants.anonymousUser1;
|
||||||
|
|
||||||
this.#onAuthStateChangedObservers.forEach((observer) => observer(user));
|
this.onAuthStateChangedObservers.forEach((observer) => observer(user));
|
||||||
|
|
||||||
return Promise.resolve(user);
|
return Promise.resolve(user);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,6 @@
|
|||||||
import Reference from './reference';
|
import Reference from './reference';
|
||||||
|
|
||||||
class DataSnapshot {
|
class DataSnapshot {
|
||||||
#eventType = '';
|
|
||||||
#reference = null;
|
|
||||||
#value = undefined;
|
|
||||||
|
|
||||||
constructor(eventType, reference, value = undefined) {
|
constructor(eventType, reference, value = undefined) {
|
||||||
if (!eventType) {
|
if (!eventType) {
|
||||||
throw new Error('eventType must be provided.');
|
throw new Error('eventType must be provided.');
|
||||||
@ -12,7 +8,7 @@ class DataSnapshot {
|
|||||||
throw new Error('eventType should be a string.');
|
throw new Error('eventType should be a string.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#eventType = eventType;
|
this.eventTypeField = eventType;
|
||||||
|
|
||||||
if (!reference) {
|
if (!reference) {
|
||||||
throw new Error('reference must be provided.');
|
throw new Error('reference must be provided.');
|
||||||
@ -20,24 +16,24 @@ class DataSnapshot {
|
|||||||
throw new Error('reference must be an instance of the Reference class.');
|
throw new Error('reference must be an instance of the Reference class.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#reference = reference;
|
this.referenceField = reference;
|
||||||
|
|
||||||
this.#value = value;
|
this.valueField = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get eventType() {
|
get eventType() {
|
||||||
return this.#eventType;
|
return this.eventTypeField;
|
||||||
}
|
}
|
||||||
|
|
||||||
get value() {
|
get value() {
|
||||||
return this.#value;
|
return this.valueField;
|
||||||
}
|
}
|
||||||
|
|
||||||
val() {
|
val() {
|
||||||
if (this.eventType === 'value') {
|
if (this.eventType === 'value') {
|
||||||
return typeof this.value !== 'undefined'
|
return typeof this.value !== 'undefined'
|
||||||
? this.value
|
? this.value
|
||||||
: this.#reference.getData();
|
: this.referenceField.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
@ -3,64 +3,64 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
import { DatabaseConstants } from '../constants';
|
import { DatabaseConstants } from '../constants';
|
||||||
import DataSnapshot from './dataSnapshot';
|
import DataSnapshot from './dataSnapshot';
|
||||||
|
|
||||||
class Reference {
|
const rootPath = '.';
|
||||||
#rootPath = '.';
|
|
||||||
#path = '';
|
|
||||||
#uuid = '';
|
|
||||||
#dataSnapshots = {};
|
|
||||||
#getDatabaseData = () => null;
|
|
||||||
#orderByChildPath = '';
|
|
||||||
#equalToValue = '';
|
|
||||||
|
|
||||||
|
class Reference {
|
||||||
constructor(path, getDatabaseData) {
|
constructor(path, getDatabaseData) {
|
||||||
if (typeof path === 'undefined' || path === null) {
|
if (typeof path === 'undefined' || path === null) {
|
||||||
this.#path = this.#rootPath;
|
this.pathField = rootPath;
|
||||||
} else if (typeof path !== 'string') {
|
} else if (typeof path !== 'string') {
|
||||||
throw new Error('path should be a string.');
|
throw new Error('path should be a string.');
|
||||||
|
} else {
|
||||||
|
this.pathField = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.uuidField = uuidv4();
|
||||||
|
this.dataSnapshotsField = {};
|
||||||
|
|
||||||
if (!getDatabaseData) {
|
if (!getDatabaseData) {
|
||||||
throw new Error('getDatabaseData must be provided.');
|
throw new Error('getDatabaseData must be provided.');
|
||||||
} else if (typeof getDatabaseData !== 'function') {
|
} else if (typeof getDatabaseData !== 'function') {
|
||||||
throw new Error('getDatabaseData should be a function.');
|
throw new Error('getDatabaseData should be a function.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#path = path;
|
this.getDatabaseDataField = getDatabaseData;
|
||||||
this.#uuid = uuidv4();
|
|
||||||
this.#getDatabaseData = getDatabaseData;
|
this.orderByChildPathField = '';
|
||||||
|
this.equalToValueField = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
get path() {
|
get path() {
|
||||||
return this.#path;
|
return this.pathField;
|
||||||
}
|
}
|
||||||
|
|
||||||
get uuid() {
|
get uuid() {
|
||||||
return this.#uuid;
|
return this.uuidField;
|
||||||
}
|
}
|
||||||
|
|
||||||
getData() {
|
getData() {
|
||||||
const databaseData = this.#getDatabaseData();
|
const databaseData = this.getDatabaseDataField();
|
||||||
|
|
||||||
if (!databaseData) {
|
if (!databaseData) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.#path === this.#rootPath) {
|
if (this.path === rootPath) {
|
||||||
return databaseData;
|
return databaseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = null;
|
let data = null;
|
||||||
if (
|
if (
|
||||||
this.#path === DatabaseConstants.resumesPath ||
|
this.path === DatabaseConstants.resumesPath ||
|
||||||
this.#path === DatabaseConstants.usersPath
|
this.path === DatabaseConstants.usersPath
|
||||||
) {
|
) {
|
||||||
data = this.#path in databaseData ? databaseData[this.#path] : null;
|
data = this.path in databaseData ? databaseData[this.path] : null;
|
||||||
|
|
||||||
if (data && this.#orderByChildPath && this.#equalToValue) {
|
if (data && this.orderByChildPathField && this.equalToValueField) {
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
Object.entries(data).filter(
|
Object.entries(data).filter(
|
||||||
([key, value]) =>
|
([, value]) =>
|
||||||
value[this.#orderByChildPath] === this.#equalToValue,
|
value[this.orderByChildPathField] === this.equalToValueField,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -69,17 +69,17 @@ class Reference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.#path.startsWith(`${DatabaseConstants.resumesPath}/`) ||
|
this.path.startsWith(`${DatabaseConstants.resumesPath}/`) ||
|
||||||
this.#path.startsWith(`${DatabaseConstants.usersPath}/`)
|
this.path.startsWith(`${DatabaseConstants.usersPath}/`)
|
||||||
) {
|
) {
|
||||||
const databaseLocationId = this.#path.substring(
|
const databaseLocationId = this.path.substring(
|
||||||
this.#path.indexOf('/') + 1,
|
this.path.indexOf('/') + 1,
|
||||||
);
|
);
|
||||||
if (!databaseLocationId) {
|
if (!databaseLocationId) {
|
||||||
throw new Error('Unknown database location id.');
|
throw new Error('Unknown database location id.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathWithoutId = this.#path.substring(0, this.#path.indexOf('/'));
|
const pathWithoutId = this.path.substring(0, this.path.indexOf('/'));
|
||||||
if (pathWithoutId in databaseData) {
|
if (pathWithoutId in databaseData) {
|
||||||
return databaseLocationId in databaseData[pathWithoutId]
|
return databaseLocationId in databaseData[pathWithoutId]
|
||||||
? databaseData[pathWithoutId][databaseLocationId]
|
? databaseData[pathWithoutId][databaseLocationId]
|
||||||
@ -90,7 +90,9 @@ class Reference {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
off() {}
|
off() {
|
||||||
|
return this !== null;
|
||||||
|
}
|
||||||
|
|
||||||
on(eventType, callback) {
|
on(eventType, callback) {
|
||||||
if (eventType !== 'value') {
|
if (eventType !== 'value') {
|
||||||
@ -99,9 +101,9 @@ class Reference {
|
|||||||
|
|
||||||
let snapshot = new DataSnapshot(eventType, this, null);
|
let snapshot = new DataSnapshot(eventType, this, null);
|
||||||
|
|
||||||
if (this.#path === DatabaseConstants.connectedPath) {
|
if (this.path === DatabaseConstants.connectedPath) {
|
||||||
snapshot = new DataSnapshot(eventType, this, true);
|
snapshot = new DataSnapshot(eventType, this, true);
|
||||||
} else if (this.#path === DatabaseConstants.resumesPath) {
|
} else if (this.path === DatabaseConstants.resumesPath) {
|
||||||
snapshot = new DataSnapshot(eventType, this);
|
snapshot = new DataSnapshot(eventType, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,31 +112,43 @@ class Reference {
|
|||||||
|
|
||||||
async once(eventType) {
|
async once(eventType) {
|
||||||
const newDataSnapshot = new DataSnapshot(eventType, this);
|
const newDataSnapshot = new DataSnapshot(eventType, this);
|
||||||
const existingDataSnapshot = this.#dataSnapshots[newDataSnapshot.eventType];
|
const existingDataSnapshot = this.dataSnapshotsField[
|
||||||
|
newDataSnapshot.eventType
|
||||||
|
];
|
||||||
if (existingDataSnapshot) {
|
if (existingDataSnapshot) {
|
||||||
return Promise.resolve(existingDataSnapshot);
|
return Promise.resolve(existingDataSnapshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#dataSnapshots[newDataSnapshot.eventType] = newDataSnapshot;
|
this.dataSnapshotsField[newDataSnapshot.eventType] = newDataSnapshot;
|
||||||
return Promise.resolve(newDataSnapshot);
|
return Promise.resolve(newDataSnapshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
orderByChild(path) {
|
orderByChild(path) {
|
||||||
this.#orderByChildPath = path;
|
this.orderByChildPathField = path;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
equalTo(value) {
|
equalTo(value) {
|
||||||
this.#equalToValue = value;
|
this.equalToValueField = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(value) {
|
async update(value) {
|
||||||
return Promise.resolve(true);
|
if (typeof value === 'undefined') {
|
||||||
|
throw new Error('value must be provided.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = this !== null;
|
||||||
|
return Promise.resolve(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(value) {
|
async set(value) {
|
||||||
return Promise.resolve(true);
|
if (typeof value === 'undefined') {
|
||||||
|
throw new Error('value must be provided.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = this !== null;
|
||||||
|
return Promise.resolve(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user