-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as cache from './cache'; | ||
|
||
const metaMap = new WeakMap(); | ||
const _ = (h, v) => v; | ||
|
||
function getMeta(Model) { | ||
let meta = metaMap.get(Model); | ||
if (!meta) { | ||
meta = { | ||
get: new Set(), | ||
set: new Set(), | ||
}; | ||
metaMap.set(Model, meta); | ||
} | ||
return meta; | ||
} | ||
|
||
export function observe(Model, callbacks = {}) { | ||
const meta = getMeta(Model); | ||
|
||
if (callbacks.get) { | ||
meta.get.add((id) => { | ||
const result = callbacks.get(id); | ||
if (result !== undefined) { | ||
cache.set(Model, id, _, result, true); | ||
} | ||
}); | ||
} | ||
|
||
if (callbacks.set) meta.set.add(callbacks.set); | ||
|
||
return () => { | ||
meta.get.delete(callbacks.get); | ||
meta.set.delete(callbacks.set); | ||
}; | ||
} | ||
|
||
export function isReady() { | ||
return true; | ||
} | ||
|
||
export function get(Model, id) { | ||
const model = cache.get(Model, id, _); | ||
|
||
if (model === undefined) { | ||
const meta = getMeta(Model); | ||
meta.get.forEach(cb => cb(id)); | ||
return cache.get(Model, id, _); | ||
} | ||
|
||
return model; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { test, resolveRaf } from '../helpers'; | ||
import { define, html } from '../../src'; | ||
|
||
import * as store from '../../src/store'; | ||
|
||
fdescribe('store:', () => { | ||
describe('one type with synchronous storage', () => { | ||
const Profile = { | ||
firstName: '', | ||
lastName: '', | ||
}; | ||
|
||
store.observe(Profile, { | ||
get: () => ({ firstName: 'John', lastName: 'Smith' }), | ||
}); | ||
|
||
define('test-store', { | ||
id: '1', | ||
profile: ({ id }) => store.get(Profile, id), | ||
render: ({ profile }) => { | ||
console.log('render'); | ||
return html` | ||
${profile && html`<h1>${profile.firstName} ${profile.lastName}</h1>`} | ||
`; | ||
}, | ||
}); | ||
|
||
const tree = test('<test-store></test-store>'); | ||
|
||
it('renders profile data', tree(el => resolveRaf(() => { | ||
expect(el.shadowRoot.innerHTML.trim()).toBe('<h1>John Smith</h1>'); | ||
}))); | ||
}); | ||
}); |