This is an automated email from the ASF dual-hosted git repository. ovilia pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git
commit 4da347c9e820f2007ffcd6bd1904e1108f7b07d0 Author: Ovilia <zwl.s...@gmail.com> AuthorDate: Fri Aug 2 11:18:04 2019 +0800 fix: dispose checking for chart instance --- src/echarts.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/src/echarts.js b/src/echarts.js index acc3496..fc9621c 100644 --- a/src/echarts.js +++ b/src/echarts.js @@ -98,7 +98,12 @@ var OPTION_UPDATED = '__optionUpdated'; var ACTION_REG = /^[a-zA-Z0-9_]+$/; -function createRegisterEventWithLowercaseName(method) { +function createRegisterEventWithLowercaseName(method, ignoreDisposed) { + if (!ignoreDisposed && this._disposed) { + disposedWarning(this.id); + return; + } + return function (eventName, handler, context) { // Event name is all lowercase eventName = eventName && eventName.toLowerCase(); @@ -112,9 +117,9 @@ function createRegisterEventWithLowercaseName(method) { function MessageCenter() { Eventful.call(this); } -MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on'); -MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off'); -MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one'); +MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on', true); +MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off', true); +MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one', true); zrUtil.mixin(MessageCenter, Eventful); /** @@ -350,6 +355,10 @@ echartsProto.setOption = function (option, notMerge, lazyUpdate) { if (__DEV__) { assert(!this[IN_MAIN_PROCESS], '`setOption` should not be called during main process.'); } + if (this._disposed) { + disposedWarning(this.id); + return; + } var silent; if (isObject(notMerge)) { @@ -485,6 +494,11 @@ echartsProto.getSvgDataUrl = function () { * @param {string} [opts.excludeComponents] */ echartsProto.getDataURL = function (opts) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + opts = opts || {}; var excludeComponents = opts.excludeComponents; var ecModel = this._model; @@ -525,6 +539,11 @@ echartsProto.getDataURL = function (opts) { * @param {string} [opts.backgroundColor] */ echartsProto.getConnectedDataURL = function (opts) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + if (!env.canvasSupported) { return; } @@ -644,6 +663,11 @@ echartsProto.convertToPixel = zrUtil.curry(doConvertPixel, 'convertToPixel'); echartsProto.convertFromPixel = zrUtil.curry(doConvertPixel, 'convertFromPixel'); function doConvertPixel(methodName, finder, value) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + var ecModel = this._model; var coordSysList = this._coordSysMgr.getCoordinateSystems(); var result; @@ -684,6 +708,11 @@ function doConvertPixel(methodName, finder, value) { * @return {boolean} result */ echartsProto.containPixel = function (finder, value) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + var ecModel = this._model; var result; @@ -1056,6 +1085,10 @@ echartsProto.resize = function (opts) { if (__DEV__) { assert(!this[IN_MAIN_PROCESS], '`resize` should not be called during main process.'); } + if (this._disposed) { + disposedWarning(this.id); + return; + } this._zr.resize(opts); @@ -1098,6 +1131,11 @@ function updateStreamModes(ecIns, ecModel) { * @param {Object} [cfg] */ echartsProto.showLoading = function (name, cfg) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + if (isObject(name)) { cfg = name; name = ''; @@ -1122,6 +1160,11 @@ echartsProto.showLoading = function (name, cfg) { * Hide loading effect */ echartsProto.hideLoading = function () { + if (this._disposed) { + disposedWarning(this.id); + return; + } + this._loadingFX && this._zr.remove(this._loadingFX); this._loadingFX = null; }; @@ -1149,6 +1192,11 @@ echartsProto.makeActionFromEvent = function (eventObj) { * undefined: Auto decide whether perform flush. */ echartsProto.dispatchAction = function (payload, opt) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + if (!isObject(opt)) { opt = {silent: !!opt}; } @@ -1317,6 +1365,11 @@ function bindRenderedEvent(zr, ecIns) { * @param {Array|TypedArray} params.data */ echartsProto.appendData = function (params) { + if (this._disposed) { + disposedWarning(this.id); + return; + } + var seriesIndex = params.seriesIndex; var ecModel = this.getModel(); var seriesModel = ecModel.getSeriesByIndex(seriesIndex); @@ -1342,9 +1395,9 @@ echartsProto.appendData = function (params) { * Register event * @method */ -echartsProto.on = createRegisterEventWithLowercaseName('on'); -echartsProto.off = createRegisterEventWithLowercaseName('off'); -echartsProto.one = createRegisterEventWithLowercaseName('one'); +echartsProto.on = createRegisterEventWithLowercaseName('on', false); +echartsProto.off = createRegisterEventWithLowercaseName('off', false); +echartsProto.one = createRegisterEventWithLowercaseName('one', false); /** * Prepare view instances of charts and components @@ -1622,6 +1675,10 @@ echartsProto.isDisposed = function () { * Clear */ echartsProto.clear = function () { + if (this._disposed) { + disposedWarning(this.id); + return; + } this.setOption({ series: [] }, true); }; @@ -1630,9 +1687,7 @@ echartsProto.clear = function () { */ echartsProto.dispose = function () { if (this._disposed) { - if (__DEV__) { - console.warn('Instance ' + this.id + ' has been disposed'); - } + disposedWarning(this.id); return; } this._disposed = true; @@ -1657,6 +1712,12 @@ echartsProto.dispose = function () { zrUtil.mixin(ECharts, Eventful); +function disposedWarning(id) { + if (__DEV__) { + console.warn('Instance ' + id + ' has been disposed'); + } +} + function updateHoverLayerStatus(ecIns, ecModel) { var zr = ecIns._zr; var storage = zr.storage; @@ -2006,9 +2067,9 @@ export function init(dom, theme, opts) { ) ) { console.warn('Can\'t get DOM width or height. Please check ' - + 'dom.clientWidth and dom.clientHeight. They should not be 0.' - + 'For example, you may need to call this in the callback ' - + 'of window.onload.'); + + 'dom.clientWidth and dom.clientHeight. They should not be 0.' + + 'For example, you may need to call this in the callback ' + + 'of window.onload.'); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org For additional commands, e-mail: commits-h...@echarts.apache.org