Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return cached content and use onLoad for new content #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,28 @@ class Wurd {
// Check for cached sections
const cachedContent = store.load(sections, { lang });

const uncachedSections = sections.filter(section => cachedContent[section] === undefined);
const uncachedSections = cachedContent._expired
? sections
: sections.filter(section => cachedContent[section] === undefined);

if (debug) console.info('Wurd: from cache:', sections.filter(section => cachedContent[section] !== undefined));

// Return now if all content was in cache
if (uncachedSections.length === 0) {
// Pass main content Block to callbacks
if (onLoad) onLoad(content);

return Promise.resolve(content);
}

// Otherwise fetch remaining sections
return this._fetchSections(uncachedSections)
// If missing sections, refetch in background
if (uncachedSections.length) {
this._fetchSections(uncachedSections)
.then(result => {
// Cache for next time
store.save(result, { lang });

// Pass main content Block to callbacks
if (onLoad) onLoad(content);

return content;
if (onLoad) onLoad(store.get());
});
}

// Return content in all case
if (onLoad) onLoad(content);
return content;
}

_fetchSections(sectionNames) {
Expand Down
34 changes: 24 additions & 10 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ describe('Wurd', function() {
});

it('resolves the main content Block', function (done) {
client.load(['lorem', 'ipsum'])
Promise.resolve()
.then(() => client.load(['lorem', 'ipsum']))
.then(content => {
test.ok(content instanceof Block);

Expand All @@ -99,8 +100,9 @@ describe('Wurd', function() {
});

it('loads content from cache and server', function (done) {
client.load(['lorem','ipsum','dolor','amet'])
.then(content => {
Promise.resolve()
.then(() => client.load(['lorem','ipsum','dolor','amet']))
.then(async content => {
// Should only call to server for missing sections
same(client._fetchSections.callCount, 1);
test.deepEqual(client._fetchSections.args[0][0], ['ipsum', 'amet']);
Expand All @@ -113,21 +115,31 @@ describe('Wurd', function() {
// Should return the main content Block
test.deepEqual(content.get(), {
lorem: { title: 'Lorem' },
ipsum: { title: 'Ipsum' },
dolor: { title: 'Dolor' },
amet: { title: 'Amet' }
});

// Should pass the main content Block to the onLoad() callback
same(client.onLoad.callCount, 1);
same(client.onLoad.args[0][0], content);

// wait another cycle
await new Promise(r => setTimeout(r, 2));

same(client.onLoad.callCount, 2);
test.deepStrictEqual(client.onLoad.args[1][0], {
lorem: { title: 'Lorem' },
dolor: { title: 'Dolor' },
ipsum: { title: 'Ipsum' },
amet: { title: 'Amet' }
});

done();
}).catch(done);
});

it('does not fetch from server if all content is available', function (done) {
client.load(['lorem', 'dolor'])
Promise.resolve()
.then(() => client.load(['lorem', 'dolor']))
.then(content => {
// Should not call to server
same(client._fetchSections.callCount, 0);
Expand All @@ -151,10 +163,11 @@ describe('Wurd', function() {
});

it('works with an array of sectionNames', function (done) {
client.load(['lorem', 'ipsum'])
Promise.resolve()
.then(() => client.load(['lorem', 'ipsum']))
.then(content => {
test.deepEqual(content.get('lorem.title'), 'Lorem');
test.deepEqual(content.get('ipsum.title'), 'Ipsum');
test.deepEqual(content.get('ipsum.title'), undefined);

test.deepEqual(console.info.args, [
['Wurd: from cache:', ['lorem']],
Expand All @@ -166,10 +179,11 @@ describe('Wurd', function() {
})

it('works with a comma separated string', function (done) {
client.load('dolor,amet')
Promise.resolve()
.then(() => client.load('dolor,amet'))
.then(content => {
test.deepEqual(content.get('dolor.title'), 'Dolor');
test.deepEqual(content.get('amet.title'), 'Amet');
test.deepEqual(content.get('amet.title'), undefined);

test.deepEqual(console.info.args, [
['Wurd: from cache:', ['dolor']],
Expand Down
10 changes: 5 additions & 5 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export default class Store {
const cachedContent = JSON.parse(localStorage.getItem(storageKey));
const metaData = cachedContent && cachedContent._wurd;

// Check if it has expired
if (!cachedContent || !metaData || (metaData.savedAt + ttl) < Date.now()) {
// Check it's in the correct language
if (!cachedContent || !metaData || metaData.lang !== lang) {
return rawContent;
}

// Check it's in the correct language
if (metaData.lang !== lang) {
return rawContent;
// Check if it has expired
if ((metaData.savedAt + ttl) < Date.now()) {
rawContent._expired = true;
}

// Remove metadata
Expand Down
4 changes: 3 additions & 1 deletion src/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('store', function() {
});
});

it('returns memory content if localStorage has expired', function () {
it('returns all content if localStorage has expired and add _expired flag', function () {
global.localStorage.getItem.returns(JSON.stringify({
b: { a: 'BA' },
_wurd: {
Expand All @@ -134,6 +134,8 @@ describe('store', function() {

test.deepEqual(store.load(), {
a: { a: 'AA' },
b: { a: 'BA' },
_expired: true,
});
});

Expand Down