abstracted-firebase
Advanced tools
Comparing version 0.25.8 to 0.26.0
@@ -0,1 +1,2 @@ | ||
import { IDictionary } from "common-types"; | ||
import { IMockConfigOptions } from "firemock"; | ||
@@ -5,2 +6,3 @@ import { SerializedQuery } from "serialized-query"; | ||
import { IFirebaseConfig, IEmitter, IMockLoadingState, IFirebaseWatchHandler, IMultiPathSet } from "./types"; | ||
import { IFirebaseListener, IFirebaseConnectionCallback } from "."; | ||
declare type Mock = import("firemock").Mock; | ||
@@ -19,2 +21,3 @@ /** time by which the dynamically loaded mock library should be loaded */ | ||
protected abstract _eventManager: IEmitter; | ||
protected abstract _clientType: "client" | "admin"; | ||
protected _isConnected: boolean; | ||
@@ -28,5 +31,10 @@ protected _mockLoadingState: IMockLoadingState; | ||
protected _allowMocking: boolean; | ||
protected _onConnected: IFirebaseListener[]; | ||
protected _onDisconnected: IFirebaseListener[]; | ||
protected app: any; | ||
protected _database: FirebaseDatabase; | ||
/** the config the db was started with */ | ||
protected _config: IFirebaseConfig; | ||
protected abstract _auth: any; | ||
constructor(config: IFirebaseConfig); | ||
initialize(config?: IFirebaseConfig): void; | ||
@@ -57,2 +65,12 @@ /** | ||
waitForConnection(): Promise<this>; | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb: IFirebaseConnectionCallback, id?: string, ctx?: IDictionary): string; | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id: string): this; | ||
/** set a "value" in the database at a given path */ | ||
@@ -166,2 +184,9 @@ set<T = any>(path: string, value: T): Promise<void>; | ||
exists(path: string): Promise<boolean>; | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
protected _monitorConnection(snap: DataSnapshot): void; | ||
protected abstract connectToFirebase(config: any): Promise<void>; | ||
@@ -168,0 +193,0 @@ protected abstract listenForConnectionStatus(): void; |
@@ -13,6 +13,8 @@ "use strict"; | ||
const AbstractedProxyError_1 = require("./errors/AbstractedProxyError"); | ||
const _1 = require("."); | ||
const AbstractedError_1 = require("./errors/AbstractedError"); | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
exports.MOCK_LOADING_TIMEOUT = 2000; | ||
class RealTimeDB { | ||
constructor() { | ||
constructor(config) { | ||
/** how many miliseconds before the attempt to connect to DB is timed out */ | ||
@@ -26,2 +28,5 @@ this.CONNECTION_TIMEOUT = 5000; | ||
this._allowMocking = false; | ||
this._onConnected = []; | ||
this._onDisconnected = []; | ||
this._config = config; | ||
} | ||
@@ -53,13 +58,4 @@ get isMockDb() { | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
if (config.mockData) { | ||
this.mock.updateDB(config.mockData); | ||
} | ||
// this._fakerReady = this._mock.importFakerLibrary(); | ||
} | ||
else { | ||
this._mocking = false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
this._mocking = config.mocking ? true : false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
@@ -142,5 +138,6 @@ /** | ||
async waitForConnection() { | ||
if (this._mocking) { | ||
const config = this._config; | ||
if (_1.isMockConfig(config)) { | ||
// MOCKING | ||
await this.getFireMock(); | ||
await this.getFireMock({ db: config.mockData, auth: config.mockAuth }); | ||
} | ||
@@ -158,3 +155,3 @@ else { | ||
else { | ||
throw Error(`While waiting for connection received a disconnect message`); | ||
throw new AbstractedError_1.AbstractedError(`While waiting for a connection received a disconnect message instead`, `no-connection`); | ||
} | ||
@@ -165,3 +162,3 @@ }); | ||
await common_types_1.wait(this.CONNECTION_TIMEOUT); | ||
throw common_types_1.createError("abstracted-firebase/connection-timeout", `The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`); | ||
throw new AbstractedError_1.AbstractedError(`The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`, "connection-timeout"); | ||
}; | ||
@@ -172,3 +169,30 @@ await Promise.race([connectionEvent, timeout]); | ||
} | ||
this._onConnected.map(i => i.cb(this, i.ctx)); | ||
} | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb, id, ctx) { | ||
if (!id) { | ||
id = Math.random() | ||
.toString(36) | ||
.substr(2, 10); | ||
} | ||
else { | ||
if (this._onConnected.map(i => i.id).includes(id)) { | ||
throw new AbstractedError_1.AbstractedError(`Request for onConnect() notifications was done with an explicit key [ ${id} ] which is already in use!`, `duplicate-listener`); | ||
} | ||
} | ||
this._onConnected = this._onConnected.concat({ id, cb, ctx }); | ||
return id; | ||
} | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id) { | ||
this._onConnected = this._onConnected.filter(i => i.id !== id); | ||
return this; | ||
} | ||
/** set a "value" in the database at a given path */ | ||
@@ -467,2 +491,19 @@ async set(path, value) { | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
_monitorConnection(snap) { | ||
this._isConnected = snap.val(); | ||
// call active listeners | ||
if (this._isConnected) { | ||
// this._eventManager.connection(this._isConnected); | ||
this._onConnected.forEach(listener => listener.cb(this)); | ||
} | ||
else { | ||
this._onDisconnected.forEach(listener => listener.cb(this)); | ||
} | ||
} | ||
/** | ||
* **getFireMock** | ||
@@ -469,0 +510,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { IDictionary } from "firemock"; | ||
import { IDictionary, IMockAuthConfig } from "firemock"; | ||
export { RealTimeDB } from "./db"; | ||
@@ -8,4 +8,4 @@ export { FileDepthExceeded } from "./errors/FileDepthExceeded"; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps | IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps | IFirebaseConfigMocked; | ||
export * from "./types"; | ||
@@ -19,2 +19,3 @@ export interface IFirebaseClientConfigProps extends IAbstractedFirebaseConfig { | ||
messagingSenderId?: string; | ||
mocking?: false; | ||
} | ||
@@ -32,2 +33,3 @@ export interface IFirebaseAdminConfigProps extends IAbstractedFirebaseConfig { | ||
databaseUrl?: string; | ||
mocking?: false | undefined; | ||
} | ||
@@ -45,5 +47,9 @@ export interface IAbstractedFirebaseConfig { | ||
export interface IFirebaseConfigMocked extends IAbstractedFirebaseConfig { | ||
mocking?: true; | ||
mocking: true; | ||
/** initialize the database to a known state */ | ||
mockData?: IDictionary; | ||
/** optionally configure mocking for Firebase Authentication */ | ||
mockAuth?: IMockAuthConfig; | ||
} | ||
export declare function isMockConfig(config?: IFirebaseConfig): config is IFirebaseConfigMocked; | ||
export declare function isRealDbConfig(config: IFirebaseConfig): config is IFirebaseAdminConfigProps | IFirebaseClientConfigProps; |
@@ -15,1 +15,9 @@ "use strict"; | ||
__export(require("./types")); | ||
function isMockConfig(config = {}) { | ||
return config.mocking === true; | ||
} | ||
exports.isMockConfig = isMockConfig; | ||
function isRealDbConfig(config) { | ||
return config.mocking !== true; | ||
} | ||
exports.isRealDbConfig = isRealDbConfig; |
import { DataSnapshot, OnDisconnect, Query, ThenableReference, EventType } from "@firebase/database-types"; | ||
import { IDictionary } from "common-types"; | ||
import { IFirebaseClientConfig, IFirebaseAdminConfig } from "."; | ||
import { RealTimeDB } from "./db"; | ||
export declare type IMockLoadingState = "not-applicable" | "loaded" | "loading" | "timed-out"; | ||
export declare type DebuggingCallback = (message: string) => void; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export interface IFirebaseListener { | ||
id: string; | ||
cb: IFirebaseConnectionCallback; | ||
ctx?: IDictionary; | ||
} | ||
export declare type IFirebaseConnectionCallback = (db: RealTimeDB, ctx?: IDictionary) => void; | ||
export interface IEmitter { | ||
@@ -8,0 +15,0 @@ emit: (event: string | symbol, ...args: any[]) => boolean; |
@@ -0,1 +1,2 @@ | ||
import { IDictionary } from "common-types"; | ||
import { IMockConfigOptions } from "firemock"; | ||
@@ -5,2 +6,3 @@ import { SerializedQuery } from "serialized-query"; | ||
import { IFirebaseConfig, IEmitter, IMockLoadingState, IFirebaseWatchHandler, IMultiPathSet } from "./types"; | ||
import { IFirebaseListener, IFirebaseConnectionCallback } from "."; | ||
declare type Mock = import("firemock").Mock; | ||
@@ -19,2 +21,3 @@ /** time by which the dynamically loaded mock library should be loaded */ | ||
protected abstract _eventManager: IEmitter; | ||
protected abstract _clientType: "client" | "admin"; | ||
protected _isConnected: boolean; | ||
@@ -28,5 +31,10 @@ protected _mockLoadingState: IMockLoadingState; | ||
protected _allowMocking: boolean; | ||
protected _onConnected: IFirebaseListener[]; | ||
protected _onDisconnected: IFirebaseListener[]; | ||
protected app: any; | ||
protected _database: FirebaseDatabase; | ||
/** the config the db was started with */ | ||
protected _config: IFirebaseConfig; | ||
protected abstract _auth: any; | ||
constructor(config: IFirebaseConfig); | ||
initialize(config?: IFirebaseConfig): void; | ||
@@ -57,2 +65,12 @@ /** | ||
waitForConnection(): Promise<this>; | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb: IFirebaseConnectionCallback, id?: string, ctx?: IDictionary): string; | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id: string): this; | ||
/** set a "value" in the database at a given path */ | ||
@@ -166,2 +184,9 @@ set<T = any>(path: string, value: T): Promise<void>; | ||
exists(path: string): Promise<boolean>; | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
protected _monitorConnection(snap: DataSnapshot): void; | ||
protected abstract connectToFirebase(config: any): Promise<void>; | ||
@@ -168,0 +193,0 @@ protected abstract listenForConnectionStatus(): void; |
// tslint:disable:no-implicit-dependencies | ||
import { wait, createError } from "common-types"; | ||
import { wait } from "common-types"; | ||
import * as convert from "typed-conversions"; | ||
@@ -11,6 +11,8 @@ import { SerializedQuery } from "serialized-query"; | ||
import { AbstractedProxyError } from "./errors/AbstractedProxyError"; | ||
import { isMockConfig } from "."; | ||
import { AbstractedError } from "./errors/AbstractedError"; | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
export const MOCK_LOADING_TIMEOUT = 2000; | ||
export class RealTimeDB { | ||
constructor() { | ||
constructor(config) { | ||
/** how many miliseconds before the attempt to connect to DB is timed out */ | ||
@@ -24,2 +26,5 @@ this.CONNECTION_TIMEOUT = 5000; | ||
this._allowMocking = false; | ||
this._onConnected = []; | ||
this._onDisconnected = []; | ||
this._config = config; | ||
} | ||
@@ -51,13 +56,4 @@ get isMockDb() { | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
if (config.mockData) { | ||
this.mock.updateDB(config.mockData); | ||
} | ||
// this._fakerReady = this._mock.importFakerLibrary(); | ||
} | ||
else { | ||
this._mocking = false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
this._mocking = config.mocking ? true : false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
@@ -140,5 +136,6 @@ /** | ||
async waitForConnection() { | ||
if (this._mocking) { | ||
const config = this._config; | ||
if (isMockConfig(config)) { | ||
// MOCKING | ||
await this.getFireMock(); | ||
await this.getFireMock({ db: config.mockData, auth: config.mockAuth }); | ||
} | ||
@@ -156,3 +153,3 @@ else { | ||
else { | ||
throw Error(`While waiting for connection received a disconnect message`); | ||
throw new AbstractedError(`While waiting for a connection received a disconnect message instead`, `no-connection`); | ||
} | ||
@@ -163,3 +160,3 @@ }); | ||
await wait(this.CONNECTION_TIMEOUT); | ||
throw createError("abstracted-firebase/connection-timeout", `The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`); | ||
throw new AbstractedError(`The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`, "connection-timeout"); | ||
}; | ||
@@ -170,3 +167,30 @@ await Promise.race([connectionEvent, timeout]); | ||
} | ||
this._onConnected.map(i => i.cb(this, i.ctx)); | ||
} | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb, id, ctx) { | ||
if (!id) { | ||
id = Math.random() | ||
.toString(36) | ||
.substr(2, 10); | ||
} | ||
else { | ||
if (this._onConnected.map(i => i.id).includes(id)) { | ||
throw new AbstractedError(`Request for onConnect() notifications was done with an explicit key [ ${id} ] which is already in use!`, `duplicate-listener`); | ||
} | ||
} | ||
this._onConnected = this._onConnected.concat({ id, cb, ctx }); | ||
return id; | ||
} | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id) { | ||
this._onConnected = this._onConnected.filter(i => i.id !== id); | ||
return this; | ||
} | ||
/** set a "value" in the database at a given path */ | ||
@@ -465,2 +489,19 @@ async set(path, value) { | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
_monitorConnection(snap) { | ||
this._isConnected = snap.val(); | ||
// call active listeners | ||
if (this._isConnected) { | ||
// this._eventManager.connection(this._isConnected); | ||
this._onConnected.forEach(listener => listener.cb(this)); | ||
} | ||
else { | ||
this._onDisconnected.forEach(listener => listener.cb(this)); | ||
} | ||
} | ||
/** | ||
* **getFireMock** | ||
@@ -467,0 +508,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { IDictionary } from "firemock"; | ||
import { IDictionary, IMockAuthConfig } from "firemock"; | ||
export { RealTimeDB } from "./db"; | ||
@@ -8,4 +8,4 @@ export { FileDepthExceeded } from "./errors/FileDepthExceeded"; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps | IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps | IFirebaseConfigMocked; | ||
export * from "./types"; | ||
@@ -19,2 +19,3 @@ export interface IFirebaseClientConfigProps extends IAbstractedFirebaseConfig { | ||
messagingSenderId?: string; | ||
mocking?: false; | ||
} | ||
@@ -32,2 +33,3 @@ export interface IFirebaseAdminConfigProps extends IAbstractedFirebaseConfig { | ||
databaseUrl?: string; | ||
mocking?: false | undefined; | ||
} | ||
@@ -45,5 +47,9 @@ export interface IAbstractedFirebaseConfig { | ||
export interface IFirebaseConfigMocked extends IAbstractedFirebaseConfig { | ||
mocking?: true; | ||
mocking: true; | ||
/** initialize the database to a known state */ | ||
mockData?: IDictionary; | ||
/** optionally configure mocking for Firebase Authentication */ | ||
mockAuth?: IMockAuthConfig; | ||
} | ||
export declare function isMockConfig(config?: IFirebaseConfig): config is IFirebaseConfigMocked; | ||
export declare function isRealDbConfig(config: IFirebaseConfig): config is IFirebaseAdminConfigProps | IFirebaseClientConfigProps; |
@@ -6,1 +6,7 @@ export { RealTimeDB } from "./db"; | ||
export * from "./types"; | ||
export function isMockConfig(config = {}) { | ||
return config.mocking === true; | ||
} | ||
export function isRealDbConfig(config) { | ||
return config.mocking !== true; | ||
} |
import { DataSnapshot, OnDisconnect, Query, ThenableReference, EventType } from "@firebase/database-types"; | ||
import { IDictionary } from "common-types"; | ||
import { IFirebaseClientConfig, IFirebaseAdminConfig } from "."; | ||
import { RealTimeDB } from "./db"; | ||
export declare type IMockLoadingState = "not-applicable" | "loaded" | "loading" | "timed-out"; | ||
export declare type DebuggingCallback = (message: string) => void; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export interface IFirebaseListener { | ||
id: string; | ||
cb: IFirebaseConnectionCallback; | ||
ctx?: IDictionary; | ||
} | ||
export declare type IFirebaseConnectionCallback = (db: RealTimeDB, ctx?: IDictionary) => void; | ||
export interface IEmitter { | ||
@@ -8,0 +15,0 @@ emit: (event: string | symbol, ...args: any[]) => boolean; |
@@ -0,1 +1,2 @@ | ||
import { IDictionary } from "common-types"; | ||
import { IMockConfigOptions } from "firemock"; | ||
@@ -5,2 +6,3 @@ import { SerializedQuery } from "serialized-query"; | ||
import { IFirebaseConfig, IEmitter, IMockLoadingState, IFirebaseWatchHandler, IMultiPathSet } from "./types"; | ||
import { IFirebaseListener, IFirebaseConnectionCallback } from "."; | ||
declare type Mock = import("firemock").Mock; | ||
@@ -19,2 +21,3 @@ /** time by which the dynamically loaded mock library should be loaded */ | ||
protected abstract _eventManager: IEmitter; | ||
protected abstract _clientType: "client" | "admin"; | ||
protected _isConnected: boolean; | ||
@@ -28,5 +31,10 @@ protected _mockLoadingState: IMockLoadingState; | ||
protected _allowMocking: boolean; | ||
protected _onConnected: IFirebaseListener[]; | ||
protected _onDisconnected: IFirebaseListener[]; | ||
protected app: any; | ||
protected _database: FirebaseDatabase; | ||
/** the config the db was started with */ | ||
protected _config: IFirebaseConfig; | ||
protected abstract _auth: any; | ||
constructor(config: IFirebaseConfig); | ||
initialize(config?: IFirebaseConfig): void; | ||
@@ -57,2 +65,12 @@ /** | ||
waitForConnection(): Promise<this>; | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb: IFirebaseConnectionCallback, id?: string, ctx?: IDictionary): string; | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id: string): this; | ||
/** set a "value" in the database at a given path */ | ||
@@ -166,2 +184,9 @@ set<T = any>(path: string, value: T): Promise<void>; | ||
exists(path: string): Promise<boolean>; | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
protected _monitorConnection(snap: DataSnapshot): void; | ||
protected abstract connectToFirebase(config: any): Promise<void>; | ||
@@ -168,0 +193,0 @@ protected abstract listenForConnectionStatus(): void; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "common-types", "typed-conversions", "serialized-query", "./util", "./errors/FileDepthExceeded", "./errors/UndefinedAssignment", "./WatcherEventWrapper", "./errors", "./errors/AbstractedProxyError"], factory); | ||
define(["require", "exports", "common-types", "typed-conversions", "serialized-query", "./util", "./errors/FileDepthExceeded", "./errors/UndefinedAssignment", "./WatcherEventWrapper", "./errors", "./errors/AbstractedProxyError", ".", "./errors/AbstractedError"], factory); | ||
} | ||
@@ -24,6 +24,8 @@ })(function (require, exports) { | ||
const AbstractedProxyError_1 = require("./errors/AbstractedProxyError"); | ||
const _1 = require("."); | ||
const AbstractedError_1 = require("./errors/AbstractedError"); | ||
/** time by which the dynamically loaded mock library should be loaded */ | ||
exports.MOCK_LOADING_TIMEOUT = 2000; | ||
class RealTimeDB { | ||
constructor() { | ||
constructor(config) { | ||
/** how many miliseconds before the attempt to connect to DB is timed out */ | ||
@@ -37,2 +39,5 @@ this.CONNECTION_TIMEOUT = 5000; | ||
this._allowMocking = false; | ||
this._onConnected = []; | ||
this._onDisconnected = []; | ||
this._config = config; | ||
} | ||
@@ -64,13 +69,4 @@ get isMockDb() { | ||
initialize(config = {}) { | ||
if (config.mocking) { | ||
this._mocking = true; | ||
if (config.mockData) { | ||
this.mock.updateDB(config.mockData); | ||
} | ||
// this._fakerReady = this._mock.importFakerLibrary(); | ||
} | ||
else { | ||
this._mocking = false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
this._mocking = config.mocking ? true : false; | ||
this.connectToFirebase(config).then(() => this.listenForConnectionStatus()); | ||
} | ||
@@ -153,5 +149,6 @@ /** | ||
async waitForConnection() { | ||
if (this._mocking) { | ||
const config = this._config; | ||
if (_1.isMockConfig(config)) { | ||
// MOCKING | ||
await this.getFireMock(); | ||
await this.getFireMock({ db: config.mockData, auth: config.mockAuth }); | ||
} | ||
@@ -169,3 +166,3 @@ else { | ||
else { | ||
throw Error(`While waiting for connection received a disconnect message`); | ||
throw new AbstractedError_1.AbstractedError(`While waiting for a connection received a disconnect message instead`, `no-connection`); | ||
} | ||
@@ -176,3 +173,3 @@ }); | ||
await common_types_1.wait(this.CONNECTION_TIMEOUT); | ||
throw common_types_1.createError("abstracted-firebase/connection-timeout", `The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`); | ||
throw new AbstractedError_1.AbstractedError(`The database didn't connect after the allocated period of ${this.CONNECTION_TIMEOUT}ms`, "connection-timeout"); | ||
}; | ||
@@ -183,3 +180,30 @@ await Promise.race([connectionEvent, timeout]); | ||
} | ||
this._onConnected.map(i => i.cb(this, i.ctx)); | ||
} | ||
/** | ||
* get a notification when DB is connected; returns a unique id | ||
* which can be used to remove the callback. You may, optionally, | ||
* state a unique id of your own. | ||
*/ | ||
notifyWhenConnected(cb, id, ctx) { | ||
if (!id) { | ||
id = Math.random() | ||
.toString(36) | ||
.substr(2, 10); | ||
} | ||
else { | ||
if (this._onConnected.map(i => i.id).includes(id)) { | ||
throw new AbstractedError_1.AbstractedError(`Request for onConnect() notifications was done with an explicit key [ ${id} ] which is already in use!`, `duplicate-listener`); | ||
} | ||
} | ||
this._onConnected = this._onConnected.concat({ id, cb, ctx }); | ||
return id; | ||
} | ||
/** | ||
* removes a callback notification previously registered | ||
*/ | ||
removeNotificationOnConnection(id) { | ||
this._onConnected = this._onConnected.filter(i => i.id !== id); | ||
return this; | ||
} | ||
/** set a "value" in the database at a given path */ | ||
@@ -478,2 +502,19 @@ async set(path, value) { | ||
/** | ||
* monitorConnection | ||
* | ||
* allows interested parties to hook into event messages when the | ||
* DB connection either connects or disconnects | ||
*/ | ||
_monitorConnection(snap) { | ||
this._isConnected = snap.val(); | ||
// call active listeners | ||
if (this._isConnected) { | ||
// this._eventManager.connection(this._isConnected); | ||
this._onConnected.forEach(listener => listener.cb(this)); | ||
} | ||
else { | ||
this._onDisconnected.forEach(listener => listener.cb(this)); | ||
} | ||
} | ||
/** | ||
* **getFireMock** | ||
@@ -480,0 +521,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { IDictionary } from "firemock"; | ||
import { IDictionary, IMockAuthConfig } from "firemock"; | ||
export { RealTimeDB } from "./db"; | ||
@@ -8,4 +8,4 @@ export { FileDepthExceeded } from "./errors/FileDepthExceeded"; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps & IFirebaseConfigMocked; | ||
export declare type IFirebaseClientConfig = IFirebaseClientConfigProps | IFirebaseConfigMocked; | ||
export declare type IFirebaseAdminConfig = IFirebaseAdminConfigProps | IFirebaseConfigMocked; | ||
export * from "./types"; | ||
@@ -19,2 +19,3 @@ export interface IFirebaseClientConfigProps extends IAbstractedFirebaseConfig { | ||
messagingSenderId?: string; | ||
mocking?: false; | ||
} | ||
@@ -32,2 +33,3 @@ export interface IFirebaseAdminConfigProps extends IAbstractedFirebaseConfig { | ||
databaseUrl?: string; | ||
mocking?: false | undefined; | ||
} | ||
@@ -45,5 +47,9 @@ export interface IAbstractedFirebaseConfig { | ||
export interface IFirebaseConfigMocked extends IAbstractedFirebaseConfig { | ||
mocking?: true; | ||
mocking: true; | ||
/** initialize the database to a known state */ | ||
mockData?: IDictionary; | ||
/** optionally configure mocking for Firebase Authentication */ | ||
mockAuth?: IMockAuthConfig; | ||
} | ||
export declare function isMockConfig(config?: IFirebaseConfig): config is IFirebaseConfigMocked; | ||
export declare function isRealDbConfig(config: IFirebaseConfig): config is IFirebaseAdminConfigProps | IFirebaseClientConfigProps; |
@@ -24,2 +24,10 @@ (function (factory) { | ||
__export(require("./types")); | ||
function isMockConfig(config = {}) { | ||
return config.mocking === true; | ||
} | ||
exports.isMockConfig = isMockConfig; | ||
function isRealDbConfig(config) { | ||
return config.mocking !== true; | ||
} | ||
exports.isRealDbConfig = isRealDbConfig; | ||
}); |
import { DataSnapshot, OnDisconnect, Query, ThenableReference, EventType } from "@firebase/database-types"; | ||
import { IDictionary } from "common-types"; | ||
import { IFirebaseClientConfig, IFirebaseAdminConfig } from "."; | ||
import { RealTimeDB } from "./db"; | ||
export declare type IMockLoadingState = "not-applicable" | "loaded" | "loading" | "timed-out"; | ||
export declare type DebuggingCallback = (message: string) => void; | ||
export declare type IFirebaseConfig = IFirebaseClientConfig | IFirebaseAdminConfig; | ||
export interface IFirebaseListener { | ||
id: string; | ||
cb: IFirebaseConnectionCallback; | ||
ctx?: IDictionary; | ||
} | ||
export declare type IFirebaseConnectionCallback = (db: RealTimeDB, ctx?: IDictionary) => void; | ||
export interface IEmitter { | ||
@@ -8,0 +15,0 @@ emit: (event: string | symbol, ...args: any[]) => boolean; |
{ | ||
"name": "abstracted-firebase", | ||
"version": "0.25.8", | ||
"version": "0.26.0", | ||
"description": "Core functional library supporting 'abstracted-admin' and 'abstracted-client'", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138704
74
3335