Skip to content

Commit 6c50239

Browse files
committed
first pass on making aexprs iterable
SQUASHED: AUTO-COMMIT-src-client-reactive-active-expression-active-expression.js,AUTO-COMMIT-src-client-reactive-test-active-expression-utility-methods.spec.js,
1 parent 8278d81 commit 6c50239

File tree

2 files changed

+68
-29
lines changed

2 files changed

+68
-29
lines changed

src/client/reactive/active-expression/active-expression.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -488,33 +488,16 @@ export class BaseActiveExpression {
488488
}
489489

490490
values() {
491-
492-
const disposed = new Promise(resolve => {
493-
if (this.isDisposed()) {
494-
resolve()
495-
} else {
496-
this.on('')
497-
}
498-
});
499-
500-
Promise.race([
501-
this.nextValue().then(v => ({ value: v, done: false })),
502-
])
503-
504-
function newResult(v, done) {
505-
506-
}
507-
491+
const me = this;
508492
return {
509493
[Symbol.asyncIterator]() {
510494
return {
511-
i: 0,
512495
next() {
513-
if (this.i < 3) {
514-
return Promise.resolve({ value: ++this.i, done: false });
515-
}
516-
517-
return Promise.resolve({ done: true });
496+
console.log("NEXT", me.getCurrentValue())
497+
return Promise.race([
498+
me.nextValue().then(v => ({ value: v, done: false })),
499+
me.gotDisposed().then(() => ({ done: true }))
500+
]);
518501
}
519502
};
520503
}

src/client/reactive/test/active-expression/utility-methods.spec.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,81 @@ describe('Iterators and Utility Methods for Active Expressions', () => {
5555
const ae = aexpr(() => val);
5656

5757
(async () => {
58-
lively.sleep(10);
58+
59+
console.log("START ASYNC");
60+
61+
await lively.sleep(10);
62+
console.log("SET VAL", ae.getCurrentValue());
5963
val++;
60-
lively.sleep(10);
64+
65+
await lively.sleep(10);
66+
console.log("SET VAL", ae.getCurrentValue());
6167
val++;
62-
lively.sleep(10);
68+
69+
await lively.sleep(10);
70+
console.log("SET VAL", ae.getCurrentValue());
6371
val++;
6472

65-
lively.sleep(10);
73+
await lively.sleep(10);
74+
console.log("DISPOSE", ae.getCurrentValue());
6675
ae.dispose();
6776

68-
lively.sleep(10);
77+
await lively.sleep(10);
78+
console.log("SET VAL", ae.getCurrentValue());
6979
val++;
7080
})();
71-
81+
82+
let j = 0;
83+
console.log("BEFORE LOOP", val, j);
84+
for await (let v of ae.values()) {
85+
j++;
86+
console.log("IN LOOP", val, v, j);
87+
expect(v).to.equal(j);
88+
}
89+
console.log("AFTER LOOP", val, j);
90+
expect(j).to.equal(3);
91+
});
92+
93+
// #TODO: next: introduce a queue of changed-expressions-results
94+
xit(".values can deal with multiple changes in a synchronous execution", async () => {
95+
let val = 0,
96+
spy = sinon.spy();
97+
98+
const ae = aexpr(() => val);
99+
100+
(async () => {
101+
102+
console.log("START ASYNC");
103+
104+
await lively.sleep(10);
105+
console.log("SET VAL", ae.getCurrentValue());
106+
val++;
107+
108+
await lively.sleep(10);
109+
console.log("SET VAL", ae.getCurrentValue());
110+
val++;
111+
112+
await lively.sleep(10);
113+
console.log("SET VAL", ae.getCurrentValue());
114+
val++;
115+
116+
await lively.sleep(10);
117+
console.log("DISPOSE", ae.getCurrentValue());
118+
ae.dispose();
119+
120+
await lively.sleep(10);
121+
console.log("SET VAL", ae.getCurrentValue());
122+
val++;
123+
})();
124+
72125
let j = 0;
126+
console.log("BEFORE LOOP", val, j);
73127
for await (let v of ae.values()) {
74128
j++;
129+
console.log("IN LOOP", val, v, j);
75130
expect(v).to.equal(j);
76131
}
132+
console.log("AFTER LOOP", val, j);
77133
expect(j).to.equal(3);
78134
});
79135

0 commit comments

Comments
 (0)