11import { DiffSequence } from 'meteor/diff-sequence' ;
22import { _ } from 'meteor/underscore' ;
33import { LocalCollection , Minimongo } from 'meteor/minimongo' ;
4- import cloneDeep from 'lodash.clonedeep' ;
54import fieldProjectionIsExclusion from './lib/fieldProjectionIsExclusion' ;
65import getChannels from './lib/getChannels' ;
76import extractFieldsFromFilters from './lib/extractFieldsFromFilters' ;
87import { MongoIDMap } from './mongoIdMap' ;
8+ import { EJSON } from 'meteor/ejson' ;
99import isRemovedNonExistent from '../utils/isRemovedNonExistent' ;
1010import getStrategy from '../processors/getStrategy' ;
1111
@@ -24,19 +24,20 @@ const { Matcher } = Minimongo;
2424
2525export default class ObservableCollection {
2626 /**
27- * @param observer
28- * @param cursor
29- * @param config
27+ * Instantiate the collection
28+ * @param {* } param
3029 */
31- constructor ( multiplexer , matcher , sorter , cursorDescription ) {
30+ constructor ( { multiplexer, matcher, sorter, cursorDescription } ) {
3231 this . multiplexer = multiplexer ;
32+ this . matcher = matcher ;
3333 this . cursorDescription = cursorDescription ;
3434
3535 this . collectionName = this . cursorDescription . collectionName ;
3636 this . collection = Mongo . Collection . __getCollectionByName (
3737 cursorDescription . collectionName
3838 ) ;
3939
40+ // Here we apply the logic of changing the cursor based on the collection-level configuration
4041 if ( this . collection . _redisOplog ) {
4142 const { cursor } = this . collection . _redisOplog ;
4243 if ( cursor ) {
@@ -102,7 +103,6 @@ export default class ObservableCollection {
102103 }
103104
104105 this . channels = getChannels ( this . collectionName , this . options ) ;
105- this . testDocEligibility = this . _createTestDocEligibility ( ) ;
106106 this . fieldsOfInterest = this . _getFieldsOfInterest ( ) ;
107107 this . __isInitialized = false ;
108108
@@ -128,8 +128,8 @@ export default class ObservableCollection {
128128 * @returns {* }
129129 */
130130 isEligible ( doc ) {
131- if ( this . testDocEligibility ) {
132- return this . testDocEligibility ( doc ) ;
131+ if ( this . matcher ) {
132+ return this . matcher . documentMatches ( doc ) . result ;
133133 }
134134
135135 return true ;
@@ -140,7 +140,7 @@ export default class ObservableCollection {
140140 * @returns {boolean }
141141 */
142142 isEligibleByDB ( _id ) {
143- if ( this . testDocEligibility ) {
143+ if ( this . matcher ) {
144144 return ! ! this . collection . findOne (
145145 _ . extend ( { } , this . selector , { _id } ) ,
146146 { fields : { _id : 1 } }
@@ -191,7 +191,7 @@ export default class ObservableCollection {
191191 * @param safe {Boolean} If this is set to true, it assumes that the object is cleaned
192192 */
193193 add ( doc , safe = false ) {
194- doc = cloneDeep ( doc ) ;
194+ doc = EJSON . clone ( doc ) ;
195195
196196 if ( ! safe ) {
197197 if ( this . fieldsArray ) {
@@ -257,7 +257,7 @@ export default class ObservableCollection {
257257 }
258258
259259 let storedDoc = this . store . get ( docId ) ;
260- let oldDoc = cloneDeep ( storedDoc ) ;
260+ let oldDoc = EJSON . clone ( storedDoc ) ;
261261
262262 LocalCollection . _modify ( storedDoc , modifier ) ;
263263
@@ -303,42 +303,6 @@ export default class ObservableCollection {
303303 return false ;
304304 }
305305
306- /**
307- * Used at initialization
308- *
309- * Creates the function that checks if the document is valid
310- *
311- * @returns {null }
312- * @private
313- */
314- _createTestDocEligibility ( ) {
315- const self = this ;
316-
317- if ( _ . keys ( this . selector ) . length ) {
318- try {
319- const matcher = new Matcher ( this . selector ) ;
320-
321- return function ( object ) {
322- return matcher . documentMatches ( object ) . result ;
323- } ;
324- } catch ( e ) {
325- // The logic here is that if our matcher is too complex for minimongo
326- // We put our matching function to query db
327- if (
328- e . toString ( ) . indexOf ( 'Unrecognized logical operator' ) >= 0
329- ) {
330- return function ( object ) {
331- return self . isEligibleByDB ( object . _id ) ;
332- } ;
333- } else {
334- throw e ;
335- }
336- }
337- }
338-
339- return null ;
340- }
341-
342306 /**
343307 * Used at initialization
344308 *
0 commit comments