Skip to content

Commit 5f2f7c6

Browse files
committed
toplevel S.subclock() must flush changes in RootClock
1 parent f07714e commit 5f2f7c6

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

dist/es/withsubclocks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ S.subclock = function subclock(fn) {
254254
finally {
255255
RunningClock = running;
256256
}
257+
// if we were run from top level, have to flush any changes in RootClock
258+
if (RunningClock === null)
259+
event();
257260
return result;
258261
}
259262
};

dist/withsubclocks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ S.subclock = function subclock(fn) {
259259
finally {
260260
RunningClock = running;
261261
}
262+
// if we were run from top level, have to flush any changes in RootClock
263+
if (RunningClock === null)
264+
event();
262265
return result;
263266
}
264267
};

spec/S.subclock.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ if (S.subclock) {
154154
});
155155
});
156156

157+
it("runs any changes in outer clocks when created at top level", function () {
158+
S.root(() => {
159+
var outer = S.data(1);
160+
161+
S.subclock(() => {
162+
outer(2);
163+
});
164+
165+
expect(outer()).toBe(2);
166+
outer(3);
167+
});
168+
});
169+
170+
it("runs any changes in sibling clocks when created at top level", function () {
171+
S.root(() => {
172+
var outer = S.subclock(() => S.data(1));
173+
174+
S.subclock(() => {
175+
outer(2);
176+
});
177+
178+
expect(outer()).toBe(2);
179+
outer(3);
180+
expect(outer()).toBe(3);
181+
});
182+
});
183+
157184
it("handles whiteboard case", function () {
158185
S.root(function () {
159186
var t = "",
@@ -217,5 +244,26 @@ if (S.subclock) {
217244
expect(t).toBe('p2c1,p3c1,p1c1,c2,c1,');
218245
});
219246
});
247+
248+
it("handles irisjay test case", function () {
249+
S.root(() => {
250+
var A = S.data(0),
251+
log = S.data();
252+
253+
S.subclock(() => {
254+
S(() => log(A() + ' logged'));
255+
})
256+
257+
var logs = [];
258+
S(() => logs.push(log()));
259+
260+
expect(logs).toEqual(["0 logged"]);
261+
262+
A(1);
263+
expect(logs).toEqual(["0 logged", "1 logged"]);
264+
A(8);
265+
expect(logs).toEqual(["0 logged", "1 logged", "8 logged"]);
266+
});
267+
});
220268
});
221269
}

src/withsubclocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ S.subclock = function subclock<T>(fn? : () => T) {
306306
} finally {
307307
RunningClock = running;
308308
}
309+
// if we were run from top level, have to flush any changes in RootClock
310+
if (RunningClock === null) event();
309311
return result;
310312
}
311313
}

0 commit comments

Comments
 (0)