Comparing version 0.3.4 to 0.3.5
import _each from 'lodash/collection/each'; | ||
import _isObject from 'lodash/lang/isObject'; | ||
import EventEmitter from 'eventemitter3'; | ||
@@ -39,15 +40,11 @@ import invariant from 'invariant'; | ||
const res = pipeObj.value(x); | ||
if (cursor._observing && res && res.__onceJustUpdated) { | ||
invariant( | ||
!res.__haveListeners, | ||
'joins(...): for using observable joins `observe` must be called without arguments' | ||
); | ||
res.__onceJustUpdated(() => { | ||
res.stop(); | ||
cursor.update(); | ||
}); | ||
if (_isObject(res) && res.then) { | ||
if (res.parent) { | ||
res.parent(cursor); | ||
cursor.once('stopped', res.stop); | ||
} | ||
return res.then(() => x); | ||
} else { | ||
return x; | ||
} | ||
return res; | ||
})); | ||
@@ -54,0 +51,0 @@ }, |
@@ -49,8 +49,6 @@ import Cursor from './Cursor'; | ||
observe(listener) { | ||
// Listen for changes of the cursor | ||
this._observing = true; | ||
this._haveListeners = this._haveListeners || !!listener; | ||
if (listener) { | ||
this.on('update', listener); | ||
} | ||
// Debounce listener a little for update propagation | ||
// when joins updated | ||
listener = debounce(listener, 0, 0); | ||
this.on('update', listener); | ||
@@ -69,13 +67,11 @@ // Make new wrapper for make possible to observe | ||
this.db.removeListener('remove', updateWrapper); | ||
if (listener) { | ||
this.removeListener('update', listener); | ||
} | ||
this.removeListener('update', listener); | ||
this.emit('stopped'); | ||
}; | ||
const parentSetter = (cursor) => { | ||
this._parentCursor = cursor; | ||
}; | ||
const createStoppablePromise = (currPromise) => { | ||
// __onceUpdate is used when we do not need to know | ||
// a new result of a cursor, but just need to know | ||
// absout some changes happen. Used in observable joins. | ||
return { | ||
__haveListeners: this._haveListeners, // must be false | ||
__onceJustUpdated: this.once.bind(this, 'justUpdated'), | ||
parent: parentSetter, | ||
stop: stopper, | ||
@@ -98,17 +94,14 @@ then: function(successFn, failFn) { | ||
update(firstRun = false) { | ||
if (!this._haveListeners && !firstRun) { | ||
// Fast path for just notifying about some changes | ||
// happen when no listeners to `observe` provided | ||
// and it's not a first run (initial data). | ||
// It's used in observable joins | ||
this.emit('justUpdated', null, firstRun); | ||
return Promise.resolve(); | ||
} else { | ||
return this.exec().then((result) => { | ||
this._latestResult = result; | ||
this._latestIds = new Set(result.map(x => x._id)); | ||
this.emit('update', result, firstRun); | ||
return result; | ||
}); | ||
} | ||
return this.exec().then((result) => { | ||
this._latestResult = result; | ||
this._latestIds = new Set(result.map(x => x._id)); | ||
this.emit('update', result, firstRun); | ||
if (this._parentCursor && !firstRun) { | ||
const parentResult = this._parentCursor._latestResult; | ||
this._parentCursor.emit('update', parentResult, false); | ||
} | ||
return result; | ||
}); | ||
} | ||
@@ -115,0 +108,0 @@ |
{ | ||
"name": "marsdb", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Artem Artemev", |
@@ -26,3 +26,3 @@ <div style="text-align:center"><img src="https://static.studytime.me/marsdb.png" /></div> | ||
* **Observable queries** - live queries just like in Meteor, but with simplier interface | ||
* **Joinable cursor** – joins is simple, joins is observable (live) | ||
* **Reactive joins** – out of the box | ||
@@ -114,2 +114,3 @@ ## Examples | ||
``` | ||
### Find with joins | ||
@@ -122,21 +123,23 @@ ```javascript | ||
// Return a Promise for waiting of the result. | ||
// Calling `observe` makes result of root query depends | ||
// on chnages of results of this query. So, when author | ||
// object changed root result is updated (yeah!) | ||
return users.findOne(doc.authorId).observe().then(user => { | ||
return users.findOne(doc.authorId).then(user => { | ||
doc.authorObj = user; | ||
return doc; | ||
// any return is ignored | ||
}); | ||
}) | ||
.join(doc => { | ||
// For reactive join you must invoke `observe` instead `then` | ||
// That's it! | ||
return users.findOne(doc.authorId).observe(user => { | ||
doc.authorObj = user; | ||
}); | ||
}) | ||
.join(doc => { | ||
// Also any other “join” mutations supported | ||
doc.another = _cached_data_by_post[doc._id]; | ||
// You need to return a document, bacause it's | ||
// a pipeline operation | ||
return doc; | ||
}) | ||
.observe((posts) => { | ||
// do somethin wiht posts with authors | ||
}); | ||
// do something with posts with authors | ||
// invoked any time when posts changed | ||
// (and when observed joins changed too) | ||
}) | ||
``` | ||
@@ -143,0 +146,0 @@ ### Inserting |
@@ -106,19 +106,19 @@ import Collection from '../../lib/Collection'; | ||
.join((doc) => { | ||
return db.find({b: 30}).observe().then(res => { | ||
return db.find({b: 30}).observe(res => { | ||
doc.joined = res; | ||
return doc; | ||
}); | ||
}).observe(res => { | ||
}).observe(result => { | ||
if (calls === 0) { | ||
expect(res).to.be.an('array'); | ||
res.should.have.length(2); | ||
expect(res[0].joined).to.have.length(0); | ||
expect(res[1].joined).to.have.length(0); | ||
db.insert({b: 30}); | ||
calls += 1; | ||
} else if (calls === 1) { | ||
expect(res[0].joined).to.have.length(1); | ||
expect(res[1].joined).to.have.length(1); | ||
expect(result).to.be.an('array'); | ||
result.should.have.length(2); | ||
expect(result[0].joined).to.have.length(0); | ||
expect(result[1].joined).to.have.length(0); | ||
calls++; | ||
} else { | ||
expect(result[0].joined).to.have.length(1); | ||
expect(result[1].joined).to.have.length(1); | ||
done(); | ||
} | ||
}).then(() => { | ||
return db.insert({b: 30}); | ||
}); | ||
@@ -125,0 +125,0 @@ }); |
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
190
1052022
24520