|
| 1 | +library angular.cache.js; |
| 2 | + |
| 3 | +// This is a separate module since it depends on dart:js |
| 4 | + |
| 5 | +import 'dart:js' as js; |
| 6 | +import 'package:di/di.dart'; |
| 7 | +import 'package:angular/core/annotation_src.dart'; |
| 8 | +import 'package:angular/cache/module.dart'; |
| 9 | + |
| 10 | +Key JS_CACHE_REGISTER_KEY = new Key(JsCacheRegister); |
| 11 | + |
| 12 | +/** |
| 13 | + * Publishes an interface to the CacheRegister in Javascript. When installed, |
| 14 | + * a 'ngCaches' object will be available in Javascript. |
| 15 | + * |
| 16 | + * ngCaches.sizes() returns a map of cache name -> number of entries in the cache |
| 17 | + * ngCaches.dump() prints the cache information to the console |
| 18 | + * ngCaches.clear(name) clears the cache named 'name', or if name is omitted, all caches. |
| 19 | + */ |
| 20 | +@Injectable() |
| 21 | +class JsCacheRegister { |
| 22 | + CacheRegister _caches; |
| 23 | + |
| 24 | + JsCacheRegister(CacheRegister this._caches) { |
| 25 | + js.context['ngCaches'] = new js.JsObject.jsify({ |
| 26 | + "sizes": new js.JsFunction.withThis(sizesAsMap), |
| 27 | + "clear": new js.JsFunction.withThis((_, [name]) => _caches.clear(name)), |
| 28 | + "dump": new js.JsFunction.withThis(dump) |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + void dump(_) { |
| 33 | + var toPrint = ['Angular Cache Sizes:']; |
| 34 | + _caches.stats.forEach((CacheRegisterStats stat) { |
| 35 | + toPrint.add('${stat.name.padLeft(35)} ${stat.length}'); |
| 36 | + }); |
| 37 | + print(toPrint.join('\n')); |
| 38 | + } |
| 39 | + |
| 40 | + js.JsObject sizesAsMap(_) { |
| 41 | + var map = {}; |
| 42 | + _caches.stats.forEach((CacheRegisterStats stat) { |
| 43 | + map[stat.name] = stat.length; |
| 44 | + }); |
| 45 | + return new js.JsObject.jsify(map); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class JsCacheModule extends Module { |
| 50 | + JsCacheModule() { |
| 51 | + bind(JsCacheRegister); |
| 52 | + } |
| 53 | +} |
0 commit comments