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 Cache API, intended to facilitate | |
6 // end to end serviceworker testing. | |
7 | |
8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm l#cache | |
9 | |
10 // FIXME: Support AbstractResponse/OpaqueResponse correctly. | |
11 // FIXME: Serialize the cache. | |
12 // FIXME: Implement CacheStorage API. | |
13 // FIXME: Bind all function references. | |
14 (function (global) { | |
15 var _castToRequest = function (item) { | |
16 if (typeof item === 'string') { | |
17 item = new Request({ | |
18 url: item, | |
19 }); | |
20 } | |
21 return item; | |
22 }; | |
23 | |
24 var Cache = function() { | |
25 // An object containing a property for each HTTP fetch method. Those | |
26 // referenced objects contain a property for each URL, which is the | |
27 // Response. | |
28 this.entriesByMethod = {}; | |
29 }; | |
30 | |
31 // FIXME: Should this be in the spec? | |
32 Cache.prototype.keys = function() { | |
33 var that = this; | |
34 | |
35 return Promise.resolve(Array.prototype.concat.apply([], Object.keys(this .entriesByMethod).map(function (method) { | |
jsbell
2014/06/06 17:25:31
Maybe extract the Array.prototype.concat.apply([],
| |
36 return Object.keys(that.entriesByMethod[method]).map(function (url) { | |
37 return new Request({method: method, url: url}); | |
gavinp
2014/06/06 21:39:55
Have I followed the standard indentation practice
| |
38 }); | |
39 }))); | |
40 }; | |
41 | |
42 // FIXME: Implement this. | |
43 // FIXME: Should spec rename each --> forEach? | |
44 Cache.prototype.forEach = Promise.reject.bind(Promise, 'Cache.prototype.forE ach() not implemented.'); | |
45 Cache.prototype.each = Promise.reject.bind(Promise, 'Cache.prototype.each() not implemented.'); | |
46 | |
47 Cache.prototype.put = function (request, response) { | |
48 request = _castToRequest(request); | |
jsbell
2014/06/06 17:25:31
Consider doing this inside the new Promise(functio
gavinp
2014/06/06 21:39:55
Done.
| |
49 | |
50 var that = this; | |
51 return new Promise(function (resolve, reject) { | |
52 if (!that.entriesByMethod.hasOwnProperty(request.method)) { | |
53 that.entriesByMethod[request.method] = {}; | |
54 } | |
55 | |
56 var entriesByUrl = that.entriesByMethod[request.method]; | |
57 entriesByUrl[request.url] = response; | |
58 | |
59 resolve(); | |
60 }); | |
61 }; | |
62 | |
63 // FIXME: Implement this. | |
64 Cache.prototype.add = Promise.reject.bind(Promise, 'Cache.prototype.add() no t implemented.'); | |
65 | |
66 // FIXME: Add QueryParams argument. | |
67 Cache.prototype.delete = function (request) { | |
68 request = _castToRequest(request); | |
69 | |
70 var that = this; | |
71 return new Promise(function (resolve, reject) { | |
72 if (!that.entriesByMethod.hasOwnProperty(request.method)) { | |
73 reject('not found'); | |
jsbell
2014/06/06 17:25:31
This seems inconsistent - we reject if the *method
gavinp
2014/06/06 21:39:55
Done.
| |
74 return; | |
75 } | |
76 | |
77 var entriesByUrl = that.entriesByMethod[request.method]; | |
78 delete entriesByUrl[request.url]; | |
79 resolve(); | |
80 }); | |
81 }; | |
82 | |
83 // FIXME: Add QueryParams argument. | |
84 Cache.prototype.match = function (request) { | |
85 request = _castToRequest(request); | |
86 | |
87 var that = this; | |
88 return new Promise(function (resolve, reject) { | |
89 if (!that.entriesByMethod.hasOwnProperty(request.method)) { | |
90 reject('not found'); | |
91 return; | |
92 } | |
93 | |
94 var entriesByUrl = that.entriesByMethod[request.method]; | |
95 | |
96 if (!entriesByUrl.hasOwnProperty(request.url)) { | |
97 reject('not found'); | |
98 return; | |
99 } | |
100 | |
101 var entry = entriesByUrl[request.url]; | |
102 resolve(entry); | |
103 }); | |
104 }; | |
105 | |
106 // FIXME: Implement this. | |
107 Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.mat chAll not implemented.'); | |
108 | |
109 global.Cache = global.Cache || Cache; | |
110 }(self)); // window or worker global scope. | |
OLD | NEW |