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

Commit

Permalink
feat(Scope): Brand new scope implementation which takes advantage of …
Browse files Browse the repository at this point in the history
…the new change detection
  • Loading branch information
mhevery committed Feb 20, 2014
1 parent 3bde820 commit 390aea5
Show file tree
Hide file tree
Showing 77 changed files with 3,868 additions and 5,609 deletions.
4 changes: 2 additions & 2 deletions demo/bouncing_balls/bouncy_balls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BounceController {

timeDigest() {
var start = window.performance.now();
scope.$evalAsync(() {
scope.runAsync(() {
digestTime = (window.performance.now() - start).round();
}, outsideDigest: true);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ class BallPositionDirective {

set position(BallModel model) {
element.style.backgroundColor = model.color;
scope.$watch(() {
scope.watch(() {
element.style.left = '${model.x + 10}px';
element.style.top = '${model.y + 10}px';
});
Expand Down
12 changes: 7 additions & 5 deletions lib/change_detection/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class ContextReferenceAST extends AST {
class ConstantAST extends AST {
final constant;

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

WatchRecord<_Handler> setupWatch(WatchGroup watchGroup)
Expand Down Expand Up @@ -107,9 +109,9 @@ class MethodAST extends AST {

class CollectionAST extends AST {
final AST valueAST;
CollectionAST(valueAST):
super('#collection($valueAST)'),
valueAST = valueAST;
CollectionAST(valueAST)
: super('#collection($valueAST)'),
valueAST = valueAST;

WatchRecord<_Handler> setupWatch(WatchGroup watchGroup) {
return watchGroup.addCollectionWatch(valueAST);
Expand Down
69 changes: 65 additions & 4 deletions lib/change_detection/change_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,64 @@ abstract class ChangeRecord<H> extends Record<H> {
}

/**
* If [ChangeDetector] is watching a collection (an [Iterable]) then the
* If [ChangeDetector] is watching a an [Map] then the
* [currentValue] of [Record] will contain this object. The object contains a
* summary of changes to the map since the last execution. The changes
* are reported as a list of [MapKeyValue]s which contain the current
* and previous value in the list as well as the key.
*/
abstract class MapChangeRecord<K, V> {
/// The underlying iterable object
Map get map;

/// A list of [CollectionKeyValue]s which are in the iteration order. */
KeyValue<K, V> get mapHead;
/// A list of changed items.
ChangedKeyValue<K, V> get changesHead;
/// A list of new added items.
AddedKeyValue<K, V> get additionsHead;
/// A list of removed items
RemovedKeyValue<K, V> get removalsHead;

void forEachChange(void f(ChangedKeyValue<K, V> change));
void forEachAddition(void f(AddedKeyValue<K, V> addition));
void forEachRemoval(void f(RemovedKeyValue<K, V> removal));
}

/**
* Each item in map is wrapped in [MapKeyValue], which can track
* the [item]s [currentValue] and [previousValue] location.
*/
abstract class MapKeyValue<K, V> {
/// The item.
K get key;

/// Previous item location in the list or [null] if addition.
V get previousValue;

/// Current item location in the list or [null] if removal.
V get currentValue;
}

abstract class KeyValue<K, V> extends MapKeyValue<K, V> {
KeyValue<K, V> get nextKeyValue;
}

abstract class AddedKeyValue<K, V> extends MapKeyValue<K, V> {
AddedKeyValue<K, V> get nextAddedKeyValue;
}

abstract class RemovedKeyValue<K, V> extends MapKeyValue<K, V> {
RemovedKeyValue<K, V> get nextRemovedKeyValue;
}

abstract class ChangedKeyValue<K, V> extends MapKeyValue<K, V> {
ChangedKeyValue<K, V> get nextChangedKeyValue;
}


/**
* If [ChangeDetector] is watching a an [Iterable] then the
* [currentValue] of [Record] will contain this object. The object contains a
* summary of changes to the collection since the last execution. The changes
* are reported as a list of [CollectionChangeItem]s which contain the current
Expand All @@ -130,18 +187,22 @@ abstract class CollectionChangeRecord<K, V> {
MovedItem<K, V> get movesHead;
/** A list of [RemovedItem]s. */
RemovedItem<K, V> get removalsHead;

void forEachAddition(void f(AddedItem<K, V> addition));
void forEachMove(void f(MovedItem<K, V> move));
void forEachRemoval(void f(RemovedItem<K, V> removal));
}

/**
* Each item in collection is wrapped in [CollectionChangeItem], which can track
* the [item]s [currentKey] and [previousKey] location.
*/
abstract class CollectionChangeItem<K, V> {
abstract class CollectionChangeItem<K, V> { // TODO(misko): change <K,V> to <V> since K is int.
/** Previous item location in the list or [null] if addition. */
K get previousKey;
K get previousKey; // TODO(misko): rename to previousIndex

/** Current item location in the list or [null] if removal. */
K get currentKey;
K get currentKey; // TODO(misko): rename to CurrentIndex

/** The item. */
V get item;
Expand Down
Loading

0 comments on commit 390aea5

Please sign in to comment.