This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): Add a JS interface to CacheRegister
Closes #1181
- Loading branch information
Showing
6 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
library angular.cache.js; | ||
|
||
// This is a separate module since it depends on dart:js | ||
|
||
import 'dart:js' as js; | ||
import 'package:di/di.dart'; | ||
import 'package:angular/core/annotation_src.dart'; | ||
import 'package:angular/cache/module.dart'; | ||
|
||
Key JS_CACHE_REGISTER_KEY = new Key(JsCacheRegister); | ||
|
||
/** | ||
* Publishes an interface to the CacheRegister in Javascript. When installed, | ||
* a 'ngCaches' object will be available in Javascript. | ||
* | ||
* ngCaches.sizes() returns a map of cache name -> number of entries in the cache | ||
* ngCaches.dump() prints the cache information to the console | ||
* ngCaches.clear(name) clears the cache named 'name', or if name is omitted, all caches. | ||
*/ | ||
@Injectable() | ||
class JsCacheRegister { | ||
CacheRegister _caches; | ||
|
||
JsCacheRegister(CacheRegister this._caches) { | ||
js.context['ngCaches'] = new js.JsObject.jsify({ | ||
"sizes": new js.JsFunction.withThis(sizesAsMap), | ||
"clear": new js.JsFunction.withThis((_, [name]) => _caches.clear(name)), | ||
"dump": new js.JsFunction.withThis(dump) | ||
}); | ||
} | ||
|
||
void dump(_) { | ||
var toPrint = ['Angular Cache Sizes:']; | ||
_caches.stats.forEach((CacheRegisterStats stat) { | ||
toPrint.add('${stat.name.padLeft(35)} ${stat.length}'); | ||
}); | ||
print(toPrint.join('\n')); | ||
} | ||
|
||
js.JsObject sizesAsMap(_) { | ||
var map = {}; | ||
_caches.stats.forEach((CacheRegisterStats stat) { | ||
map[stat.name] = stat.length; | ||
}); | ||
return new js.JsObject.jsify(map); | ||
} | ||
} | ||
|
||
class JsCacheModule extends Module { | ||
JsCacheModule() { | ||
bind(JsCacheRegister); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
library js_cache_register_spec; | ||
|
||
import '../_specs.dart'; | ||
import 'dart:js' as js; | ||
import 'package:angular/application_factory.dart'; | ||
|
||
main() => describe('JsCacheRegister', () { | ||
s() => js.context['ngCaches']['sizes'].apply([]); | ||
|
||
// Create some caches in the system | ||
beforeEach((JsCacheRegister js, DynamicParser dp, ViewCache vc) { }); | ||
|
||
it('should publish a JS interface', () { | ||
expect(js.context['ngCaches']).toBeDefined(); | ||
}); | ||
|
||
it('should return a map of caches', () { | ||
expect(js.context['Object']['keys'].apply([s()]).length > 0).toBeTruthy(); | ||
}); | ||
|
||
it('should clear one cache', (DynamicParser p) { | ||
p('1'); | ||
|
||
expect(s()['DynamicParser'] > 0).toBeTruthy(); | ||
|
||
js.context['ngCaches']['clear'].apply(['DynamicParser']); | ||
expect(s()['DynamicParser']).toEqual(0); | ||
}); | ||
|
||
it('should clear all caches', (DynamicParser p) { | ||
p('1'); | ||
|
||
var stats = s(); | ||
var caches = js.context['Object']['keys'].apply([stats]); | ||
expect(caches.length > 0).toBeTruthy(); | ||
js.context['ngCaches']['clear'].apply([]); | ||
|
||
var clearedStats = s(); | ||
caches.forEach((cacheName) { | ||
expect(clearedStats[cacheName]).toEqual(0); | ||
}); | ||
}); | ||
}); |