From fe2ae8d77545e4c8df3900197b92954301faed77 Mon Sep 17 00:00:00 2001 From: zsmj Date: Sun, 23 Apr 2023 11:26:46 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14817=20feat:=20=E5=AF=B9freeze=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1watch=E5=AE=B9=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/fineui/src/fix/fix.js | 95 ++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/packages/fineui/src/fix/fix.js b/packages/fineui/src/fix/fix.js index 6c9640e1f..1cd977916 100644 --- a/packages/fineui/src/fix/fix.js +++ b/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) { return typeof Ctor === "function" && /native code/.test(Ctor.toString()); @@ -65,7 +66,7 @@ function parsePath(path) { // } const segments = path.split("."); - return function (obj) { + return function(obj) { for (let i = 0; i < segments.length; i++) { if (!obj) return; obj = obj[segments[i]]; @@ -75,7 +76,7 @@ function parsePath(path) { }; } -const nextTick = (function () { +const nextTick = (function() { const callbacks = []; let pending = false; let timerFunc = void 0; @@ -169,7 +170,7 @@ let uid = 0; * directives subscribing to it. */ -const Dep = (function () { +const Dep = (function() { function Dep() { _classCallCheck(this, Dep); @@ -332,7 +333,7 @@ function queueWatcher(watcher, options) { let uid$1 = 0; -const Watcher = (function () { +const Watcher = (function() { function Watcher(vm, expOrFn, cb, options) { _classCallCheck(this, Watcher); @@ -363,7 +364,8 @@ const Watcher = (function () { } else { this.getter = parsePath(expOrFn); if (!this.getter) { - this.getter = function () {}; + this.getter = function() { + }; } } this.value = this.lazy ? undefined : this.get(); @@ -598,7 +600,7 @@ function def(obj, key, val, enumerable) { * collect dependencies and dispatches updates. */ -const Observer = (function () { +const Observer = (function() { function Observer(value) { _classCallCheck(this, Observer); @@ -974,41 +976,43 @@ function watch(model, expOrFn, cb, options) { } const getter = exp === "**" || exp === "*" - ? function (m) { - return m; - } + ? function(m) { + return m; + } : parsePath(exp); const v = getter.call(model, model); - const _dep = new Dep(); - if (isGlobal) { - (v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push( - _dep + if (v.__ob__) { + const _dep = new Dep(); + if (isGlobal) { + (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 { - (v.__ob__._deps || (v.__ob__._deps = [])).push(_dep); + watchers.push(() => { + _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; } @@ -1017,6 +1021,11 @@ function watch(model, expOrFn, cb, options) { // 先获取到能获取到的对象 const _paths = exp.split("."); const _currentModel = model[_paths[1]]; + + if (!_currentModel.__ob__) { + return; + } + exp = `${_paths[1]}.**`; // 补全路径 let _parent = _currentModel.__ob__.parent, @@ -1079,6 +1088,11 @@ function watch(model, expOrFn, cb, options) { } currentModel = model[paths[_i]]; } + + if (!currentModel.__ob__) { + return; + } + exp = exp.substr(exp.indexOf("*")); // 补全路径 let parent = currentModel.__ob__.parent, @@ -1408,7 +1422,7 @@ function getInjectValues(vm) { return result; } -const Model = (function () { +const Model = (function() { function 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() { this._init();