blob: 01b971ddc0cb9c7ef3da42a0437ae4e937519948 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028// Flags: --harmony-object-observe
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029// Flags: --allow-natives-syntax
30
31var ordering = [];
32function reset() {
33 ordering = [];
34}
35
36function assertArrayValues(expected, actual) {
37 assertEquals(expected.length, actual.length);
38 for (var i = 0; i < expected.length; i++) {
39 assertEquals(expected[i], actual[i]);
40 }
41}
42
43function assertOrdering(expected) {
44 %RunMicrotasks();
45 assertArrayValues(expected, ordering);
46}
47
48function newPromise(id, fn) {
49 var r;
50 var t = 1;
51 var promise = new Promise(function(resolve) {
52 r = resolve;
53 if (fn) fn();
54 });
55
56 var next = promise.then(function(value) {
57 ordering.push('p' + id);
58 return value;
59 });
60
61 return {
62 resolve: r,
63 then: function(fn) {
64 next = next.then(function(value) {
65 ordering.push('p' + id + ':' + t++);
66 return fn ? fn(value) : value;
67 });
68
69 return this;
70 }
71 };
72}
73
74function 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() {
91 reset();
92
93 var p1 = newPromise(1).then();
94 var p2 = newPromise(2).then();
95
96 p1.resolve();
97 p2.resolve();
98
99 assertOrdering(['p1', 'p2', 'p1:1', 'p2:1']);
100})();
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})();