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

Commit

Permalink
feat(scope2): Basic implementation of Scope v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Feb 19, 2014
1 parent bc4a186 commit 3bde820
Show file tree
Hide file tree
Showing 9 changed files with 1,920 additions and 45 deletions.
10 changes: 7 additions & 3 deletions lib/change_detection/ast.dart
Expand Up @@ -9,7 +9,11 @@ part of angular.watch_group;
abstract class AST {
static final String _CONTEXT = '#';
final String expression;
AST(this.expression) { assert(expression!=null); }
AST(expression)
: expression = expression.startsWith('#.') ? expression.substring(2) : expression
{
assert(expression!=null);
}
WatchRecord<_Handler> setupWatch(WatchGroup watchGroup);
toString() => expression;
}
Expand All @@ -34,7 +38,7 @@ class ConstantAST extends AST {
final constant;

ConstantAST(dynamic constant):
super('$constant'),
super(constant is String ? '"$constant"' : '$constant'),
constant = constant;

WatchRecord<_Handler> setupWatch(WatchGroup watchGroup)
Expand All @@ -51,7 +55,7 @@ class FieldReadAST extends AST {
final String name;

FieldReadAST(lhs, name)
: super(lhs.expression == AST._CONTEXT ? name : '$lhs.$name'),
: super('$lhs.$name'),
lhs = lhs,
name = name;

Expand Down
4 changes: 3 additions & 1 deletion lib/change_detection/change_detection.dart
@@ -1,5 +1,7 @@
library change_detection;

typedef EvalExceptionHandler(error, stack);

/**
* An interface for [ChangeDetectorGroup] groups related watches together. It
* guarentees that within the group all watches will be reported in the order in
Expand Down Expand Up @@ -53,7 +55,7 @@ abstract class ChangeDetector<H> extends ChangeDetectorGroup<H> {
* linked list of [ChangeRecord]s. The [ChangeRecord]s are to be returned in
* the same order as they were registered.
*/
ChangeRecord<H> collectChanges();
ChangeRecord<H> collectChanges([EvalExceptionHandler exceptionHandler]);
}

abstract class Record<H> {
Expand Down
18 changes: 13 additions & 5 deletions lib/change_detection/dirty_checking_change_detector.dart
Expand Up @@ -216,17 +216,25 @@ class DirtyCheckingChangeDetector<H> extends DirtyCheckingChangeDetectorGroup<H>
implements ChangeDetector<H> {
DirtyCheckingChangeDetector(GetterCache getterCache): super(null, getterCache);

DirtyCheckingRecord<H> collectChanges() {
DirtyCheckingRecord<H> collectChanges([EvalExceptionHandler exceptionHandler]) {
DirtyCheckingRecord changeHead = null;
DirtyCheckingRecord changeTail = null;
DirtyCheckingRecord current = _head; // current index

while (current != null) {
if (current.check() != null) {
if (changeHead == null) {
changeHead = changeTail = current;
try {
if (current.check() != null) {
if (changeHead == null) {
changeHead = changeTail = current;
} else {
changeTail = changeTail.nextChange = current;
}
}
} catch (e, s) {
if (exceptionHandler == null) {
rethrow;
} else {
changeTail = changeTail.nextChange = current;
exceptionHandler(e, s);
}
}
current = current._nextWatch;
Expand Down
27 changes: 21 additions & 6 deletions lib/change_detection/watch_group.dart
Expand Up @@ -7,7 +7,8 @@ part 'linked_list.dart';
part 'ast.dart';
part 'prototype_map.dart';

typedef ReactionFn(value, previousValue, object);
typedef ReactionFn(value, previousValue);
typedef ChangeLog(expression);

/**
* Extend this class if you wish to pretend to be a function, but you don't know
Expand Down Expand Up @@ -333,11 +334,14 @@ class RootWatchGroup extends WatchGroup {
* Each step is called in sequence. ([ReactionFn]s are not called until all previous steps are
* completed).
*/
int detectChanges() {
int detectChanges({ExceptionHandler exceptionHandler, ChangeLog changeLog}) {
// Process the ChangeRecords from the change detector
ChangeRecord<_Handler> changeRecord =
(_changeDetector as ChangeDetector<_Handler>).collectChanges();
(_changeDetector as ChangeDetector<_Handler>).collectChanges(exceptionHandler);
while (changeRecord != null) {
if (changeLog != null) {
changeLog(changeRecord.handler.expression);
}
changeRecord.handler.onChange(changeRecord);
changeRecord = changeRecord.nextChange;
}
Expand All @@ -346,7 +350,14 @@ class RootWatchGroup extends WatchGroup {
// Process our own function evaluations
_EvalWatchRecord evalRecord = _evalWatchHead;
while (evalRecord != null) {
evalRecord.check();
try {
var change = evalRecord.check();
if (change != null && changeLog != null) {
changeLog(evalRecord.handler.expression);
}
} catch (e, s) {
if (exceptionHandler == null) rethrow; else exceptionHandler(e, s);
}
evalRecord = evalRecord._nextEvalWatch;
}

Expand All @@ -356,7 +367,11 @@ class RootWatchGroup extends WatchGroup {
Watch dirtyWatch = _dirtyWatchHead;
while(dirtyWatch != null) {
count++;
dirtyWatch.invoke();
try {
dirtyWatch.invoke();
} catch (e, s) {
if (exceptionHandler == null) rethrow; else exceptionHandler(e, s);
}
dirtyWatch = dirtyWatch._nextDirtyWatch;
}
_dirtyWatchHead = _dirtyWatchTail = null;
Expand Down Expand Up @@ -400,7 +415,7 @@ class Watch {

invoke() {
_dirty = false;
reactionFn(_record.currentValue, _record.previousValue, _record.object);
reactionFn(_record.currentValue, _record.previousValue);
}

remove() {
Expand Down

0 comments on commit 3bde820

Please sign in to comment.