Git Product home page Git Product logo

hooks's Introduction

โš  Warning: This project has been archived and is not maintained.

Hooks

Reaction Hooks provides a simple API that allows you to attach arbitrary callbacks to any particular event that you define. You simply decide what your events are and then add callbacks to it from anywhere in the app. Then you can run all callbacks when that event is fired. The canonical examples are onCoreInit/afterCoreInit event hooks that run code either during Reaction's startup sequence, or after it has completed. (the on/after is just a naming convention, but we suggest that code extending this code utilize this same convention)

Install

import Hooks from "@reactioncommerce/hooks";

Usage

The API has the following methods:

Hooks.Event.add(name, callback)

Here you pass in the name of the event as a String and then the callback function you wish to be executed.

Hooks.Event.remove(name, callback)

Remove a named callback function that has already been added to an event.

Hooks.Events.run(name, item, constant)

For already defined events you will never need to use this, but if you are defining your own events this is what you would execute to fire the event hook. The name argument is the name of the event, the object/modifier on which to run the callbacks, and a constant to be passed to every callback (e.g. for a user-related event, it might be the user)

Example

Let's say we wanted to create our own event for "onCreateUser". We would call the run method when the event occurred.

import Hooks from "@reactioncommerce/hooks";

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

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

import Hooks from "@reactioncommerce/hooks";

// 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
Hooks.Events.add("onCreateUser", logUserEmail);

hooks's People

Contributors

jshimko avatar spencern avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hooks's Issues

Snyk: Deactivate

This repo is 2 years stale and has been migrated away from within current reaction versions.

For backward compatibility, this probably cannot be archived but this repo should be deactivated from within snyk.

Add a version that will do the equivalent of `Promise.all`

We need to be able to run all event handlers and then run a function afterwards.

So possibly a version that is Hooks.Events.addAsync
and then Hooks.Events.runAsync that runs all the handlers and then can run a function.

Use Case: After I import a product, I want other modules to be able to run transformations on that imported product, but after they are all done, I want to publish the product.

We could just use Promise.all if we ensured that all functions added to Hook.Events returned a promise. Or we could use something like async.parallel from the async library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.