Skip to content

Commit

Permalink
Flatten array of items to precache. Fixes GoogleChromeLabs#24
Browse files Browse the repository at this point in the history
  • Loading branch information
wibblymat committed Jul 30, 2015
1 parent c88637c commit 8da6cef
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/sw-toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ helpers.debug('Service Worker Toolbox is loading');

// Install

var flatten = function(items) {
return items.reduce(function(a, b) {
return a.concat(b);
}, []);
};

self.addEventListener('install', function(event) {
var inactiveCache = options.cacheName + '$$$inactive$$$';
helpers.debug('install event fired');
helpers.debug('creating cache [' + inactiveCache + ']');
helpers.debug('preCache list: ' + (options.preCacheItems.join(', ') || '(none)'));
event.waitUntil(
helpers.openCache({cacheName: inactiveCache}).then(function(cache) {
return Promise.all(options.preCacheItems).then(cache.addAll.bind(cache));
return Promise.all(options.preCacheItems)
.then(flatten)
.then(function(preCacheItems) {
helpers.debug('preCache list: ' + (preCacheItems.join(', ') || '(none)'));
return cache.addAll(preCacheItems);
});
})
);
});
Expand Down
2 changes: 1 addition & 1 deletion sw-toolbox.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sw-toolbox.map.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/fixtures/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions tests/fixtures/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions tests/fixtures/c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
1 change: 1 addition & 0 deletions tests/fixtures/d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d
1 change: 1 addition & 0 deletions tests/fixtures/e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e
1 change: 1 addition & 0 deletions tests/fixtures/f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f
1 change: 1 addition & 0 deletions tests/fixtures/g
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g
1 change: 1 addition & 0 deletions tests/fixtures/h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h
1 change: 1 addition & 0 deletions tests/fixtures/i
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i
1 change: 1 addition & 0 deletions tests/fixtures/j
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
j
1 change: 1 addition & 0 deletions tests/fixtures/k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
k
1 change: 1 addition & 0 deletions tests/fixtures/l
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
l
1 change: 1 addition & 0 deletions tests/fixtures/m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m
1 change: 1 addition & 0 deletions tests/fixtures/n
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
n
12 changes: 12 additions & 0 deletions tests/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ toolbox.router.post('cache/:name', function(request) {
toolbox.router.delete('cache/:name', function(request) {
return toolbox.uncache(request.url).then(respondOK, respondError);
});

toolbox.router.get('fixtures/:foo', toolbox.cacheOnly)
// Single item
toolbox.precache('fixtures/a');
// Array of items
toolbox.precache(['fixtures/b', 'fixtures/c']);
// Array of Promises
toolbox.precache([Promise.resolve('fixtures/d'), Promise.resolve('fixtures/e')]);
// Array of Arrays
toolbox.precache(['fixtures/f', ['fixtures/g', 'fixtures/h'], ['fixtures/i', 'fixtures/j']]);
// Array of Promises for Arrays
toolbox.precache([Promise.resolve(['fixtures/k', 'fixtures/l']), Promise.resolve(['fixtures/m', 'fixtures/n'])]);
19 changes: 18 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ navigator.serviceWorker.ready.then(function() {
// Catch-all error handler
assert.ok(false, 'Failed: ' + reason);
}).then(done);;
})
});
});

QUnit.test('Precaching', function(assert) {
checkValue('fixtures/a', 'a', assert);
checkValue('fixtures/b', 'b', assert);
checkValue('fixtures/c', 'c', assert);
checkValue('fixtures/d', 'd', assert);
checkValue('fixtures/e', 'e', assert);
checkValue('fixtures/f', 'f', assert);
checkValue('fixtures/g', 'g', assert);
checkValue('fixtures/h', 'h', assert);
checkValue('fixtures/i', 'i', assert);
checkValue('fixtures/j', 'j', assert);
checkValue('fixtures/k', 'k', assert);
checkValue('fixtures/l', 'l', assert);
checkValue('fixtures/m', 'm', assert);
checkValue('fixtures/n', 'n', assert);
});
});

0 comments on commit 8da6cef

Please sign in to comment.