diff --git a/packages/fineui/src/base/collection/collection.js b/packages/fineui/src/base/collection/collection.js index 2019bfa7a..9f933ed20 100644 --- a/packages/fineui/src/base/collection/collection.js +++ b/packages/fineui/src/base/collection/collection.js @@ -124,6 +124,12 @@ export class CollectionView extends Widget { this.element.scrollLeft(scrollLeft); } } + + destroyed() { + each(this.renderedCells, (i, cell) => { + cell.el._destroy(); + }); + } _calculateSizeAndPositionData() { const { items, cellSizeAndPositionGetter } = this.options; diff --git a/packages/fineui/src/fix/fix.js b/packages/fineui/src/fix/fix.js index 92cba70f8..a28f5e4a8 100644 --- a/packages/fineui/src/fix/fix.js +++ b/packages/fineui/src/fix/fix.js @@ -1,11 +1,72 @@ import { _ } from "@/core"; -function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); +let uid = 0; + +const pendingCleanupDeps = []; + +const cleanupDeps = () => { + for (let i = 0; i < pendingCleanupDeps.length; i++) { + const dep = pendingCleanupDeps[i]; + dep.subs = dep.subs.filter(s => s); + dep._pending = false; + } + pendingCleanupDeps.length = 0; +}; + + +/** + * A dep is an observable that can have multiple + * directives subscribing to it. + */ +class Dep { + constructor() { + this.id = uid++; + this._pending = false; + this.subs = []; + } + + addSub(sub) { + this.subs.push(sub); + } + + removeSub(sub) { + this.subs[this.subs.indexOf(sub)] = null; + if (!this._pending) { + this._pending = true; + pendingCleanupDeps.push(this); + } + } + + depend() { + if (Dep.target) { + Dep.target.addDep(this); + } + } + + notify(options) { + // stabilize the subscriber list first + const subs = this.subs.filter(s => s); + for (let i = 0, l = subs.length; i < l; i++) { + subs[i].update(options); + } } } +// the current target watcher being evaluated. +// this is globally unique because there could be only one +// watcher being evaluated at any time. +Dep.target = null; +const targetStack = []; + +function pushTarget(_target) { + if (Dep.target) targetStack.push(Dep.target); + Dep.target = _target; +} + +function popTarget() { + Dep.target = targetStack.pop(); +} + function noop(a, b, c) { } @@ -13,6 +74,7 @@ function isNative(Ctor) { return typeof Ctor === "function" && /native code/.test(Ctor.toString()); } + const hasProto = "__proto__" in {}; const _toString = Object.prototype.toString; @@ -23,13 +85,10 @@ function isPlainObject(obj) { function isConfigurable(obj, key) { let configurable = true; - const property = - Object.getOwnPropertyDescriptor && - Object.getOwnPropertyDescriptor(obj, key); + const property = Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(obj, key); if (property && property.configurable === false) { configurable = false; } - return configurable; } @@ -42,17 +101,17 @@ function isExtensible(obj) { name += "?"; } obj[name] = true; - const returnValue = obj.hasOwnProperty(name); + let returnValue = obj.hasOwnProperty(name); delete obj[name]; - return returnValue; } + function remove(arr, item) { if (arr && arr.length) { - const _index = arr.indexOf(item); - if (_index > -1) { - return arr.splice(_index, 1); + const index = arr.indexOf(item); + if (index > -1) { + return arr.splice(index, 1); } } } @@ -65,21 +124,19 @@ function parsePath(path) { // return // } 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]]; } - return obj; }; } -const nextTick = (function() { +const nextTick = (function () { const callbacks = []; let pending = false; - let timerFunc = void 0; + let timerFunc; function nextTickHandler() { pending = false; @@ -100,37 +157,37 @@ const nextTick = (function() { // same loop is by using MessageChannel. /* istanbul ignore if */ if (typeof setImmediate !== "undefined" && isNative(setImmediate)) { - timerFunc = function timerFunc() { + timerFunc = () => { setImmediate(nextTickHandler); }; - } else if ( - typeof MessageChannel !== "undefined" && - (isNative(MessageChannel) || - // PhantomJS - MessageChannel.toString() === "[object MessageChannelConstructor]") - ) { + } else if (typeof MessageChannel !== "undefined" && ( + isNative(MessageChannel) || + // PhantomJS + MessageChannel.toString() === "[object MessageChannelConstructor]" + )) { const channel = new MessageChannel(); const port = channel.port2; channel.port1.onmessage = nextTickHandler; - timerFunc = function timerFunc() { + timerFunc = () => { port.postMessage(1); }; - } else if (typeof Promise !== "undefined" && isNative(Promise)) { + } else /* istanbul ignore next */ + if (typeof Promise !== "undefined" && isNative(Promise)) { // use microtask in non-DOM environments, e.g. Weex const p = Promise.resolve(); - timerFunc = function timerFunc() { + timerFunc = () => { p.then(nextTickHandler); }; } else { // fallback to setTimeout - timerFunc = function timerFunc() { + timerFunc = () => { setTimeout(nextTickHandler, 0); }; } return function queueNextTick(cb, ctx) { - let _resolve = void 0; + let _resolve; callbacks.push(() => { if (cb) { try { @@ -155,7 +212,7 @@ const nextTick = (function() { }; })(); -let falsy; +var falsy; const $$skipArray = { __ob__: falsy, $accessors: falsy, @@ -163,64 +220,8 @@ const $$skipArray = { $vbsetter: falsy }; -let uid = 0; - -/** - * A dep is an observable that can have multiple - * directives subscribing to it. - */ - -const Dep = (function() { - function Dep() { - _classCallCheck(this, Dep); - - this.id = uid++; - this.subs = []; - } - - Dep.prototype.addSub = function addSub(sub) { - this.subs.push(sub); - }; - - Dep.prototype.removeSub = function removeSub(sub) { - remove(this.subs, sub); - }; - - Dep.prototype.depend = function depend() { - if (Dep.target) { - Dep.target.addDep(this); - } - }; - - Dep.prototype.notify = function notify(options) { - // stabilize the subscriber list first - const subs = this.subs.slice(); - for (let i = 0, l = subs.length; i < l; i++) { - subs[i].update(options); - } - }; - - return Dep; -})(); - -// the current target watcher being evaluated. -// this is globally unique because there could be only one -// watcher being evaluated at any time. - -Dep.target = null; -const targetStack = []; - -function pushTarget(_target) { - if (Dep.target) targetStack.push(Dep.target); - Dep.target = _target; -} - -function popTarget() { - Dep.target = targetStack.pop(); -} - -// 如果浏览器不支持ecma262v5的Object.defineProperties或者存在BUG,比如IE8 -// 标准浏览器使用__defineGetter__, __defineSetter__实现 +//如果浏览器不支持ecma262v5的Object.defineProperties或者存在BUG,比如IE8 +//标准浏览器使用__defineGetter__, __defineSetter__实现 let canHideProperty = true; try { Object.defineProperty({}, "_", { @@ -233,13 +234,14 @@ try { canHideProperty = false; } + let createViewModel = Object.defineProperties; -let defineProperty = void 0; +let defineProperty; /* istanbul ignore if*/ if (!canHideProperty) { if ("__defineGetter__" in {}) { - defineProperty = function defineProperty(obj, prop, desc) { + defineProperty = function (obj, prop, desc) { if ("value" in desc) { obj[prop] = desc.value; } @@ -249,22 +251,20 @@ if (!canHideProperty) { if ("set" in desc) { obj.__defineSetter__(prop, desc.set); } - return obj; }; - createViewModel = function createViewModel(obj, descs) { - for (const prop in descs) { + createViewModel = function (obj, descs) { + for (var prop in descs) { if (descs.hasOwnProperty(prop)) { defineProperty(obj, prop, descs[prop]); } } - return obj; }; } } -const createViewModel$1 = createViewModel; +var createViewModel$1 = createViewModel; const queue = []; const activatedChildren = []; @@ -281,9 +281,7 @@ function resetSchedulerState() { function flushSchedulerQueue() { flushing = true; - let watcher = void 0, - id = void 0, - options = void 0; + let watcher, id, options; // Sort queue before flush. // This ensures that: @@ -306,14 +304,16 @@ function flushSchedulerQueue() { } resetSchedulerState(); + cleanupDeps(); } + function queueWatcher(watcher, options) { const id = watcher.id; if (has[id] == null) { has[id] = true; if (!flushing) { - queue.push({ watcher, options }); + queue.push({ watcher: watcher, options: options }); } else { // if already flushing, splice the watcher based on its id // if already past its id, it will be run next immediately. @@ -321,7 +321,7 @@ function queueWatcher(watcher, options) { while (i > index && queue[i].watcher.id > watcher.id) { i--; } - queue.splice(i + 1, 0, { watcher, options }); + queue.splice(i + 1, 0, { watcher: watcher, options: options }); } // queue the flush if (!waiting) { @@ -333,10 +333,11 @@ function queueWatcher(watcher, options) { let uid$1 = 0; -const Watcher = (function() { - function Watcher(vm, expOrFn, cb, options) { - _classCallCheck(this, Watcher); - +class Watcher { + constructor(vm, + expOrFn, + cb, + options) { this.vm = vm; // vm._watchers || (vm._watchers = []) // vm._watchers.push(this) @@ -364,16 +365,18 @@ const Watcher = (function() { } else { this.getter = parsePath(expOrFn); if (!this.getter) { - this.getter = function() { + this.getter = function () { }; } } - this.value = this.lazy ? undefined : this.get(); + this.value = this.lazy + ? undefined + : this.get(); } - Watcher.prototype.get = function get() { + get() { pushTarget(this); - let value = void 0; + let value; const vm = this.vm; try { value = this.getter.call(vm, vm); @@ -391,11 +394,10 @@ const Watcher = (function() { popTarget(); this.cleanupDeps(); } - return value; - }; + } - Watcher.prototype.addDep = function addDep(dep) { + addDep(dep) { const id = dep.id; if (!this.newDepIds.has(id)) { this.newDepIds.add(id); @@ -404,9 +406,9 @@ const Watcher = (function() { dep.addSub(this); } } - }; + } - Watcher.prototype.cleanupDeps = function cleanupDeps() { + cleanupDeps() { let i = this.deps.length; while (i--) { const dep = this.deps[i]; @@ -422,9 +424,9 @@ const Watcher = (function() { this.deps = this.newDeps; this.newDeps = tmp; this.newDeps.length = 0; - }; + } - Watcher.prototype.update = function update(options) { + update(options) { /* istanbul ignore else */ if (this.lazy) { this.dirty = true; @@ -433,9 +435,9 @@ const Watcher = (function() { } else { queueWatcher(this, options); } - }; + } - Watcher.prototype.run = function run(options) { + run(options) { if (this.active) { const value = this.get(); if ( @@ -443,8 +445,7 @@ const Watcher = (function() { // Deep watchers and watchers on Object/Arrays should fire even // when the value is the same, because the value may // have mutated. - (_.isObject(value) && options && options.refresh) || - this.deep + (_.isObject(value) && options && options.refresh) || this.deep ) { // set new value const oldValue = this.value; @@ -464,21 +465,21 @@ const Watcher = (function() { } } } - }; + } - Watcher.prototype.evaluate = function evaluate() { + evaluate() { this.value = this.get(); this.dirty = false; - }; + } - Watcher.prototype.depend = function depend() { + depend() { let i = this.deps.length; while (i--) { this.deps[i].depend(); } - }; + } - Watcher.prototype.teardown = function teardown() { + teardown() { if (this.active) { // remove self from vm's watcher list // this is a somewhat expensive operation so we skip it @@ -490,10 +491,8 @@ const Watcher = (function() { } this.active = false; } - }; - - return Watcher; -})(); + } +} const seenObjects = new Set(); @@ -503,10 +502,9 @@ function traverse(val) { } function _traverse(val, seen) { - let i = void 0, - keys = void 0; + let i, keys; const isA = _.isArray(val); - if (!isA && !_.isObject(val)) { + if ((!isA && !_.isObject(val))) { return; } if (val.__ob__) { @@ -518,65 +516,55 @@ function _traverse(val, seen) { } if (isA) { i = val.length; - while (i--) { - _traverse(val[i], seen); - } + while (i--) _traverse(val[i], seen); } else { keys = _.keys(val); i = keys.length; - while (i--) { - _traverse(val[keys[i]], seen); - } + while (i--) _traverse(val[keys[i]], seen); } } const arrayProto = Array.prototype; const arrayMethods = []; -_.each( - ["push", "pop", "shift", "unshift", "splice", "sort", "reverse"], - (method) => { - const original = arrayProto[method]; - arrayMethods[method] = function mutator() { - for ( - var _len = arguments.length, args = Array(_len), _key2 = 0; - _key2 < _len; - _key2++ - ) { - args[_key2] = arguments[_key2]; - } - - const ob = this.__ob__; - let inserted = void 0; - switch (method) { - case "push": - case "unshift": - inserted = args; - break; - case "splice": - inserted = args.slice(2); - break; - } - if (inserted) inserted = ob.observeArray(inserted); - switch (method) { - case "push": - case "unshift": - args = inserted; - break; - case "splice": - if (args.length > 2) { - args = [args[0], args[1]].concat( - inserted ? inserted : [] - ); - } - break; - } - const result = original.apply(this, args); - notify(ob.parent, ob.parentKey, ob.dep, true); - - return result; - }; - } -); +_.each([ + "push", + "pop", + "shift", + "unshift", + "splice", + "sort", + "reverse" +], function (method) { + const original = arrayProto[method]; + arrayMethods[method] = function mutator(...args) { + const ob = this.__ob__; + let inserted; + switch (method) { + case "push": + case "unshift": + inserted = args; + break; + case "splice": + inserted = args.slice(2); + break; + } + if (inserted) inserted = ob.observeArray(inserted); + switch (method) { + case "push": + case "unshift": + args = inserted; + break; + case "splice": + if (args.length > 2) { + args = [args[0], args[1]].concat(inserted ? inserted : []); + } + break; + } + const result = original.apply(this, args); + notify(ob.parent, ob.parentKey, ob.dep, true); + return result; + }; +}); const arrayKeys = _.keys(arrayMethods); @@ -599,16 +587,15 @@ function def(obj, key, val, enumerable) { * object's property keys into getter/setters that * collect dependencies and dispatches updates. */ - -const Observer = (function() { - function Observer(value) { - _classCallCheck(this, Observer); - +class Observer { + constructor(value) { this.value = value; this.dep = new Dep(); this.vmCount = 0; if (_.isArray(value)) { - const augment = hasProto ? protoAugment : copyAugment; + const augment = hasProto + ? protoAugment + : copyAugment; augment(value, arrayMethods, arrayKeys); this.model = this.observeArray(value); } else { @@ -617,26 +604,21 @@ const Observer = (function() { def(this.model, "__ob__", this); } - Observer.prototype.walk = function walk(obj) { + walk(obj) { return defineReactive(obj, this); - }; + } - Observer.prototype.observeArray = function observeArray(items) { + observeArray(items) { for (let i = 0, l = items.length; i < l; i++) { const ob = observe(items[i], this, i); items[i] = ob ? ob.model : items[i]; } - return items; - }; - - return Observer; -})(); + } +} function protoAugment(target, src, keys) { - /* eslint-disable no-proto */ Object.setPrototypeOf(target, src); - /* eslint-enable no-proto */ } /* istanbul ignore next */ @@ -651,12 +633,11 @@ function observe(value, parentObserver, parentKey) { if (!_.isObject(value)) { return; } - let ob = void 0; + let ob; if (value.__ob__ instanceof Observer) { ob = value.__ob__; } else if ( - observerState.shouldConvert && - isExtensible(value) && + observerState.shouldConvert && isExtensible(value) && (_.isArray(value) || isPlainObject(value)) ) { ob = new Observer(value); @@ -665,39 +646,33 @@ function observe(value, parentObserver, parentKey) { ob.parent = parentObserver || ob.parent; ob.parentKey = parentKey; } - return ob; } function notify(observer, key, dep, refresh) { - dep.notify({ observer, key, refresh }); + dep.notify({ observer: observer, key: key, refresh: refresh }); if (observer) { - // 触发a.*绑定的依赖 - _.each(observer._deps, (dep) => { - dep.notify({ observer, key }); + //触发a.*绑定的依赖 + _.each(observer._deps, function (dep) { + dep.notify({ observer: observer, key: key }); }); - // 触发a.**绑定的依赖 - let parent = observer, - root = observer, - route = key || ""; + //触发a.**绑定的依赖 + let parent = observer, root = observer, route = key || ""; while (parent) { - _.each(parent._scopeDeps, (dep) => { - dep.notify({ observer, key }); + _.each(parent._scopeDeps, function (dep) { + dep.notify({ observer: observer, key: key }); }); if (parent.parentKey != null) { - route = `${parent.parentKey}.${route}`; + route = parent.parentKey + "." + route; } root = parent; parent = parent.parent; } - for (const _key in root._globalDeps) { + for (let _key in root._globalDeps) { const reg = new RegExp(_key); if (reg.test(route)) { for (let i = 0; i < root._globalDeps[_key].length; i++) { - root._globalDeps[_key][i].notify({ - observer, - key: _key - }); + root._globalDeps[_key][i].notify({ observer: observer, key: _key }); } } } @@ -706,14 +681,14 @@ function notify(observer, key, dep, refresh) { function defineReactive(obj, observer, shallow) { const props = {}; - let model = void 0; - _.each(obj, (val, key) => { + let model; + _.each(obj, function (val, key) { if (key in $$skipArray) { return; } - const configurable = isConfigurable(obj, key); - const dep = (observer && observer[`__dep${key}`]) || new Dep(); - observer && (observer[`__dep${key}`] = dep); + let configurable = isConfigurable(obj, key); + const dep = (observer && observer["__dep" + key]) || new Dep(); + observer && (observer["__dep" + key] = dep); let childOb = configurable && !shallow && observe(val, observer, key); props[key] = { enumerable: true, @@ -729,20 +704,15 @@ function defineReactive(obj, observer, shallow) { } } } - return value; }, set: function reactiveSetter(newVal) { const value = childOb ? childOb.model : val; - if ( - newVal === value || - (newVal !== newVal && value !== value) - ) { + if (newVal === value || (newVal !== newVal && value !== value)) { return; } val = newVal; - childOb = - configurable && !shallow && observe(newVal, observer, key); + childOb = configurable && !shallow && observe(newVal, observer, key); if (childOb && value && value.__ob__) { childOb._scopeDeps = value.__ob__._scopeDeps; childOb._deps = value.__ob__._deps; @@ -752,14 +722,14 @@ function defineReactive(obj, observer, shallow) { } }; }); - - return (model = createViewModel$1(obj, props)); + return model = createViewModel$1(obj, props); } function defineReactiveProperty(obj, key, val, shallow) { + const dep = new Dep(); - const configurable = isConfigurable(obj, key); + let configurable = isConfigurable(obj, key); if (!configurable) { return; } @@ -783,7 +753,6 @@ function defineReactiveProperty(obj, key, val, shallow) { } } } - return value; }, set: function reactiveSetter(newVal) { @@ -809,24 +778,20 @@ function set(target, key, val) { if (_.isArray(target)) { target.length = Math.max(target.length, key); target.splice(key, 1, val); - return val; } if (_.has(target, key)) { target[key] = val; - return val; } - const ob = target.__ob__; + const ob = (target).__ob__; if (!ob) { target[key] = val; - return val; } ob.value[key] = val; target = defineReactive(ob.value, ob); notify(ob, key, ob.dep); - return target; } @@ -840,22 +805,19 @@ function freeze() { function del(target, key) { if (_.isArray(target)) { target.splice(key, 1); - return; } - const ob = target.__ob__; + const ob = (target).__ob__; if (!_.has(target, key)) { return; } if (!ob) { delete target[key]; - return target; } delete ob.value[key]; target = defineReactive(ob.value, ob); notify(ob, key, ob.dep); - return target; } @@ -864,7 +826,7 @@ function del(target, key) { * we cannot intercept array element access like property getters. */ function dependArray(value) { - for (var e, i = 0, l = value.length; i < l; i++) { + for (let e, i = 0, l = value.length; i < l; i++) { e = value[i]; e && e.__ob__ && e.__ob__.dep.depend(); if (_.isArray(e)) { @@ -873,7 +835,7 @@ function dependArray(value) { } } -let falsy$1; +var falsy$1; const operators = { "||": falsy$1, "&&": falsy$1, @@ -890,16 +852,13 @@ function runBinaryFunction(binarys) { expr += "false"; } } - - return new Function(`return ${expr}`)(); + return new Function("return " + expr)(); } -function routeToRegExp(route) { - route = route - .replace(/\*\*/g, "[a-zA-Z0-9_]+") - .replace(/\*./g, "[a-zA-Z0-9_]+."); - return `^${route}$`; +function routeToRegExp(route) { + route = route.replace(/\*\*/g, "[a-zA-Z0-9_]+").replace(/\*./g, "[a-zA-Z0-9_]+."); + return "^" + route + "$"; } function watch(model, expOrFn, cb, options) { @@ -912,26 +871,20 @@ function watch(model, expOrFn, cb, options) { } options = options || {}; options.user = true; - let exps = void 0; - if ( - _.isFunction(expOrFn) || - !(exps = expOrFn.match(/[a-zA-Z0-9_.*]+|[|][|]|[&][&]|[(]|[)]/g)) || - (exps.length === 1 && expOrFn.indexOf("*") < 0) - ) { + let exps; + if (_.isFunction(expOrFn) || !(exps = expOrFn.match(/[a-zA-Z0-9_.*]+|[|][|]|[&][&]|[(]|[)]/g)) || (exps.length === 1 && expOrFn.indexOf("*") < 0)) { const watcher = new Watcher(model, expOrFn, cb, options); if (options.immediate) { cb(watcher.value); } - return function unwatchFn() { watcher.teardown(); }; } const watchers = []; let fns = exps.slice(); - let complete = false, - running = false; - const callback = function callback(index, newValue, oldValue, attrs) { + let complete = false, running = false; + const callback = function (index, newValue, oldValue, attrs) { if (complete === true) { return; } @@ -947,7 +900,7 @@ function watch(model, expOrFn, cb, options) { } else { if (!running) { running = true; - nextTick(() => { + nextTick(function () { complete = false; running = false; fns = exps.slice(); @@ -955,62 +908,42 @@ function watch(model, expOrFn, cb, options) { } } }; - _.each(exps, (exp, i) => { + _.each(exps, function (exp, i) { if (_.has(operators, exp)) { return; } if (exp.indexOf("*") >= 0) { - // a.**或a.*形式 - if ( - /^[1-9a-zA-Z.]+(\*\*$|\*$)/.test(exp) || - exp === "**" || - exp === "*" - ) { + //a.**或a.*形式 + if (/^[1-9a-zA-Z.]+(\*\*$|\*$)/.test(exp) || exp === "**" || exp === "*") { const isGlobal = exp.indexOf("**") >= 0; if (isGlobal) { - // a.**的形式 + //a.**的形式 exp = exp.replace(".**", ""); } else { - // a.*的形式 + //a.*的形式 exp = exp.replace(".*", ""); } - const getter = - exp === "**" || exp === "*" - ? function(m) { - return m; - } - : parsePath(exp); + const getter = (exp === "**" || exp === "*") ? function (m) { + return m; + } : parsePath(exp); const v = getter.call(model, model); if (v.__ob__) { - const _dep = new Dep(); + const dep = new Dep(); if (isGlobal) { - (v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push( - _dep - ); + (v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push(dep); } else { - (v.__ob__._deps || (v.__ob__._deps = [])).push(_dep); + (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 - ); - 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, function () { + dep.depend(); + return NaN; + }, function (newValue, oldValue, attrs) { + callback(i, newValue, oldValue, _.extend({ index: i }, attrs)); + }, options); + watchers.push(function unwatchFn() { + w.teardown(); + v.__ob__._scopeDeps && remove(v.__ob__._scopeDeps, dep); + v.__ob__._deps && remove(v.__ob__._deps, dep); }); } @@ -1018,87 +951,71 @@ function watch(model, expOrFn, cb, options) { } // **.a.**的情况,场景:a.b.c, 如果用b.**监听, a被重新赋值b上的_scopeDes就不存在了 if (/^(\*\*\.)+[1-9a-zA-Z]+(\.\*\*$)/.test(exp)) { - // 先获取到能获取到的对象 - const _paths = exp.split("."); - const _currentModel = model[_paths[1]]; + //先获取到能获取到的对象 + const paths = exp.split("."); + const currentModel = model[paths[1]]; - if (!_currentModel.__ob__) { + if (!currentModel.__ob__) { return; } - exp = `${_paths[1]}.**`; - // 补全路径 - let _parent = _currentModel.__ob__.parent, - _root = _currentModel.__ob__; - while (_parent) { - exp = `*.${exp}`; - _root = _parent; - _parent = _parent.parent; + exp = paths[1] + ".**"; + //补全路径 + let parent = currentModel.__ob__.parent, root = currentModel.__ob__; + while (parent) { + exp = "*." + exp; + root = parent; + parent = parent.parent; } - const _regStr = routeToRegExp(exp); - const _dep2 = new Dep(); - _root._globalDeps || (_root._globalDeps = {}); - if (_.isArray(_root._globalDeps[_regStr])) { - _root._globalDeps[_regStr].push(_dep2); + const regStr = routeToRegExp(exp); + const dep = new Dep(); + root._globalDeps || (root._globalDeps = {}); + if (_.isArray(root._globalDeps[regStr])) { + root._globalDeps[regStr].push(dep); } else { - _root._globalDeps[_regStr] = [_dep2]; + root._globalDeps[regStr] = [dep]; } - const _w2 = new Watcher( - _currentModel, - () => { - _dep2.depend(); - - return NaN; - }, - (newValue, oldValue, attrs) => { - callback( - i, - newValue, - oldValue, - _.extend({ index: i }, attrs) - ); - }, - options - ); - watchers.push(() => { - if (_root._globalDeps) { - remove(_root._globalDeps[_regStr], _dep2); - - if (_root._globalDeps[_regStr].length === 0) { - delete _root._globalDeps[_regStr]; - _w2.teardown(); + const w = new Watcher(currentModel, function () { + dep.depend(); + return NaN; + }, function (newValue, oldValue, attrs) { + callback(i, newValue, oldValue, _.extend({ index: i }, attrs)); + }, options); + watchers.push(function unwatchFn() { + if (root._globalDeps) { + remove(root._globalDeps[regStr], dep); + + if (root._globalDeps[regStr].length === 0) { + delete root._globalDeps[regStr]; + w.teardown(); } } }); - return; } // 再有结尾有*的就不支持了 if (exp[exp.length - 1] === "*") { throw new Error("not support"); } - // 其他含有*的情况,如*.a,*.*.a,a.*.a + //其他含有*的情况,如*.a,*.*.a,a.*.a let currentModel = model; - // 先获取到能获取到的对象 + //先获取到能获取到的对象 const paths = exp.split("."); - for (let _i = 0, len = paths.length; _i < len; _i++) { - if (paths[_i] === "*") { + for (let i = 0, len = paths.length; i < len; i++) { + if (paths[i] === "*") { break; } - currentModel = model[paths[_i]]; + currentModel = model[paths[i]]; } - + exp = exp.substr(exp.indexOf("*")); if (!currentModel.__ob__) { return; } - - exp = exp.substr(exp.indexOf("*")); - // 补全路径 - let parent = currentModel.__ob__.parent, - root = currentModel.__ob__; + //补全路径 + let parent = currentModel.__ob__.parent, root = currentModel.__ob__; while (parent) { - exp = `*.${exp}`; + exp = "*." + exp; root = parent; parent = parent.parent; } @@ -1111,24 +1028,13 @@ function watch(model, expOrFn, cb, options) { root._globalDeps[regStr] = [dep]; } - const w = new Watcher( - currentModel, - () => { - dep.depend(); - - return NaN; - }, - (newValue, oldValue, attrs) => { - callback( - i, - newValue, - oldValue, - _.extend({ index: i }, attrs) - ); - }, - options - ); - watchers.push(() => { + const w = new Watcher(currentModel, function () { + dep.depend(); + return NaN; + }, function (newValue, oldValue, attrs) { + callback(i, newValue, oldValue, _.extend({ index: i }, attrs)); + }, options); + watchers.push(function unwatchFn() { if (root._globalDeps) { remove(root._globalDeps[regStr], dep); if (root._globalDeps[regStr].length === 0) { @@ -1137,27 +1043,21 @@ function watch(model, expOrFn, cb, options) { } } }); - return; } - const watcher = new Watcher( - model, - exp, - (newValue, oldValue, attrs) => { - callback(i, newValue, oldValue, _.extend({ index: i }, attrs)); - }, - options - ); - watchers.push(() => { + const watcher = new Watcher(model, exp, function (newValue, oldValue, attrs) { + callback(i, newValue, oldValue, _.extend({ index: i }, attrs)); + }, options); + watchers.push(function unwatchFn() { watcher.teardown(); }); }); - return watchers; } const mixinInjection = {}; + function getMixins(type) { return mixinInjection[type]; } @@ -1185,9 +1085,17 @@ function initComputed(vm, computed) { function defineComputedWatcher(vm, userDef) { const context = vm.$$model ? vm.model : vm; - const getter = typeof userDef === "function" ? userDef : userDef.get; - - return new Watcher(context, getter || noop, noop, computedWatcherOptions); + const getter = + typeof userDef === "function" + ? userDef + : userDef.get; + + return new Watcher( + context, + getter || noop, + noop, + computedWatcherOptions + ); } function defineOneComputedGetter(vm, key, userDef) { @@ -1209,7 +1117,6 @@ function defineOneComputedGetter(vm, key, userDef) { : noop; sharedPropertyDefinition.set = userDef.set ? userDef.set : noop; } - return sharedPropertyDefinition; } @@ -1233,7 +1140,6 @@ function createComputedGetter(vm, key) { if (REACTIVE && Dep.target) { watcher.depend(); } - return watcher.value; } }; @@ -1241,7 +1147,7 @@ function createComputedGetter(vm, key) { function initWatch(vm, watch$$1) { vm._watchers || (vm._watchers = []); - for (const key in watch$$1) { + for (let key in watch$$1) { const handler = watch$$1[key]; if (_.isArray(handler)) { for (let i = 0; i < handler.length; i++) { @@ -1261,7 +1167,6 @@ function createWatcher(vm, keyOrFn, cb, options) { if (typeof cb === "string") { cb = vm[cb]; } - return watch( vm.model, keyOrFn, @@ -1282,7 +1187,7 @@ function initMethods(vm, methods) { function initMixins(vm, mixins) { mixins = (mixins || []).slice(0); - _.each(mixins.reverse(), (mixinType) => { + _.each(mixins.reverse(), function (mixinType) { const mixin$$1 = getMixins(mixinType); for (const key in mixin$$1) { @@ -1327,14 +1232,13 @@ function defineProps(vm, keys) { // } // }) // } - - const _loop = function _loop(i, len) { + for (let i = 0, len = keys.length; i < len; i++) { const key = keys[i]; if (!(key in $$skipArray)) { props[key] = { enumerable: true, configurable: true, - get: function get() { + get: function () { if (vm.$$computed && key in vm.$$computed) { return vm.$$computed[key]; } @@ -1352,7 +1256,7 @@ function defineProps(vm, keys) { p = p._parent; } }, - set: function set(val) { + set: function (val) { if (vm.$$state && key in vm.$$state) { return (vm.$$state[key] = val); } @@ -1369,35 +1273,26 @@ function defineProps(vm, keys) { } }; } - }; - - for (let i = 0, len = keys.length; i < len; i++) { - _loop(i, len); } vm.model = createViewModel$1({}, props); } function defineContext(vm, keys) { const props = {}; - - const _loop2 = function _loop2(i, len) { + for (let i = 0, len = keys.length; i < len; i++) { const key = keys[i]; if (!(key in $$skipArray)) { props[key] = { enumerable: true, configurable: true, - get: function get() { + get: function () { return vm.model[key]; }, - set: function set(val) { + set: function (val) { return (vm.model[key] = val); } }; } - }; - - for (let i = 0, len = keys.length; i < len; i++) { - _loop2(i, len); } vm.$$context = createViewModel$1({}, props); } @@ -1415,22 +1310,14 @@ function getInjectValue(vm, key) { function getInjectValues(vm) { const inject = vm.inject || []; const result = {}; - _.each(inject, (key) => { + _.each(inject, function (key) { result[key] = getInjectValue(vm, key); }); - return result; } -const Model = (function() { - function Model() { - _classCallCheck(this, Model); - } - - Model.prototype._constructor = function _constructor( - model, - destroyHandler - ) { +class Model { + _constructor(model, destroyHandler) { if (model instanceof Observer || model instanceof Model) { model = model.model; } @@ -1470,22 +1357,23 @@ const Model = (function() { if (this.$$model) { return this.model; } - }; + } - Model.prototype._init = function _init() { - }; + _init() { - Model.prototype.init = function init() { + } + + init() { this._init(); - }; + } - Model.prototype.destroy = function destroy() { - for (const _key3 in this._computedWatchers) { - this._computedWatchers[_key3].teardown(); + destroy() { + for (const key in this._computedWatchers) { + this._computedWatchers[key].teardown(); } - _.each(this._watchers, (unwatches) => { + _.each(this._watchers, function (unwatches) { unwatches = _.isArray(unwatches) ? unwatches : [unwatches]; - _.each(unwatches, (unwatch) => { + _.each(unwatches, function (unwatch) { unwatch(); }); }); @@ -1495,10 +1383,8 @@ const Model = (function() { this.$$computed = null; this.$$state = null; this._destroyHandler && this._destroyHandler(); - }; - - return Model; -})(); + } +} function define(model) { return REACTIVE ? new Observer(model).model : model; @@ -1514,7 +1400,7 @@ function config(options) { } function toJSON(model) { - let result = void 0; + let result; if (_.isArray(model)) { result = []; for (let i = 0, len = model.length; i < len; i++) { @@ -1522,15 +1408,14 @@ function toJSON(model) { } } else if (model && isPlainObject(model)) { result = {}; - for (const _key4 in model) { - if (!_.has($$skipArray, _key4)) { - result[_key4] = toJSON(model[_key4]); + for (let key in model) { + if (!_.has($$skipArray, key)) { + result[key] = toJSON(model[key]); } } } else { result = model; } - return result; } @@ -1554,6 +1439,7 @@ export const Fix = { freeze, del, Watcher, + cleanupDeps, pushTarget, popTarget, watch,