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 { v4 as uuidv4 } from 'uuid';
import Constants from '../constants/auth'; import Constants from '../constants/auth';
@ -11,8 +12,8 @@ class Auth {
throw new Error('Cannot construct singleton'); throw new Error('Cannot construct singleton');
} }
this.uuidField = uuidv4(); this._uuid = uuidv4();
this.onAuthStateChangedObserversField = []; this._onAuthStateChangedObservers = [];
} }
static get instance() { static get instance() {
@ -24,22 +25,22 @@ class Auth {
} }
get uuid() { get uuid() {
return this.uuidField; return this._uuid;
} }
get onAuthStateChangedObservers() { get onAuthStateChangedObservers() {
return this.onAuthStateChangedObserversField; return this._onAuthStateChangedObservers;
} }
dispose() { dispose() {
this.onAuthStateChangedObserversField = []; this._onAuthStateChangedObservers = [];
} }
onAuthStateChanged(observer) { onAuthStateChanged(observer) {
this.onAuthStateChangedObservers.push(observer); this.onAuthStateChangedObservers.push(observer);
return () => { return () => {
this.onAuthStateChangedObserversField = this.onAuthStateChangedObservers.filter( this._onAuthStateChangedObservers = this.onAuthStateChangedObservers.filter(
(obs) => obs !== observer, (obs) => obs !== observer,
); );
}; };

View File

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

View File

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

View File

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