From 431f15d3a64d4a5e56753028c8a42ef936462912 Mon Sep 17 00:00:00 2001 From: James deBoer Date: Thu, 26 Jun 2014 10:11:29 -0700 Subject: [PATCH] feat(cache): Add a JS interface to CacheRegister --- lib/application.dart | 4 ++ lib/cache/cache_register.dart | 2 +- lib/cache/js_cache_register.dart | 53 ++++++++++++++++++++++++++ lib/core/static_keys.dart | 2 +- test/_specs.dart | 1 + test/cache/js_cache_register_spec.dart | 43 +++++++++++++++++++++ 6 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 lib/cache/js_cache_register.dart create mode 100644 test/cache/js_cache_register_spec.dart diff --git a/lib/application.dart b/lib/application.dart index 421e7f470..14a0d354a 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -74,6 +74,7 @@ import 'package:di/di.dart'; import 'package:angular/angular.dart'; import 'package:angular/perf/module.dart'; import 'package:angular/cache/module.dart'; +import 'package:angular/cache/js_cache_register.dart'; import 'package:angular/core/module_internal.dart'; import 'package:angular/core/registry.dart'; import 'package:angular/core_dom/module_internal.dart'; @@ -100,6 +101,7 @@ class AngularModule extends Module { install(new CoreDomModule()); install(new DirectiveModule()); install(new FormatterModule()); + install(new JsCacheModule()); install(new PerfModule()); install(new RoutingModule()); @@ -169,6 +171,8 @@ abstract class Application { var rootElements = [element]; Injector injector = createInjector(); ExceptionHandler exceptionHandler = injector.getByKey(EXCEPTION_HANDLER_KEY); + // Publish cache register interface + injector.getByKey(JS_CACHE_REGISTER_KEY); initializeDateFormatting(null, null).then((_) { try { var compiler = injector.getByKey(COMPILER_KEY); diff --git a/lib/cache/cache_register.dart b/lib/cache/cache_register.dart index 4cce1ed5b..713338a3a 100644 --- a/lib/cache/cache_register.dart +++ b/lib/cache/cache_register.dart @@ -50,7 +50,7 @@ class CacheRegister { */ void clear([String name]) { if (name == null) { - _caches.forEach((k, Map v) { + _caches.forEach((k, v) { v.clear(); }); return; diff --git a/lib/cache/js_cache_register.dart b/lib/cache/js_cache_register.dart new file mode 100644 index 000000000..177f3e5fa --- /dev/null +++ b/lib/cache/js_cache_register.dart @@ -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": sizesAsMap, + "clear": _caches.clear, + "dump": 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); + } +} diff --git a/lib/core/static_keys.dart b/lib/core/static_keys.dart index 8f810b894..58b3778e5 100644 --- a/lib/core/static_keys.dart +++ b/lib/core/static_keys.dart @@ -8,4 +8,4 @@ Key ROOT_SCOPE_KEY = new Key(RootScope); Key SCOPE_KEY = new Key(Scope); Key SCOPE_STATS_CONFIG_KEY = new Key(ScopeStatsConfig); Key FORMATTER_MAP_KEY = new Key(FormatterMap); -Key INTERPOLATE_KEY = new Key(Interpolate); \ No newline at end of file +Key INTERPOLATE_KEY = new Key(Interpolate); diff --git a/test/_specs.dart b/test/_specs.dart index 69e55a03f..d0c81243d 100644 --- a/test/_specs.dart +++ b/test/_specs.dart @@ -19,6 +19,7 @@ export 'package:angular/angular.dart'; export 'package:angular/application.dart'; export 'package:angular/introspection.dart'; export 'package:angular/cache/module.dart'; +export 'package:angular/cache/js_cache_register.dart'; export 'package:angular/core/annotation.dart'; export 'package:angular/core/registry.dart'; export 'package:angular/core/module_internal.dart'; diff --git a/test/cache/js_cache_register_spec.dart b/test/cache/js_cache_register_spec.dart new file mode 100644 index 000000000..667456eec --- /dev/null +++ b/test/cache/js_cache_register_spec.dart @@ -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); + }); + }); +});