Skip to content

Commit

Permalink
fix(cache): clear previous value only if flag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed May 18, 2018
1 parent 0cd9205 commit 856e5c8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export function get(target, key, getter) {

if (entry.invalid === entry.state) {
entry.state += 1;
entry.value = undefined;
} else if (entry.checksum !== undefined && entry.checksum === calculateChecksum(entry)) {
return entry.value;
}
Expand Down Expand Up @@ -85,12 +84,16 @@ export function set(target, key, setter, value, callback) {
}
}

export function invalidate(target, key) {
export function invalidate(target, key, clearValue) {
if (context) {
context = null;
throw Error(`[cache] Try to invalidate '${key}' in '${target}' during get invocation`);
}

const entry = getEntry(target, key);

entry.invalid = entry.state;
if (clearValue) {
entry.value = undefined;
}
}
2 changes: 1 addition & 1 deletion src/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ if (process.env.NODE_ENV !== 'production') {
node.disconnectedCallback();

Object.keys(node.constructor.hybrids).forEach((key) => {
cache.invalidate(node, key);
cache.invalidate(node, key, true);
});

node.connectedCallback();
Expand Down
8 changes: 8 additions & 0 deletions test/spec/define.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import define from '../../src/define';
import { invalidate } from '../../src/cache';

describe('define:', () => {
it('should return custom element with a name', () => {
Expand Down Expand Up @@ -49,6 +50,13 @@ describe('define:', () => {
expect(spy.calls.first().args[0]).toBe(el);
expect(spy.calls.first().args[1]).toBe('one');
}));

it('should return previus value when invalidate', () => tree((el) => {
el.one = 10;
expect(el.one).toBe(11);
invalidate(el, 'one');
expect(el.one).toBe(12);
}));
});

describe('for primitive value', () => {
Expand Down

0 comments on commit 856e5c8

Please # to comment.