1+ /*
2+ * Copyright 2015 IBM Corp.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ var Utils = require ( './utils.js' ) ;
18+
19+ /**
20+ * A data type that can be accumulated, ie has an commutative and associative "add" operation,
21+ * but where the result type, `R`, may be different from the element type being added, `T`.
22+ *
23+ * You must define how to add data, and how to merge two of these together. For some data types,
24+ * such as a counter, these might be the same operation. In that case, you can use the simpler
25+ * {@link Accumulator}. They won't always be the same, though -- e.g., imagine you are
26+ * accumulating a set. You will add items to the set, and you will union two sets together.
27+ *
28+ * @classdesc
29+ * @constructor
30+ */
31+ /*
32+ * NOTE for now EclairJS will only support floats and int types
33+ *
34+ *
35+ */
36+ function Accumulable ( kernelP , refIdP ) {
37+ this . kernelP = kernelP ;
38+ this . refIdP = refIdP ;
39+ }
40+
41+ /**
42+ * Add more data to this accumulator / accumulable
43+ * @param {object } term the data to add
44+ * @returns {Promise.<Void> } A Promise that resolves to nothing.
45+ */
46+ Accumulable . prototype . add = function ( term ) {
47+ var templateStr = '{{inRefId}}.add({{term}});' ;
48+ return Utils . generateVoidPromise ( this , templateStr , { term : Utils . prepForReplacement ( term ) } ) ;
49+ } ;
50+
51+
52+ /**
53+ * Merge two accumulable objects together
54+ *
55+ * Normally, a user will not want to use this version, but will instead call `add`.
56+ * @param {object } term the other `R` that will get merged with this
57+ * @returns {Promise.<Void> } A Promise that resolves to nothing.
58+ */
59+ Accumulable . prototype . merge = function ( term ) {
60+ var templateStr = '{{inRefId}}.merge({{term}});' ;
61+ return Utils . generateVoidPromise ( this , templateStr , { term : Utils . prepForReplacement ( term ) } ) ;
62+ } ;
63+
64+ /**
65+ * Access the accumulator's current value; only allowed on master.
66+ * @returns {Promise.<object> }
67+ */
68+ Accumulable . prototype . value = function ( ) {
69+ function _resolve ( result , resolve , reject ) {
70+ resolve ( parseFloat ( result ) ) ;
71+ }
72+
73+ var templateStr = '{{inRefId}}.value();' ;
74+
75+ return Utils . generateResultPromise ( this , templateStr , null , _resolve ) ;
76+ } ;
77+
78+ /**
79+ * Get the current value of this accumulator from within a task.
80+ *
81+ * This is NOT the global value of the accumulator. To get the global value after a
82+ * completed operation on the dataset, call `value`.
83+ *
84+ * The typical use of this method is to directly mutate the local value, eg., to add
85+ * an element to a Set.
86+ * @returns {Promise.<object> }
87+ */
88+ Accumulable . prototype . localValue = function ( ) {
89+ function _resolve ( result , resolve , reject ) {
90+ resolve ( parseFloat ( result ) ) ;
91+ }
92+
93+ var templateStr = '{{inRefId}}.localValue();' ;
94+
95+ return Utils . generateResultPromise ( this , templateStr , null , _resolve ) ;
96+ } ;
97+
98+ /**
99+ * Set the accumulator's value; only allowed on master
100+ * @param {object }
101+ * @returns {Promise.<Void> } A Promise that resolves to nothing.
102+ */
103+ Accumulable . prototype . setValue = function ( newValue ) {
104+ var templateStr = '{{inRefId}}.setValue({{newValue}});' ;
105+
106+ return Utils . generateVoidPromise ( this , templateStr , { newValue : Utils . prepForReplacement ( newValue ) } ) ;
107+ } ;
108+
109+ /**
110+ * @returns {Promise.<string> }
111+ */
112+ Accumulable . prototype . toString = function ( ) {
113+ var templateStr = '{{inRefId}}.toString();' ;
114+ return Utils . generateResultPromise ( this , templateStr ) ;
115+ } ;
116+
117+ module . exports = Accumulable ;
0 commit comments