2015-02-05 13:08:05 -08:00
|
|
|
import {isPresent, isBlank, BaseException, FunctionWrapper} from 'angular2/src/facade/lang';
|
|
|
|
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-01-14 13:51:16 -08:00
|
|
|
import {ContextWithVariableBindings} from './parser/context_with_variable_bindings';
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
import {AbstractChangeDetector} from './abstract_change_detector';
|
2015-02-12 14:56:41 -08:00
|
|
|
import {PipeRegistry} from './pipes/pipe_registry';
|
2015-01-21 12:05:52 -08:00
|
|
|
import {ChangeDetectionUtil, SimpleChange, uninitialized} from './change_detection_util';
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
ProtoRecord,
|
|
|
|
RECORD_TYPE_SELF,
|
|
|
|
RECORD_TYPE_PROPERTY,
|
|
|
|
RECORD_TYPE_INVOKE_METHOD,
|
|
|
|
RECORD_TYPE_CONST,
|
|
|
|
RECORD_TYPE_INVOKE_CLOSURE,
|
2015-01-21 12:05:52 -08:00
|
|
|
RECORD_TYPE_PRIMITIVE_OP,
|
|
|
|
RECORD_TYPE_KEYED_ACCESS,
|
2015-02-19 17:47:25 -08:00
|
|
|
RECORD_TYPE_PIPE,
|
2015-02-19 10:15:06 +01:00
|
|
|
RECORD_TYPE_INTERPOLATE
|
|
|
|
} from './proto_record';
|
2015-01-14 13:51:16 -08:00
|
|
|
|
|
|
|
import {ExpressionChangedAfterItHasBeenChecked, ChangeDetectionError} from './exceptions';
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
export class DynamicChangeDetector extends AbstractChangeDetector {
|
2015-01-14 13:51:16 -08:00
|
|
|
dispatcher:any;
|
2015-02-12 14:56:41 -08:00
|
|
|
pipeRegistry;
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
values:List;
|
2015-01-26 16:16:17 -08:00
|
|
|
changes:List;
|
2015-02-12 14:56:41 -08:00
|
|
|
pipes:List;
|
|
|
|
prevContexts:List;
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
protos:List<ProtoRecord>;
|
|
|
|
|
2015-02-20 10:59:14 -08:00
|
|
|
constructor(dispatcher:any, pipeRegistry:PipeRegistry, protoRecords:List<ProtoRecord>) {
|
2015-01-21 12:05:52 -08:00
|
|
|
super();
|
2015-01-14 13:51:16 -08:00
|
|
|
this.dispatcher = dispatcher;
|
2015-02-12 14:56:41 -08:00
|
|
|
this.pipeRegistry = pipeRegistry;
|
2015-01-26 16:16:17 -08:00
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
this.values = ListWrapper.createFixedSize(protoRecords.length + 1);
|
2015-02-12 14:56:41 -08:00
|
|
|
this.pipes = ListWrapper.createFixedSize(protoRecords.length + 1);
|
|
|
|
this.prevContexts = ListWrapper.createFixedSize(protoRecords.length + 1);
|
2015-01-26 16:16:17 -08:00
|
|
|
this.changes = ListWrapper.createFixedSize(protoRecords.length + 1);
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
this.protos = protoRecords;
|
|
|
|
}
|
|
|
|
|
|
|
|
setContext(context:any) {
|
2015-02-06 17:03:40 -08:00
|
|
|
ListWrapper.fill(this.values, uninitialized);
|
2015-02-12 14:56:41 -08:00
|
|
|
ListWrapper.fill(this.changes, false);
|
|
|
|
ListWrapper.fill(this.pipes, null);
|
|
|
|
ListWrapper.fill(this.prevContexts, uninitialized);
|
2015-01-14 13:51:16 -08:00
|
|
|
this.values[0] = context;
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
detectChangesInRecords(throwOnChange:boolean) {
|
2015-01-14 13:51:16 -08:00
|
|
|
var protos:List<ProtoRecord> = this.protos;
|
|
|
|
|
|
|
|
var updatedRecords = null;
|
|
|
|
for (var i = 0; i < protos.length; ++i) {
|
|
|
|
var proto:ProtoRecord = protos[i];
|
|
|
|
var change = this._check(proto);
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
if (isPresent(change)) {
|
2015-01-27 17:30:32 -08:00
|
|
|
var record = ChangeDetectionUtil.changeRecord(proto.bindingMemento, change);
|
|
|
|
updatedRecords = ChangeDetectionUtil.addRecord(updatedRecords, record);
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:35:05 -08:00
|
|
|
if (proto.lastInDirective && isPresent(updatedRecords)) {
|
2015-01-21 12:05:52 -08:00
|
|
|
if (throwOnChange) ChangeDetectionUtil.throwOnChange(proto, updatedRecords[0]);
|
|
|
|
|
2015-02-06 13:35:05 -08:00
|
|
|
this.dispatcher.onRecordChange(proto.directiveMemento, updatedRecords);
|
2015-01-21 12:05:52 -08:00
|
|
|
updatedRecords = null;
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_check(proto:ProtoRecord) {
|
|
|
|
try {
|
2015-02-19 17:47:25 -08:00
|
|
|
if (proto.mode == RECORD_TYPE_PIPE) {
|
2015-02-12 14:56:41 -08:00
|
|
|
return this._pipeCheck(proto);
|
2015-01-14 13:51:16 -08:00
|
|
|
} else {
|
|
|
|
return this._referenceCheck(proto);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw new ChangeDetectionError(proto, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_referenceCheck(proto:ProtoRecord) {
|
2015-01-26 16:16:17 -08:00
|
|
|
if (this._pureFuncAndArgsDidNotChange(proto)) {
|
|
|
|
this._setChanged(proto, false);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
var prevValue = this._readSelf(proto);
|
|
|
|
var currValue = this._calculateCurrValue(proto);
|
|
|
|
|
2015-01-22 11:52:36 +01:00
|
|
|
if (!isSame(prevValue, currValue)) {
|
2015-01-14 13:51:16 -08:00
|
|
|
this._writeSelf(proto, currValue);
|
2015-01-26 16:16:17 -08:00
|
|
|
this._setChanged(proto, true);
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
if (proto.lastInBinding) {
|
2015-01-27 17:30:32 -08:00
|
|
|
return ChangeDetectionUtil.simpleChange(prevValue, currValue);
|
2015-01-21 12:05:52 -08:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-01-14 13:51:16 -08:00
|
|
|
} else {
|
2015-01-26 16:16:17 -08:00
|
|
|
this._setChanged(proto, false);
|
2015-01-14 13:51:16 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_calculateCurrValue(proto:ProtoRecord) {
|
2015-01-14 13:51:16 -08:00
|
|
|
switch (proto.mode) {
|
|
|
|
case RECORD_TYPE_SELF:
|
2015-01-25 16:59:06 -08:00
|
|
|
return this._readContext(proto);
|
2015-01-14 13:51:16 -08:00
|
|
|
|
|
|
|
case RECORD_TYPE_CONST:
|
|
|
|
return proto.funcOrValue;
|
|
|
|
|
|
|
|
case RECORD_TYPE_PROPERTY:
|
|
|
|
var context = this._readContext(proto);
|
2015-01-21 12:05:52 -08:00
|
|
|
var c = ChangeDetectionUtil.findContext(proto.name, context);
|
|
|
|
if (c instanceof ContextWithVariableBindings) {
|
|
|
|
return c.get(proto.name);
|
|
|
|
} else {
|
|
|
|
var propertyGetter:Function = proto.funcOrValue;
|
|
|
|
return propertyGetter(c);
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
2015-01-21 12:05:52 -08:00
|
|
|
break;
|
2015-01-14 13:51:16 -08:00
|
|
|
|
|
|
|
case RECORD_TYPE_INVOKE_METHOD:
|
2015-02-20 15:50:12 -08:00
|
|
|
var context = this._readContext(proto);
|
|
|
|
var args = this._readArgs(proto);
|
|
|
|
var c = ChangeDetectionUtil.findContext(proto.name, context);
|
|
|
|
if (c instanceof ContextWithVariableBindings) {
|
|
|
|
return FunctionWrapper.apply(c.get(proto.name), args);
|
|
|
|
} else {
|
|
|
|
var methodInvoker:Function = proto.funcOrValue;
|
|
|
|
return methodInvoker(c, args);
|
|
|
|
}
|
|
|
|
break;
|
2015-01-14 13:51:16 -08:00
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
case RECORD_TYPE_KEYED_ACCESS:
|
|
|
|
var arg = this._readArgs(proto)[0];
|
|
|
|
return this._readContext(proto)[arg];
|
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
case RECORD_TYPE_INVOKE_CLOSURE:
|
|
|
|
return FunctionWrapper.apply(this._readContext(proto), this._readArgs(proto));
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
case RECORD_TYPE_INTERPOLATE:
|
|
|
|
case RECORD_TYPE_PRIMITIVE_OP:
|
2015-01-14 13:51:16 -08:00
|
|
|
return FunctionWrapper.apply(proto.funcOrValue, this._readArgs(proto));
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new BaseException(`Unknown operation ${proto.mode}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 14:56:41 -08:00
|
|
|
_pipeCheck(proto:ProtoRecord) {
|
2015-01-14 13:51:16 -08:00
|
|
|
var context = this._readContext(proto);
|
2015-02-12 14:56:41 -08:00
|
|
|
var pipe = this._pipeFor(proto, context);
|
2015-01-14 13:51:16 -08:00
|
|
|
|
2015-02-12 14:56:41 -08:00
|
|
|
var newValue = pipe.transform(context);
|
|
|
|
if (! ChangeDetectionUtil.noChangeMarker(newValue)) {
|
2015-02-20 16:23:16 -08:00
|
|
|
var prevValue = this._readSelf(proto);
|
2015-02-12 14:56:41 -08:00
|
|
|
this._writeSelf(proto, newValue);
|
|
|
|
this._setChanged(proto, true);
|
|
|
|
|
|
|
|
if (proto.lastInBinding) {
|
|
|
|
return ChangeDetectionUtil.simpleChange(prevValue, newValue);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._setChanged(proto, false);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_pipeFor(proto:ProtoRecord, context) {
|
|
|
|
var storedPipe = this._readPipe(proto);
|
|
|
|
if (isPresent(storedPipe) && storedPipe.supports(context)) {
|
|
|
|
return storedPipe;
|
|
|
|
} else {
|
2015-02-19 17:47:25 -08:00
|
|
|
var pipe = this.pipeRegistry.get(proto.name, context);
|
2015-02-12 14:56:41 -08:00
|
|
|
this._writePipe(proto, pipe);
|
|
|
|
return pipe;
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_readContext(proto:ProtoRecord) {
|
2015-01-14 13:51:16 -08:00
|
|
|
return this.values[proto.contextIndex];
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_readSelf(proto:ProtoRecord) {
|
2015-01-21 12:05:52 -08:00
|
|
|
return this.values[proto.selfIndex];
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_writeSelf(proto:ProtoRecord, value) {
|
2015-01-21 12:05:52 -08:00
|
|
|
this.values[proto.selfIndex] = value;
|
2015-01-14 13:51:16 -08:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:56:41 -08:00
|
|
|
_readPipe(proto:ProtoRecord) {
|
|
|
|
return this.pipes[proto.selfIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
_writePipe(proto:ProtoRecord, value) {
|
|
|
|
this.pipes[proto.selfIndex] = value;
|
|
|
|
}
|
|
|
|
|
2015-01-26 16:16:17 -08:00
|
|
|
_setChanged(proto:ProtoRecord, value:boolean) {
|
|
|
|
this.changes[proto.selfIndex] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_pureFuncAndArgsDidNotChange(proto:ProtoRecord):boolean {
|
|
|
|
return proto.isPureFunction() && !this._argsChanged(proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
_argsChanged(proto:ProtoRecord):boolean {
|
|
|
|
var args = proto.args;
|
|
|
|
for(var i = 0; i < args.length; ++i) {
|
|
|
|
if (this.changes[args[i]]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:08:10 +01:00
|
|
|
_readArgs(proto:ProtoRecord) {
|
2015-01-14 13:51:16 -08:00
|
|
|
var res = ListWrapper.createFixedSize(proto.args.length);
|
|
|
|
var args = proto.args;
|
|
|
|
for (var i = 0; i < args.length; ++i) {
|
|
|
|
res[i] = this.values[args[i]];
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _singleElementList = [null];
|
|
|
|
|
|
|
|
function isSame(a, b) {
|
|
|
|
if (a === b) return true;
|
|
|
|
if (a instanceof String && b instanceof String && a == b) return true;
|
|
|
|
if ((a !== a) && (b !== b)) return true;
|
|
|
|
return false;
|
2015-01-22 11:08:10 +01:00
|
|
|
}
|