{"version":3,"file":"binop.cjs","names":["BaseChannel","EmptyChannelError"],"sources":["../../src/channels/binop.ts"],"sourcesContent":["import { EmptyChannelError } from \"../errors.js\";\nimport { BaseChannel } from \"./base.js\";\n\nexport type BinaryOperator<ValueType, UpdateType> = (\n  a: ValueType,\n  b: UpdateType\n) => ValueType;\n\n/**\n * Stores the result of applying a binary operator to the current value and each new value.\n */\nexport class BinaryOperatorAggregate<\n  ValueType,\n  UpdateType = ValueType\n> extends BaseChannel<ValueType, UpdateType, ValueType> {\n  lc_graph_name = \"BinaryOperatorAggregate\";\n\n  value: ValueType | undefined;\n\n  operator: BinaryOperator<ValueType, UpdateType>;\n\n  initialValueFactory?: () => ValueType;\n\n  constructor(\n    operator: BinaryOperator<ValueType, UpdateType>,\n    initialValueFactory?: () => ValueType\n  ) {\n    super();\n\n    this.operator = operator;\n    this.initialValueFactory = initialValueFactory;\n    this.value = initialValueFactory?.();\n  }\n\n  public fromCheckpoint(checkpoint?: ValueType) {\n    const empty = new BinaryOperatorAggregate(\n      this.operator,\n      this.initialValueFactory\n    );\n    if (typeof checkpoint !== \"undefined\") {\n      empty.value = checkpoint;\n    }\n    return empty as this;\n  }\n\n  public update(values: UpdateType[]): boolean {\n    let newValues = values;\n    if (!newValues.length) return false;\n\n    if (this.value === undefined) {\n      [this.value as UpdateType] = newValues;\n      newValues = newValues.slice(1);\n    }\n\n    for (const value of newValues) {\n      if (this.value !== undefined) {\n        this.value = this.operator(this.value, value);\n      }\n    }\n    return true;\n  }\n\n  public get(): ValueType {\n    if (this.value === undefined) {\n      throw new EmptyChannelError();\n    }\n    return this.value;\n  }\n\n  public checkpoint(): ValueType {\n    if (this.value === undefined) {\n      throw new EmptyChannelError();\n    }\n    return this.value;\n  }\n\n  isAvailable(): boolean {\n    return this.value !== undefined;\n  }\n}\n"],"mappings":";;;;;;;AAWA,IAAa,0BAAb,MAAa,gCAGHA,yBAA8C;CACtD,gBAAgB;CAEhB;CAEA;CAEA;CAEA,YACE,UACA,qBACA;AACA;AAEA,OAAK,WAAW;AAChB,OAAK,sBAAsB;AAC3B,OAAK,QAAQ;;CAGf,AAAO,eAAe,YAAwB;EAC5C,MAAM,QAAQ,IAAI,wBAChB,KAAK,UACL,KAAK;AAEP,MAAI,OAAO,eAAe,YACxB,OAAM,QAAQ;AAEhB,SAAO;;CAGT,AAAO,OAAO,QAA+B;EAC3C,IAAI,YAAY;AAChB,MAAI,CAAC,UAAU,OAAQ,QAAO;AAE9B,MAAI,KAAK,UAAU,QAAW;AAC5B,IAAC,KAAK,SAAuB;AAC7B,eAAY,UAAU,MAAM;;AAG9B,OAAK,MAAM,SAAS,UAClB,KAAI,KAAK,UAAU,OACjB,MAAK,QAAQ,KAAK,SAAS,KAAK,OAAO;AAG3C,SAAO;;CAGT,AAAO,MAAiB;AACtB,MAAI,KAAK,UAAU,OACjB,OAAM,IAAIC;AAEZ,SAAO,KAAK;;CAGd,AAAO,aAAwB;AAC7B,MAAI,KAAK,UAAU,OACjB,OAAM,IAAIA;AAEZ,SAAO,KAAK;;CAGd,cAAuB;AACrB,SAAO,KAAK,UAAU"}