From 5ba978dae5987a8e3b6305819230460ba2330d7d Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Fri, 15 Jan 2021 10:25:44 +0100 Subject: [PATCH] Firebase Stub: refactoring --- .../database/reference.js | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/__mocks__/gatsby-plugin-firebase/database/reference.js b/__mocks__/gatsby-plugin-firebase/database/reference.js index 18e5d75c..3b3447d6 100644 --- a/__mocks__/gatsby-plugin-firebase/database/reference.js +++ b/__mocks__/gatsby-plugin-firebase/database/reference.js @@ -27,7 +27,11 @@ class Reference { 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) { throw new Error('getDatabaseData must be provided.'); @@ -96,46 +100,51 @@ class Reference { 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') { throw new Error('value must be provided.'); } const currentData = this._getData(); - - const pathElements = this.path.split('/'); - let parentReference = null; - if (pathElements.length === 2) { - parentReference = this._getReference(pathElements[0]); - } + const parentReference = this._getParent(); this._setDatabaseData(this.path, value); if (value === null) { if (parentReference) { - parentReference.debounceEventCallback( + parentReference.triggerEventCallback( DatabaseConstants.childRemovedEventType, currentData, ); - parentReference.debounceEventCallback(DatabaseConstants.valueEventType); } } else { - const eventType = DatabaseConstants.valueEventType; - this.debounceEventCallback(eventType); - if (parentReference) { - parentReference.debounceEventCallback(eventType); - } + this.triggerEventCallback(DatabaseConstants.valueEventType); + } + + if (parentReference) { + parentReference.triggerEventCallback(DatabaseConstants.valueEventType); } } - debounceEventCallback(eventType, snapshotValue = undefined) { + triggerEventCallback(eventType, snapshotValue = undefined) { if (!(eventType in this.eventCallbacks)) { return; } const snapshot = this.path === DatabaseConstants.connectedPath - ? new DataSnapshot(() => this._getData(), true) + ? this._dataSnapshot : new DataSnapshot(() => this._getData(), snapshotValue); const debouncedEventCallback = debounce( @@ -163,7 +172,7 @@ class Reference { this.eventCallbacks[eventType] = callback; if (eventType === DatabaseConstants.valueEventType) { - this.debounceEventCallback(eventType); + this.triggerEventCallback(eventType); } } @@ -174,14 +183,7 @@ class Reference { throw new Error('eventType should be a string.'); } - if (this._dataSnapshot) { - return Promise.resolve(this._dataSnapshot); - } - - const newDataSnapshot = new DataSnapshot(() => this._getData()); - this._dataSnapshot = newDataSnapshot; - - return Promise.resolve(newDataSnapshot); + return Promise.resolve(this._dataSnapshot); } orderByChild(path) { @@ -190,19 +192,19 @@ class Reference { } async update(value) { - this._setData(value); + this._handleDataUpdate(value); return Promise.resolve(true); } async remove() { - this._setData(null); + this._handleDataUpdate(null); return Promise.resolve(true); } async set(value) { - this._setData(value); + this._handleDataUpdate(value); return Promise.resolve(true); }