Firebase Stub: resolving ESLint errors

This commit is contained in:
gianantoniopini
2021-01-08 12:11:02 +01:00
parent e78d3dbaa4
commit 9c5b8398a3
4 changed files with 81 additions and 65 deletions

View File

@ -7,7 +7,7 @@ import {
class FirebaseStub {
static auth() {
return new Auth();
return Auth.instance;
}
static database() {

View File

@ -1,48 +1,54 @@
import { v4 as uuidv4 } from 'uuid';
import { AuthConstants } from '../constants';
import Constants from '../constants/auth';
const singleton = Symbol('');
const singletonEnforcer = Symbol('');
class Auth {
static #instance = undefined;
#uuid = '';
#onAuthStateChangedObservers = [];
constructor() {
if (Auth.#instance) {
return Auth.#instance;
constructor(enforcer) {
if (enforcer !== singletonEnforcer) {
throw new Error('Cannot construct singleton');
}
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() {
return this.#uuid;
return this.uuidField;
}
get onAuthStateChangedObservers() {
return this.#onAuthStateChangedObservers;
return this.onAuthStateChangedObserversField;
}
dispose() {
this.#onAuthStateChangedObservers = [];
this.onAuthStateChangedObserversField = [];
}
onAuthStateChanged(observer) {
this.#onAuthStateChangedObservers.push(observer);
this.onAuthStateChangedObservers.push(observer);
return () => {
this.#onAuthStateChangedObservers = this.#onAuthStateChangedObservers.filter(
(observer) => observer !== observer,
this.onAuthStateChangedObserversField = this.onAuthStateChangedObservers.filter(
(obs) => obs !== observer,
);
};
}
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);
}

View File

@ -1,10 +1,6 @@
import Reference from './reference';
class DataSnapshot {
#eventType = '';
#reference = null;
#value = undefined;
constructor(eventType, reference, value = undefined) {
if (!eventType) {
throw new Error('eventType must be provided.');
@ -12,7 +8,7 @@ class DataSnapshot {
throw new Error('eventType should be a string.');
}
this.#eventType = eventType;
this.eventTypeField = eventType;
if (!reference) {
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.');
}
this.#reference = reference;
this.referenceField = reference;
this.#value = value;
this.valueField = value;
}
get eventType() {
return this.#eventType;
return this.eventTypeField;
}
get value() {
return this.#value;
return this.valueField;
}
val() {
if (this.eventType === 'value') {
return typeof this.value !== 'undefined'
? this.value
: this.#reference.getData();
: this.referenceField.getData();
}
return undefined;

View File

@ -3,64 +3,64 @@ import { v4 as uuidv4 } from 'uuid';
import { DatabaseConstants } from '../constants';
import DataSnapshot from './dataSnapshot';
class Reference {
#rootPath = '.';
#path = '';
#uuid = '';
#dataSnapshots = {};
#getDatabaseData = () => null;
#orderByChildPath = '';
#equalToValue = '';
const rootPath = '.';
class Reference {
constructor(path, getDatabaseData) {
if (typeof path === 'undefined' || path === null) {
this.#path = this.#rootPath;
this.pathField = rootPath;
} else if (typeof path !== 'string') {
throw new Error('path should be a string.');
} else {
this.pathField = path;
}
this.uuidField = uuidv4();
this.dataSnapshotsField = {};
if (!getDatabaseData) {
throw new Error('getDatabaseData must be provided.');
} else if (typeof getDatabaseData !== 'function') {
throw new Error('getDatabaseData should be a function.');
}
this.#path = path;
this.#uuid = uuidv4();
this.#getDatabaseData = getDatabaseData;
this.getDatabaseDataField = getDatabaseData;
this.orderByChildPathField = '';
this.equalToValueField = '';
}
get path() {
return this.#path;
return this.pathField;
}
get uuid() {
return this.#uuid;
return this.uuidField;
}
getData() {
const databaseData = this.#getDatabaseData();
const databaseData = this.getDatabaseDataField();
if (!databaseData) {
return null;
}
if (this.#path === this.#rootPath) {
if (this.path === rootPath) {
return databaseData;
}
let data = null;
if (
this.#path === DatabaseConstants.resumesPath ||
this.#path === DatabaseConstants.usersPath
this.path === DatabaseConstants.resumesPath ||
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(
Object.entries(data).filter(
([key, value]) =>
value[this.#orderByChildPath] === this.#equalToValue,
([, value]) =>
value[this.orderByChildPathField] === this.equalToValueField,
),
);
}
@ -69,17 +69,17 @@ class Reference {
}
if (
this.#path.startsWith(`${DatabaseConstants.resumesPath}/`) ||
this.#path.startsWith(`${DatabaseConstants.usersPath}/`)
this.path.startsWith(`${DatabaseConstants.resumesPath}/`) ||
this.path.startsWith(`${DatabaseConstants.usersPath}/`)
) {
const databaseLocationId = this.#path.substring(
this.#path.indexOf('/') + 1,
const databaseLocationId = this.path.substring(
this.path.indexOf('/') + 1,
);
if (!databaseLocationId) {
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) {
return databaseLocationId in databaseData[pathWithoutId]
? databaseData[pathWithoutId][databaseLocationId]
@ -90,7 +90,9 @@ class Reference {
return null;
}
off() {}
off() {
return this !== null;
}
on(eventType, callback) {
if (eventType !== 'value') {
@ -99,9 +101,9 @@ class Reference {
let snapshot = new DataSnapshot(eventType, this, null);
if (this.#path === DatabaseConstants.connectedPath) {
if (this.path === DatabaseConstants.connectedPath) {
snapshot = new DataSnapshot(eventType, this, true);
} else if (this.#path === DatabaseConstants.resumesPath) {
} else if (this.path === DatabaseConstants.resumesPath) {
snapshot = new DataSnapshot(eventType, this);
}
@ -110,31 +112,43 @@ class Reference {
async once(eventType) {
const newDataSnapshot = new DataSnapshot(eventType, this);
const existingDataSnapshot = this.#dataSnapshots[newDataSnapshot.eventType];
const existingDataSnapshot = this.dataSnapshotsField[
newDataSnapshot.eventType
];
if (existingDataSnapshot) {
return Promise.resolve(existingDataSnapshot);
}
this.#dataSnapshots[newDataSnapshot.eventType] = newDataSnapshot;
this.dataSnapshotsField[newDataSnapshot.eventType] = newDataSnapshot;
return Promise.resolve(newDataSnapshot);
}
orderByChild(path) {
this.#orderByChildPath = path;
this.orderByChildPathField = path;
return this;
}
equalTo(value) {
this.#equalToValue = value;
this.equalToValueField = value;
return this;
}
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) {
return Promise.resolve(true);
if (typeof value === 'undefined') {
throw new Error('value must be provided.');
}
const result = this !== null;
return Promise.resolve(result);
}
}