Skip to content

Commit

Permalink
Merge pull request #1069 from glimmerjs/bugfix/fix-untrack-logic
Browse files Browse the repository at this point in the history
[BUGFIX] Fix untrack logic
  • Loading branch information
rwjblue authored Apr 12, 2020
2 parents 18b3400 + add42e0 commit bc5d06f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/@glimmer/validator/lib/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,22 @@ class Tracker {
*/
let CURRENT_TRACKER: Option<Tracker> = null;

const OPEN_TRACK_FRAMES: Tracker[] = [];
const OPEN_TRACK_FRAMES: Option<Tracker>[] = [];

export function beginTrackFrame(): void {
if (CURRENT_TRACKER !== null) {
OPEN_TRACK_FRAMES.push(CURRENT_TRACKER);
}
OPEN_TRACK_FRAMES.push(CURRENT_TRACKER);

CURRENT_TRACKER = new Tracker();
}

export function endTrackFrame(): Tag {
let current = CURRENT_TRACKER;

if (DEBUG && !CURRENT_TRACKER) {
if (DEBUG && OPEN_TRACK_FRAMES.length === 0) {
throw new Error('attempted to close a tracking frame, but one was not open');
}

CURRENT_TRACKER = OPEN_TRACK_FRAMES.length > 0 ? OPEN_TRACK_FRAMES.pop()! : null;
CURRENT_TRACKER = OPEN_TRACK_FRAMES.pop()!;

return current!.combine();
}
Expand Down Expand Up @@ -197,13 +195,13 @@ export function isTracking() {
}

export function untrack(callback: () => void) {
let parent = CURRENT_TRACKER;
OPEN_TRACK_FRAMES.push(CURRENT_TRACKER);
CURRENT_TRACKER = null;

try {
callback();
} finally {
CURRENT_TRACKER = parent;
CURRENT_TRACKER = OPEN_TRACK_FRAMES.pop()!;
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/@glimmer/validator/test/tracking-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ module('@glimmer/validator: tracking', () => {
});
});
});

test('nested tracks work', assert => {
assert.notOk(isTracking());

track(() => {
assert.ok(isTracking());

untrack(() => {
assert.notOk(isTracking());
});
});
});

test('nested tracks and untracks work', assert => {
track(() => {
track(() => {
untrack(() => {
track(() => {
assert.ok(isTracking(), 'tracking');
});
});
});
});
});
});

module('manual track frames', () => {
Expand Down

0 comments on commit bc5d06f

Please sign in to comment.