import { QueryBodyType , IReducerOption , IQueryContext } from "../../defs" ; import { SPECIAL_PARAM_FIELD } from "../../constants" ; import { INode } from "./INode" ; interface ReducerNodeOptions extends IReducerOption { body : QueryBodyType ; } export default class ReducerNode implements INode { public name : string ; public props : any ; public isSpread : boolean = false ; public reduceFunction ? : any ; public pipeline : any [ ] ; public projection : any ; public dependency : QueryBodyType ; public dependencies : ReducerNode [ ] = [ ] ; public scheduledForDeletion : boolean = false ; constructor ( name , options , public readonly context ? ) { this . name = name ; this . reduceFunction = options . reduce ; this . dependency = options . dependency ; this . pipeline = options . pipeline || [ ] ; this . projection = options . projection || { } ; if ( ! options . projection && ! this . reduceFunction ) { this . projection = { [ name ] : 1 } ; } this . props = options . body [ SPECIAL_PARAM_FIELD ] || { } ; } param { * } object param { * } args public async compute ( object ) { if ( ! this . reduceFunction ) { return ; } object [ this . name ] = await this . reduce . call ( this , object , { ... this . props , context : this . context , } ) ; } param object param args public async reduce ( object , ... args ) { return this . reduceFunction . call ( this , object , ... args ) ; } param projection public blendInProjection ( projection ) { if ( this . projection ) { Object . assign ( projection , this . projection ) ; } } get hasPipeline ( ) { return this . pipeline && this . pipeline . length > 0 ; } }