Browse Source

Pull request #3499: KERNEL-14817 feat: 对freeze的对象watch容错

Merge in VISUAL/fineui from ~DAILER/fineui:es6 to es6

* commit 'fe2ae8d77545e4c8df3900197b92954301faed77':
  KERNEL-14817 feat: 对freeze的对象watch容错
es6
Dailer-刘荣歆 1 year ago
parent
commit
f1311f450a
  1. 95
      packages/fineui/src/fix/fix.js

95
packages/fineui/src/fix/fix.js

@ -6,7 +6,8 @@ function _classCallCheck(instance, Constructor) {
} }
} }
function noop(a, b, c) {} function noop(a, b, c) {
}
function isNative(Ctor) { function isNative(Ctor) {
return typeof Ctor === "function" && /native code/.test(Ctor.toString()); return typeof Ctor === "function" && /native code/.test(Ctor.toString());
@ -65,7 +66,7 @@ function parsePath(path) {
// } // }
const segments = path.split("."); const segments = path.split(".");
return function (obj) { return function(obj) {
for (let i = 0; i < segments.length; i++) { for (let i = 0; i < segments.length; i++) {
if (!obj) return; if (!obj) return;
obj = obj[segments[i]]; obj = obj[segments[i]];
@ -75,7 +76,7 @@ function parsePath(path) {
}; };
} }
const nextTick = (function () { const nextTick = (function() {
const callbacks = []; const callbacks = [];
let pending = false; let pending = false;
let timerFunc = void 0; let timerFunc = void 0;
@ -169,7 +170,7 @@ let uid = 0;
* directives subscribing to it. * directives subscribing to it.
*/ */
const Dep = (function () { const Dep = (function() {
function Dep() { function Dep() {
_classCallCheck(this, Dep); _classCallCheck(this, Dep);
@ -332,7 +333,7 @@ function queueWatcher(watcher, options) {
let uid$1 = 0; let uid$1 = 0;
const Watcher = (function () { const Watcher = (function() {
function Watcher(vm, expOrFn, cb, options) { function Watcher(vm, expOrFn, cb, options) {
_classCallCheck(this, Watcher); _classCallCheck(this, Watcher);
@ -363,7 +364,8 @@ const Watcher = (function () {
} else { } else {
this.getter = parsePath(expOrFn); this.getter = parsePath(expOrFn);
if (!this.getter) { if (!this.getter) {
this.getter = function () {}; this.getter = function() {
};
} }
} }
this.value = this.lazy ? undefined : this.get(); this.value = this.lazy ? undefined : this.get();
@ -598,7 +600,7 @@ function def(obj, key, val, enumerable) {
* collect dependencies and dispatches updates. * collect dependencies and dispatches updates.
*/ */
const Observer = (function () { const Observer = (function() {
function Observer(value) { function Observer(value) {
_classCallCheck(this, Observer); _classCallCheck(this, Observer);
@ -974,41 +976,43 @@ function watch(model, expOrFn, cb, options) {
} }
const getter = const getter =
exp === "**" || exp === "*" exp === "**" || exp === "*"
? function (m) { ? function(m) {
return m; return m;
} }
: parsePath(exp); : parsePath(exp);
const v = getter.call(model, model); const v = getter.call(model, model);
const _dep = new Dep(); if (v.__ob__) {
if (isGlobal) { const _dep = new Dep();
(v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push( if (isGlobal) {
_dep (v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push(
_dep
);
} else {
(v.__ob__._deps || (v.__ob__._deps = [])).push(_dep);
}
const _w = new Watcher(
model,
() => {
_dep.depend();
return NaN;
},
(newValue, oldValue, attrs) => {
callback(
i,
newValue,
oldValue,
_.extend({ index: i }, attrs)
);
},
options
); );
} else { watchers.push(() => {
(v.__ob__._deps || (v.__ob__._deps = [])).push(_dep); _w.teardown();
v.__ob__._scopeDeps && remove(v.__ob__._scopeDeps, _dep);
v.__ob__._deps && remove(v.__ob__._deps, _dep);
});
} }
const _w = new Watcher(
model,
() => {
_dep.depend();
return NaN;
},
(newValue, oldValue, attrs) => {
callback(
i,
newValue,
oldValue,
_.extend({ index: i }, attrs)
);
},
options
);
watchers.push(() => {
_w.teardown();
v.__ob__._scopeDeps && remove(v.__ob__._scopeDeps, _dep);
v.__ob__._deps && remove(v.__ob__._deps, _dep);
});
return; return;
} }
@ -1017,6 +1021,11 @@ function watch(model, expOrFn, cb, options) {
// 先获取到能获取到的对象 // 先获取到能获取到的对象
const _paths = exp.split("."); const _paths = exp.split(".");
const _currentModel = model[_paths[1]]; const _currentModel = model[_paths[1]];
if (!_currentModel.__ob__) {
return;
}
exp = `${_paths[1]}.**`; exp = `${_paths[1]}.**`;
// 补全路径 // 补全路径
let _parent = _currentModel.__ob__.parent, let _parent = _currentModel.__ob__.parent,
@ -1079,6 +1088,11 @@ function watch(model, expOrFn, cb, options) {
} }
currentModel = model[paths[_i]]; currentModel = model[paths[_i]];
} }
if (!currentModel.__ob__) {
return;
}
exp = exp.substr(exp.indexOf("*")); exp = exp.substr(exp.indexOf("*"));
// 补全路径 // 补全路径
let parent = currentModel.__ob__.parent, let parent = currentModel.__ob__.parent,
@ -1408,7 +1422,7 @@ function getInjectValues(vm) {
return result; return result;
} }
const Model = (function () { const Model = (function() {
function Model() { function Model() {
_classCallCheck(this, Model); _classCallCheck(this, Model);
} }
@ -1458,7 +1472,8 @@ const Model = (function () {
} }
}; };
Model.prototype._init = function _init() {}; Model.prototype._init = function _init() {
};
Model.prototype.init = function init() { Model.prototype.init = function init() {
this._init(); this._init();

Loading…
Cancel
Save