11import { actionTypes } from '../constants'
22import { promisesForPopulate } from '../utils/populate'
3- import { getQueryIdFromPath , applyParamsToQuery } from '../utils/query'
3+ import {
4+ applyParamsToQuery ,
5+ getWatcherCount ,
6+ setWatcher ,
7+ unsetWatcher
8+ } from '../utils/query'
49
5- const { SET , NO_VALUE } = actionTypes
6-
7- const getWatchPath = ( event , path ) =>
8- `${ event } :${ ( ( path . substring ( 0 , 1 ) === '/' ) ? '' : '/' ) } ${ path } `
9-
10- /**
11- * @description Set a new watcher
12- * @param {Object } firebase - Internal firebase object
13- * @param {String } event - Type of event to watch for
14- * @param {String } path - Path to watch with watcher
15- * @param {String } queryId - Id of query
16- */
17- const setWatcher = ( firebase , event , path , queryId = undefined ) => {
18- const id = queryId ? `${ event } :/${ queryId } ` : getWatchPath ( event , path )
19-
20- if ( firebase . _ . watchers [ id ] ) {
21- firebase . _ . watchers [ id ] ++
22- } else {
23- firebase . _ . watchers [ id ] = 1
24- }
25-
26- return firebase . _ . watchers [ id ]
27- }
28-
29- /**
30- * @description Get count of currently attached watchers
31- * @param {Object } firebase - Internal firebase object
32- * @param {String } event - Type of event to watch for
33- * @param {String } path - Path to watch with watcher
34- * @param {String } queryId - Id of query
35- */
36- const getWatcherCount = ( firebase , event , path , queryId = undefined ) => {
37- const id = queryId ? `${ event } :/${ queryId } ` : getWatchPath ( event , path )
38- return firebase . _ . watchers [ id ]
39- }
40-
41- /**
42- * @description Remove/Unset a watcher
43- * @param {Object } firebase - Internal firebase object
44- * @param {String } event - Type of event to watch for
45- * @param {String } path - Path to watch with watcher
46- * @param {String } queryId - Id of query
47- */
48- const unsetWatcher = ( firebase , event , path , queryId = undefined ) => {
49- let id = queryId || getQueryIdFromPath ( path )
50- path = path . split ( '#' ) [ 0 ]
51-
52- if ( ! id ) {
53- id = getWatchPath ( event , path )
54- }
55-
56- if ( firebase . _ . watchers [ id ] <= 1 ) {
57- delete firebase . _ . watchers [ id ]
58- if ( event !== 'first_child' ) {
59- firebase . database ( ) . ref ( ) . child ( path ) . off ( event )
60- }
61- } else if ( firebase . _ . watchers [ id ] ) {
62- firebase . _ . watchers [ id ] --
63- }
64- }
10+ const { SET , NO_VALUE , ERROR } = actionTypes
6511
6612/**
6713 * @description Watch a specific event type
@@ -102,6 +48,12 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
10248 path
10349 } )
10450 }
51+ return snapshot
52+ } , ( err ) => {
53+ dispatch ( {
54+ type : ERROR ,
55+ payload : err
56+ } )
10557 } )
10658 }
10759
@@ -115,16 +67,25 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
11567 // Handle once queries
11668 if ( e === 'once' ) {
11769 return q . once ( 'value' )
118- . then ( snapshot =>
70+ . then ( snapshot => {
71+ if ( snapshot . val ( ) !== null ) {
72+ dispatch ( {
73+ type : SET ,
74+ path,
75+ data : snapshot . val ( )
76+ } )
77+ }
78+ return snapshot
79+ } , ( err ) => {
11980 dispatch ( {
120- type : SET ,
121- path,
122- data : snapshot . val ( )
81+ type : ERROR ,
82+ payload : err
12383 } )
124- )
84+ } )
12585 }
12686 // Handle all other queries
127- // TODO: Handle onError of query (i.e. security errors)
87+
88+ /* istanbul ignore next: is run by tests but doesn't show in coverage */
12889 q . on ( e , snapshot => {
12990 let data = ( e === 'child_removed' ) ? undefined : snapshot . val ( )
13091 const resultPath = dest || ( e === 'value' ) ? p : `${ p } /${ snapshot . key } `
@@ -156,10 +117,15 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
156117 data : list
157118 } )
158119 } )
120+ } , ( err ) => {
121+ dispatch ( {
122+ type : ERROR ,
123+ payload : err
124+ } )
159125 } )
160126 }
161127
162- runQuery ( query , type , path , queryParams )
128+ return runQuery ( query , type , path , queryParams )
163129}
164130
165131/**
0 commit comments