UNPKG

@sap/odata-v4

Version:

OData V4.0 server library

72 lines (60 loc) 2.27 kB
'use strict'; const EventEmitter = require('events').EventEmitter; /** * The `listener` function which will be called on event. * * @callback Listener * @param {*} nextProperty The first context property which is the next context property for the next recursive call * @param {Array} nextStack The current stack * @param {*} nextContext The second context property which is the next context property for the next recursive call * @param {Next} next The callback to be called */ /** * The `next` function which has to be called on event for recursion. * * @callback Next * @param {*} nextProperty The first context property which is the next context property for the next recursive call * @param {Array} nextStack The current stack * @param {*} nextContext The second context property which is the next context property for the next recursive call * @param {string} [eventName] The name of the event to fire. This is optional */ /** * The Visitor visits each property of the provided object * * @extends EventEmitter */ class Visitor extends EventEmitter { /** * Creates an instance of Visitor. */ constructor() { super(); } /** * Registers an event listener on the visitor. * * @param {string} name The name of the event listener. * @param {Listener} listener The listener to call on the registered event name * @returns {EventEmitter} this instance */ on(name, listener) { return super.on(name, listener.bind(this)); } /** * This method visits the target and can do recursive calls up on calling the next function. * * @param {*} target The root context to visit * @param {?*} currentContext The current context to have as a second context * @param {*|Array} stack The current stack filled by the application */ visit(target, currentContext = null, stack = []) { const next = (nextProperty, nextContext, nextStack, eventName) => { if (eventName) { this.emit(eventName, nextProperty, nextStack, nextContext, next); } this.visit(nextProperty, nextContext, nextStack); }; this.emit('visit', target, stack, currentContext, next); } } module.exports = Visitor;