OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // A simple, incomplete implementation of the CacheStorage API, intended to faci litate | |
6 // end to end serviceworker testing. | |
7 | |
8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm l#cache-storage | |
9 | |
10 (function(global) { | |
11 var CacheStorage = function() { | |
12 this.cachesByName = {}; | |
13 }; | |
14 | |
15 CacheStorage.prototype.get = function(key) { | |
16 if (this.cachesByName.hasOwnProperty(key)) { | |
17 return Promise.resolve(this.cachesByName[key]); | |
jsbell
2014/06/10 18:36:19
Overall: this polyfill is doing all of the changes
gavinp
2014/06/10 20:03:08
Aha, yes, good point. This way, when the user chai
| |
18 } | |
19 return Promise.reject('not found'); | |
20 } | |
21 | |
22 CacheStorage.prototype.has = function(key) { | |
23 if (this.cachesByName.hasOwnProperty(key)) { | |
24 return Promise.resolve(true); | |
25 } | |
26 return Promise.reject(false); | |
jsbell
2014/06/10 18:36:19
Should resolve(false);
reject() should only be us
gavinp
2014/06/10 20:03:08
Done.
| |
27 } | |
28 | |
29 // FIXME: Engage standardisation on removing this method from the spec. | |
30 CacheStorage.prototype.set = Promise.reject.bind(Promise, 'CacheStorage.prot otype.set() not implemented.'); | |
31 | |
32 // FIXME: Engage standarisation on adding this method to the spec. | |
jsbell
2014/06/10 18:36:19
typo: standar*d*isation
(Also, insert 's' vs. 'z'
gavinp
2014/06/10 20:03:08
Done. While I'm religious about US english in iden
| |
33 CacheStorage.prototype.create = function(key) { | |
34 this.cachesByName[key] = new Cache(); | |
35 | |
36 return Promise.resolve(); | |
37 } | |
38 | |
39 // FIXME: Engage standarisation on adding this method to the spec. | |
40 CacheStorage.prototype.rename = function(fromKey, toKey) { | |
41 if (!this.cachesByName.hasOwnProperty(fromKey)) { | |
42 return Promise.reject('not found'); | |
43 } | |
44 this.cachesByName[toKey] = this.cachesByName[fromKey]; | |
jsbell
2014/06/10 18:36:19
I assume it's intentional that no checking is done
gavinp
2014/06/10 20:03:08
Yes. Akin to doing "set" followed by "delete."
| |
45 delete this.cachesByName[fromKey]; | |
46 | |
47 return Promise.resolve(); | |
48 } | |
49 | |
50 CacheStorage.prototype.clear = function() { | |
51 this.cachesByName = {}; | |
52 | |
53 return Promise.resolve(); | |
54 } | |
55 | |
56 CacheStorage.prototype.delete = function(key) { | |
57 delete this.cachesByName[key]; | |
58 | |
59 return Promise.resolve(); | |
60 } | |
61 | |
62 CacheStorage.prototype.forEach = function(callback, thisArg) { | |
63 Object.keys(this.cachesByName).map(function(key) { | |
64 thisArg.callback(this.cachesByName[key], key, this); | |
65 }); | |
66 return Promise.resolve(); | |
67 } | |
68 | |
69 // FIXME: Implement this. | |
70 CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage. prototype.entries() not implemented.'); | |
71 | |
72 CacheStorage.prototype.keys = function() { | |
73 return Promise.resolve(this.cachesByName.keys()); | |
74 } | |
75 | |
76 CacheStorage.prototype.values = function() { | |
77 return Promise.resolve(Object.keys(this.cachesByName).map(function (key) { | |
falken
2014/06/10 18:36:12
function space here
gavinp
2014/06/10 20:03:09
Done.
| |
78 return this.cachesByName[key]; | |
79 })); | |
80 } | |
81 | |
82 CacheStorage.prototype.size = function() { | |
83 return Promise.resolve(Object.keys(this.cachesByName).length); | |
84 } | |
85 | |
86 CacheStorage.prototype.match = function(url, cacheName) { | |
87 this.get(cacheName).then(function (cache) { | |
falken
2014/06/10 18:36:12
here too
gavinp
2014/06/10 20:03:08
Done.
| |
88 return cache.match(url); | |
89 }); | |
90 } | |
91 | |
92 global.CacheStorage = global.CacheStorage || CacheStorage; | |
jsbell
2014/06/10 18:36:19
Intentional to expose constructor for this, in the
gavinp
2014/06/10 20:03:08
Fair point, no real need. Done.
| |
93 global.caches = global.caches || new CacheStorage(); | |
94 }(self)); // window or worker global scope. | |
OLD | NEW |