Skip to content

Commit 0bb1012

Browse files
committed
Drop a few more bytes off #7
1 parent 0314902 commit 0bb1012

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import delve from 'dlv';
77
* @returns {function} linkedStateHandler
88
*/
99
export default function linkState(component, key, eventPath) {
10-
let path = key.split('.');
11-
const cache = component._linkedStates || (component._linkedStates = {});
10+
let path = key.split('.'),
11+
cache = component.__lsc || (component.__lsc = {});
1212

13-
return cache[`${key}\n${eventPath}`] || (cache[`${key}\n${eventPath}`] = function(e) {
13+
return cache[key+eventPath] || (cache[key+eventPath] = function(e) {
1414
let t = e && e.target || this,
1515
state = {},
1616
obj = state,

test/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ describe('linkstate', () => {
2525
expect(linkState(component, 'testStateKey')).to.be.a('function');
2626
});
2727

28+
it('should be memoized', () => {
29+
expect(linkState(component, 'a')).to.equal(linkState(component, 'a'));
30+
expect(linkState(component, 'a', 'x')).to.equal(linkState(component, 'a', 'x'));
31+
32+
expect(linkState(component, 'a')).not.to.equal(linkState(component, 'b'));
33+
expect(linkState(component, 'a')).not.to.equal(linkState(component, 'a', 'x'));
34+
expect(linkState(component, 'a', 'x')).not.to.equal(linkState(component, 'a', 'y'));
35+
});
36+
2837
describe('linkState without eventPath argument', () => {
2938
beforeEach( () => {
3039
linkFunction = linkState(component, 'testStateKey');
@@ -110,13 +119,4 @@ describe('linkstate', () => {
110119
expect(component.setState).to.have.been.calledWith({'testStateKey': 'nestedPathValueFromEvent'});
111120
});
112121
});
113-
114-
describe('linkState memoisation', () => {
115-
it('should return the same function when called with the same inputs', () => {
116-
linkFunction = linkState(component, 'nested.state.key');
117-
const linkFunctionB = linkState(component, 'nested.state.key');
118-
119-
expect(linkFunction).to.equal(linkFunctionB);
120-
});
121-
});
122122
});

0 commit comments

Comments
 (0)