OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-object-observe | |
29 // Flags: --allow-natives-syntax | 28 // Flags: --allow-natives-syntax |
30 | 29 |
31 var ordering = []; | 30 var ordering = []; |
32 function reset() { | 31 function reset() { |
33 ordering = []; | 32 ordering = []; |
34 } | 33 } |
35 | 34 |
36 function assertArrayValues(expected, actual) { | 35 function assertArrayValues(expected, actual) { |
37 assertEquals(expected.length, actual.length); | 36 assertEquals(expected.length, actual.length); |
38 for (var i = 0; i < expected.length; i++) { | 37 for (var i = 0; i < expected.length; i++) { |
(...skipping 25 matching lines...) Expand all Loading... |
64 next = next.then(function(value) { | 63 next = next.then(function(value) { |
65 ordering.push('p' + id + ':' + t++); | 64 ordering.push('p' + id + ':' + t++); |
66 return fn ? fn(value) : value; | 65 return fn ? fn(value) : value; |
67 }); | 66 }); |
68 | 67 |
69 return this; | 68 return this; |
70 } | 69 } |
71 }; | 70 }; |
72 } | 71 } |
73 | 72 |
74 function newObserver(id, fn, obj) { | |
75 var observer = { | |
76 value: 1, | |
77 recordCounts: [] | |
78 }; | |
79 | |
80 Object.observe(observer, function(records) { | |
81 ordering.push('o' + id); | |
82 observer.recordCounts.push(records.length); | |
83 if (fn) fn(); | |
84 }); | |
85 | |
86 return observer; | |
87 } | |
88 | |
89 | |
90 (function PromiseThens() { | 73 (function PromiseThens() { |
91 reset(); | 74 reset(); |
92 | 75 |
93 var p1 = newPromise(1).then(); | 76 var p1 = newPromise(1).then(); |
94 var p2 = newPromise(2).then(); | 77 var p2 = newPromise(2).then(); |
95 | 78 |
96 p1.resolve(); | 79 p1.resolve(); |
97 p2.resolve(); | 80 p2.resolve(); |
98 | 81 |
99 assertOrdering(['p1', 'p2', 'p1:1', 'p2:1']); | 82 assertOrdering(['p1', 'p2', 'p1:1', 'p2:1']); |
100 })(); | 83 })(); |
101 | |
102 | |
103 (function ObserversBatch() { | |
104 reset(); | |
105 | |
106 var p1 = newPromise(1); | |
107 var p2 = newPromise(2); | |
108 var p3 = newPromise(3); | |
109 | |
110 var ob1 = newObserver(1); | |
111 var ob2 = newObserver(2, function() { | |
112 ob3.value++; | |
113 p3.resolve(); | |
114 ob1.value++; | |
115 }); | |
116 var ob3 = newObserver(3); | |
117 | |
118 p1.resolve(); | |
119 ob1.value++; | |
120 p2.resolve(); | |
121 ob2.value++; | |
122 | |
123 assertOrdering(['p1', 'o1', 'o2', 'p2', 'o1', 'o3', 'p3']); | |
124 assertArrayValues([1, 1], ob1.recordCounts); | |
125 assertArrayValues([1], ob2.recordCounts); | |
126 assertArrayValues([1], ob3.recordCounts); | |
127 })(); | |
128 | |
129 | |
130 (function ObserversGetAllRecords() { | |
131 reset(); | |
132 | |
133 var p1 = newPromise(1); | |
134 var p2 = newPromise(2); | |
135 var ob1 = newObserver(1, function() { | |
136 ob2.value++; | |
137 }); | |
138 var ob2 = newObserver(2); | |
139 | |
140 p1.resolve(); | |
141 ob1.value++; | |
142 p2.resolve(); | |
143 ob2.value++; | |
144 | |
145 assertOrdering(['p1', 'o1', 'o2', 'p2']); | |
146 assertArrayValues([1], ob1.recordCounts); | |
147 assertArrayValues([2], ob2.recordCounts); | |
148 })(); | |
149 | |
150 | |
151 (function NewObserverDeliveryGetsNewMicrotask() { | |
152 reset(); | |
153 | |
154 var p1 = newPromise(1); | |
155 var p2 = newPromise(2); | |
156 var ob1 = newObserver(1); | |
157 var ob2 = newObserver(2, function() { | |
158 ob1.value++; | |
159 }); | |
160 | |
161 p1.resolve(); | |
162 ob1.value++; | |
163 p2.resolve(); | |
164 ob2.value++; | |
165 | |
166 assertOrdering(['p1', 'o1', 'o2', 'p2', 'o1']); | |
167 assertArrayValues([1, 1], ob1.recordCounts); | |
168 assertArrayValues([1], ob2.recordCounts); | |
169 })(); | |
OLD | NEW |