Skip to content

Commit bb85f4d

Browse files
committed
Integrate lai:collection-extensions
1 parent e48b062 commit bb85f4d

File tree

3 files changed

+15
-35
lines changed

3 files changed

+15
-35
lines changed

packages/meteor-collection-hooks/collection-hooks.js

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Meteor } from 'meteor/meteor'
22
import { Mongo } from 'meteor/mongo'
33
import { EJSON } from 'meteor/ejson'
44
import { 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

366354
CollectionHooks.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-
}

packages/meteor-collection-hooks/package.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Package.onUse(function (api) {
1616
'tracker',
1717
'ejson',
1818
'minimongo',
19-
'ecmascript'
19+
'ecmascript',
20+
'lai:collection-extensions'
2021
])
2122

2223
api.use('zodern:[email protected]', 'server')
@@ -37,10 +38,7 @@ Package.onTest(function (api) {
3738
'accounts-base',
3839
'accounts-password',
3940
'mongo',
40-
'ddp',
41-
'test-helpers',
4241
'ecmascript',
4342
'jquery'
44-
]);
45-
43+
])
4644
})

tests-app/.meteor/versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ [email protected]
4141
4242
4343
44+
4445
4546
4647

0 commit comments

Comments
 (0)