@@ -5,31 +5,35 @@ import {
55 LOGIN ,
66 LOGOUT ,
77 LOGIN_ERROR ,
8- NO_VALUE
8+ START ,
9+ INIT_BY_PATH
10+ // NO_VALUE
911} from './constants'
1012
1113import { Promise } from 'es6-promise'
1214
1315const getWatchPath = ( event , path ) => event + ':' + ( ( path . substring ( 0 , 1 ) === '/' ) ? '' : '/' ) + path
1416
1517const setWatcher = ( firebase , event , path , queryId = undefined ) => {
16- const id = ( queryId ) ? event + ':/' + queryId : getWatchPath ( event , path )
18+ const id = queryId || getQueryIdFromPath ( path , event ) || getWatchPath ( event , path )
1719
18- if ( firebase . _ . watchers [ id ] ) {
19- firebase . _ . watchers [ id ] ++
20- } else {
21- firebase . _ . watchers [ id ] = 1
22- }
20+ if ( event != 'once' ) {
21+ if ( firebase . _ . watchers [ id ] ) {
22+ firebase . _ . watchers [ id ] ++
23+ } else {
24+ firebase . _ . watchers [ id ] = 1
25+ }
26+ }
2327
2428 return firebase . _ . watchers [ id ]
2529}
2630
2731const getWatcherCount = ( firebase , event , path , queryId = undefined ) => {
28- const id = ( queryId ) ? event + ':/' + queryId : getWatchPath ( event , path )
32+ const id = queryId || getQueryIdFromPath ( path , event ) || getWatchPath ( event , path )
2933 return firebase . _ . watchers [ id ]
3034}
3135
32- const getQueryIdFromPath = ( path ) => {
36+ const getQueryIdFromPath = ( path , event = undefined ) => {
3337 const origPath = path
3438 let pathSplitted = path . split ( '#' )
3539 path = pathSplitted [ 0 ]
@@ -44,32 +48,37 @@ const getQueryIdFromPath = (path) => {
4448 } ) . filter ( q => q ) : undefined
4549
4650 return ( queryId && queryId . length > 0 )
47- ? queryId [ 0 ]
51+ ? ( event ? event + ':/' + queryId : queryId [ 0 ] )
4852 : ( ( isQuery ) ? origPath : undefined )
4953}
5054
51- const unsetWatcher = ( firebase , event , path , queryId = undefined ) => {
52- let id = queryId || getQueryIdFromPath ( path )
55+ const unsetWatcher = ( firebase , dispatch , event , path , queryId = undefined , isCleanFromState = true ) => {
56+ const id = queryId || getQueryIdFromPath ( path , event ) || getWatchPath ( event , path )
5357 path = path . split ( '#' ) [ 0 ]
5458
55- if ( ! id ) {
56- id = getWatchPath ( event , path )
57- }
58-
5959 if ( firebase . _ . watchers [ id ] <= 1 ) {
6060 delete firebase . _ . watchers [ id ]
61- if ( event !== 'first_child' ) {
62- firebase . database ( ) . ref ( ) . child ( path ) . off ( event )
61+ if ( event != 'once' ) {
62+ // if (event !== 'first_child') {
63+ // firebase.database().ref().child(path).off(event)
64+ // }
65+ firebase . database ( ) . ref ( ) . child ( path ) . off ( event )
66+ if ( isCleanFromState ) {
67+ dispatch ( {
68+ type : INIT_BY_PATH ,
69+ path
70+ } )
71+ }
6372 }
6473 } else if ( firebase . _ . watchers [ id ] ) {
6574 firebase . _ . watchers [ id ] --
6675 }
6776}
6877
69- export const watchEvent = ( firebase , dispatch , event , path , dest , onlyLastEvent = false ) => {
78+ export const watchEvent = ( firebase , dispatch , event , path , dest ) => {
7079 let isQuery = false
7180 let queryParams = [ ]
72- let queryId = getQueryIdFromPath ( path )
81+ let queryId = getQueryIdFromPath ( path , event )
7382
7483 if ( queryId ) {
7584 let pathSplitted = path . split ( '#' )
@@ -82,29 +91,26 @@ export const watchEvent = (firebase, dispatch, event, path, dest, onlyLastEvent
8291 const counter = getWatcherCount ( firebase , event , watchPath , queryId )
8392
8493 if ( counter > 0 ) {
85- if ( onlyLastEvent ) {
86- // listen only to last query on same path
8794 if ( queryId ) {
88- unsetWatcher ( firebase , event , path , queryId )
95+ unsetWatcher ( firebase , dispatch , event , path , queryId , false )
8996 } else {
90- return
97+ return
9198 }
92- }
9399 }
94100
95101 setWatcher ( firebase , event , watchPath , queryId )
96102
97- if ( event === 'first_child' ) {
98- // return
99- return firebase . database ( ) . ref ( ) . child ( path ) . orderByKey ( ) . limitToFirst ( 1 ) . once ( 'value' , snapshot => {
100- if ( snapshot . val ( ) === null ) {
101- dispatch ( {
102- type : NO_VALUE ,
103- path
104- } )
105- }
106- } )
107- }
103+ // if (event === 'first_child') {
104+ // // return
105+ // return firebase.database().ref().child(path).orderByKey().limitToFirst(1).once('value', snapshot => {
106+ // if (snapshot.val() === null) {
107+ // dispatch({
108+ // type: NO_VALUE,
109+ // path
110+ // })
111+ // }
112+ // })
113+ // }
108114
109115 let query = firebase . database ( ) . ref ( ) . child ( path )
110116
@@ -162,9 +168,44 @@ export const watchEvent = (firebase, dispatch, event, path, dest, onlyLastEvent
162168 }
163169
164170 const runQuery = ( q , e , p ) => {
171+ dispatch ( {
172+ type : START ,
173+ timestamp : Date . now ( ) ,
174+ requesting : true ,
175+ requested : false ,
176+ path
177+ } )
178+
179+ if ( e === 'once' ) {
180+ return q . once ( 'value' )
181+ . then ( snapshot => {
182+ if ( snapshot . val ( ) !== null ) {
183+ let data = {
184+ _id : snapshot . key ,
185+ val : snapshot . val ( )
186+ }
187+ let resultPath = dest || ( e === 'value' ) ? p : p + '/' + snapshot . key
188+ let rootPath = dest || path
189+
190+ dispatch ( {
191+ type : SET ,
192+ path : resultPath ,
193+ rootPath,
194+ data,
195+ snapshot,
196+ timestamp : Date . now ( ) ,
197+ requesting : false ,
198+ requested : true
199+ } )
200+ }
201+ return snapshot
202+ } )
203+ }
204+
165205 q . on ( e , snapshot => {
166206 let data = ( e === 'child_removed' ) ? undefined : snapshot . val ( )
167207 const resultPath = dest || ( e === 'value' ) ? p : p + '/' + snapshot . key
208+ const rootPath = dest || path
168209 if ( dest && e !== 'child_removed' ) {
169210 data = {
170211 _id : snapshot . key ,
@@ -174,23 +215,29 @@ export const watchEvent = (firebase, dispatch, event, path, dest, onlyLastEvent
174215 dispatch ( {
175216 type : SET ,
176217 path : resultPath ,
218+ rootPath,
177219 data,
178- snapshot
220+ snapshot,
221+ timestamp : Date . now ( ) ,
222+ requesting : false ,
223+ requested : true
179224 } )
180225 } )
181226 }
182227
183228 runQuery ( query , event , path )
184229}
185230
186- export const unWatchEvent = ( firebase , event , path , queryId = undefined ) =>
187- unsetWatcher ( firebase , event , path , queryId )
231+ export const unWatchEvent = ( firebase , dispatch , event , path , isCleanState = true ) => {
232+ let queryId = getQueryIdFromPath ( path , event )
233+ unsetWatcher ( firebase , dispatch , event , path , queryId , isCleanState )
234+ }
188235
189236export const watchEvents = ( firebase , dispatch , events ) =>
190237 events . forEach ( event => watchEvent ( firebase , dispatch , event . name , event . path ) )
191238
192- export const unWatchEvents = ( firebase , events ) =>
193- events . forEach ( event => unWatchEvent ( firebase , event . name , event . path ) )
239+ export const unWatchEvents = ( firebase , dispatch , events , isCleanState = true ) =>
240+ events . forEach ( event => unWatchEvent ( firebase , dispatch , event . name , event . path , isCleanState ) )
194241
195242const dispatchLoginError = ( dispatch , authError ) =>
196243 dispatch ( {
@@ -255,6 +302,11 @@ export const init = (dispatch, firebase) => {
255302 } )
256303
257304 firebase . auth ( ) . currentUser
305+
306+ // Run onAuthStateChanged if it exists in config
307+ if ( firebase . _ . config . onAuthStateChanged ) {
308+ firebase . _ . config . onAuthStateChanged ( authData , firebase )
309+ }
258310}
259311
260312export const logout = ( dispatch , firebase , preserve = [ ] , remove = [ ] ) => {
0 commit comments