Skip to content

Commit

Permalink
fix(cache): invalidate should increase state for related values
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed May 30, 2018
1 parent b98b123 commit a9f29ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export function get(target, key, getter) {
context.deps.push(entry);
}

if (entry.invalid === entry.state) {
entry.state += 1;
} else if (entry.checksum !== undefined && entry.checksum === calculateChecksum(entry)) {
if (
entry.invalid !== entry.state &&
entry.checksum !== undefined &&
entry.checksum === calculateChecksum(entry)
) {
return entry.value;
}

Expand Down Expand Up @@ -92,7 +94,9 @@ export function invalidate(target, key, clearValue) {

const entry = getEntry(target, key);

entry.state += 1;
entry.invalid = entry.state;

if (clearValue) {
entry.value = undefined;
}
Expand Down
10 changes: 10 additions & 0 deletions test/spec/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ describe('cache:', () => {
expect(spy).toHaveBeenCalledTimes(2);
});

it('updates related value', () => {
get(target, 'key', () => {
get(target, 'related', () => 'one');
return 'one';
});

invalidate(target, 'related');
expect(get(target, 'key', () => 'two')).toBe('two');
});

it('throws when called inside getter', () => {
expect(() => {
get(target, 'key', () => invalidate(target, 'key'));
Expand Down

0 comments on commit a9f29ea

Please sign in to comment.