Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js

Issue 329433002: Initial ServiceWorker CacheStorage API polyfill. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: one moar typo Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/serviceworkers/polyfills/cachePolyfill.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
jsbell 2014/06/10 22:26:34 Nit: missing ; after the function expression in th
gavinp 2014/06/11 16:58:30 Done.
21
22 CacheStorage.prototype.has = function(key) {
23 return Promise.resolve(this.cachesByName.hasOwnProperty(key));
24 }
25
26 // FIXME: Engage standardization on removing this method from the spec.
27 CacheStorage.prototype.set = Promise.reject.bind(Promise, 'CacheStorage.prot otype.set() not implemented.');
28
29 // FIXME: Engage standardization on adding this method to the spec.
30 CacheStorage.prototype.create = function(key) {
31 this.cachesByName[key] = new Cache();
32
33 return Promise.resolve();
34 }
35
36 // FIXME: Engage standarization on adding this method to the spec.
37 CacheStorage.prototype.rename = function(fromKey, toKey) {
38 if (!this.cachesByName.hasOwnProperty(fromKey)) {
39 return Promise.reject('not found');
40 }
41 this.cachesByName[toKey] = this.cachesByName[fromKey];
42 delete this.cachesByName[fromKey];
43
44 return Promise.resolve();
45 }
46
47 CacheStorage.prototype.clear = function() {
48 this.cachesByName = {};
49
50 return Promise.resolve();
51 }
52
53 CacheStorage.prototype.delete = function(key) {
54 delete this.cachesByName[key];
55
56 return Promise.resolve();
57 }
58
59 CacheStorage.prototype.forEach = function(callback, thisArg) {
60 Object.keys(this.cachesByName).map(function(key) {
61 thisArg.callback(this.cachesByName[key], key, this);
62 });
63 return Promise.resolve();
64 }
65
66 // FIXME: Implement this.
67 CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage. prototype.entries() not implemented.');
68
69 CacheStorage.prototype.keys = function() {
70 return Promise.resolve(this.cachesByName.keys());
jsbell 2014/06/10 22:26:34 `Object.prototype.keys` is not defined. Do you me
gavinp 2014/06/11 16:58:30 Done.
71 }
72
73 CacheStorage.prototype.values = function() {
74 return Promise.resolve(Object.keys(this.cachesByName).map(function(key) {
75 return this.cachesByName[key];
76 }));
77 }
78
79 CacheStorage.prototype.size = function() {
80 return Promise.resolve(Object.keys(this.cachesByName).length);
81 }
82
83 CacheStorage.prototype.match = function(url, cacheName) {
84 this.get(cacheName).then(function(cache) {
jsbell 2014/06/10 22:26:33 Missing `return` at the start of this?
gavinp 2014/06/11 16:58:30 Done.
85 return cache.match(url);
86 });
87 }
88
89 global.caches = global.caches || new CacheStorage();
90 }(self)); // window or worker global scope.
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/polyfills/cachePolyfill.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698