Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

feat(http): Added a HttpResponse object #43

Merged
merged 1 commit into from
Jul 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,16 @@ class BlockCache {
/**
* A convinience wrapper for "templates" cache.
*/
class TemplateCache implements Cache {
class TemplateCache implements Cache<HttpResponse> {
Cache _cache;

TemplateCache(CacheFactory $cacheFactory) {
_cache = $cacheFactory('templates');
}

Object get(key) => _cache.get(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if get should return String, not HttpResponse, so that the cache is symmetric (not sure if it's the right term), meaning you get back what you put in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Fixed".

get and put now use HttpResponse objects only.
putString is the string interface.

Object put(key, Object value) => _cache.put(key, value);
put(key, HttpResponse value) => _cache.put(key, value);
putString(key, String value) => _cache.put(key, new HttpResponse(200, value));
void remove(key) => _cache.remove(key);
void removeAll() => _cache.removeAll();
CacheInfo info() => _cache.info();
Expand Down
18 changes: 9 additions & 9 deletions lib/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ part of angular;
* A simple map-backed cache.
* TODO(pavelgj): add LRU support.
*/
class Cache {
class Cache<T> {
final String _id;
Map<String, Object> _data = <String, Object>{};
CacheFactory _factory;

Cache._newCache(String this._id, CacheFactory this._factory);

Object get(key) {
T get(String key) {
_checkIfDestroyed();
key = _stringifyKey(key);
return _data[key];
}

Object put(key, Object value) {
T put(String key, T value) {
_checkIfDestroyed();
if (value == null) {
return null;
Expand All @@ -26,7 +26,7 @@ class Cache {
return _data[key] = value;
}

void remove(key) {
void remove(String key) {
_checkIfDestroyed();
key = _stringifyKey(key);
_data.remove(key);
Expand Down Expand Up @@ -59,19 +59,19 @@ class Cache {
}
}

class CacheFactory {
class CacheFactory<T> {
Map<String, Cache> _cacheMap = <String, Cache>{};

Cache call(String cacheId) {
Cache<T> call(String cacheId) {
var cache = _cacheMap[cacheId];
if (cache != null) {
throw "[\$cacheFactory:iid] CacheId '$cacheId' is already taken!";
}
_cacheMap[cacheId] = cache = new Cache._newCache(cacheId, this);
_cacheMap[cacheId] = cache = new Cache<T>._newCache(cacheId, this);
return cache;
}

Cache get(String cacheId) {
Cache<T> get(String cacheId) {
return _cacheMap[cacheId];
}

Expand All @@ -95,4 +95,4 @@ class CacheInfo {
}

String toString() => '{size: $size, id: $id}';
}
}
20 changes: 15 additions & 5 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class HttpBackend {
onProgress: onProgress);
}

class HttpResponse {
int status;
String responseText;
Map<String, String> headers;
HttpResponse([this.status, this.responseText, this.headers]);
}

class Http {
Map<String, async.Future<String>> _pendingRequests = <String, async.Future<String>>{};
UrlRewriter rewriter;
Expand All @@ -38,17 +45,17 @@ class Http {
return request(url,
withCredentials: withCredentials,
onProgress: onProgress,
cache: cache).then((xhr) => xhr.responseText);
cache: cache).then((HttpResponse xhr) => xhr.responseText);
}

// TODO(deboer): The cache is keyed on the url only. It should be keyed on
// (url, method, mimeType, requestHeaders, ...)
// Better yet, we should be using a HTTP standard cache.
async.Future request(String rawUrl,
async.Future<HttpResponse> request(String rawUrl,
{String method, bool withCredentials, String responseType,
String mimeType, Map<String, String> requestHeaders, sendData,
void onProgress(dom.ProgressEvent e),
Cache cache}) {
Cache<HttpResponse> cache}) {
String url = rewriter(rawUrl);

// We return a pending request only if caching is enabled.
Expand All @@ -67,11 +74,14 @@ class Http {
requestHeaders: requestHeaders,
sendData: sendData,
onProgress: onProgress).then((value) {
// NOTE(deboer): Missing headers. Ask the Dart team for a sane API.
var response = new HttpResponse(value.status, value.responseText);

if (cache != null) {
cache.put(url, value);
cache.put(url, response);
}
_pendingRequests.remove(url);
return value;
return response;
}, onError: (error) {
_pendingRequests.remove(url);
throw error;
Expand Down
11 changes: 2 additions & 9 deletions test/_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MockHttp extends Http {
}
var expectedValue = data.value;
if (cache != null) {
cache.put(url, expectedValue);
cache.put(url, new HttpResponse(200, expectedValue));
}
var future = new Future.value(expectedValue);
futures.add(future);
Expand Down Expand Up @@ -61,13 +61,6 @@ class MockHttpFutures extends HttpFutures {
}
}

// NOTE(deboer): If I try to extend HttpRequest, I get an error:
// class 'MockHttpRequest' is trying to extend a native fields class, but library '_http.dart' has no native resolvers
class MockHttpRequest /*extends HttpRequest*/ {
String responseText;
MockHttpRequest(this.responseText);
}

class MockHttpBackend extends HttpBackend {
Map<String, MockHttpData> gets = {};
List completersAndValues = [];
Expand Down Expand Up @@ -97,7 +90,7 @@ class MockHttpBackend extends HttpBackend {
if (data.times <= 0) {
gets.remove(url);
}
var expectedValue = new MockHttpRequest(data.value);
var expectedValue = new HttpResponse(200, data.value);
var completer = new Completer.sync();
completersAndValues.add([completer, expectedValue]);
return completer.future;
Expand Down
2 changes: 1 addition & 1 deletion test/_http_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ main() {
http.getString('request', cache: cache).then(expectAsync1((data) {
expect(data).toEqual('response');
expect(cache.info().size).toEqual(1);
expect(cache.get('request')).toEqual('response');
expect(cache.get('request').responseText).toEqual('response');
}));
}));

Expand Down
13 changes: 0 additions & 13 deletions test/cache_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,6 @@ main() => describe('CacheFactory', () {
expect(cache.remove('non-existent')).toBeNull();
}));


it('should stringify keys', inject((CacheFactory $cacheFactory) {
cache.put('123', 'foo');
cache.put(123, 'bar');

expect(cache.get('123')).toBe('bar');
expect(cache.info().size).toBe(1);

cache.remove(123);
expect(cache.info().size).toBe(0);
}));


it("should return value from put", inject((CacheFactory $cacheFactory) {
var obj = {};
expect(cache.put('k1', obj)).toBe(obj);
Expand Down
2 changes: 1 addition & 1 deletion test/http_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var CACHED_VALUE = 'cached_value';


class FakeCache implements Cache {
get(x) => x == 'f' ? new MockHttpRequest(CACHED_VALUE) : null;
get(x) => x == 'f' ? new HttpResponse(200, CACHED_VALUE) : null;
put(_,__) => null;

}
Expand Down
1 change: 1 addition & 0 deletions test/templateurl_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ main() {
$compile(element)(injector, element);

backend.flush();
backend.assertAllGetsCalled();

expect(renderedText(element)).toEqual('@import "PREFIX:simple.css"Simple!');
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
Expand Down