firebase-saga
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -10,2 +10,3 @@ 'use strict'; | ||
exports.get = get; | ||
exports.getAll = getAll; | ||
exports.create = create; | ||
@@ -17,12 +18,16 @@ exports.update = update; | ||
var _marked = [get, create, update, runSync, sync].map(regeneratorRuntime.mark); | ||
var _effects = require('redux-saga/effects'); | ||
var _marked = [get, getAll, create, update, runSync, sync].map(regeneratorRuntime.mark); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function newOps() { | ||
var EVENT_TYPES = ['child_added', 'child_removed']; | ||
function newOpts() { | ||
var name = arguments.length <= 0 || arguments[0] === undefined ? 'data' : arguments[0]; | ||
var ops = {}; | ||
var opts = {}; | ||
var chan = (0, _reduxSaga.eventChannel)(function (emit) { | ||
ops.handler = function (obj) { | ||
opts.handler = function (obj) { | ||
emit(_defineProperty({}, name, obj)); | ||
@@ -32,4 +37,4 @@ }; | ||
}); | ||
chan.handler = ops.handler; | ||
return ch; | ||
chan.handler = opts.handler; | ||
return chan; | ||
} | ||
@@ -42,3 +47,3 @@ | ||
function get(path, key) { | ||
var ops, ref, data; | ||
var ref, data; | ||
return regeneratorRuntime.wrap(function get$(_context) { | ||
@@ -48,12 +53,11 @@ while (1) { | ||
case 0: | ||
ops = newOps('error'); | ||
ref = firebase.database().ref(path + '/' + key); | ||
_context.next = 4; | ||
return (0, _reduxSaga.call)([ref, ref.once], 'value'); | ||
_context.next = 3; | ||
return (0, _effects.call)([ref, ref.once], 'value'); | ||
case 4: | ||
case 3: | ||
data = _context.sent; | ||
return _context.abrupt('return', data.val()); | ||
case 6: | ||
case 5: | ||
case 'end': | ||
@@ -66,106 +70,128 @@ return _context.stop(); | ||
function getAll(path) { | ||
var ref, data; | ||
return regeneratorRuntime.wrap(function getAll$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
ref = firebase.database().ref(path); | ||
_context2.next = 3; | ||
return (0, _effects.call)([ref, ref.once], 'value'); | ||
case 3: | ||
data = _context2.sent; | ||
return _context2.abrupt('return', data.val()); | ||
case 5: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked[1], this); | ||
} | ||
function create(path, fn) { | ||
var key, payload, ops, ref, _ref, _ref2, _, error; | ||
var key, payload, opts, ref, _ref, _ref2, _, error; | ||
return regeneratorRuntime.wrap(function create$(_context2) { | ||
return regeneratorRuntime.wrap(function create$(_context3) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _reduxSaga.call)(newKey, path); | ||
_context3.next = 2; | ||
return (0, _effects.call)(newKey, path); | ||
case 2: | ||
key = _context2.sent; | ||
_context2.next = 5; | ||
return (0, _reduxSaga.call)(fn, key); | ||
key = _context3.sent; | ||
_context3.next = 5; | ||
return (0, _effects.call)(fn, key); | ||
case 5: | ||
payload = _context2.sent; | ||
ops = newOps('error'); | ||
payload = _context3.sent; | ||
opts = newOpts('error'); | ||
ref = firebase.database().ref(); | ||
_context2.next = 10; | ||
return [(0, _reduxSaga.call)([ref, ref.update], payload, ops.handler), (0, _reduxSaga.take)(ops)]; | ||
_context3.next = 10; | ||
return [(0, _effects.call)([ref, ref.update], payload, opts.handler), (0, _effects.take)(opts)]; | ||
case 10: | ||
_ref = _context2.sent; | ||
_ref = _context3.sent; | ||
_ref2 = _slicedToArray(_ref, 2); | ||
_ = _ref2[0]; | ||
error = _ref2[1].error; | ||
return _context2.abrupt('return', error); | ||
return _context3.abrupt('return', error); | ||
case 15: | ||
case 'end': | ||
return _context2.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _marked[1], this); | ||
}, _marked[2], this); | ||
} | ||
function update(path, key, payload) { | ||
var ops, ref, _ref3, _ref4, _, error; | ||
var opts, ref, _ref3, _ref4, _, error; | ||
return regeneratorRuntime.wrap(function update$(_context3) { | ||
return regeneratorRuntime.wrap(function update$(_context4) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (!(typeof payload === 'function')) { | ||
_context3.next = 4; | ||
_context4.next = 4; | ||
break; | ||
} | ||
_context3.next = 3; | ||
return (0, _reduxSaga.call)(payload); | ||
_context4.next = 3; | ||
return (0, _effects.call)(payload); | ||
case 3: | ||
payload = _context3.sent; | ||
payload = _context4.sent; | ||
case 4: | ||
ops = newOps('error'); | ||
opts = newOpts('error'); | ||
ref = firebase.database().ref(path + '/' + key); | ||
_context3.next = 8; | ||
return [(0, _reduxSaga.call)([ref, ref.update], payload, ops.handler), (0, _reduxSaga.take)(ops)]; | ||
_context4.next = 8; | ||
return [(0, _effects.call)([ref, ref.update], payload, opts.handler), (0, _effects.take)(opts)]; | ||
case 8: | ||
_ref3 = _context3.sent; | ||
_ref3 = _context4.sent; | ||
_ref4 = _slicedToArray(_ref3, 2); | ||
_ = _ref4[0]; | ||
error = _ref4[1].error; | ||
return _context3.abrupt('return', error); | ||
return _context4.abrupt('return', error); | ||
case 13: | ||
case 'end': | ||
return _context3.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _marked[2], this); | ||
}, _marked[3], this); | ||
} | ||
function runSync(ref, eventType, creator) { | ||
var ops, _ref5, data; | ||
var opts, _ref5, data; | ||
return regeneratorRuntime.wrap(function runSync$(_context4) { | ||
return regeneratorRuntime.wrap(function runSync$(_context5) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
ops = newOps(); | ||
_context4.next = 3; | ||
return (0, _reduxSaga.call)([ref, ref.on], eventType, ops.handler); | ||
opts = newOpts(); | ||
_context5.next = 3; | ||
return (0, _effects.call)([ref, ref.on], eventType, opts.handler); | ||
case 3: | ||
if (!true) { | ||
_context4.next = 12; | ||
_context5.next = 12; | ||
break; | ||
} | ||
_context4.next = 6; | ||
return (0, _reduxSaga.take)(ops); | ||
_context5.next = 6; | ||
return (0, _effects.take)(opts); | ||
case 6: | ||
_ref5 = _context4.sent; | ||
_ref5 = _context5.sent; | ||
data = _ref5.data; | ||
_context4.next = 10; | ||
return (0, _reduxSaga.put)(creator({ data: data })); | ||
_context5.next = 10; | ||
return (0, _effects.put)(creator({ data: data })); | ||
case 10: | ||
_context4.next = 3; | ||
_context5.next = 3; | ||
break; | ||
@@ -175,23 +201,23 @@ | ||
case 'end': | ||
return _context4.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _marked[3], this); | ||
}, _marked[4], this); | ||
} | ||
var EVENT_TYPES = ['child_added', 'child_removed']; | ||
function sync(path) { | ||
var mapEventToAction = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var limit = arguments.length <= 2 || arguments[2] === undefined ? 20 : arguments[2]; | ||
var ref, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, type, action; | ||
return regeneratorRuntime.wrap(function sync$(_context5) { | ||
return regeneratorRuntime.wrap(function sync$(_context6) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
ref = firebase.database().ref(path).limitToLast(20); | ||
ref = firebase.database().ref(path).limitToLast(limit); | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context5.prev = 4; | ||
_context6.prev = 4; | ||
_iterator = EVENT_TYPES[Symbol.iterator](); | ||
@@ -201,3 +227,3 @@ | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context5.next = 15; | ||
_context6.next = 15; | ||
break; | ||
@@ -210,27 +236,27 @@ } | ||
if (!(typeof action === 'function')) { | ||
_context5.next = 12; | ||
_context6.next = 12; | ||
break; | ||
} | ||
_context5.next = 12; | ||
return (0, _reduxSaga.fork)(runSync, ref, type, action); | ||
_context6.next = 12; | ||
return (0, _effects.fork)(runSync, ref, type, action); | ||
case 12: | ||
_iteratorNormalCompletion = true; | ||
_context5.next = 6; | ||
_context6.next = 6; | ||
break; | ||
case 15: | ||
_context5.next = 21; | ||
_context6.next = 21; | ||
break; | ||
case 17: | ||
_context5.prev = 17; | ||
_context5.t0 = _context5['catch'](4); | ||
_context6.prev = 17; | ||
_context6.t0 = _context6['catch'](4); | ||
_didIteratorError = true; | ||
_iteratorError = _context5.t0; | ||
_iteratorError = _context6.t0; | ||
case 21: | ||
_context5.prev = 21; | ||
_context5.prev = 22; | ||
_context6.prev = 21; | ||
_context6.prev = 22; | ||
@@ -242,6 +268,6 @@ if (!_iteratorNormalCompletion && _iterator.return) { | ||
case 24: | ||
_context5.prev = 24; | ||
_context6.prev = 24; | ||
if (!_didIteratorError) { | ||
_context5.next = 27; | ||
_context6.next = 27; | ||
break; | ||
@@ -253,13 +279,13 @@ } | ||
case 27: | ||
return _context5.finish(24); | ||
return _context6.finish(24); | ||
case 28: | ||
return _context5.finish(21); | ||
return _context6.finish(21); | ||
case 29: | ||
case 'end': | ||
return _context5.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _marked[4], this, [[4, 17, 21, 29], [22,, 24, 28]]); | ||
}, _marked[5], this, [[4, 17, 21, 29], [22,, 24, 28]]); | ||
} |
{ | ||
"name": "firebase-saga", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "A library for connecting Redux saga middleware to Firebase.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,7 +0,10 @@ | ||
import { put, fork, call, take, eventChannel } from 'redux-saga'; | ||
import { eventChannel } from 'redux-saga'; | ||
import { put, fork, call, take } from 'redux-saga/effects'; | ||
function newOps(name = 'data') { | ||
const ops = {}; | ||
const EVENT_TYPES = ['child_added', 'child_removed']; | ||
function newOpts(name = 'data') { | ||
const opts = {}; | ||
const chan = eventChannel(emit => { | ||
ops.handler = obj => { | ||
opts.handler = obj => { | ||
emit({ [name]: obj }); | ||
@@ -11,4 +14,4 @@ }; | ||
}); | ||
chan.handler = ops.handler; | ||
return ch; | ||
chan.handler = opts.handler; | ||
return chan; | ||
} | ||
@@ -21,17 +24,23 @@ | ||
export function* get(path, key) { | ||
const ops = newOps('error'); | ||
const ref = firebase.database().ref(`${path}/${key}`); | ||
const data = yield call([ref, ref.once], 'value'); | ||
return data.val(); | ||
} | ||
export function* getAll(path) { | ||
const ref = firebase.database().ref(path); | ||
const data = yield call([ref, ref.once], 'value'); | ||
return data.val(); | ||
} | ||
export function* create(path, fn) { | ||
const key = yield call(newKey, path); | ||
const payload = yield call(fn, key); | ||
const ops = newOps('error'); | ||
const opts = newOpts('error'); | ||
const ref = firebase.database().ref(); | ||
const [ _, { error } ] = yield [ | ||
call([ref, ref.update], payload, ops.handler), | ||
take(ops) | ||
call([ref, ref.update], payload, opts.handler), | ||
take(opts) | ||
]; | ||
@@ -45,7 +54,7 @@ return error; | ||
} | ||
const ops = newOps('error'); | ||
const opts = newOpts('error'); | ||
const ref = firebase.database().ref(`${path}/${key}`); | ||
const [ _, { error } ] = yield [ | ||
call([ref, ref.update], payload, ops.handler), | ||
take(ops) | ||
call([ref, ref.update], payload, opts.handler), | ||
take(opts) | ||
]; | ||
@@ -56,7 +65,7 @@ return error; | ||
function* runSync(ref, eventType, creator) { | ||
const ops = newOps(); | ||
yield call([ref, ref.on], eventType, ops.handler); | ||
const opts = newOpts(); | ||
yield call([ref, ref.on], eventType, opts.handler); | ||
while (true) { | ||
const { data } = yield take(ops); | ||
const { data } = yield take(opts); | ||
yield put(creator({ data })); | ||
@@ -66,5 +75,5 @@ } | ||
const EVENT_TYPES = ['child_added', 'child_removed']; | ||
export function* sync(path, mapEventToAction = {}) { | ||
const ref = firebase.database().ref(path).limitToLast(20); | ||
export function* sync(path, mapEventToAction = {}, limit = 20) { | ||
const ref = firebase.database().ref(path).limitToLast(limit); | ||
for (let type of EVENT_TYPES) { | ||
@@ -71,0 +80,0 @@ const action = mapEventToAction[type]; |
Sorry, the diff of this file is too big to display
85950
2368