Comparing version 0.5.11 to 0.5.12
@@ -32,2 +32,6 @@ 'use strict'; | ||
var _keys2 = require('fast.js/object/keys'); | ||
var _keys3 = _interopRequireDefault(_keys2); | ||
var _map2 = require('fast.js/map'); | ||
@@ -138,3 +142,2 @@ | ||
if (cursorPromise) { | ||
cursorPromise.cursor._trackParentCursor(cursor); | ||
cursor._trackChildCursorPromise(cursorPromise); | ||
@@ -176,3 +179,3 @@ } | ||
_this._pipeline = []; | ||
_this._executing = null; | ||
_this._latestResult = null; | ||
_this._childrenCursors = {}; | ||
@@ -231,3 +234,3 @@ _this._parentCursors = {}; | ||
this.addPipeline(PIPELINE_TYPE.Sort, sortFn); | ||
this._addPipeline(PIPELINE_TYPE.Sort, sortFn); | ||
return this; | ||
@@ -240,3 +243,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Filter, filterFn); | ||
this._addPipeline(PIPELINE_TYPE.Filter, filterFn); | ||
return this; | ||
@@ -249,3 +252,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Map, mapperFn); | ||
this._addPipeline(PIPELINE_TYPE.Map, mapperFn); | ||
return this; | ||
@@ -258,3 +261,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Reduce, reduceFn, initial); | ||
this._addPipeline(PIPELINE_TYPE.Reduce, reduceFn, initial); | ||
return this; | ||
@@ -267,3 +270,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Aggregate, aggrFn); | ||
this._addPipeline(PIPELINE_TYPE.Aggregate, aggrFn); | ||
return this; | ||
@@ -276,3 +279,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
return this; | ||
@@ -285,3 +288,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.JoinEach, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.JoinEach, joinFn); | ||
return this; | ||
@@ -294,3 +297,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.JoinAll, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.JoinAll, joinFn); | ||
return this; | ||
@@ -301,8 +304,24 @@ } | ||
value: function ifNotEmpty() { | ||
this.addPipeline(PIPELINE_TYPE.IfNotEmpty); | ||
this._addPipeline(PIPELINE_TYPE.IfNotEmpty); | ||
return this; | ||
} | ||
}, { | ||
key: 'addPipeline', | ||
value: function addPipeline(type, val) { | ||
key: 'exec', | ||
value: function exec() { | ||
var _this2 = this; | ||
this.emit('beforeExecute'); | ||
return this._createCursorPromise(this._doExecute().then(function (result) { | ||
_this2._latestResult = result; | ||
return result; | ||
})); | ||
} | ||
}, { | ||
key: 'then', | ||
value: function then(resolve, reject) { | ||
return this.exec().then(resolve, reject); | ||
} | ||
}, { | ||
key: '_addPipeline', | ||
value: function _addPipeline(type, val) { | ||
(0, _invariant2.default)(type && PIPELINE_TYPE[type], 'Unknown pipeline processor type %s', type); | ||
@@ -322,5 +341,5 @@ | ||
}, { | ||
key: 'processPipeline', | ||
value: function processPipeline(docs) { | ||
var _this2 = this; | ||
key: '_processPipeline', | ||
value: function _processPipeline(docs) { | ||
var _this3 = this; | ||
@@ -337,3 +356,3 @@ var i = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | ||
} else { | ||
return _this2.processPipeline(result, i + 1); | ||
return _this3._processPipeline(result, i + 1); | ||
} | ||
@@ -344,29 +363,12 @@ }); | ||
}, { | ||
key: 'processSkipLimits', | ||
value: function processSkipLimits(docs) { | ||
var skip = this._skip || 0; | ||
var limit = this._limit || docs.length; | ||
return docs.slice(skip, limit + skip); | ||
} | ||
}, { | ||
key: 'exec', | ||
value: function exec() { | ||
return this._createCursorPromise(this._doExecute()); | ||
} | ||
}, { | ||
key: 'then', | ||
value: function then(resolve, reject) { | ||
return this.exec().then(resolve, reject); | ||
} | ||
}, { | ||
key: '_doExecute', | ||
value: function _doExecute() { | ||
var _this3 = this; | ||
var _this4 = this; | ||
return this._matchObjects().then(function (docs) { | ||
var clonned = undefined; | ||
if (_this3.options.noClone) { | ||
if (_this4.options.noClone) { | ||
clonned = docs; | ||
} else { | ||
if (!_this3._projector) { | ||
if (!_this4._projector) { | ||
clonned = (0, _map3.default)(docs, function (doc) { | ||
@@ -376,6 +378,6 @@ return _EJSON2.default.clone(doc); | ||
} else { | ||
clonned = _this3._projector.project(docs); | ||
clonned = _this4._projector.project(docs); | ||
} | ||
} | ||
return _this3.processPipeline(clonned); | ||
return _this4._processPipeline(clonned); | ||
}); | ||
@@ -386,14 +388,14 @@ } | ||
value: function _matchObjects() { | ||
var _this4 = this; | ||
var _this5 = this; | ||
return new _DocumentRetriver2.default(this.db).retriveForQeury(this._query).then(function (docs) { | ||
var results = []; | ||
var withFastLimit = _this4._limit && !_this4._skip && !_this4._sorter; | ||
var withFastLimit = _this5._limit && !_this5._skip && !_this5._sorter; | ||
(0, _forEach2.default)(docs, function (d) { | ||
var match = _this4._matcher.documentMatches(d); | ||
var match = _this5._matcher.documentMatches(d); | ||
if (match.result) { | ||
results.push(d); | ||
} | ||
if (withFastLimit && results.length === _this4._limit) { | ||
if (withFastLimit && results.length === _this5._limit) { | ||
return false; | ||
@@ -407,8 +409,10 @@ } | ||
if (_this4._sorter) { | ||
var comparator = _this4._sorter.getComparator(); | ||
if (_this5._sorter) { | ||
var comparator = _this5._sorter.getComparator(); | ||
results.sort(comparator); | ||
} | ||
return _this4.processSkipLimits(results); | ||
var skip = _this5._skip || 0; | ||
var limit = _this5._limit || results.length; | ||
return results.slice(skip, limit + skip); | ||
}); | ||
@@ -428,15 +432,21 @@ } | ||
key: '_trackChildCursorPromise', | ||
value: function _trackChildCursorPromise(cursorPromise) { | ||
var cursor = cursorPromise.cursor; | ||
this._childrenCursors[cursor._id] = cursor; | ||
value: function _trackChildCursorPromise(childCursorPromise) { | ||
var _this6 = this; | ||
var childCursor = childCursorPromise.cursor; | ||
this._childrenCursors[childCursor._id] = childCursor; | ||
childCursor._parentCursors[this._id] = this; | ||
this.once('beforeExecute', function () { | ||
delete _this6._childrenCursors[childCursor._id]; | ||
delete childCursor._parentCursors[_this6._id]; | ||
if ((0, _keys3.default)(childCursor._parentCursors).length === 0) { | ||
childCursor.emit('beforeExecute'); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: '_trackParentCursor', | ||
value: function _trackParentCursor(cursor) { | ||
this._parentCursors[cursor._id] = cursor; | ||
} | ||
}, { | ||
key: '_createCursorPromise', | ||
value: function _createCursorPromise(promise) { | ||
var _this5 = this; | ||
var _this7 = this; | ||
@@ -448,3 +458,3 @@ var mixin = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
then: function then(successFn, failFn) { | ||
return _this5._createCursorPromise(promise.then(successFn, failFn), mixin); | ||
return _this7._createCursorPromise(promise.then(successFn, failFn), mixin); | ||
} | ||
@@ -451,0 +461,0 @@ }, mixin); |
@@ -51,3 +51,2 @@ 'use strict'; | ||
var _defaultBatchSize = 10; | ||
var _noop = function _noop() {}; | ||
@@ -70,3 +69,2 @@ /** | ||
_this._doUpdate = (0, _debounce2.default)((0, _bind3.default)(_this._doUpdate, _this), _defaultDebounce, _defaultBatchSize); | ||
_this._latestResult = null; | ||
_this._observers = 0; | ||
@@ -119,6 +117,5 @@ return _this; | ||
key: 'observe', | ||
value: function observe() { | ||
value: function observe(listener) { | ||
var _this2 = this; | ||
var listener = arguments.length <= 0 || arguments[0] === undefined ? _noop : arguments[0]; | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -131,2 +128,3 @@ | ||
}; | ||
listener = listener || function () {}; | ||
@@ -150,5 +148,5 @@ this.db.on('insert', updateWrapper); | ||
if (self._observers === 0) { | ||
self._latestIds = null; | ||
self._latestResult = null; | ||
self._latestIds = null; | ||
self.emit('stopped'); | ||
self.emit('observeStopped'); | ||
} | ||
@@ -293,3 +291,2 @@ } | ||
return this.exec().then(function (result) { | ||
_this3._latestResult = result; | ||
_this3._updateLatestIds(); | ||
@@ -325,34 +322,9 @@ return _this3._propagateUpdate(firstRun).then(function () { | ||
value: function _trackChildCursorPromise(cursorPromise) { | ||
var _this4 = this; | ||
_get(Object.getPrototypeOf(CursorObservable.prototype), '_trackChildCursorPromise', this).call(this, cursorPromise); | ||
var cursor = cursorPromise.cursor; | ||
var cleaner = function cleaner() { | ||
return delete _this4._childrenCursors[cursor._id]; | ||
}; | ||
cursor.once('stopped', cleaner); | ||
if (cursorPromise.stop) { | ||
this.once('cursorChanged', cursorPromise.stop); | ||
this.once('stopped', cursorPromise.stop); | ||
this.once('observeStopped', cursorPromise.stop); | ||
this.once('beforeExecute', cursorPromise.stop); | ||
} | ||
} | ||
/** | ||
* Track parent cursor for propagating update event | ||
* to parent observers. Also remove parent if it is stopped. | ||
* @param {Cursor} cursor | ||
*/ | ||
}, { | ||
key: '_trackParentCursor', | ||
value: function _trackParentCursor(cursor) { | ||
var _this5 = this; | ||
_get(Object.getPrototypeOf(CursorObservable.prototype), '_trackParentCursor', this).call(this, cursor); | ||
var cleaner = function cleaner() { | ||
return delete _this5._parentCursors[cursor._id]; | ||
}; | ||
cursor.once('stopped', cleaner); | ||
} | ||
}], [{ | ||
@@ -359,0 +331,0 @@ key: 'defaultDebounce', |
@@ -6,2 +6,3 @@ import _bind from 'fast.js/function/bind'; | ||
import _assign from 'fast.js/object/assign'; | ||
import _keys from 'fast.js/object/keys'; | ||
import _map from 'fast.js/map'; | ||
@@ -83,3 +84,2 @@ import _check from 'check-types'; | ||
if (cursorPromise) { | ||
cursorPromise.cursor._trackParentCursor(cursor); | ||
cursor._trackChildCursorPromise(cursorPromise); | ||
@@ -117,3 +117,3 @@ } | ||
this._pipeline = []; | ||
this._executing = null; | ||
this._latestResult = null; | ||
this._childrenCursors = {}; | ||
@@ -176,3 +176,3 @@ this._parentCursors = {}; | ||
this.addPipeline(PIPELINE_TYPE.Sort, sortFn); | ||
this._addPipeline(PIPELINE_TYPE.Sort, sortFn); | ||
return this; | ||
@@ -187,3 +187,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Filter, filterFn); | ||
this._addPipeline(PIPELINE_TYPE.Filter, filterFn); | ||
return this; | ||
@@ -198,3 +198,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Map, mapperFn); | ||
this._addPipeline(PIPELINE_TYPE.Map, mapperFn); | ||
return this; | ||
@@ -209,3 +209,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Reduce, reduceFn, initial); | ||
this._addPipeline(PIPELINE_TYPE.Reduce, reduceFn, initial); | ||
return this; | ||
@@ -220,3 +220,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Aggregate, aggrFn); | ||
this._addPipeline(PIPELINE_TYPE.Aggregate, aggrFn); | ||
return this; | ||
@@ -231,3 +231,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
return this; | ||
@@ -242,3 +242,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.JoinEach, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.JoinEach, joinFn); | ||
return this; | ||
@@ -253,3 +253,3 @@ } | ||
this.addPipeline(PIPELINE_TYPE.JoinAll, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.JoinAll, joinFn); | ||
return this; | ||
@@ -259,7 +259,21 @@ } | ||
ifNotEmpty() { | ||
this.addPipeline(PIPELINE_TYPE.IfNotEmpty); | ||
this._addPipeline(PIPELINE_TYPE.IfNotEmpty); | ||
return this; | ||
} | ||
addPipeline(type, val, ...args) { | ||
exec() { | ||
this.emit('beforeExecute'); | ||
return this._createCursorPromise( | ||
this._doExecute().then((result) => { | ||
this._latestResult = result; | ||
return result; | ||
}) | ||
); | ||
} | ||
then(resolve, reject) { | ||
return this.exec().then(resolve, reject); | ||
} | ||
_addPipeline(type, val, ...args) { | ||
invariant( | ||
@@ -279,3 +293,3 @@ type && PIPELINE_TYPE[type], | ||
processPipeline(docs, i = 0) { | ||
_processPipeline(docs, i = 0) { | ||
const pipeObj = this._pipeline[i]; | ||
@@ -291,3 +305,3 @@ if (!pipeObj) { | ||
} else { | ||
return this.processPipeline(result, i + 1); | ||
return this._processPipeline(result, i + 1); | ||
} | ||
@@ -298,16 +312,2 @@ }); | ||
processSkipLimits(docs) { | ||
const skip = this._skip || 0; | ||
const limit = this._limit || docs.length; | ||
return docs.slice(skip, limit + skip); | ||
} | ||
exec() { | ||
return this._createCursorPromise(this._doExecute()); | ||
} | ||
then(resolve, reject) { | ||
return this.exec().then(resolve, reject); | ||
} | ||
_doExecute() { | ||
@@ -326,3 +326,3 @@ return this._matchObjects() | ||
} | ||
return this.processPipeline(clonned); | ||
return this._processPipeline(clonned); | ||
}); | ||
@@ -357,3 +357,5 @@ } | ||
return this.processSkipLimits(results); | ||
const skip = this._skip || 0; | ||
const limit = this._limit || results.length; | ||
return results.slice(skip, limit + skip); | ||
} | ||
@@ -373,9 +375,14 @@ ); | ||
_trackChildCursorPromise(cursorPromise) { | ||
const cursor = cursorPromise.cursor; | ||
this._childrenCursors[cursor._id] = cursor; | ||
} | ||
_trackChildCursorPromise(childCursorPromise) { | ||
const childCursor = childCursorPromise.cursor; | ||
this._childrenCursors[childCursor._id] = childCursor; | ||
childCursor._parentCursors[this._id] = this; | ||
_trackParentCursor(cursor) { | ||
this._parentCursors[cursor._id] = cursor; | ||
this.once('beforeExecute', () => { | ||
delete this._childrenCursors[childCursor._id]; | ||
delete childCursor._parentCursors[this._id]; | ||
if (_keys(childCursor._parentCursors).length === 0) { | ||
childCursor.emit('beforeExecute'); | ||
} | ||
}); | ||
} | ||
@@ -382,0 +389,0 @@ |
@@ -13,3 +13,2 @@ import _bind from 'fast.js/function/bind'; | ||
let _defaultBatchSize = 10; | ||
const _noop = function() {}; | ||
@@ -30,3 +29,2 @@ /** | ||
); | ||
this._latestResult = null; | ||
this._observers = 0; | ||
@@ -85,6 +83,7 @@ } | ||
*/ | ||
observe(listener = _noop, options = {}) { | ||
observe(listener, options = {}) { | ||
// Make new wrapper for make possible to observe | ||
// multiple times (for removeListener) | ||
const updateWrapper = (a, b) => this.maybeUpdate(a, b); | ||
listener = listener || function() {}; | ||
@@ -108,5 +107,5 @@ this.db.on('insert', updateWrapper); | ||
if (self._observers === 0) { | ||
self._latestIds = null; | ||
self._latestResult = null; | ||
self._latestIds = null; | ||
self.emit('stopped'); | ||
self.emit('observeStopped'); | ||
} | ||
@@ -242,3 +241,2 @@ } | ||
return this.exec().then((result) => { | ||
this._latestResult = result; | ||
this._updateLatestIds(); | ||
@@ -268,24 +266,10 @@ return this._propagateUpdate(firstRun) | ||
super._trackChildCursorPromise(cursorPromise); | ||
const cursor = cursorPromise.cursor; | ||
const cleaner = () => delete this._childrenCursors[cursor._id]; | ||
cursor.once('stopped', cleaner); | ||
if (cursorPromise.stop) { | ||
this.once('cursorChanged', cursorPromise.stop); | ||
this.once('stopped', cursorPromise.stop); | ||
this.once('observeStopped', cursorPromise.stop); | ||
this.once('beforeExecute', cursorPromise.stop); | ||
} | ||
} | ||
/** | ||
* Track parent cursor for propagating update event | ||
* to parent observers. Also remove parent if it is stopped. | ||
* @param {Cursor} cursor | ||
*/ | ||
_trackParentCursor(cursor) { | ||
super._trackParentCursor(cursor); | ||
const cleaner = () => delete this._parentCursors[cursor._id]; | ||
cursor.once('stopped', cleaner); | ||
} | ||
} | ||
export default CursorObservable; |
{ | ||
"name": "marsdb", | ||
"version": "0.5.11", | ||
"version": "0.5.12", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Artem Artemev", |
@@ -13,5 +13,5 @@ <div style="text-align:center"><img src="https://static.studytime.me/marsdb.png" /></div> | ||
MarsDB is a lightweight client-side database. | ||
It's based on a Meteor’s **minimongo** matching/modifying implementation. It's carefully written on **ES6**, have a **Promise based** interface and may be backed with any storage implementation ([see plugins](https://github.com/c58/marsdb#plugins)). It's also supports **observable** cursors. | ||
It's based on a Meteor's **minimongo** matching/modifying implementation. It's carefully written on **ES6**, have a **Promise based** interface and may be backed with any storage implementation ([see plugins](https://github.com/c58/marsdb#plugins)). It's also supports **observable** cursors. | ||
MarsDB supports any kind of find/update/remove operations that Meteor’s minimongo does. So, go to the Meteor docs for supported query/modifier operations. | ||
MarsDB supports any kind of find/update/remove operations that Meteor's minimongo does. So, go to the Meteor docs for supported query/modifier operations. | ||
@@ -25,4 +25,4 @@ You can use it in any JS environment (Browser, Electron, NW.js, Node.js). | ||
* **Very very flexible** – just take a look to the [plugins section](https://github.com/c58/marsdb#plugins) | ||
* **Supports many of MongoDB query/modify operations** – thanks to a Meteor’s minimongo | ||
* **Flexible pipeline** – map, reduce, custom sorting function, filtering. All with a sexy JS interface (no ugly mongo’s aggregation language) | ||
* **Supports many of MongoDB query/modify operations** – thanks to a Meteor's minimongo | ||
* **Flexible pipeline** – map, reduce, custom sorting function, filtering. All with a sexy JS interface (no ugly mongo's aggregation language) | ||
* **Persistence API** – all collections can be stored (and restored) with any kind of storage (in-memory, LocalStorage, LevelUP, etc) | ||
@@ -50,3 +50,3 @@ * **Observable queries** - live queries just like in Meteor, but with simplier interface | ||
### Using within non-ES6 environment | ||
The `./dist` folder contains already compiled to a ES5 code, but some polyfills needed. For using in a browser you must to include `marsdb.polyfills.js` before `marsdb.min.js`. In node.js you need to `require(‘marsdb/polyfills’)`. | ||
The `./dist` folder contains already compiled to a ES5 code, but some polyfills needed. For using in a browser you must to include `marsdb.polyfills.js` before `marsdb.min.js`. In node.js you need to `require('marsdb/polyfills')`. | ||
It sets in a window/global: Promise, Set and Symbol. | ||
@@ -56,3 +56,3 @@ | ||
```javascript | ||
import Collection from ‘marsdb’; | ||
import Collection from 'marsdb'; | ||
import LocalStorageManager from 'marsdb-localstorage'; | ||
@@ -70,10 +70,10 @@ | ||
const users = new Collection(‘users’); | ||
const users = new Collection('users'); | ||
``` | ||
### Find documents | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
posts.find({author: ‘Bob’}) | ||
const posts = new Collection('posts'); | ||
posts.find({author: 'Bob'}) | ||
.project({author: 1}) | ||
.sort([‘createdAt’]) | ||
.sort(['createdAt']) | ||
.then(docs => { | ||
@@ -86,3 +86,3 @@ // do something with docs | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
const posts = new Collection('posts'); | ||
@@ -113,4 +113,4 @@ // Get number of all comments in the DB | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
const stopper = posts.find({tags: {$in: [‘marsdb’, ‘is’, ‘awesome’]}}) | ||
const posts = new Collection('posts'); | ||
const stopper = posts.find({tags: {$in: ['marsdb', 'is', 'awesome']}}) | ||
.observe(docs => { | ||
@@ -128,4 +128,4 @@ // invoked on every result change | ||
```javascript | ||
const users = new Collection(‘users’); | ||
const posts = new Collection(‘posts’); | ||
const users = new Collection('users'); | ||
const posts = new Collection('posts'); | ||
posts.find() | ||
@@ -166,10 +166,10 @@ .join(doc => { | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
posts.insert({text: ‘MarsDB is awesome’}).then(docId => { | ||
const posts = new Collection('posts'); | ||
posts.insert({text: 'MarsDB is awesome'}).then(docId => { | ||
// Invoked after persisting document | ||
}) | ||
posts.insertAll( | ||
{text: ‘MarsDB’}, | ||
{text: ‘is’}, | ||
{text: ‘awesome’} | ||
{text: 'MarsDB'}, | ||
{text: 'is'}, | ||
{text: 'awesome'} | ||
).then(docsIds => { | ||
@@ -181,6 +181,6 @@ // invoked when all documents inserted | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
const posts = new Collection('posts'); | ||
posts.update( | ||
{authorId: {$in: [1, 2, 3]}}, | ||
{$set: {text: ‘noop’}} | ||
{$set: {text: 'noop'}} | ||
).then(result => { | ||
@@ -194,3 +194,3 @@ console.log(result.modified) // count of modified docs | ||
```javascript | ||
const posts = new Collection(‘posts’); | ||
const posts = new Collection('posts'); | ||
posts.remove({authorId: {$in: [1,2,3]}}) | ||
@@ -209,6 +209,6 @@ .then(removedDocs => { | ||
## Contributing | ||
I’m waiting for your pull requests and issues. | ||
Don’t forget to execute `gulp lint` before requesting. Accepted only requests without errors. | ||
I'm waiting for your pull requests and issues. | ||
Don't forget to execute `gulp lint` before requesting. Accepted only requests without errors. | ||
## License | ||
See [License](LICENSE) |
@@ -75,2 +75,20 @@ import Collection from '../../lib/Collection'; | ||
}); | ||
it('should set latest result', function () { | ||
const cursor = new Cursor(db, {}, {noClone: true}); | ||
cursor.find({b: {$gt: 4}}).skip(1).sort({b: 1}); | ||
expect(cursor._latestResult).to.be.null; | ||
return cursor.exec().then((docs) => { | ||
cursor._latestResult.should.have.length(2); | ||
}) | ||
}); | ||
it('should emit `beforeExecute` event', function () { | ||
const cb = sinon.spy(); | ||
const cursor = new Cursor(db, {}, {noClone: true}); | ||
cursor.on('beforeExecute', cb); | ||
cursor.find({b: {$gt: 4}}).skip(1).sort({b: 1}); | ||
cursor.exec(); | ||
cb.should.have.callCount(1); | ||
}); | ||
}); | ||
@@ -631,2 +649,81 @@ | ||
}); | ||
describe('#_trackChildCursorPromise', function () { | ||
it('should destroy only not used cursors', function () { | ||
const cursor_3 = new Cursor(db).find({a: 'c'}); | ||
const cursor_3_1 = new Cursor(db).find({a: 'c'}); | ||
const cursor_2 = new Cursor(db).find({a: 'b'}).join(() => [cursor_3, cursor_3_1]); | ||
const cursor_1 = new Cursor(db).find({a: 'a'}).join(() => cursor_2); | ||
const cursor_1_1 = new Cursor(db).find({a: 'd'}).join(() => cursor_3); | ||
return Promise.all([ | ||
cursor_1, cursor_1_1 | ||
]).then(() => { | ||
cursor_1._childrenCursors.should.be.deep.equal({[cursor_2._id]: cursor_2}); | ||
cursor_1._parentCursors.should.be.deep.equal({}); | ||
cursor_2._childrenCursors.should.be.deep.equal({ | ||
[cursor_3._id]: cursor_3, | ||
[cursor_3_1._id]: cursor_3_1, | ||
}); | ||
cursor_2._parentCursors.should.be.deep.equal({[cursor_1._id]: cursor_1}); | ||
cursor_3._childrenCursors.should.be.deep.equal({}); | ||
cursor_3._parentCursors.should.be.deep.equal({ | ||
[cursor_1_1._id]: cursor_1_1, | ||
[cursor_2._id]: cursor_2, | ||
}); | ||
cursor_3_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_3_1._parentCursors.should.be.deep.equal({ | ||
[cursor_2._id]: cursor_2, | ||
}); | ||
cursor_1_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._childrenCursors.should.be.deep.equal({[cursor_3._id]: cursor_3}); | ||
let a = cursor_1.exec(); | ||
cursor_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_1._parentCursors.should.be.deep.equal({}); | ||
cursor_2._childrenCursors.should.be.deep.equal({}); | ||
cursor_2._parentCursors.should.be.deep.equal({}); | ||
cursor_3._childrenCursors.should.be.deep.equal({}); | ||
cursor_3._parentCursors.should.be.deep.equal({ | ||
[cursor_1_1._id]: cursor_1_1, | ||
}); | ||
cursor_3_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_3_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._childrenCursors.should.be.deep.equal({[cursor_3._id]: cursor_3}); | ||
let b = cursor_1_1.exec(); | ||
cursor_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_1._parentCursors.should.be.deep.equal({}); | ||
cursor_2._childrenCursors.should.be.deep.equal({}); | ||
cursor_2._parentCursors.should.be.deep.equal({}); | ||
cursor_3._childrenCursors.should.be.deep.equal({}); | ||
cursor_3._parentCursors.should.be.deep.equal({}); | ||
cursor_3_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_3_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._childrenCursors.should.be.deep.equal({}); | ||
return Promise.all([a, b]); | ||
}).then(() => { | ||
cursor_1._childrenCursors.should.be.deep.equal({[cursor_2._id]: cursor_2}); | ||
cursor_1._parentCursors.should.be.deep.equal({}); | ||
cursor_2._childrenCursors.should.be.deep.equal({ | ||
[cursor_3._id]: cursor_3, | ||
[cursor_3_1._id]: cursor_3_1, | ||
}); | ||
cursor_2._parentCursors.should.be.deep.equal({[cursor_1._id]: cursor_1}); | ||
cursor_3._childrenCursors.should.be.deep.equal({}); | ||
cursor_3._parentCursors.should.be.deep.equal({ | ||
[cursor_1_1._id]: cursor_1_1, | ||
[cursor_2._id]: cursor_2, | ||
}); | ||
cursor_3_1._childrenCursors.should.be.deep.equal({}); | ||
cursor_3_1._parentCursors.should.be.deep.equal({ | ||
[cursor_2._id]: cursor_2, | ||
}); | ||
cursor_1_1._parentCursors.should.be.deep.equal({}); | ||
cursor_1_1._childrenCursors.should.be.deep.equal({[cursor_3._id]: cursor_3}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -86,6 +86,6 @@ import Collection from '../../lib/Collection'; | ||
describe('#observe', function () { | ||
it('should generate `stopped` event when all observers stopped', function () { | ||
it('should generate `observeStopped` event when all observers stopped', function () { | ||
const cursor = db.find({b: 1}) | ||
const cb = sinon.spy(); | ||
cursor.on('stopped', cb); | ||
cursor.on('observeStopped', cb); | ||
const obs1 = cursor.observe(() => {}); | ||
@@ -540,2 +540,62 @@ const obs2 = cursor.observe(() => {}); | ||
}); | ||
describe('#_trackChildCursorPromise', function () { | ||
it('should stop observer throught not observable cursor', function () { | ||
const cursor_3 = new CursorObservable(db).find({a: 'c'}); | ||
const cursor_2 = new CursorObservable(db).find({a: 'b'}).join(() => cursor_3.observe()); | ||
const cursor_1 = new CursorObservable(db).find({a: 'a'}).join(() => cursor_2); | ||
return Promise.all([ | ||
cursor_1.observe() | ||
]) | ||
.then(() => cursor_1.update()) | ||
.then(() => { | ||
cursor_3.listeners('update').should.have.length(1); | ||
cursor_2.listeners('update').should.have.length(0); | ||
cursor_1.listeners('update').should.have.length(1); | ||
}); | ||
}); | ||
it('should stop only useless observers', function () { | ||
const cursor_3 = new CursorObservable(db).find({a: 'c'}); | ||
const cursor_2 = new CursorObservable(db).find({a: 'b'}).join(() => cursor_3.observe()); | ||
const cursor_1 = new CursorObservable(db).find({a: 'a'}).join(() => cursor_2); | ||
return Promise.all([ | ||
cursor_1.observe(), | ||
cursor_3.observe(), | ||
]) | ||
.then(() => { | ||
cursor_3.listeners('update').should.have.length(2); | ||
return cursor_1.update(); | ||
}) | ||
.then(() => { | ||
cursor_3.listeners('update').should.have.length(2); | ||
cursor_2.listeners('update').should.have.length(0); | ||
cursor_1.listeners('update').should.have.length(1); | ||
}); | ||
}); | ||
it('should not stop observer in parallel cursor tree', function () { | ||
const cursor_3 = new CursorObservable(db).find({a: 'c'}); | ||
const cursor_2 = new CursorObservable(db).find({a: 'b'}).join(() => cursor_3.observe()); | ||
const cursor_1 = new CursorObservable(db).find({a: 'a'}).join(() => cursor_2); | ||
const cursor_1_1 = new CursorObservable(db).find({a: 'a'}).join(() => cursor_3.observe()); | ||
return Promise.all([ | ||
cursor_1.observe(), | ||
cursor_1_1.observe(), | ||
]) | ||
.then(() => { | ||
cursor_3.listeners('update').should.have.length(2); | ||
return cursor_1.update(); | ||
}) | ||
.then(() => { | ||
cursor_3.listeners('update').should.have.length(2); | ||
cursor_2.listeners('update').should.have.length(0); | ||
cursor_1.listeners('update').should.have.length(1); | ||
cursor_1_1.listeners('update').should.have.length(1); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
936884
21858