For events and triggers using AWS SQS.
QUEUE_URL
: The url link to the queue. (SQS url)QUEUE_CALLBACK_URL
: The url that is called by queue.AWS_SQS_REGION
: Region of AWS queueAWS_SQS_API_VERSION
: SQS API version
-
Create a object with key as name of handler and value as function that should run when the handler is called.
const handlers = { sendEmailToBuyer: (args) => console.log(`Thank you for buying the product: ${args.productName} with us.`), sendEmailToSeller: (args) => console.log(`Congratulations! Your product ${args.productName} is purchased.`) }
-
Pass the
handler
object as argument while creating class Handlerimport { Handler } from '@maskeynihal/pursue'; // const handlers = {} define the handler function as given in step 1. const handlerClass = new Handler(handlers)
-
Create a event and handler mapper object.
// const handlerClass = new Handler(handlers) const eventHandlerMapper = { productSold: [ handlerClass.keys.sendEmailToBuyer, handlerClass.keys.sendEmailToSeller ] }
-
Pass the
eventHandlerMapper
object as argument while creating class EventHandlerMapperimport { EventHandlerMapper } from '@maskeynihal/pursue'; // const eventHandlerMapper = {} const eventHandlerMapperClass = new EventHandlerMapper(eventHandlerMapper)
-
Create a new class Job and pass the two classes Handler and EventHandlerMapper as argument
import { Job } from '@maskeynihal/pursue'; // handlerClass // eventHandlerMapperClass const job = new Job(handlerClass, eventHandlerMapperClass);
-
Create a route file to handle the callback from the queue
// import the instance of job // import job from './job'; // using express router.get('/api/queue/callback', (req, res) => { job.dispatch(req.body.scope, req.body.data); res.json({ message: "Dispatched!" }) });
The instance of the class Job should be exported from a file so that it's always pointing to same class (singleton).
All the event key can be found from the instance of class Job
. Event can be triggered calling the trigger
function from instance of the class.
// Get the required key
import job from './job';
const eventKey = job.events.productSold;
job.trigger(eventKey, {
productName: "Macbook 16 inch"
});