Debian Codegraph @

deferred/ext/array/reduce.js

raw
1 // Promise aware Array's reduce
2
3 "use strict";
4
5 var assign = require("es5-ext/object/assign")
6 , isValue = require("es5-ext/object/is-value")
7 , ensureValue = require("es5-ext/object/valid-value")
8 , callable = require("es5-ext/object/valid-callable")
9 , toNaturalNumber = require("es5-ext/number/to-pos-integer")
10 , deferred = require("../../deferred")
11 , isPromise = require("../../is-promise")
12 , assimilate = require("../../assimilate");
13
14 var call = Function.prototype.call, resolve = deferred.resolve, Reduce;
15
16 Reduce = function (list, cb, initial, initialized) {
17 this.list = list;
18 this.cb = cb;
19 this.initialized = initialized;
20 this.length = toNaturalNumber(list.length);
21
22 initial = assimilate(initial);
23 if (isPromise(initial)) {
24 if (!initial.resolved) {
25 assign(this, deferred());
26 initial.done(
27 function (resolvedInitial) {
28 this.value = resolvedInitial;
29 this.init();
30 }.bind(this),
31 this.reject
32 );
33 return this.promise;
34 }
35 this.value = initial.value;
36 if (initial.failed) return initial;
37 } else {
38 this.value = initial;
39 }
40
41 return this.init();
42 };
43
44 Reduce.prototype = {
45 current: 0,
46 state: false,
47 init: function () {
48 while (this.current < this.length) {
49 if (hasOwnProperty.call(this.list, this.current)) break;
50 ++this.current;
51 }
52 if (this.current === this.length) {
53 if (!this.initialized) {
54 throw new Error("Reduce of empty array with no initial value");
55 }
56 return this.resolve ? this.resolve(this.value) : resolve(this.value);
57 }
58 if (!this.promise) assign(this, deferred());
59 this.processCb = this.processCb.bind(this);
60 this.processValue = this.processValue.bind(this);
61 this.continue();
62 return this.promise;
63 },
64 continue: function () {
65 var result;
66 while (!this.state) {
67 result = this.process();
68 if (this.state !== "cb") break;
69 result = this.processCb(result);
70 if (this.state !== "value") break;
71 this.processValue(result);
72 }
73 },
74 process: function () {
75 var value = assimilate(this.list[this.current]);
76 if (isPromise(value)) {
77 if (!value.resolved) {
78 value.done(
79 function (result) {
80 result = this.processCb(result);
81 if (this.state !== "value") return;
82 this.processValue(result);
83 if (!this.state) this.continue();
84 }.bind(this),
85 this.reject
86 );
87 return null;
88 }
89 if (value.failed) {
90 this.reject(value.value);
91 return null;
92 }
93 value = value.value;
94 }
95 this.state = "cb";
96 return value;
97 },
98 processCb: function (value) {
99 if (!this.initialized) {
100 this.initialized = true;
101 this.state = "value";
102 return value;
103 }
104 if (this.cb) {
105 try {
106 value = call.call(this.cb, undefined, this.value, value, this.current, this.list);
107 } catch (e) {
108 this.reject(e);
109 return null;
110 }
111 value = assimilate(value);
112 if (isPromise(value)) {
113 if (!value.resolved) {
114 value.done(
115 function (result) {
116 this.state = "value";
117 this.processValue(result);
118 if (!this.state) this.continue();
119 }.bind(this),
120 this.reject
121 );
122 return null;
123 }
124 if (value.failed) {
125 this.reject(value.value);
126 return null;
127 }
128 value = value.value;
129 }
130 }
131 this.state = "value";
132 return value;
133 },
134 processValue: function (value) {
135 this.value = value;
136 while (++this.current < this.length) {
137 if (hasOwnProperty.call(this.list, this.current)) {
138 this.state = false;
139 return;
140 }
141 }
142 this.resolve(this.value);
143 }
144 };
145
146 module.exports = function (cb/*, initial*/) {
147 ensureValue(this);
148 if (isValue(cb)) callable(cb);
149
150 return new Reduce(this, cb, arguments[1], arguments.length > 1);
151 };