firex-store
Advanced tools
Comparing version 0.6.0 to 0.7.0
export * from './store'; | ||
export { findFirestore } from './find'; | ||
export { ErrorHandler, OnCompleted, Mapper, AfterMutationCalled } from './types'; | ||
export { Mapper, AfterMutationCalled, ErrorHandler, CompletionHandler, NotFoundHandler } from './types'; | ||
export { Payload, DocumentResult } from './models'; |
@@ -1,6 +0,24 @@ | ||
import { Mapper, ErrorHandler, OnCompleted } from '../types'; | ||
import { Mapper, ErrorHandler, CompletionHandler, OnCompleted } from '../types'; | ||
export interface CriteriaOptions<T> { | ||
/** | ||
* @param mapper convert subscribed data to something, if defined | ||
* - type: <T>(data: { [key: string]: any }) => T | ||
*/ | ||
mapper?: Mapper<T>; | ||
/** | ||
* @param errorHandler it is called when error occured, if defined. But if not, call `console.error(error)` | ||
* - type: (error: any) => any | ||
*/ | ||
errorHandler?: ErrorHandler; | ||
/** | ||
* @param completionHandler it is called when completed fetching data , if defined. | ||
* - type: () => void | ||
*/ | ||
completionHandler?: CompletionHandler; | ||
/** | ||
* @param onCompleted it is called when completed fetching data , if defined. | ||
* @warning Deprecated. It is removed on v1.0.0~. So, use `completionHandler`, please | ||
* - type: () => void | ||
*/ | ||
onCompleted?: OnCompleted; | ||
} |
import { CriteriaOptions } from './criteria-options.interface'; | ||
import { AfterMutationCalled } from '../types'; | ||
import { AfterMutationCalled, NotFoundHandler } from '../types'; | ||
export interface SubscribeCriteriaOptions<T> extends CriteriaOptions<T> { | ||
/** | ||
* @param afterMutationCalled convert subscribed data to something, if defined | ||
* - `subscribeFirestore` and `subscribeFirestoreActions` only. | ||
* - type: (payload: Payload) => void | ||
* - payload = { data: { docId: string, [key: string]: any }, isLast: boolean } | ||
*/ | ||
afterMutationCalled?: AfterMutationCalled; | ||
/** | ||
* @param notFoundHandler (type?: string, isAll?: boolean) => void | ||
* - type: 'document' | 'collection' | ||
* - isAll: | ||
* - undefined when subscribe Document data | ||
* - true when subscribe Collection data | ||
* - false when subscribe Collection data and document in Collection is not existed | ||
*/ | ||
notFoundHandler?: NotFoundHandler; | ||
} |
export * from './map-to-if-defined'; | ||
export * from './mutation-handlers'; | ||
export * from './notifications'; |
@@ -7,1 +7,3 @@ "use strict"; | ||
__export(require("./map-to-if-defined")); | ||
__export(require("./mutation-handlers")); | ||
__export(require("./notifications")); |
@@ -12,7 +12,7 @@ import { CallMutation, NullOr } from '../types'; | ||
export declare class FirestoreService { | ||
static subscribe<T = any>({ ref, callMutation, mapper, errorHandler, onCompleted, afterMutationCalled }: SubscribeCriteria<T, firebase.firestore.DocumentReference>): Unsubscribe; | ||
static subscribeAll<T = any>({ ref, callMutation, mapper, errorHandler, onCompleted, afterMutationCalled }: SubscribeCriteria<T, firebase.firestore.CollectionReference | firebase.firestore.Query>): Unsubscribe; | ||
static find<T = any>({ ref, mapper, errorHandler, onCompleted }: FindCriteria<T, firebase.firestore.DocumentReference>): Promise<NullOr<T | any>>; | ||
static findAll<T = any>({ ref, mapper, errorHandler, onCompleted }: FindCriteria<T, firebase.firestore.CollectionReference | firebase.firestore.Query>): Promise<NullOr<T[] | any | any[]>>; | ||
static subscribe<T = any>({ ref, callMutation, mapper, errorHandler, completionHandler, afterMutationCalled, notFoundHandler, onCompleted }: SubscribeCriteria<T, firebase.firestore.DocumentReference>): Unsubscribe; | ||
static subscribeAll<T = any>({ ref, callMutation, mapper, errorHandler, completionHandler, afterMutationCalled, notFoundHandler, onCompleted }: SubscribeCriteria<T, firebase.firestore.CollectionReference | firebase.firestore.Query>): Unsubscribe; | ||
static find<T = any>({ ref, mapper, errorHandler, completionHandler, onCompleted }: FindCriteria<T, firebase.firestore.DocumentReference>): Promise<NullOr<T | any>>; | ||
static findAll<T = any>({ ref, mapper, errorHandler, completionHandler, onCompleted }: FindCriteria<T, firebase.firestore.CollectionReference | firebase.firestore.Query>): Promise<NullOr<T[] | any | any[]>>; | ||
} | ||
export {}; |
@@ -5,52 +5,32 @@ "use strict"; | ||
class FirestoreService { | ||
static subscribe({ ref, callMutation, mapper, errorHandler, onCompleted, afterMutationCalled }) { | ||
return ref.onSnapshot((doc) => { | ||
if (!doc.exists) { | ||
return; | ||
} | ||
const data = helpers_1.mapToIfDefined(doc, mapper); | ||
const payload = { data, isLast: true }; | ||
callMutation('added', payload); | ||
if (afterMutationCalled) { | ||
afterMutationCalled(payload); | ||
} | ||
}, (error) => errorHandler ? errorHandler(error) : console.error(error), () => { | ||
if (!onCompleted) { | ||
return; | ||
} | ||
onCompleted(); | ||
}); | ||
static subscribe({ ref, callMutation, mapper, errorHandler, completionHandler, afterMutationCalled, notFoundHandler, onCompleted }) { | ||
return ref.onSnapshot((doc) => !doc.exists | ||
? helpers_1.notifyNotFound('document', notFoundHandler) | ||
: helpers_1.callDocumentMutation({ | ||
snapshot: doc, | ||
callMutation, | ||
mapper, | ||
afterMutationCalled | ||
}), (error) => helpers_1.notifyErrorOccurred(error, errorHandler), () => helpers_1.notifyCompletionIfDefined(completionHandler ? completionHandler : onCompleted)); | ||
} | ||
static subscribeAll({ ref, callMutation, mapper, errorHandler, onCompleted, afterMutationCalled }) { | ||
return ref.onSnapshot((snapshot) => { | ||
const length = snapshot.docChanges().length; | ||
snapshot.docChanges().forEach((change, index) => { | ||
if (!change.doc.exists) { | ||
return; | ||
} | ||
const data = helpers_1.mapToIfDefined(change.doc, mapper); | ||
const payload = { data, isLast: length === index + 1 }; | ||
callMutation(change.type, payload); | ||
if (afterMutationCalled) { | ||
afterMutationCalled(payload); | ||
} | ||
}); | ||
}, (error) => errorHandler ? errorHandler(error) : console.error(error), () => { | ||
if (!onCompleted) { | ||
return; | ||
} | ||
onCompleted(); | ||
}); | ||
static subscribeAll({ ref, callMutation, mapper, errorHandler, completionHandler, afterMutationCalled, notFoundHandler, onCompleted }) { | ||
return ref.onSnapshot((snapshot) => snapshot.empty | ||
? helpers_1.notifyNotFound('collection', notFoundHandler, true) | ||
: helpers_1.callCollectionMutation({ | ||
snapshot, | ||
callMutation, | ||
mapper, | ||
afterMutationCalled, | ||
notifyNotFound: () => helpers_1.notifyNotFound('collection', notFoundHandler, false) | ||
}), (error) => helpers_1.notifyErrorOccurred(error, errorHandler), () => helpers_1.notifyCompletionIfDefined(completionHandler ? completionHandler : onCompleted)); | ||
} | ||
static async find({ ref, mapper, errorHandler, onCompleted }) { | ||
static async find({ ref, mapper, errorHandler, completionHandler, onCompleted }) { | ||
const result = await ref | ||
.get() | ||
.then((doc) => (!doc.exists ? null : helpers_1.mapToIfDefined(doc, mapper))) | ||
.catch((error) => errorHandler ? errorHandler(error) : console.error(error)); | ||
if (onCompleted) { | ||
onCompleted(); | ||
} | ||
.catch((error) => helpers_1.notifyErrorOccurred(error, errorHandler)); | ||
helpers_1.notifyCompletionIfDefined(completionHandler ? completionHandler : onCompleted); | ||
return result; | ||
} | ||
static async findAll({ ref, mapper, errorHandler, onCompleted }) { | ||
static async findAll({ ref, mapper, errorHandler, completionHandler, onCompleted }) { | ||
const result = await ref | ||
@@ -62,9 +42,7 @@ .get() | ||
.then((documentResults) => { | ||
const resultWithoutNull = documentResults.filter((it) => it !== null); | ||
return resultWithoutNull.length > 0 ? resultWithoutNull : null; | ||
const resultsWithoutNull = documentResults.filter((it) => it !== null); | ||
return resultsWithoutNull.length > 0 ? resultsWithoutNull : null; | ||
}) | ||
.catch((error) => errorHandler ? errorHandler(error) : console.error(error)); | ||
if (onCompleted) { | ||
onCompleted(); | ||
} | ||
.catch((error) => helpers_1.notifyErrorOccurred(error, errorHandler)); | ||
helpers_1.notifyCompletionIfDefined(completionHandler ? completionHandler : onCompleted); | ||
return result; | ||
@@ -71,0 +49,0 @@ } |
@@ -5,5 +5,7 @@ export * from './error-handler.type'; | ||
export * from './mutation-type.type'; | ||
export * from './on-completed.type'; | ||
export * from './completion-handler.type'; | ||
export * from './firestore-ref.type'; | ||
export * from './subscribe-hooks.type'; | ||
export * from './null-or.type'; | ||
export * from './after-mutation-called.type'; | ||
export * from './not-found-handler.type'; | ||
export * from './on-completed.type'; |
@@ -1,3 +0,3 @@ | ||
export declare type Mapper<T> = (args: { | ||
export declare type Mapper<T> = (data: { | ||
[key: string]: any; | ||
}) => T; |
{ | ||
"name": "firex-store", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "subscribe firebase data to vuex", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -315,6 +315,10 @@ # firex-store | ||
- onCompleted | ||
- CompletionHandler | ||
- If it defined, call it when completed | ||
- OnCompleted ※ It is deprecated and removed on v1.0.0~ | ||
- If it defined, call it when completed | ||
- afterMutationCalled | ||
@@ -343,3 +347,3 @@ | ||
```javascript | ||
const onCompleted = () => { | ||
const completionHandler = () => { | ||
console.log('completed!') | ||
@@ -381,3 +385,4 @@ } | ||
errorHandler, | ||
onCompleted, | ||
completionHandler, | ||
// onCompleted, <- obsoleted | ||
afterMutationCalled | ||
@@ -384,0 +389,0 @@ } |
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
38082
79
668
394