The thorin dispatcher

The Thorin dispatcher can be viewed as a small router within your application, handling action matching, intent creation, sending and receiving. You use thorin.dispatcher to register any kind of action that your application is capable to handle as well as any middleware or authorization functions that can be reused in other actions.

Some Thorin plugins extend the core functionality of your application by registering authorization middleware that you can later include in your action declaration as dependencies.

Since most Thorin components will interact with the dispatcher, it also emits a few events:

  • middleware - emitted when a new middleware was registered in the dispatcher
  • action - emitted when a new action was registered in the dispatcher
  • transport - emitted when a new transport was registered in the dispatcher
  • intent - emitted whenever an intent has generated an output (success or error) and completed.
Functionality

Next to the functions exported below, the Thorin dispatcher also exposes its actions property, as an array of actionObj that were registered.

dispatcher.registerTransport(transportObj)
Registers the given transport instance and attaches any previously registered actions to it. Transports will then be able to trigger actions by creating an intent and allowing the dispatcher to route that intent to the respective actions.
  • transportObjinstance of Transport - a transport instance that extends thorin.Interface.Transport
dispatcher.addTemplate(actionObj)
Registers a new template action to the dispatcher. Template actions are special actions that are used to encapsulate action specific information (such as middlewares, authorisations or root alias paths). An action can then extend a template, essentially inheriting its preconditions.
  • actionObjinstance of thorin.Action an action instance that will be used as a template
dispatcher.addAction(actionObj, opt)
Registers an action to the dispatcher. Whenever the given action's code is triggered by one of the transports, it will be routed through its inner callback stack. You can also add actions that are attached only to specific transports, by specifying the transport's name in opt.transport
  • actionObjinstance of thorin.Action* | string* an action instance that will be used by the inner routing system. If a string is provided, a new action will be created by the dispatcher and use the string as a code.
  • optobject additional options that can be attached for this action.
When specifying opt.transport, the action will only be attached to the given transport name.
dispatcher.addMiddleware(middlewareObj)
Registers a new middleware inside the dispatcher. A middleware encapsulates specific functionality that can be used and reused throughout the application when defining an action, another middleware or authorization.
  • middlewareObjinstance of thorin.Middleware* | string* a middleware instance that will be available to other actions or middlewares to include in their execution stack. If a string is provided, a new middleware will be created by the dispatcher and use the string as its name.
dispatcher.addAuthorization(authObj)
Authorization functions should be used to perform any kind of authorization on the incoming action request.
  • authObjinstance of thorin.Authorization* | string* an authorization instance. If a string is provided, a new authorization instance will be created by the dispatcher and use the string as its name.
dispatcher.getMiddleware(name) : middlewareObj
Returns a previously registered middleware by its name.
  • namestring the middleware name.
dispatcher.getAuthorization(name) : authObj
Returns a previously registered authorization by its name.
  • namestring the authorization name.
dispatcher.getAction(name) : actionObj
Returns a previously registered action by its name.
  • namestring* the action name.
dispatcher.validate(sanitizeType, opt) : validateObj
Returns a new intent validator instance that is used to sanitize a key in the incoming payload
  • sanitizeTypestring* the sanitizer code to use for sanitization.
  • optobject an object containing additional options to pass to the sanitizer function.
dispatcher.start()
Starts the dispatcher and binds all the registered actions to all transports. This is used internally.
dispatcher.triggerIntent(intentObj)
The function should be called by the transport layer to signal a new intent. The dispatcher will then route the intent to the appropriate action handler
  • intentObjinstance of thorin.Intent The intent instance to trigger.
dispatcher.validateIntent(intentObj, validations, done)
Validates the incoming intent data by applying any previous validations. This is an internal function.
  • intentObjinstance of thorin.Intent* The intent instance to validate
  • validationsarray of validateObj* An array of validation objects to apply on the intent's input data
  • donefunction* a function to call back when the validation is completed.
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.