Firebase Stub: refactoring

This commit is contained in:
gianantoniopini
2021-01-15 10:25:44 +01:00
parent aac1e12cfc
commit 5ba978dae5

View File

@ -27,7 +27,11 @@ class Reference {
this._uuid = uuidv4(); this._uuid = uuidv4();
this._dataSnapshot = null; if (this.path === DatabaseConstants.connectedPath) {
this._dataSnapshot = new DataSnapshot(() => {}, true);
} else {
this._dataSnapshot = new DataSnapshot(() => this._getData());
}
if (!getDatabaseData) { if (!getDatabaseData) {
throw new Error('getDatabaseData must be provided.'); throw new Error('getDatabaseData must be provided.');
@ -96,46 +100,51 @@ class Reference {
return databaseData; return databaseData;
} }
_setData(value) { _getParent() {
const pathElements = this.path.split('/');
let parent = null;
if (pathElements.length === 2) {
parent = this._getReference(pathElements[0]);
}
return parent;
}
_handleDataUpdate(value) {
if (typeof value === 'undefined') { if (typeof value === 'undefined') {
throw new Error('value must be provided.'); throw new Error('value must be provided.');
} }
const currentData = this._getData(); const currentData = this._getData();
const parentReference = this._getParent();
const pathElements = this.path.split('/');
let parentReference = null;
if (pathElements.length === 2) {
parentReference = this._getReference(pathElements[0]);
}
this._setDatabaseData(this.path, value); this._setDatabaseData(this.path, value);
if (value === null) { if (value === null) {
if (parentReference) { if (parentReference) {
parentReference.debounceEventCallback( parentReference.triggerEventCallback(
DatabaseConstants.childRemovedEventType, DatabaseConstants.childRemovedEventType,
currentData, currentData,
); );
parentReference.debounceEventCallback(DatabaseConstants.valueEventType);
} }
} else { } else {
const eventType = DatabaseConstants.valueEventType; this.triggerEventCallback(DatabaseConstants.valueEventType);
this.debounceEventCallback(eventType); }
if (parentReference) {
parentReference.debounceEventCallback(eventType); if (parentReference) {
} parentReference.triggerEventCallback(DatabaseConstants.valueEventType);
} }
} }
debounceEventCallback(eventType, snapshotValue = undefined) { triggerEventCallback(eventType, snapshotValue = undefined) {
if (!(eventType in this.eventCallbacks)) { if (!(eventType in this.eventCallbacks)) {
return; return;
} }
const snapshot = const snapshot =
this.path === DatabaseConstants.connectedPath this.path === DatabaseConstants.connectedPath
? new DataSnapshot(() => this._getData(), true) ? this._dataSnapshot
: new DataSnapshot(() => this._getData(), snapshotValue); : new DataSnapshot(() => this._getData(), snapshotValue);
const debouncedEventCallback = debounce( const debouncedEventCallback = debounce(
@ -163,7 +172,7 @@ class Reference {
this.eventCallbacks[eventType] = callback; this.eventCallbacks[eventType] = callback;
if (eventType === DatabaseConstants.valueEventType) { if (eventType === DatabaseConstants.valueEventType) {
this.debounceEventCallback(eventType); this.triggerEventCallback(eventType);
} }
} }
@ -174,14 +183,7 @@ class Reference {
throw new Error('eventType should be a string.'); throw new Error('eventType should be a string.');
} }
if (this._dataSnapshot) { return Promise.resolve(this._dataSnapshot);
return Promise.resolve(this._dataSnapshot);
}
const newDataSnapshot = new DataSnapshot(() => this._getData());
this._dataSnapshot = newDataSnapshot;
return Promise.resolve(newDataSnapshot);
} }
orderByChild(path) { orderByChild(path) {
@ -190,19 +192,19 @@ class Reference {
} }
async update(value) { async update(value) {
this._setData(value); this._handleDataUpdate(value);
return Promise.resolve(true); return Promise.resolve(true);
} }
async remove() { async remove() {
this._setData(null); this._handleDataUpdate(null);
return Promise.resolve(true); return Promise.resolve(true);
} }
async set(value) { async set(value) {
this._setData(value); this._handleDataUpdate(value);
return Promise.resolve(true); return Promise.resolve(true);
} }