2014-09-26 11:20:08 -07:00
|
|
|
import {ProtoRecord, Record} from './record';
|
2014-09-28 13:55:01 -07:00
|
|
|
import {FIELD} from 'facade/lang';
|
2014-10-27 17:57:36 +01:00
|
|
|
import {ListWrapper} from 'facade/collection';
|
2014-09-26 11:20:08 -07:00
|
|
|
|
|
|
|
export class ProtoWatchGroup {
|
2014-10-01 16:29:45 +02:00
|
|
|
@FIELD('final headRecord:ProtoRecord')
|
|
|
|
@FIELD('final tailRecord:ProtoRecord')
|
2014-09-26 11:20:08 -07:00
|
|
|
constructor() {
|
2014-10-01 16:29:45 +02:00
|
|
|
this.headRecord = null;
|
|
|
|
this.tailRecord = null;
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-30 15:50:20 -07:00
|
|
|
/**
|
|
|
|
* Parses [expression] into [ProtoRecord]s and adds them to [ProtoWatchGroup].
|
|
|
|
*
|
|
|
|
* @param expression The expression to watch
|
2014-10-02 15:14:32 +02:00
|
|
|
* @param memento an opaque object which will be passed to WatchGroupDispatcher on
|
2014-09-30 15:50:20 -07:00
|
|
|
* detecting a change.
|
|
|
|
* @param shallow Should collections be shallow watched
|
|
|
|
*/
|
2014-10-02 15:14:32 +02:00
|
|
|
watch(expression:string,
|
|
|
|
memento,
|
2014-10-27 17:57:36 +01:00
|
|
|
shallow = false)
|
2014-09-26 11:20:08 -07:00
|
|
|
{
|
2014-10-27 17:57:36 +01:00
|
|
|
var parts = expression.split('.');
|
|
|
|
var protoRecords = ListWrapper.createFixedSize(parts.length);
|
2014-10-02 15:14:32 +02:00
|
|
|
|
2014-10-27 17:57:36 +01:00
|
|
|
for (var i = parts.length - 1; i >= 0; i--) {
|
|
|
|
protoRecords[i] = new ProtoRecord(this, parts[i], memento);
|
|
|
|
memento = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
var protoRecord = protoRecords[i];
|
|
|
|
if (this.headRecord === null) {
|
|
|
|
this.headRecord = this.tailRecord = protoRecord;
|
|
|
|
} else {
|
|
|
|
this.tailRecord.next = protoRecord;
|
|
|
|
protoRecord.prev = this.tailRecord;
|
|
|
|
this.tailRecord = protoRecord;
|
|
|
|
}
|
2014-10-02 15:14:32 +02:00
|
|
|
}
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
instantiate(dispatcher:WatchGroupDispatcher):WatchGroup {
|
|
|
|
var watchGroup:WatchGroup = new WatchGroup(this, dispatcher);
|
|
|
|
var tail:Record = null;
|
2014-10-02 15:14:32 +02:00
|
|
|
var proto:ProtoRecord;
|
|
|
|
var prevRecord:Record = null;
|
2014-09-26 11:20:08 -07:00
|
|
|
|
2014-10-02 15:14:32 +02:00
|
|
|
if (this.headRecord !== null) {
|
|
|
|
watchGroup.headRecord = tail = new Record(watchGroup, this.headRecord);
|
2014-09-26 11:20:08 -07:00
|
|
|
|
2014-10-02 15:14:32 +02:00
|
|
|
for (proto = this.headRecord.next; proto != null; proto = proto.next) {
|
|
|
|
prevRecord = tail;
|
|
|
|
tail = new Record(watchGroup, proto);
|
|
|
|
tail.prev = prevRecord;
|
|
|
|
prevRecord.next = tail;
|
|
|
|
tail.checkPrev = prevRecord;
|
|
|
|
prevRecord.checkNext = tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
watchGroup.tailRecord = tail;
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return watchGroup;
|
|
|
|
}
|
2014-09-30 15:50:20 -07:00
|
|
|
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-19 16:38:37 -07:00
|
|
|
export class WatchGroup {
|
2014-09-26 11:20:08 -07:00
|
|
|
@FIELD('final protoWatchGroup:ProtoWatchGroup')
|
2014-09-19 16:38:37 -07:00
|
|
|
@FIELD('final dispatcher:WatchGroupDispatcher')
|
2014-10-01 16:29:45 +02:00
|
|
|
@FIELD('final headRecord:Record')
|
|
|
|
@FIELD('final tailRecord:Record')
|
2014-09-26 11:20:08 -07:00
|
|
|
constructor(protoWatchGroup:ProtoWatchGroup, dispatcher:WatchGroupDispatcher) {
|
|
|
|
this.protoWatchGroup = protoWatchGroup;
|
|
|
|
this.dispatcher = dispatcher;
|
2014-10-01 16:29:45 +02:00
|
|
|
this.headRecord = null;
|
|
|
|
this.tailRecord = null;
|
2014-10-02 15:14:32 +02:00
|
|
|
this.context = null;
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
insertChildGroup(newChild:WatchGroup, insertAfter:WatchGroup) {
|
2014-10-02 15:14:32 +02:00
|
|
|
throw 'not implemented';
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
remove() {
|
2014-10-02 15:14:32 +02:00
|
|
|
throw 'not implemented';
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:39:37 -07:00
|
|
|
/**
|
|
|
|
* Sets the context (the object) on which the change detection expressions will
|
|
|
|
* dereference themselves on. Since the WatchGroup can be reused the context
|
|
|
|
* can be re-set many times during the lifetime of the WatchGroup.
|
|
|
|
*
|
2014-10-02 15:14:32 +02:00
|
|
|
* @param context the new context for change detection for the current WatchGroup
|
2014-09-30 16:39:37 -07:00
|
|
|
*/
|
|
|
|
setContext(context) {
|
2014-10-02 15:14:32 +02:00
|
|
|
for (var record:Record = this.headRecord;
|
|
|
|
record != null;
|
|
|
|
record = record.next) {
|
|
|
|
record.setContext(context);
|
|
|
|
}
|
2014-09-30 16:39:37 -07:00
|
|
|
}
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|
2014-09-28 16:29:11 -07:00
|
|
|
|
|
|
|
export class WatchGroupDispatcher {
|
2014-10-02 15:14:32 +02:00
|
|
|
// The record holds the previous value at the time of the call
|
2014-09-28 16:29:11 -07:00
|
|
|
onRecordChange(record:Record, context) {}
|
|
|
|
}
|