@graffy/fill
Advanced tools
Comparing version 0.14.7-alpha.9 to 0.14.7-alpha.10
114
index.js
@@ -8,8 +8,4 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = fill; | ||
exports.default = fill; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs3/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator")); | ||
var _common = require("@graffy/common"); | ||
@@ -19,94 +15,32 @@ | ||
var MAX_RECURSIONS = 10; | ||
const MAX_RECURSIONS = 10; | ||
function fill(_) { | ||
return function (store) { | ||
store.on('read', [], /*#__PURE__*/function () { | ||
var _fillOnRead = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(query, options, next) { | ||
var value, budget, _slice, known, unknown, res; | ||
return store => { | ||
store.on('read', [], async function fillOnRead(query, options, next) { | ||
let value = await next(query); | ||
if (options.skipFill) return value; | ||
if (!value || !value.length) return null; | ||
let budget = MAX_RECURSIONS; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return next(query); | ||
while (budget-- > 1) { | ||
const { | ||
known, | ||
unknown | ||
} = (0, _sliceInstanceProperty(_common))(value, query); | ||
value = known; | ||
if (!unknown) break; | ||
const res = await store.call('read', unknown, { | ||
skipFill: true | ||
}); | ||
(0, _common.merge)(value, res); | ||
} | ||
case 2: | ||
value = _context.sent; | ||
if (!budget) throw new Error('fill.max_recursion'); // console.log('Read', debug(query), 'returned', debug(value)); | ||
if (!options.skipFill) { | ||
_context.next = 5; | ||
break; | ||
} | ||
return _context.abrupt("return", value); | ||
case 5: | ||
if (!(!value || !value.length)) { | ||
_context.next = 7; | ||
break; | ||
} | ||
return _context.abrupt("return", null); | ||
case 7: | ||
budget = MAX_RECURSIONS; | ||
case 8: | ||
if (!(budget-- > 1)) { | ||
_context.next = 19; | ||
break; | ||
} | ||
_slice = (0, _sliceInstanceProperty(_common))(value, query), known = _slice.known, unknown = _slice.unknown; | ||
value = known; | ||
if (unknown) { | ||
_context.next = 13; | ||
break; | ||
} | ||
return _context.abrupt("break", 19); | ||
case 13: | ||
_context.next = 15; | ||
return store.call('read', unknown, { | ||
skipFill: true | ||
}); | ||
case 15: | ||
res = _context.sent; | ||
(0, _common.merge)(value, res); | ||
_context.next = 8; | ||
break; | ||
case 19: | ||
if (budget) { | ||
_context.next = 21; | ||
break; | ||
} | ||
throw new Error('fill.max_recursion'); | ||
case 21: | ||
return _context.abrupt("return", value); | ||
case 22: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
function fillOnRead(_x, _x2, _x3) { | ||
return _fillOnRead.apply(this, arguments); | ||
} | ||
return fillOnRead; | ||
}()); | ||
return value; | ||
}); | ||
store.on('watch', [], function fillOnWatch(query, options, next) { | ||
if (options.skipFill) return next(query); | ||
return (0, _subscribe["default"])(store, query, options); | ||
return (0, _subscribe.default)(store, query, options); | ||
}); | ||
@@ -113,0 +47,0 @@ }; |
@@ -5,6 +5,11 @@ { | ||
"author": "aravind (https://github.com/aravindet)", | ||
"version": "0.14.7-alpha.9", | ||
"main": "index.js", | ||
"source": "src/index.js", | ||
"esnext": "src/index.js", | ||
"version": "0.14.7-alpha.10", | ||
"main": "./index.js", | ||
"exports": { | ||
"import": "./src/index.js", | ||
"require": "./index.js" | ||
}, | ||
"module": "./src/index.js", | ||
"source": "./src/index.js", | ||
"esnext": "./src/index.js", | ||
"repository": { | ||
@@ -17,5 +22,5 @@ "type": "git", | ||
"@babel/runtime-corejs3": "^7.9.2", | ||
"@graffy/common": "0.14.7-alpha.9", | ||
"@graffy/stream": "0.14.7-alpha.9" | ||
"@graffy/common": "0.14.7-alpha.10", | ||
"@graffy/stream": "0.14.7-alpha.10" | ||
} | ||
} |
248
subscribe.js
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); | ||
var _sliceInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/slice"); | ||
exports.__esModule = true; | ||
exports["default"] = subscribe; | ||
exports.default = subscribe; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs3/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator")); | ||
var _asyncIterator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncIterator")); | ||
var _common = require("@graffy/common"); | ||
@@ -20,16 +12,14 @@ | ||
function subscribe(store, originalQuery, _ref) { | ||
var raw = _ref.raw; | ||
function subscribe(store, originalQuery, { | ||
raw | ||
}) { | ||
const empty = () => (0, _common.finalize)([], originalQuery, 0); | ||
var empty = function empty() { | ||
return (0, _common.finalize)([], originalQuery, 0); | ||
}; | ||
var push, end; | ||
var upstream; | ||
var query = []; | ||
var data = empty(); | ||
var payload = []; | ||
var stream = (0, _stream.makeStream)(function (streamPush, streamEnd) { | ||
push = function push(v) { | ||
let push, end; | ||
let upstream; | ||
let query = []; | ||
let data = empty(); | ||
let payload = []; | ||
const stream = (0, _stream.makeStream)((streamPush, streamEnd) => { | ||
push = v => { | ||
// console.log('Push', debug(v)); | ||
@@ -45,175 +35,45 @@ streamPush(v); | ||
function resubscribe(_x) { | ||
return _resubscribe.apply(this, arguments); | ||
} | ||
async function resubscribe(unknown) { | ||
try { | ||
const changed = (0, _common.add)(query, unknown); // console.log('Resubscribe', changed, debug(unknown)); | ||
function _resubscribe() { | ||
_resubscribe = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(unknown) { | ||
var changed, _yield$upstream$next, value; | ||
if (!changed) return; | ||
if (upstream) upstream.return(); // Close the existing stream. | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
changed = (0, _common.add)(query, unknown); // console.log('Resubscribe', changed, debug(unknown)); | ||
upstream = store.call('watch', query, { | ||
skipFill: true | ||
}); | ||
let { | ||
value | ||
} = await upstream.next(); // console.log('Got first subscription value', debug(value)); | ||
if (changed) { | ||
_context.next = 4; | ||
break; | ||
} | ||
if (typeof value === 'undefined') { | ||
// The upstream is a change subscription, not a live query, | ||
// so we need to fetch the initial value. | ||
// TODO: Get a version corresponding to the subscription's start | ||
// and verify that the store.read response is newer. | ||
value = await store.call('read', unknown, { | ||
skipCache: true | ||
}); | ||
} | ||
return _context.abrupt("return"); | ||
value = value && (0, _sliceInstanceProperty(_common))(value, unknown).known; | ||
putValue(value, false); | ||
} catch (e) { | ||
error(e); | ||
} | ||
case 4: | ||
if (upstream) upstream["return"](); // Close the existing stream. | ||
upstream = store.call('watch', query, { | ||
skipFill: true | ||
}); | ||
_context.next = 8; | ||
return upstream.next(); | ||
case 8: | ||
_yield$upstream$next = _context.sent; | ||
value = _yield$upstream$next.value; | ||
if (!(typeof value === 'undefined')) { | ||
_context.next = 14; | ||
break; | ||
} | ||
_context.next = 13; | ||
return store.call('read', unknown, { | ||
skipCache: true | ||
}); | ||
case 13: | ||
value = _context.sent; | ||
case 14: | ||
value = value && (0, _sliceInstanceProperty(_common))(value, unknown).known; | ||
putValue(value, false); | ||
_context.next = 21; | ||
break; | ||
case 18: | ||
_context.prev = 18; | ||
_context.t0 = _context["catch"](0); | ||
error(_context.t0); | ||
case 21: | ||
putStream(upstream); | ||
case 22: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, null, [[0, 18]]); | ||
})); | ||
return _resubscribe.apply(this, arguments); | ||
putStream(upstream); | ||
} | ||
function putStream(_x2) { | ||
return _putStream.apply(this, arguments); | ||
async function putStream(stream) { | ||
// TODO: Backpressure: pause pulling if downstream listener is saturated. | ||
// console.log('Before sinking stream', debug(data)); | ||
try { | ||
for await (const value of stream) putValue(value, true); | ||
} catch (e) { | ||
error(e); | ||
} | ||
} | ||
function _putStream() { | ||
_putStream = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(stream) { | ||
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, value; | ||
return _regenerator["default"].wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_context2.prev = 3; | ||
_iterator = (0, _asyncIterator2["default"])(stream); | ||
case 5: | ||
_context2.next = 7; | ||
return _iterator.next(); | ||
case 7: | ||
_step = _context2.sent; | ||
_iteratorNormalCompletion = _step.done; | ||
_context2.next = 11; | ||
return _step.value; | ||
case 11: | ||
_value = _context2.sent; | ||
if (_iteratorNormalCompletion) { | ||
_context2.next = 18; | ||
break; | ||
} | ||
value = _value; | ||
putValue(value, true); | ||
case 15: | ||
_iteratorNormalCompletion = true; | ||
_context2.next = 5; | ||
break; | ||
case 18: | ||
_context2.next = 24; | ||
break; | ||
case 20: | ||
_context2.prev = 20; | ||
_context2.t0 = _context2["catch"](3); | ||
_didIteratorError = true; | ||
_iteratorError = _context2.t0; | ||
case 24: | ||
_context2.prev = 24; | ||
_context2.prev = 25; | ||
if (!(!_iteratorNormalCompletion && _iterator["return"] != null)) { | ||
_context2.next = 29; | ||
break; | ||
} | ||
_context2.next = 29; | ||
return _iterator["return"](); | ||
case 29: | ||
_context2.prev = 29; | ||
if (!_didIteratorError) { | ||
_context2.next = 32; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 32: | ||
return _context2.finish(29); | ||
case 33: | ||
return _context2.finish(24); | ||
case 34: | ||
_context2.next = 39; | ||
break; | ||
case 36: | ||
_context2.prev = 36; | ||
_context2.t1 = _context2["catch"](0); | ||
error(_context2.t1); | ||
case 39: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, null, [[0, 36], [3, 20, 24, 34], [25,, 29, 33]]); | ||
})); | ||
return _putStream.apply(this, arguments); | ||
} | ||
function putValue(value, isChange) { | ||
@@ -234,7 +94,7 @@ if (typeof value === 'undefined') return; // console.log('Put', isChange ? 'Change' : 'Value', debug(value)); | ||
key: '', | ||
end: "\uFFFF", | ||
end: '\uffff', | ||
version: -1 | ||
}]); // console.log('Data before sieve', debug(data)); | ||
var sieved = (0, _common.sieve)(data, value); // console.log('Sieved: ', debug(sieved)); | ||
const sieved = (0, _common.sieve)(data, value); // console.log('Sieved: ', debug(sieved)); | ||
// console.log('Payload before adding sieved', debug(payload)); | ||
@@ -245,6 +105,6 @@ | ||
var _slice = (0, _sliceInstanceProperty(_common))(data, originalQuery), | ||
known = _slice.known, | ||
unknown = _slice.unknown; | ||
let { | ||
known, | ||
unknown | ||
} = (0, _sliceInstanceProperty(_common))(data, originalQuery); | ||
data = known || empty(); // console.log('After slice', debug(data), unknown && debug(unknown)); | ||
@@ -257,3 +117,3 @@ // console.log('Payload and value', debug(payload), value && debug(value)); | ||
// console.log('Here', debug(unknown)); | ||
var valueParts = (0, _sliceInstanceProperty(_common))(value, unknown); | ||
const valueParts = (0, _sliceInstanceProperty(_common))(value, unknown); | ||
@@ -285,3 +145,3 @@ if (valueParts.known) { | ||
function unsubscribe() { | ||
if (upstream) upstream["return"](); | ||
if (upstream) upstream.return(); | ||
} | ||
@@ -288,0 +148,0 @@ } |
11344
281
+ Added@graffy/common@0.14.7-alpha.10(transitive)
+ Added@graffy/stream@0.14.7-alpha.10(transitive)
- Removed@graffy/common@0.14.7-alpha.9(transitive)
- Removed@graffy/stream@0.14.7-alpha.9(transitive)