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

add base event hooks methods and implement a few #720

Merged
merged 3 commits into from
Feb 2, 2016
Merged

Conversation

jshimko
Copy link
Contributor

@jshimko jshimko commented Jan 21, 2016

Initial WIP implementation of event hooks to be used in places that can't be reached with Collection or Method hooks.

Methods

ReactionCore.Hooks.Events.add(hookName, func(doc));
ReactionCore.Hooks.Events.remove(hookName, funcName);
ReactionCore.Hooks.Events.run(hookName, doc, constant);
ReactionCore.Hooks.Events.runAsync(hookName, doc, constant);

Example usage

Accounts.onCreateUser(function(options, user) {
  // add a hook to alter the user object or do something with its data
  user = ReactionCore.Hooks.Events.run("onCreateUser", user, options);
});

Now you can pass any amount of functions into that hook from anywhere else in the app

// create a callback to run
function logUserEmail(user) {
  console.log("User being created with email: " + user.emails[0].address);
  // do whatever with the user doc and then return it 
  // to the next callback
  return user;
}

// add your callback to the hooks array created above
ReactionCore.Hooks.Events.add("onCreateUser", logUserEmail);

Currently available hooks

ReactionCore.Hooks.Events.add("beforeCreateDefaultAdminUser", callback(user));
ReactionCore.Hooks.Events.add("afterCreateDefaultAdminUser", callback(user));
ReactionCore.Hooks.Events.add("onCreateUser", callback(user));
ReactionCore.Hooks.Events.add("onLogin", callback(options));

ReactionCore.Hooks.Events.add()
ReactionCore.Hooks.Events.remove()
ReactionCore.Hooks.Events.run()
ReactionCore.Hooks.Events.runAsync()
@aaronjudd
Copy link
Contributor

@jshimko see code climate, let's clean those up. I think the naming convention works.. and will let us move collection and method into the same namespace without being confusing.

@tdecaluwe
Copy link
Contributor

Could we also use this for collection hooks? So we could write something like:

ReactionCore.Collections.Products.after.remove(function (...) {
  Hooks.Collections.run("afterRemove", ...)
});

This would make the collection hooks more testable. Spying on a hook now requires spying on private collection-hooks variables.

@ramusus
Copy link
Contributor

ramusus commented Jan 29, 2016

@jshimko, don't you think your hooks system could be separate independent package for any meteor application, not only for Reaction?

@aaronjudd
Copy link
Contributor

@ramusus agree. we've already discussed, we should probably move this along with our other hooks into a reaction-hooks package

@tdecaluwe
Copy link
Contributor

On the separate reaction-hooks package, I think @ramusus makes a good point in #731. Shouldn't hooks related to accounts be defined in reaction-accounts?

@jshimko
Copy link
Contributor Author

jshimko commented Jan 29, 2016

@tdecaluwe the core hooks code doesn't actually define any hooks. It just gives you the means to do it. The example accounts hooks mentioned above (onCreateUser, onLogin) are already in the reaction-accounts package. And I totally agree that's where they should be. :)

@ramusus
Copy link
Contributor

ramusus commented Jan 30, 2016

Yes, I think the core hooks could be completely independent from reaction. Something like meteor-hooks? I didn't look into the code, but I suppose this project should be reusable in any meteor project. But reaction related hooks should be spreaded inside reaction-related packages, where they should belongs to. Like accounts, or PayPal or whatever..

@aaronjudd
Copy link
Contributor

@jshimko 👍 Let's merge this, and refactor in the v0.12 branch as a package.

aaronjudd pushed a commit that referenced this pull request Feb 2, 2016
add base event hooks methods and implement a few
@aaronjudd aaronjudd merged commit 0b4ce50 into development Feb 2, 2016
@aaronjudd aaronjudd deleted the event-hooks branch February 2, 2016 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants