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

feat(cache): Add a JS interface to CacheRegister #1181

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": sizesAsMap,
"clear": _caches.clear,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fails in dart2js mode (wrap it in a JsFunction to get it to pass).  :(

Ref: http://dartbug.com/17752

"dump": dump
});
}

void dump() {
var toPrint = ['Angular Cache Sizes:'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return void
StringBuffer ?

_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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

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);
});
});
});