@@ -2,6 +2,7 @@ import { Meteor } from 'meteor/meteor'
22import { Mongo } from 'meteor/mongo'
33import { EJSON } from 'meteor/ejson'
44import { LocalCollection } from 'meteor/minimongo'
5+ import { CollectionExtensions } from 'meteor/lai:collection-extensions'
56
67// Hooks terminology:
78// Hook: User-defined function that runs before/after collection operations
@@ -336,38 +337,18 @@ CollectionHooks.reassignPrototype = function reassignPrototype (
336337 }
337338}
338339
339- CollectionHooks . wrapCollection = function wrapCollection ( ns , as ) {
340- if ( ! as . _CollectionConstructor ) as . _CollectionConstructor = as . Collection
341- if ( ! as . _CollectionPrototype ) { as . _CollectionPrototype = new as . Collection ( null ) }
340+ // Use lai:collection-extensions for clean collection constructor extension
341+ CollectionExtensions . addExtension ( function ( collection , options ) {
342+ // This function is called whenever new Mongo.Collection() is created
343+ // 'collection' is the collection instance (passed as first parameter)
344+ // 'options' are the options passed to the constructor
342345
343- const constructor = ns . _NewCollectionContructor || as . _CollectionConstructor
344- const proto = as . _CollectionPrototype
345-
346- ns . Collection = function ( ...args ) {
347- const ret = constructor . apply ( this , args )
348- CollectionHooks . extendCollectionInstance ( this , constructor )
349- return ret
350- }
351- // Retain a reference to the new constructor to allow further wrapping.
352- ns . _NewCollectionContructor = ns . Collection
353-
354- ns . Collection . prototype = proto
355- ns . Collection . prototype . constructor = ns . Collection
356-
357- for ( const prop of Object . keys ( constructor ) ) {
358- ns . Collection [ prop ] = constructor [ prop ]
346+ // Skip extension if collection instance is null (can happen with special collections)
347+ if ( ! collection ) {
348+ return
359349 }
360350
361- // Meteor overrides the apply method which is copied from the constructor in the loop above. Replace it with the
362- // default method which we need if we were to further wrap ns.Collection.
363- ns . Collection . apply = Function . prototype . apply
364- }
351+ CollectionHooks . extendCollectionInstance ( collection , Mongo . Collection )
352+ } )
365353
366354CollectionHooks . modify = LocalCollection . _modify
367-
368- if ( typeof Mongo !== 'undefined' ) {
369- CollectionHooks . wrapCollection ( Meteor , Mongo )
370- CollectionHooks . wrapCollection ( Mongo , Mongo )
371- } else {
372- CollectionHooks . wrapCollection ( Meteor , Meteor )
373- }
0 commit comments