Firebase Stub refactoring: private class field naming convention

This commit is contained in:
gianantoniopini
2021-01-11 17:18:37 +01:00
parent 253f778a63
commit b6a0527fbe
4 changed files with 51 additions and 52 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import { v4 as uuidv4 } from 'uuid';
import Constants from '../constants/auth';
@ -11,8 +12,8 @@ class Auth {
throw new Error('Cannot construct singleton');
}
this.uuidField = uuidv4();
this.onAuthStateChangedObserversField = [];
this._uuid = uuidv4();
this._onAuthStateChangedObservers = [];
}
static get instance() {
@ -24,22 +25,22 @@ class Auth {
}
get uuid() {
return this.uuidField;
return this._uuid;
}
get onAuthStateChangedObservers() {
return this.onAuthStateChangedObserversField;
return this._onAuthStateChangedObservers;
}
dispose() {
this.onAuthStateChangedObserversField = [];
this._onAuthStateChangedObservers = [];
}
onAuthStateChanged(observer) {
this.onAuthStateChangedObservers.push(observer);
return () => {
this.onAuthStateChangedObserversField = this.onAuthStateChangedObservers.filter(
this._onAuthStateChangedObservers = this.onAuthStateChangedObservers.filter(
(obs) => obs !== observer,
);
};

View File

@ -1,37 +1,36 @@
/* eslint-disable no-underscore-dangle */
class DataSnapshot {
constructor(eventType, getDataCallback, value = undefined) {
constructor(eventType, getData, value = undefined) {
if (!eventType) {
throw new Error('eventType must be provided.');
} else if (typeof eventType !== 'string') {
throw new Error('eventType should be a string.');
}
this.eventTypeField = eventType;
this._eventType = eventType;
if (!getDataCallback) {
throw new Error('getDataCallback must be provided.');
} else if (typeof getDataCallback !== 'function') {
throw new Error('getDataCallback should be a function.');
if (!getData) {
throw new Error('getData must be provided.');
} else if (typeof getData !== 'function') {
throw new Error('getData should be a function.');
}
this.getDataCallbackField = getDataCallback;
this._getData = getData;
this.valueField = value;
this._value = value;
}
get eventType() {
return this.eventTypeField;
return this._eventType;
}
get value() {
return this.valueField;
return this._value;
}
val() {
if (this.eventType === 'value') {
return typeof this.value !== 'undefined'
? this.value
: this.getDataCallbackField();
return typeof this.value !== 'undefined' ? this.value : this._getData();
}
return undefined;

View File

@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import path from 'path';
import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';
@ -21,9 +22,9 @@ class Database {
throw new Error('Cannot construct singleton');
}
this.uuidField = uuidv4();
this.dataField = {};
this.referencesField = {};
this._uuid = uuidv4();
this._data = {};
this._references = {};
}
static get instance() {
@ -35,7 +36,7 @@ class Database {
}
get uuid() {
return this.uuidField;
return this._uuid;
}
initializeData() {
@ -65,22 +66,22 @@ class Database {
resume.name = `Test Resume ${key}`;
});
this.dataField[DatabaseConstants.resumesPath] = resumes;
this._data[DatabaseConstants.resumesPath] = resumes;
const users = {};
users[DatabaseConstants.user1.uid] = DatabaseConstants.user1;
users[DatabaseConstants.user2.uid] = DatabaseConstants.user2;
this.dataField[DatabaseConstants.usersPath] = users;
this._data[DatabaseConstants.usersPath] = users;
}
ref(referencePath) {
const newRef = new Reference(referencePath, () => this.dataField);
const existingRef = this.referencesField[newRef.path];
const newRef = new Reference(referencePath, () => this._data);
const existingRef = this._references[newRef.path];
if (existingRef) {
return existingRef;
}
this.referencesField[newRef.path] = newRef;
this._references[newRef.path] = newRef;
return newRef;
}
}

View File

@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import { v4 as uuidv4 } from 'uuid';
import { debounce } from 'lodash';
@ -7,41 +8,41 @@ import DataSnapshot from './dataSnapshot';
const rootPath = '.';
class Reference {
constructor(path, getDatabaseDataCallback) {
constructor(path, getDatabaseData) {
if (typeof path === 'undefined' || path === null) {
this.pathField = rootPath;
this._path = rootPath;
} else if (typeof path !== 'string') {
throw new Error('path should be a string.');
} else {
this.pathField = path;
this._path = path;
}
this.uuidField = uuidv4();
this._uuid = uuidv4();
this.dataSnapshotsField = {};
this._dataSnapshots = {};
if (!getDatabaseDataCallback) {
throw new Error('getDatabaseDataCallback must be provided.');
} else if (typeof getDatabaseDataCallback !== 'function') {
throw new Error('getDatabaseDataCallback should be a function.');
if (!getDatabaseData) {
throw new Error('getDatabaseData must be provided.');
} else if (typeof getDatabaseData !== 'function') {
throw new Error('getDatabaseData should be a function.');
}
this.getDatabaseDataCallbackField = getDatabaseDataCallback;
this._getDatabaseData = getDatabaseData;
this.orderByChildPathField = '';
this.equalToValueField = '';
this._orderByChildPath = '';
this._equalToValue = '';
}
get path() {
return this.pathField;
return this._path;
}
get uuid() {
return this.uuidField;
return this._uuid;
}
getData() {
const databaseData = this.getDatabaseDataCallbackField();
const databaseData = this._getDatabaseData();
if (!databaseData) {
return null;
@ -58,11 +59,10 @@ class Reference {
) {
data = this.path in databaseData ? databaseData[this.path] : null;
if (data && this.orderByChildPathField && this.equalToValueField) {
if (data && this._orderByChildPath && this._equalToValue) {
return Object.fromEntries(
Object.entries(data).filter(
([, value]) =>
value[this.orderByChildPathField] === this.equalToValueField,
([, value]) => value[this._orderByChildPath] === this._equalToValue,
),
);
}
@ -115,24 +115,22 @@ class Reference {
async once(eventType) {
const newDataSnapshot = new DataSnapshot(eventType, () => this.getData());
const existingDataSnapshot = this.dataSnapshotsField[
newDataSnapshot.eventType
];
const existingDataSnapshot = this._dataSnapshots[newDataSnapshot.eventType];
if (existingDataSnapshot) {
return Promise.resolve(existingDataSnapshot);
}
this.dataSnapshotsField[newDataSnapshot.eventType] = newDataSnapshot;
this._dataSnapshots[newDataSnapshot.eventType] = newDataSnapshot;
return Promise.resolve(newDataSnapshot);
}
orderByChild(path) {
this.orderByChildPathField = path;
this._orderByChildPath = path;
return this;
}
equalTo(value) {
this.equalToValueField = value;
this._equalToValue = value;
return this;
}