Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 Connect. Similar to the firebaseConnect Higher Order
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work with the comments here.

* 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 connect = (watchArray, props) => {
const inputAsFunc = createCallable(watchArray)
const prevData = inputAsFunc(props, firebase)
const firebaseEvents = getEventsFromInput(prevData)
const promises = firebaseEvents.map(event =>
Copy link
Owner

@prescottprue prescottprue Oct 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda a nitpick: We should be able to use watchEvents and just pass the listener event settings array right into it instead of needing to call the map here.

Only reason I care is that I followed the same pattern when building the new redux-firestore stuff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I had initially but watchEvents doesn't return a promise, which is important for SSR as you have to wait for the data to be fetched before rendering. I could update watchEvents if you want.

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,
connect
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a different name for this export since react-redux uses connect and people commonly integrate with that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, happy to change it. How about firebaseConnectHelper or connectToEvents (to match watchEvents)?

}

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('connect', () => {
it('starts connect', async () => {
await store.firebase.connect(['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