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]); | |
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); | |
27 } | |
28 | |
29 CacheStorage.prototype.set = function(key, cache) { | |
30 this.cachesByName[key] = cache; | |
31 | |
32 return Promise.resolve(); | |
33 } | |
34 | |
35 CacheStorage.prototype.clear = function() { | |
36 this.cachesByName = {}; | |
37 | |
38 return Promise.resolve(); | |
39 } | |
40 | |
41 CacheStorage.prototype.delete = function(key) { | |
42 delete this.cachesByName[key]; | |
43 | |
44 return Promise.resolve(); | |
45 } | |
46 | |
47 CacheStorage.prototype.forEach = function(callback, thisArg) { | |
48 Object.keys(this.cachesByName).map(function (key) { | |
falken
2014/06/10 17:47:03
I think your other patches removed the space after
| |
49 thisArg.callback(this.cachesByName[key], key, this); | |
50 }); | |
51 return Promise.resolve(); | |
52 } | |
53 | |
54 // FIXME: Implement this. | |
55 CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage. prototype.entries() not implemented.'); | |
56 | |
57 CacheStorage.prototype.keys = function() { | |
58 return Promise.resolve(this.cachesByName.keys()); | |
59 } | |
60 | |
61 CacheStorage.prototype.values = function() { | |
62 return Promise.resolve(Object.keys(this.cachesByName).map(function (key) { | |
63 return this.cachesByName[key]; | |
64 })); | |
65 } | |
66 | |
67 CacheStorage.prototype.size = function() { | |
68 return Promise.resolve(Object.keys(this.cachesByName).length); | |
69 } | |
70 | |
71 CacheStorage.prototype.match = function(url, cacheName) { | |
72 this.get(cacheName).then(function (cache) { | |
73 return cache.match(url); | |
74 }); | |
75 } | |
76 | |
77 global.CacheStorage = global.CacheStorage || CacheStorage; | |
78 global.caches = global.caches || new CacheStorage(); | |
79 }(self)); // window or worker global scope. | |
OLD | NEW |