Debian Codegraph @

deferred/lib/some-every.js

raw
1 // Promise aware Array's some
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;
15
16 module.exports = function (resolvent) {
17 var Iterator = function (list, cb, context) {
18 this.list = list;
19 this.cb = cb;
20 this.context = context;
21 this.length = toNaturalNumber(list.length);
22
23 while (this.current < this.length) {
24 if (this.current in list) {
25 assign(this, deferred());
26 this.processCb = this.processCb.bind(this);
27 this.processValue = this.processValue.bind(this);
28 this.continue();
29 return this.promise;
30 }
31 ++this.current;
32 }
33 return resolve(!resolvent);
34 };
35
36 Iterator.prototype = {
37 current: 0,
38 state: false,
39 continue: function () {
40 var result;
41 while (!this.state) {
42 result = this.process();
43 if (this.state !== "cb") break;
44 result = this.processCb(result);
45 if (this.state !== "value") break;
46 this.processValue(result);
47 }
48 },
49 process: function () {
50 var value = assimilate(this.list[this.current]);
51 if (isPromise(value)) {
52 if (!value.resolved) {
53 value.done(
54 function (result) {
55 result = this.processCb(result);
56 if (this.state !== "value") return;
57 this.processValue(result);
58 if (!this.state) this.continue();
59 }.bind(this),
60 this.reject
61 );
62 return null;
63 }
64 if (value.failed) {
65 this.reject(value.value);
66 return null;
67 }
68 value = value.value;
69 }
70 this.state = "cb";
71 return value;
72 },
73 processCb: function (value) {
74 if (this.cb) {
75 try {
76 value = call.call(this.cb, this.context, value, this.current, this.list);
77 } catch (e) {
78 this.reject(e);
79 return null;
80 }
81 value = assimilate(value);
82 if (isPromise(value)) {
83 if (!value.resolved) {
84 value.done(
85 function (result) {
86 this.state = "value";
87 this.processValue(result);
88 if (!this.state) this.continue();
89 }.bind(this),
90 this.reject
91 );
92 return null;
93 }
94 if (value.failed) {
95 this.reject(value.value);
96 return null;
97 }
98 value = value.value;
99 }
100 }
101 this.state = "value";
102 return value;
103 },
104 processValue: function (value) {
105 if (Boolean(value) === resolvent) {
106 this.resolve(resolvent);
107 return;
108 }
109 while (++this.current < this.length) {
110 if (this.current in this.list) {
111 this.state = false;
112 return;
113 }
114 }
115 this.resolve(!resolvent);
116 }
117 };
118
119 return function (cb/*, thisArg*/) {
120 ensureValue(this);
121 if (isValue(cb)) callable(cb);
122
123 return new Iterator(this, cb, arguments[1]);
124 };
125 };