Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0b6cbf7

Browse files
committed
WIP: keep last value/result cache associated with scopes
1 parent d1fa727 commit 0b6cbf7

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/ng/interpolate.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ function $InterpolateProvider() {
130130
hasInterpolation = false,
131131
hasText = false,
132132
exp,
133-
concat = [];
133+
concat = [],
134+
lastValuesCache = { values: {}, results: {}};
134135

135136
while(index < textLength) {
136137
if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) &&
@@ -172,9 +173,6 @@ function $InterpolateProvider() {
172173
if (!mustHaveExpression || hasInterpolation) {
173174
concat.length = separators.length + expressions.length;
174175

175-
var lastValues = [];
176-
var lastResult;
177-
178176
var compute = function(values) {
179177
for(var i = 0, ii = expressions.length; i < ii; i++) {
180178
concat[2*i] = separators[i];
@@ -201,12 +199,30 @@ function $InterpolateProvider() {
201199
};
202200

203201
return extend(function interpolationFn(context) {
202+
var scopeId = context.$id || 'notAScope';
203+
var lastValues = lastValuesCache.values[scopeId];
204+
var lastResult = lastValuesCache.results[scopeId];
204205
var i = 0;
205206
var ii = expressions.length;
206207
var values = new Array(ii);
207208
var val;
208209
var inputsChanged = lastResult === undefined ? true: false;
209210

211+
212+
// if we haven't seen this context before, initialize the cache and try to setup
213+
// a cleanup routine that purges the cache when the scope goes away.
214+
if (!lastValues) {
215+
lastValues = [];
216+
inputsChanged = true;
217+
if (context.$on) {
218+
context.$on('$destroy', function() {
219+
lastValuesCache.values[scopeId] = null;
220+
lastValuesCache.results[scopeId] = null;
221+
});
222+
}
223+
}
224+
225+
210226
try {
211227
for (; i < ii; i++) {
212228
val = stringify(parseFns[i](context));
@@ -217,8 +233,8 @@ function $InterpolateProvider() {
217233
}
218234

219235
if (inputsChanged) {
220-
lastValues = values;
221-
lastResult = compute(values);
236+
lastValuesCache.values[scopeId] = values;
237+
lastValuesCache.results[scopeId] = lastResult = compute(values);
222238
}
223239
} catch(err) {
224240
var newErr = $interpolateMinErr('interr', "Can't interpolate: {0}\n{1}", text,

0 commit comments

Comments
 (0)