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

Commit

Permalink
feat(cache): Add a JS interface to CacheRegister
Browse files Browse the repository at this point in the history
Closes #1181
  • Loading branch information
jbdeboer committed Jun 26, 2014
1 parent 05e2c57 commit 435d998
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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());

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/cache_register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 53 additions & 0 deletions lib/cache/js_cache_register.dart
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);
}
}
2 changes: 1 addition & 1 deletion lib/core/static_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Key INTERPOLATE_KEY = new Key(Interpolate);
1 change: 1 addition & 0 deletions test/_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
43 changes: 43 additions & 0 deletions test/cache/js_cache_register_spec.dart
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);
});
});
});

0 comments on commit 435d998

Please sign in to comment.