Skip to content

Commit b0b8e17

Browse files
authored
Merge pull request #40 from RahavLussato/master
bunch of changes
2 parents 64d3d85 + 45584b6 commit b0b8e17

File tree

7 files changed

+279
-119
lines changed

7 files changed

+279
-119
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"react"
3030
],
3131
"dependencies": {
32-
"es6-promise": "^3.2.1"
32+
"es6-promise": "^3.2.1",
33+
"lodash": "^4.17.2"
3334
},
3435
"peerDependencies": {
3536
"firebase": "^3.1.0",

source/actions.js

Lines changed: 93 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1113
import { Promise } from 'es6-promise'
1214

1315
const getWatchPath = (event, path) => event + ':' + ((path.substring(0, 1) === '/') ? '' : '/') + path
1416

1517
const 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

2731
const 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

189236
export 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

195242
const 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

260312
export const logout = (dispatch, firebase, preserve = [], remove = []) => {

source/compose.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export default (config) => {
3535
const push = (path, value, onComplete) => ref.child(path).push(value, onComplete)
3636
const remove = (path, onComplete) => ref.child(path).remove(onComplete)
3737
const update = (path, value, onComplete) => ref.child(path).update(value, onComplete)
38-
const watchEvent = (eventName, eventPath) => Actions.watchEvent(firebase, dispatch, eventName, eventPath, true)
39-
const unWatchEvent = (eventName, eventPath, queryId = undefined) => Actions.unWatchEvent(firebase, eventName, eventPath, queryId)
38+
const watchEvent = (eventName, eventPath) => Actions.watchEvent(firebase, dispatch, eventName, eventPath)
39+
const unWatchEvent = (eventName, eventPath, isCleanState=true) => Actions.unWatchEvent(firebase, dispatch, eventName, eventPath, isCleanState)
4040
const login = credentials => Actions.login(dispatch, firebase, credentials)
4141
const logout = (preserve = [], remove = []) => Actions.logout(dispatch, firebase, preserve, remove)
4242
const createUser = (credentials, profile) => Actions.createUser(dispatch, firebase, credentials, profile)

source/connect.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {Component, PropTypes} from 'react'
22
import {watchEvents, unWatchEvents} from './actions'
3+
import { isEqual } from 'lodash'
34

45
const defaultEvent = {
56
path: '',
@@ -8,12 +9,6 @@ const defaultEvent = {
89

910
const fixPath = (path) => ((path.substring(0,1) == '/') ? '': '/') + path
1011

11-
const isEqualArrays = (a, b) => a.length == b.length && a.every((v,i) => {
12-
const v1 = typeof v === 'string' ? v : v[0];
13-
const v2 = typeof b[i] === 'string' ? b[i] : b[i][0];
14-
return v1 === v2;
15-
})
16-
1712
const ensureCallable = maybeFn =>
1813
typeof maybeFn === 'function' ? maybeFn : _ => maybeFn
1914

@@ -25,9 +20,12 @@ const createEvents = ({type, path}) => {
2520
case 'value':
2621
return [{name: 'value', path}]
2722

23+
case 'once':
24+
return [{name: 'once', path}]
25+
2826
case 'all':
2927
return [
30-
{name: 'first_child', path},
28+
//{name: 'first_child', path},
3129
{name: 'child_added', path},
3230
{name: 'child_removed', path},
3331
{name: 'child_moved', path},
@@ -56,6 +54,9 @@ const getEventsFromDefinition = def => flatMap(def.map(path => {
5654
case 'value':
5755
return createEvents(transformEvent({ path: path.path }))
5856

57+
case 'once':
58+
return createEvents(transformEvent({ type: 'once', path: path.path }))
59+
5960
case 'array':
6061
return createEvents(transformEvent({ type: 'all', path: path.path }))
6162
}
@@ -84,8 +85,8 @@ export default (dataOrFn = []) => WrappedComponent => {
8485
const linkFn = ensureCallable(dataOrFn)
8586
this._pathsToListen = linkFn(this.props, firebase)
8687

87-
const {ref, helpers} = firebase
88-
this.firebase = {ref, ...helpers}
88+
const {ref, helpers, storage, database, auth} = firebase
89+
this.firebase = {ref, storage, database, auth, ...helpers}
8990

9091
this._firebaseEvents = getEventsFromDefinition(this._pathsToListen)
9192
watchEvents(firebase, dispatch, this._firebaseEvents)
@@ -97,21 +98,19 @@ export default (dataOrFn = []) => WrappedComponent => {
9798
const linkFn = ensureCallable(dataOrFn)
9899
const newPathsToListen = linkFn(nextProps, firebase)
99100

100-
if (isEqualArrays(newPathsToListen, this._pathsToListen)) {
101-
return;
102-
}
103-
104-
this._pathsToListen = newPathsToListen;
101+
if (!isEqual(newPathsToListen, this._pathsToListen)) {
102+
this._pathsToListen = newPathsToListen;
105103

106-
unWatchEvents(firebase, this._firebaseEvents)
104+
unWatchEvents(firebase, dispatch, this._firebaseEvents, nextProps.isNeedToClean!=undefined ? nextProps.isNeedToClean : false);
107105

108-
this._firebaseEvents = getEventsFromDefinition(this._pathsToListen)
109-
watchEvents(firebase, dispatch, this._firebaseEvents)
106+
this._firebaseEvents = getEventsFromDefinition(this._pathsToListen)
107+
watchEvents(firebase, dispatch, this._firebaseEvents)
108+
}
110109
}
111110

112111
componentWillUnmount () {
113-
const {firebase} = this.context.store
114-
unWatchEvents(firebase, this._firebaseEvents)
112+
const {firebase, dispatch} = this.context.store
113+
unWatchEvents(firebase, dispatch, this._firebaseEvents, true)
115114
}
116115

117116
render () {

source/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ module.exports = {
77
LOGIN: `${prefix}LOGIN`,
88
LOGOUT: `${prefix}LOGOUT`,
99
LOGIN_ERROR: `${prefix}LOGIN_ERROR`,
10-
NO_VALUE: `${prefix}NO_VALUE`
10+
// NO_VALUE: `${prefix}NO_VALUE`,
11+
START: `${prefix}START`,
12+
INIT_BY_PATH: `${prefix}INIT_BY_PATH`
1113
}

0 commit comments

Comments
 (0)