Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/createFirebaseInstance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isObject } from 'lodash'
import { authActions, queryActions, storageActions } from './actions'
import { getEventsFromInput, createCallable } from './utils'

/**
* Create a firebase instance that has helpers attached for dispatching actions
Expand Down Expand Up @@ -239,6 +240,27 @@ export const createFirebaseInstance = (firebase, configs, dispatch) => {
const deleteFile = (path, dbPath) =>
storageActions.deleteFile(dispatch, firebase, { path, dbPath })

/**
* @description firebaseWatch. Similar to the firebaseConnect Higher Order
* Component but presented as a function. Useful for populating your redux
* state without React, e.g., for service side rendering.
* @param {Array} watchArray - Array of objects or strings for paths to sync
* from Firebase. Can also be a function that returns the array. The function
* is passed the props object specified as the next parameter.
* @param {Object} props - The props object that you would like to pass to
* your watchArray generating function.
* @return {Promise}
*/
const firebaseWatch = (watchArray, props) => {
const inputAsFunc = createCallable(watchArray)
const prevData = inputAsFunc(props, firebase)
const firebaseEvents = getEventsFromInput(prevData)
const promises = firebaseEvents.map(event =>
queryActions.watchEvent(firebase, dispatch, event))

return Promise.all(promises)
}

/**
* @description Watch event. **Note:** this method is used internally
* so examples have not yet been created, and it may not work as expected.
Expand Down Expand Up @@ -407,7 +429,8 @@ export const createFirebaseInstance = (firebase, configs, dispatch) => {
watchEvent,
unWatchEvent,
reloadAuth,
linkWithCredential
linkWithCredential,
firebaseWatch
}

return Object.assign(firebase, helpers, { helpers })
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/enhancer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,17 @@ describe('Compose', () => {
})
})

describe('firebaseWatch', () => {
it('starts firebaseWatch', async () => {
await store.firebase.firebaseWatch(['test'])
expect(store.firebase.ref('test')).to.be.an.object
})
})

describe('watchEvent', () => {
it('starts watcher', () => {
// TODO: Confirm that watcher count is updated and watcher is set
store.firebase.watchEvent('value', 'test')
it('starts watcher', async () => {
await store.firebase.watchEvent('value', 'test')
expect(store.firebase.ref('test')).to.be.an.object
})
})

Expand Down