abstracted-firebase
Advanced tools
Comparing version
@@ -5,2 +5,3 @@ 'use strict'; | ||
var commonTypes = require('common-types'); | ||
var convert = require('typed-conversions'); | ||
@@ -40,5 +41,8 @@ var serializedQuery = require('serialized-query'); | ||
})(exports.FirebaseBoolean || (exports.FirebaseBoolean = {})); | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
const MOCK_LOADING_TIMEOUT = 2000; | ||
class RealTimeDB { | ||
constructor() { | ||
constructor(config = {}) { | ||
this._isConnected = false; | ||
this._mockLoadingState = "not-applicable"; | ||
this._waitingForConnection = []; | ||
@@ -50,2 +54,6 @@ this._onConnected = []; | ||
this._allowMocking = false; | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock(); | ||
} | ||
} | ||
@@ -75,2 +83,7 @@ query(path) { | ||
} | ||
if (this._mockLoadingState === "loading") { | ||
const e = new Error(`Loading the mock library is an asynchronous task; typically it takes very little time but it is currently in process. You can listen to "waitForConnection()" to ensure the mock library is ready.`); | ||
e.name = "AbstractedFirebase::AsyncError"; | ||
throw e; | ||
} | ||
if (!this._mock) { | ||
@@ -88,11 +101,22 @@ const e = new Error(`Attempting to use mock getter but _mock is not set!`); | ||
async waitForConnection() { | ||
if (this.isConnected) { | ||
return Promise.resolve(); | ||
if (this._mocking) { | ||
// MOCKING | ||
const timeout = new Date().getTime() + MOCK_LOADING_TIMEOUT; | ||
while (this._mockLoadingState === "loading" && new Date().getTime() < timeout) { | ||
await commonTypes.wait(1); | ||
} | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
else { | ||
// NON-MOCKING | ||
if (this.isConnected) { | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
} | ||
} | ||
@@ -282,17 +306,2 @@ get isConnected() { | ||
} | ||
/** | ||
* initialize | ||
* | ||
* Allows the core module to initialize the object after the | ||
* client or admin modules constructors are called | ||
* | ||
*/ | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock().then(() => { | ||
console.log("mocking db established"); | ||
}); | ||
} | ||
} | ||
handleError(e, name, message = "") { | ||
@@ -307,14 +316,15 @@ console.error(`Error ${message}:`, e); | ||
try { | ||
this._mockLoadingState = "loading"; | ||
// tslint:disable-next-line:no-implicit-dependencies | ||
const FireMock = await Promise.resolve(require("firemock")); | ||
this._mockLoadingState = "loaded"; | ||
this._mock = new FireMock.Mock(); | ||
this._mock.db.resetDatabase(); | ||
this._mocking = true; | ||
return FireMock; | ||
} | ||
catch (e) { | ||
console.error(`There was an error asynchronously loading Firemock library.`, e.message); | ||
console.log(`The stack trace was:\n`, e.stack); | ||
console.info(`\nNo error thrown but no mocking functionality is available!`); | ||
this._mocking = false; | ||
console.error(`There was an error asynchronously loading Firemock library.`); | ||
if (e.stack) { | ||
console.log(`The stack trace was:\n`, e.stack); | ||
} | ||
throw e; | ||
} | ||
@@ -321,0 +331,0 @@ } |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('typed-conversions'), require('serialized-query'), require('firebase-api-surface')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'typed-conversions', 'serialized-query', 'firebase-api-surface'], factory) : | ||
(factory((global.AbstractedFirebase = {}),global.convert,global.serializedQuery,global.firebaseApiSurface)); | ||
}(this, (function (exports,convert,serializedQuery,firebaseApiSurface) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('common-types'), require('typed-conversions'), require('serialized-query'), require('firebase-api-surface')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'common-types', 'typed-conversions', 'serialized-query', 'firebase-api-surface'], factory) : | ||
(factory((global.AbstractedFirebase = {}),global.commonTypes,global.convert,global.serializedQuery,global.firebaseApiSurface)); | ||
}(this, (function (exports,commonTypes,convert,serializedQuery,firebaseApiSurface) { 'use strict'; | ||
@@ -37,5 +37,8 @@ class FirebaseDepthExceeded extends Error { | ||
})(exports.FirebaseBoolean || (exports.FirebaseBoolean = {})); | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
const MOCK_LOADING_TIMEOUT = 2000; | ||
class RealTimeDB { | ||
constructor() { | ||
constructor(config = {}) { | ||
this._isConnected = false; | ||
this._mockLoadingState = "not-applicable"; | ||
this._waitingForConnection = []; | ||
@@ -47,2 +50,6 @@ this._onConnected = []; | ||
this._allowMocking = false; | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock(); | ||
} | ||
} | ||
@@ -72,2 +79,7 @@ query(path) { | ||
} | ||
if (this._mockLoadingState === "loading") { | ||
const e = new Error(`Loading the mock library is an asynchronous task; typically it takes very little time but it is currently in process. You can listen to "waitForConnection()" to ensure the mock library is ready.`); | ||
e.name = "AbstractedFirebase::AsyncError"; | ||
throw e; | ||
} | ||
if (!this._mock) { | ||
@@ -85,11 +97,22 @@ const e = new Error(`Attempting to use mock getter but _mock is not set!`); | ||
async waitForConnection() { | ||
if (this.isConnected) { | ||
return Promise.resolve(); | ||
if (this._mocking) { | ||
// MOCKING | ||
const timeout = new Date().getTime() + MOCK_LOADING_TIMEOUT; | ||
while (this._mockLoadingState === "loading" && new Date().getTime() < timeout) { | ||
await commonTypes.wait(1); | ||
} | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
else { | ||
// NON-MOCKING | ||
if (this.isConnected) { | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
} | ||
} | ||
@@ -279,17 +302,2 @@ get isConnected() { | ||
} | ||
/** | ||
* initialize | ||
* | ||
* Allows the core module to initialize the object after the | ||
* client or admin modules constructors are called | ||
* | ||
*/ | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock().then(() => { | ||
console.log("mocking db established"); | ||
}); | ||
} | ||
} | ||
handleError(e, name, message = "") { | ||
@@ -304,14 +312,15 @@ console.error(`Error ${message}:`, e); | ||
try { | ||
this._mockLoadingState = "loading"; | ||
// tslint:disable-next-line:no-implicit-dependencies | ||
const FireMock = await import("firemock"); | ||
this._mockLoadingState = "loaded"; | ||
this._mock = new FireMock.Mock(); | ||
this._mock.db.resetDatabase(); | ||
this._mocking = true; | ||
return FireMock; | ||
} | ||
catch (e) { | ||
console.error(`There was an error asynchronously loading Firemock library.`, e.message); | ||
console.log(`The stack trace was:\n`, e.stack); | ||
console.info(`\nNo error thrown but no mocking functionality is available!`); | ||
this._mocking = false; | ||
console.error(`There was an error asynchronously loading Firemock library.`); | ||
if (e.stack) { | ||
console.log(`The stack trace was:\n`, e.stack); | ||
} | ||
throw e; | ||
} | ||
@@ -318,0 +327,0 @@ } |
@@ -12,2 +12,5 @@ import { SerializedQuery } from "serialized-query"; | ||
} | ||
export declare type IMockLoadingState = "not-applicable" | "loaded" | "loading" | "timed-out"; | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
export declare const MOCK_LOADING_TIMEOUT = 2000; | ||
export declare type DebuggingCallback = (message: string) => void; | ||
@@ -24,2 +27,3 @@ export interface IFirebaseConfig { | ||
protected _isConnected: boolean; | ||
protected _mockLoadingState: IMockLoadingState; | ||
protected _database: rtdb.IFirebaseDatabase; | ||
@@ -34,2 +38,3 @@ protected _mock: import("firemock").Mock; | ||
protected _allowMocking: boolean; | ||
constructor(config?: IFirebaseConfig); | ||
query<T = any>(path: string): SerializedQuery<T>; | ||
@@ -47,3 +52,3 @@ /** Get a DB reference for a given path in Firebase */ | ||
resetMockDb(): void; | ||
waitForConnection(): Promise<void | {}>; | ||
waitForConnection(): Promise<{}>; | ||
readonly isConnected: boolean; | ||
@@ -111,12 +116,4 @@ /** set a "value" in the database at a given path */ | ||
exists(path: string): Promise<boolean>; | ||
/** | ||
* initialize | ||
* | ||
* Allows the core module to initialize the object after the | ||
* client or admin modules constructors are called | ||
* | ||
*/ | ||
protected initialize(config?: IFirebaseConfig): void; | ||
protected handleError(e: any, name: string, message?: string): Promise<never>; | ||
protected getFireMock(): Promise<typeof import("firemock")>; | ||
protected getFireMock(): Promise<void>; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { wait } from "common-types"; | ||
import * as convert from "typed-conversions"; | ||
@@ -11,5 +12,8 @@ import { SerializedQuery } from "serialized-query"; | ||
})(FirebaseBoolean || (FirebaseBoolean = {})); | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
export const MOCK_LOADING_TIMEOUT = 2000; | ||
export class RealTimeDB { | ||
constructor() { | ||
constructor(config = {}) { | ||
this._isConnected = false; | ||
this._mockLoadingState = "not-applicable"; | ||
this._waitingForConnection = []; | ||
@@ -21,2 +25,6 @@ this._onConnected = []; | ||
this._allowMocking = false; | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock(); | ||
} | ||
} | ||
@@ -46,2 +54,7 @@ query(path) { | ||
} | ||
if (this._mockLoadingState === "loading") { | ||
const e = new Error(`Loading the mock library is an asynchronous task; typically it takes very little time but it is currently in process. You can listen to "waitForConnection()" to ensure the mock library is ready.`); | ||
e.name = "AbstractedFirebase::AsyncError"; | ||
throw e; | ||
} | ||
if (!this._mock) { | ||
@@ -59,11 +72,22 @@ const e = new Error(`Attempting to use mock getter but _mock is not set!`); | ||
async waitForConnection() { | ||
if (this.isConnected) { | ||
return Promise.resolve(); | ||
if (this._mocking) { | ||
// MOCKING | ||
const timeout = new Date().getTime() + MOCK_LOADING_TIMEOUT; | ||
while (this._mockLoadingState === "loading" && new Date().getTime() < timeout) { | ||
await wait(1); | ||
} | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
else { | ||
// NON-MOCKING | ||
if (this.isConnected) { | ||
return; | ||
} | ||
return new Promise(resolve => { | ||
const cb = () => { | ||
resolve(); | ||
}; | ||
this._waitingForConnection.push(cb); | ||
}); | ||
} | ||
} | ||
@@ -253,17 +277,2 @@ get isConnected() { | ||
} | ||
/** | ||
* initialize | ||
* | ||
* Allows the core module to initialize the object after the | ||
* client or admin modules constructors are called | ||
* | ||
*/ | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
this.getFireMock().then(() => { | ||
console.log("mocking db established"); | ||
}); | ||
} | ||
} | ||
handleError(e, name, message = "") { | ||
@@ -278,16 +287,17 @@ console.error(`Error ${message}:`, e); | ||
try { | ||
this._mockLoadingState = "loading"; | ||
// tslint:disable-next-line:no-implicit-dependencies | ||
const FireMock = await import("firemock"); | ||
this._mockLoadingState = "loaded"; | ||
this._mock = new FireMock.Mock(); | ||
this._mock.db.resetDatabase(); | ||
this._mocking = true; | ||
return FireMock; | ||
} | ||
catch (e) { | ||
console.error(`There was an error asynchronously loading Firemock library.`, e.message); | ||
console.log(`The stack trace was:\n`, e.stack); | ||
console.info(`\nNo error thrown but no mocking functionality is available!`); | ||
this._mocking = false; | ||
console.error(`There was an error asynchronously loading Firemock library.`); | ||
if (e.stack) { | ||
console.log(`The stack trace was:\n`, e.stack); | ||
} | ||
throw e; | ||
} | ||
} | ||
} |
import FileDepthExceeded from "./errors/FileDepthExceeded"; | ||
import UndefinedAssignment from "./errors/UndefinedAssignment"; | ||
export { RealTimeDB, IFirebaseConfig, IFirebaseListener, FirebaseBoolean, DebuggingCallback, FirebaseEvent } from "./db"; | ||
export { RealTimeDB, IFirebaseListener, FirebaseBoolean, FirebaseEvent } from "./db"; | ||
export { rtdb } from "firebase-api-surface"; | ||
export { FileDepthExceeded, UndefinedAssignment }; | ||
export declare type DebuggingCallback = (message: string) => void; | ||
export interface IFirebaseConfig { | ||
debugging?: boolean | DebuggingCallback; | ||
mocking?: boolean; | ||
} |
{ | ||
"name": "abstracted-firebase", | ||
"version": "0.11.5", | ||
"version": "0.11.6", | ||
"description": "Core library supporting 'abstracted-admin' and 'abstracted-client' libraries", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91136
4.75%1105
2.89%