I am attempting to use the npm activitystrea.ms package to ease and perhaps standardize a bit the building of activity records in my app.
My first attempt failed because I tried to do a database insert inside of a callback that is in a method at which point meteor complained that meteor functions were being executed outside of a fiber. That code looked like:
as.create().
actor(as.person().id(group.members[0].userId)).
object(as.group().id(groupId).name(group.name)).
startTimeNow().
get().
export((err, doc) => { Activities.insert(doc); });
After a little research, I found that I needed to use bindEnvironment. So, the code now looks like:
const boundFunction = Meteor.bindEnvironment((err, doc) => {
Activities.insert(doc);
}, function (e) {throw e;});
as.create().
actor(as.person().id(group.members[0].userId)).
object(as.group().id(groupId).name(group.name)).
startTimeNow().
get().
export(boundFunction);
This works, but doesn’t seem very clean and I’m going to be doing this throughout my methods.
I’ve read that promises automagically use fibers and avoid the untidyness of the bindEnvironment approach. But, the examples of Promise.denodeify that I’ve found don’t seem to address how to fix the export function in the above example so that it will take an ActivityStream object and return a promise.
My goal would be to be able to do something like this in my methods:
as.create().
actor(as.person().id(group.members[0].userId)).
object(as.group().id(groupId).name(group.name)).
startTimeNow().
get().
exportPromise().
then(function(doc) { Activities.insert(doc); });
How would this be done? Would I have to subclass “as” perhaps?
(Note that I’m using the “wip-upgrade-to-node4” branch of meteor if that makes a difference… I think the promise implementation is changing).