All files / tao-utils/src forward-chain.js

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83        4x 4x 4x 4x 4x                                               2x                                               1x                                               1x    
import { AppCtx, INTERCEPT, ASYNC, INLINE } from '@tao.js/core';
import { transferToAppCtx } from './transfer';
 
function forward(kernel, from, to, type, opts) {
  const { t, term, a, action, o, orient } = to;
  const handler = (tao, data) => transferToAppCtx(tao, data, to, opts);
  kernel[`add${type}Handler`](from, handler);
  handler.remove = () => kernel[`remove${type}Handler`](from, handler);
  return handler;
}
 
/**
 * Convenience function that strictly copies the Incoming (from) Application Context to the defined Outgoing (to)
 * Application Context to chain it along using an `InlineHandler` setting the data as:
 * - from.t => to.t
 * - from.a => to.a
 * - from.o => to.o
 *
 * Returns the created handler function as well as a .remove() function on the handler to
 * automatically remove it from the kernel that was passed in
 *
 * @export
 * @param {import('@tao.js/core').Kernel} kernel a TAO Kernel (TAO) that can attach handlers and receive TAO signals
 * @param {Object} from trigram representing the Application Context to chain from
 * @param {Object} to trigram representing the Application Context to chain to
 * @param {Object} [opts] optional functions to transform the datagrams in the new AppCtx
 * @param {function(*): *} [opts.transformTerm] transforms the term datagram
 * @param {function(*): *} [opts.transformAction] transforms the action datagram
 * @param {function(*): *} [opts.transformOrient] transforms the orient datagram
 * @return {(function(Object, Object): AppCtx) & {remove: function(): void}} the handler (also exposing `.remove()`) so that it can be removed from the kernel if needed
 */
export function forwardInline(kernel, from, to, opts) {
  return forward(kernel, from, to, INLINE, opts);
}
 
/**
 * Convenience function that strictly copies the Incoming (from) Application Context to the defined Outgoing (to)
 * Application Context to chain it along using an `AsyncHandler` setting the data as:
 * - from.t => to.t
 * - from.a => to.a
 * - from.o => to.o
 *
 * Returns the created handler function as well as a .remove() function on the handler to
 * automatically remove it from the kernel that was passed in
 *
 * @export
 * @param {import('@tao.js/core').Kernel} kernel a TAO Kernel (TAO) that can attach handlers and receive TAO signals
 * @param {Object} from trigram representing the Application Context to chain from
 * @param {Object} to trigram representing the Application Context to chain to
 * @param {Object} [opts] optional functions to transform the datagrams in the new AppCtx
 * @param {function(*): *} [opts.transformTerm] transforms the term datagram
 * @param {function(*): *} [opts.transformAction] transforms the action datagram
 * @param {function(*): *} [opts.transformOrient] transforms the orient datagram
 * @return {(function(Object, Object): AppCtx) & {remove: function(): void}} the handler (also exposing `.remove()`) so that it can be removed from the kernel if needed
 */
export function forwardAsync(kernel, from, to, opts) {
  return forward(kernel, from, to, ASYNC, opts);
}
 
/**
 * Convenience function that strictly copies the Incoming (from) Application Context to the defined Outgoing (to)
 * Application Context to chain it along using an `InterceptHandler` setting the data as:
 * - from.t => to.t
 * - from.a => to.a
 * - from.o => to.o
 *
 * Returns the created handler function as well as a .remove() function on the handler to
 * automatically remove it from the kernel that was passed in
 *
 * @export
 * @param {import('@tao.js/core').Kernel} kernel a TAO Kernel (TAO) that can attach handlers and receive TAO signals
 * @param {Object} from trigram representing the Application Context to chain from
 * @param {Object} to trigram representing the Application Context to chain to
 * @param {Object} [opts] optional functions to transform the datagrams in the new AppCtx
 * @param {function(*): *} [opts.transformTerm] transforms the term datagram
 * @param {function(*): *} [opts.transformAction] transforms the action datagram
 * @param {function(*): *} [opts.transformOrient] transforms the orient datagram
 * @return {(function(Object, Object): AppCtx) & {remove: function(): void}} the handler (also exposing `.remove()`) so that it can be removed from the kernel if needed
 */
export function forwardIntercept(kernel, from, to, opts) {
  return forward(kernel, from, to, INTERCEPT, opts);
}