Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeeinlondon committed Apr 4, 2017
1 parent 30e79da commit 83c000e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
10 changes: 6 additions & 4 deletions addon/services/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ function onAuthStateChanged(context) {
Ember.set(context, 'isAuthenticated', user ? true : false);
Ember.set(context, 'currentUser', user);
const userProfile = context._currentUserProfile;
const ac = (dispatch, firebase, cb = null) => (snap) => {
const ac = (dispatch, firebase, options= {}) => (snap) => {
dispatch({
type: 'USER_PROFILE_UPDATED',
key: snap.key,
data: snap.val()
});
if (cb) {
cb(snap);
if (options.cb) {
options.cb(snap);
}
};
if (user) {
context.watch().node(`${userProfile.path}/${user.uid}`, ac(dispatch, context, userProfile.cb));
context.watch().node(`${userProfile.path}/${user.uid}`, ac(dispatch, context, {
cb: userProfile.cb
}));
}
},
(e) => dispatch({type: 'ERROR_DISPATCHING', message: e.message}));
Expand Down
32 changes: 16 additions & 16 deletions addon/services/firebase/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ const nodeWatcher = (dispatch, actionCreator) => function nodeWatcher(snap) {
}
};

const addWatcher = (dispatch, actionCreator, cb = null) => function addWatcher(snap, prevKey) {
const payload = {
const addWatcher = (dispatch, actionCreator, options = {}) => function addWatcher(snap, prevKey) {
const payload = Ember.assign(options, {
type: `${actionCreator}_ADDED`,
operation: 'added',
key: snap.key,
data: snap.val(),
prevKey
};
if (cb) {
cb(dispatch, payload);
});
if (options.cb) {
options.cb(dispatch, payload);
}

if (typeof actionCreator === 'string') {
Expand All @@ -41,15 +41,15 @@ const addWatcher = (dispatch, actionCreator, cb = null) => function addWatcher(s
}
};

const listWatcher = (operation, dispatch, actionCreator, cb = null) => function listWatcher(snap) {
const payload = {
const listWatcher = (operation, dispatch, actionCreator, options = {}) => function listWatcher(snap) {
const payload = Ember.assign(options, {
type: `${actionCreator}_${operation.toUpperCase()}`,
operation,
key: snap.key,
data: snap.val(),
};
if (cb) {
cb(dispatch, payload);
});
if (options.cb) {
options.cb(dispatch, Ember.assign(options, payload));
}

if (typeof actionCreator === 'string') {
Expand Down Expand Up @@ -89,34 +89,34 @@ const watch = (context) => {
*
* @param {string} path the path reference to state in Firebase
* @param {mixed} actionCreator either a string "type" for the action or a action-creator function
* @param {Object} options query modifiers to the path can optionally be added
* @param {Object} options query modifiers to the path can optionally be added; also can pass additional name/value pairs that will be sent at "event time"
*/
node(path, actionCreator, options = {}) {
let reference = addOptionsToReference(context.ref(path), options);

reference.on('value', nodeWatcher(dispatch, actionCreator, options.callback));

const watcher = { path, event: 'value', fn: nodeWatcher(dispatch, actionCreator, options.callback) };
dispatch({type: '@firebase/WATCHER_ADD', watcher});
dispatch({type: '@firebase/WATCHER_ADD', watcher, options});
context.addWatcher(watcher);
},

list(path, actionCreator, options = {}) {
let reference = addOptionsToReference(context.ref(path), options);

let fn = listWatcher('added', dispatch, actionCreator);
let fn = listWatcher('added', dispatch, actionCreator, options);
let eventContext = reference.on('child_added', fn);
let watcher = {path, event: 'child_added', fn, eventContext};
context.addWatcher(watcher);
dispatch({type: '@firebase/WATCHER_ADD', watcher});
dispatch({type: '@firebase/WATCHER_ADD', watcher, options});

fn = listWatcher('removed', dispatch, actionCreator);
fn = listWatcher('removed', dispatch, actionCreator, options);
eventContext = reference.on('child_removed', fn);
watcher = {path, event: 'child_removed', fn, eventContext};
context.addWatcher(watcher);
dispatch({type: '@firebase/WATCHER_ADD', watcher});

fn = listWatcher('changed', dispatch, actionCreator);
fn = listWatcher('changed', dispatch, actionCreator, options);
eventContext = reference.on('child_changed', fn);
watcher = {path, event: 'child_changed', fn, eventContext};
context.addWatcher(watcher);
Expand Down

0 comments on commit 83c000e

Please sign in to comment.