Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 435d998

Browse files
committed
feat(cache): Add a JS interface to CacheRegister
Closes dart-archive#1181
1 parent 05e2c57 commit 435d998

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

lib/application.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import 'package:di/di.dart';
7474
import 'package:angular/angular.dart';
7575
import 'package:angular/perf/module.dart';
7676
import 'package:angular/cache/module.dart';
77+
import 'package:angular/cache/js_cache_register.dart';
7778
import 'package:angular/core/module_internal.dart';
7879
import 'package:angular/core/registry.dart';
7980
import 'package:angular/core_dom/module_internal.dart';
@@ -100,6 +101,7 @@ class AngularModule extends Module {
100101
install(new CoreDomModule());
101102
install(new DirectiveModule());
102103
install(new FormatterModule());
104+
install(new JsCacheModule());
103105
install(new PerfModule());
104106
install(new RoutingModule());
105107

@@ -169,6 +171,8 @@ abstract class Application {
169171
var rootElements = [element];
170172
Injector injector = createInjector();
171173
ExceptionHandler exceptionHandler = injector.getByKey(EXCEPTION_HANDLER_KEY);
174+
// Publish cache register interface
175+
injector.getByKey(JS_CACHE_REGISTER_KEY);
172176
initializeDateFormatting(null, null).then((_) {
173177
try {
174178
var compiler = injector.getByKey(COMPILER_KEY);

lib/cache/cache_register.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CacheRegister {
5050
*/
5151
void clear([String name]) {
5252
if (name == null) {
53-
_caches.forEach((k, Map v) {
53+
_caches.forEach((k, v) {
5454
v.clear();
5555
});
5656
return;

lib/cache/js_cache_register.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

lib/core/static_keys.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Key ROOT_SCOPE_KEY = new Key(RootScope);
88
Key SCOPE_KEY = new Key(Scope);
99
Key SCOPE_STATS_CONFIG_KEY = new Key(ScopeStatsConfig);
1010
Key FORMATTER_MAP_KEY = new Key(FormatterMap);
11-
Key INTERPOLATE_KEY = new Key(Interpolate);
11+
Key INTERPOLATE_KEY = new Key(Interpolate);

test/_specs.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export 'package:angular/angular.dart';
1919
export 'package:angular/application.dart';
2020
export 'package:angular/introspection.dart';
2121
export 'package:angular/cache/module.dart';
22+
export 'package:angular/cache/js_cache_register.dart';
2223
export 'package:angular/core/annotation.dart';
2324
export 'package:angular/core/registry.dart';
2425
export 'package:angular/core/module_internal.dart';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
library js_cache_register_spec;
2+
3+
import '../_specs.dart';
4+
import 'dart:js' as js;
5+
import 'package:angular/application_factory.dart';
6+
7+
main() => describe('JsCacheRegister', () {
8+
s() => js.context['ngCaches']['sizes'].apply([]);
9+
10+
// Create some caches in the system
11+
beforeEach((JsCacheRegister js, DynamicParser dp, ViewCache vc) { });
12+
13+
it('should publish a JS interface', () {
14+
expect(js.context['ngCaches']).toBeDefined();
15+
});
16+
17+
it('should return a map of caches', () {
18+
expect(js.context['Object']['keys'].apply([s()]).length > 0).toBeTruthy();
19+
});
20+
21+
it('should clear one cache', (DynamicParser p) {
22+
p('1');
23+
24+
expect(s()['DynamicParser'] > 0).toBeTruthy();
25+
26+
js.context['ngCaches']['clear'].apply(['DynamicParser']);
27+
expect(s()['DynamicParser']).toEqual(0);
28+
});
29+
30+
it('should clear all caches', (DynamicParser p) {
31+
p('1');
32+
33+
var stats = s();
34+
var caches = js.context['Object']['keys'].apply([stats]);
35+
expect(caches.length > 0).toBeTruthy();
36+
js.context['ngCaches']['clear'].apply([]);
37+
38+
var clearedStats = s();
39+
caches.forEach((cacheName) {
40+
expect(clearedStats[cacheName]).toEqual(0);
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)