Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Meteor v3 #309

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 64 additions & 34 deletions collection-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const CollectionHooks = {
all: { insert: {}, update: {}, remove: {}, find: {}, findOne: {}, all: {} }
},
directEnv: new Meteor.EnvironmentVariable(),
// TODO(v3): withValue returns a promise now
directOp (func) {
return this.directEnv.withValue(true, func)
},
Expand Down Expand Up @@ -90,6 +91,7 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se

const asyncMethod = method + 'Async'

// TODO(v3): don't understand why this is necessary. Maybe related to Meteor 2.x and async?
if (constructor.prototype[asyncMethod]) {
self.direct[asyncMethod] = function (...args) {
return CollectionHooks.directOp(function () {
Expand All @@ -98,43 +100,60 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
}
}

collection[method] = function (...args) {
if (CollectionHooks.directEnv.get() === true) {
return _super.apply(collection, args)
function getWrappedMethod (_super) {
return function wrappedMethod (...args) {
// TODO(v2): not quite sure why _super in the first updateAsync call points to LocalCollection's wrapped async method which
// will then again call this wrapped method
if (
(method === 'update' && this.update.isCalledFromAsync) ||
(method === 'remove' && this.remove.isCalledFromAsync) ||
CollectionHooks.directEnv.get() === true) {
return _super.apply(collection, args)
}

// NOTE: should we decide to force `update` with `{upsert:true}` to use
// the `upsert` hooks, this is what will accomplish it. It's important to
// realize that Meteor won't distinguish between an `update` and an
// `insert` though, so we'll end up with `after.update` getting called
// even on an `insert`. That's why we've chosen to disable this for now.
// if (method === "update" && Object(args[2]) === args[2] && args[2].upsert) {
// method = "upsert";
// advice = CollectionHooks.getAdvice(method);
// }

return advice.call(this,
CollectionHooks.getUserId(),
_super,
self,
method === 'upsert'
? {
insert: self._hookAspects.insert || {},
update: self._hookAspects.update || {},
upsert: self._hookAspects.upsert || {}
}
: self._hookAspects[method] || {},
function (doc) {
return (
typeof self._transform === 'function'
? function (d) { return self._transform(d || doc) }
: function (d) { return d || doc }
)
},
args,
false
)
}
}

// NOTE: should we decide to force `update` with `{upsert:true}` to use
// the `upsert` hooks, this is what will accomplish it. It's important to
// realize that Meteor won't distinguish between an `update` and an
// `insert` though, so we'll end up with `after.update` getting called
// even on an `insert`. That's why we've chosen to disable this for now.
// if (method === "update" && Object(args[2]) === args[2] && args[2].upsert) {
// method = "upsert";
// advice = CollectionHooks.getAdvice(method);
// }

return advice.call(this,
CollectionHooks.getUserId(),
_super,
self,
method === 'upsert'
? {
insert: self._hookAspects.insert || {},
update: self._hookAspects.update || {},
upsert: self._hookAspects.upsert || {}
}
: self._hookAspects[method] || {},
function (doc) {
return (
typeof self._transform === 'function'
? function (d) { return self._transform(d || doc) }
: function (d) { return d || doc }
)
},
args,
false
)
// TODO(v3): it appears this is necessary
// In Meteor 2 *Async methods call the non-async methods
if (['insert', 'update', 'upsert', 'remove', 'findOne'].includes(method)) {
const _superAsync = collection[asyncMethod]
// const wrapped = getWrappedMethod(_superAsync);
collection[asyncMethod] = getWrappedMethod(_superAsync)
}

collection[method] = getWrappedMethod(_super)
})
}

Expand Down Expand Up @@ -288,6 +307,17 @@ CollectionHooks.wrapCollection = function wrapCollection (ns, as) {
ns.Collection.apply = Function.prototype.apply
}

CollectionHooks.isPromise = (value) => {
return value && typeof value.then === 'function'
}

CollectionHooks.callAfterValueOrPromise = (value, cb) => {
if (CollectionHooks.isPromise(value)) {
return value.then((res) => cb(res))
}
return cb(value)
}

CollectionHooks.modify = LocalCollection._modify

if (typeof Mongo !== 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions findone.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ CollectionHooks.defineAdvice('findOne', function (userId, _super, instance, aspe
o.aspect.call(ctx, userId, selector, options, doc)
})
}

// return because of callAfterValueOrPromise
return doc
}

const ret = _super.call(this, selector, options)
after(ret)

return ret
return CollectionHooks.callAfterValueOrPromise(ret, (ret) => after(ret))
})
5 changes: 4 additions & 1 deletion insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ CollectionHooks.defineAdvice('insert', function (userId, _super, instance, aspec
return _super.call(this, doc, wrappedCallback)
} else {
ret = _super.call(this, doc, callback)
return after((ret && ret.insertedId) || (ret && ret[0] && ret[0]._id) || ret)

return CollectionHooks.callAfterValueOrPromise(ret, (ret) => {
return after((ret && ret.insertedId) || (ret && ret[0] && ret[0]._id) || ret)
})
}
})
Loading
Loading