Scheduler.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { each, map, isFunction, createHashMap, noop, assert } from 'zrender/lib/core/util.js';
  41. import { createTask } from './task.js';
  42. import { getUID } from '../util/component.js';
  43. import GlobalModel from '../model/Global.js';
  44. import ExtensionAPI from './ExtensionAPI.js';
  45. import { normalizeToArray } from '../util/model.js';
  46. ;
  47. var Scheduler =
  48. /** @class */
  49. function () {
  50. function Scheduler(ecInstance, api, dataProcessorHandlers, visualHandlers) {
  51. // key: handlerUID
  52. this._stageTaskMap = createHashMap();
  53. this.ecInstance = ecInstance;
  54. this.api = api; // Fix current processors in case that in some rear cases that
  55. // processors might be registered after echarts instance created.
  56. // Register processors incrementally for a echarts instance is
  57. // not supported by this stream architecture.
  58. dataProcessorHandlers = this._dataProcessorHandlers = dataProcessorHandlers.slice();
  59. visualHandlers = this._visualHandlers = visualHandlers.slice();
  60. this._allHandlers = dataProcessorHandlers.concat(visualHandlers);
  61. }
  62. Scheduler.prototype.restoreData = function (ecModel, payload) {
  63. // TODO: Only restore needed series and components, but not all components.
  64. // Currently `restoreData` of all of the series and component will be called.
  65. // But some independent components like `title`, `legend`, `graphic`, `toolbox`,
  66. // `tooltip`, `axisPointer`, etc, do not need series refresh when `setOption`,
  67. // and some components like coordinate system, axes, dataZoom, visualMap only
  68. // need their target series refresh.
  69. // (1) If we are implementing this feature some day, we should consider these cases:
  70. // if a data processor depends on a component (e.g., dataZoomProcessor depends
  71. // on the settings of `dataZoom`), it should be re-performed if the component
  72. // is modified by `setOption`.
  73. // (2) If a processor depends on sevral series, speicified by its `getTargetSeries`,
  74. // it should be re-performed when the result array of `getTargetSeries` changed.
  75. // We use `dependencies` to cover these issues.
  76. // (3) How to update target series when coordinate system related components modified.
  77. // TODO: simply the dirty mechanism? Check whether only the case here can set tasks dirty,
  78. // and this case all of the tasks will be set as dirty.
  79. ecModel.restoreData(payload); // Theoretically an overall task not only depends on each of its target series, but also
  80. // depends on all of the series.
  81. // The overall task is not in pipeline, and `ecModel.restoreData` only set pipeline tasks
  82. // dirty. If `getTargetSeries` of an overall task returns nothing, we should also ensure
  83. // that the overall task is set as dirty and to be performed, otherwise it probably cause
  84. // state chaos. So we have to set dirty of all of the overall tasks manually, otherwise it
  85. // probably cause state chaos (consider `dataZoomProcessor`).
  86. this._stageTaskMap.each(function (taskRecord) {
  87. var overallTask = taskRecord.overallTask;
  88. overallTask && overallTask.dirty();
  89. });
  90. }; // If seriesModel provided, incremental threshold is check by series data.
  91. Scheduler.prototype.getPerformArgs = function (task, isBlock) {
  92. // For overall task
  93. if (!task.__pipeline) {
  94. return;
  95. }
  96. var pipeline = this._pipelineMap.get(task.__pipeline.id);
  97. var pCtx = pipeline.context;
  98. var incremental = !isBlock && pipeline.progressiveEnabled && (!pCtx || pCtx.progressiveRender) && task.__idxInPipeline > pipeline.blockIndex;
  99. var step = incremental ? pipeline.step : null;
  100. var modDataCount = pCtx && pCtx.modDataCount;
  101. var modBy = modDataCount != null ? Math.ceil(modDataCount / step) : null;
  102. return {
  103. step: step,
  104. modBy: modBy,
  105. modDataCount: modDataCount
  106. };
  107. };
  108. Scheduler.prototype.getPipeline = function (pipelineId) {
  109. return this._pipelineMap.get(pipelineId);
  110. };
  111. /**
  112. * Current, progressive rendering starts from visual and layout.
  113. * Always detect render mode in the same stage, avoiding that incorrect
  114. * detection caused by data filtering.
  115. * Caution:
  116. * `updateStreamModes` use `seriesModel.getData()`.
  117. */
  118. Scheduler.prototype.updateStreamModes = function (seriesModel, view) {
  119. var pipeline = this._pipelineMap.get(seriesModel.uid);
  120. var data = seriesModel.getData();
  121. var dataLen = data.count(); // `progressiveRender` means that can render progressively in each
  122. // animation frame. Note that some types of series do not provide
  123. // `view.incrementalPrepareRender` but support `chart.appendData`. We
  124. // use the term `incremental` but not `progressive` to describe the
  125. // case that `chart.appendData`.
  126. var progressiveRender = pipeline.progressiveEnabled && view.incrementalPrepareRender && dataLen >= pipeline.threshold;
  127. var large = seriesModel.get('large') && dataLen >= seriesModel.get('largeThreshold'); // TODO: modDataCount should not updated if `appendData`, otherwise cause whole repaint.
  128. // see `test/candlestick-large3.html`
  129. var modDataCount = seriesModel.get('progressiveChunkMode') === 'mod' ? dataLen : null;
  130. seriesModel.pipelineContext = pipeline.context = {
  131. progressiveRender: progressiveRender,
  132. modDataCount: modDataCount,
  133. large: large
  134. };
  135. };
  136. Scheduler.prototype.restorePipelines = function (ecModel) {
  137. var scheduler = this;
  138. var pipelineMap = scheduler._pipelineMap = createHashMap();
  139. ecModel.eachSeries(function (seriesModel) {
  140. var progressive = seriesModel.getProgressive();
  141. var pipelineId = seriesModel.uid;
  142. pipelineMap.set(pipelineId, {
  143. id: pipelineId,
  144. head: null,
  145. tail: null,
  146. threshold: seriesModel.getProgressiveThreshold(),
  147. progressiveEnabled: progressive && !(seriesModel.preventIncremental && seriesModel.preventIncremental()),
  148. blockIndex: -1,
  149. step: Math.round(progressive || 700),
  150. count: 0
  151. });
  152. scheduler._pipe(seriesModel, seriesModel.dataTask);
  153. });
  154. };
  155. Scheduler.prototype.prepareStageTasks = function () {
  156. var stageTaskMap = this._stageTaskMap;
  157. var ecModel = this.api.getModel();
  158. var api = this.api;
  159. each(this._allHandlers, function (handler) {
  160. var record = stageTaskMap.get(handler.uid) || stageTaskMap.set(handler.uid, {});
  161. var errMsg = '';
  162. if (process.env.NODE_ENV !== 'production') {
  163. // Currently do not need to support to sepecify them both.
  164. errMsg = '"reset" and "overallReset" must not be both specified.';
  165. }
  166. assert(!(handler.reset && handler.overallReset), errMsg);
  167. handler.reset && this._createSeriesStageTask(handler, record, ecModel, api);
  168. handler.overallReset && this._createOverallStageTask(handler, record, ecModel, api);
  169. }, this);
  170. };
  171. Scheduler.prototype.prepareView = function (view, model, ecModel, api) {
  172. var renderTask = view.renderTask;
  173. var context = renderTask.context;
  174. context.model = model;
  175. context.ecModel = ecModel;
  176. context.api = api;
  177. renderTask.__block = !view.incrementalPrepareRender;
  178. this._pipe(model, renderTask);
  179. };
  180. Scheduler.prototype.performDataProcessorTasks = function (ecModel, payload) {
  181. // If we do not use `block` here, it should be considered when to update modes.
  182. this._performStageTasks(this._dataProcessorHandlers, ecModel, payload, {
  183. block: true
  184. });
  185. };
  186. Scheduler.prototype.performVisualTasks = function (ecModel, payload, opt) {
  187. this._performStageTasks(this._visualHandlers, ecModel, payload, opt);
  188. };
  189. Scheduler.prototype._performStageTasks = function (stageHandlers, ecModel, payload, opt) {
  190. opt = opt || {};
  191. var unfinished = false;
  192. var scheduler = this;
  193. each(stageHandlers, function (stageHandler, idx) {
  194. if (opt.visualType && opt.visualType !== stageHandler.visualType) {
  195. return;
  196. }
  197. var stageHandlerRecord = scheduler._stageTaskMap.get(stageHandler.uid);
  198. var seriesTaskMap = stageHandlerRecord.seriesTaskMap;
  199. var overallTask = stageHandlerRecord.overallTask;
  200. if (overallTask) {
  201. var overallNeedDirty_1;
  202. var agentStubMap = overallTask.agentStubMap;
  203. agentStubMap.each(function (stub) {
  204. if (needSetDirty(opt, stub)) {
  205. stub.dirty();
  206. overallNeedDirty_1 = true;
  207. }
  208. });
  209. overallNeedDirty_1 && overallTask.dirty();
  210. scheduler.updatePayload(overallTask, payload);
  211. var performArgs_1 = scheduler.getPerformArgs(overallTask, opt.block); // Execute stubs firstly, which may set the overall task dirty,
  212. // then execute the overall task. And stub will call seriesModel.setData,
  213. // which ensures that in the overallTask seriesModel.getData() will not
  214. // return incorrect data.
  215. agentStubMap.each(function (stub) {
  216. stub.perform(performArgs_1);
  217. });
  218. if (overallTask.perform(performArgs_1)) {
  219. unfinished = true;
  220. }
  221. } else if (seriesTaskMap) {
  222. seriesTaskMap.each(function (task, pipelineId) {
  223. if (needSetDirty(opt, task)) {
  224. task.dirty();
  225. }
  226. var performArgs = scheduler.getPerformArgs(task, opt.block); // FIXME
  227. // if intending to declare `performRawSeries` in handlers, only
  228. // stream-independent (specifically, data item independent) operations can be
  229. // performed. Because if a series is filtered, most of the tasks will not
  230. // be performed. A stream-dependent operation probably cause wrong biz logic.
  231. // Perhaps we should not provide a separate callback for this case instead
  232. // of providing the config `performRawSeries`. The stream-dependent operations
  233. // and stream-independent operations should better not be mixed.
  234. performArgs.skip = !stageHandler.performRawSeries && ecModel.isSeriesFiltered(task.context.model);
  235. scheduler.updatePayload(task, payload);
  236. if (task.perform(performArgs)) {
  237. unfinished = true;
  238. }
  239. });
  240. }
  241. });
  242. function needSetDirty(opt, task) {
  243. return opt.setDirty && (!opt.dirtyMap || opt.dirtyMap.get(task.__pipeline.id));
  244. }
  245. this.unfinished = unfinished || this.unfinished;
  246. };
  247. Scheduler.prototype.performSeriesTasks = function (ecModel) {
  248. var unfinished;
  249. ecModel.eachSeries(function (seriesModel) {
  250. // Progress to the end for dataInit and dataRestore.
  251. unfinished = seriesModel.dataTask.perform() || unfinished;
  252. });
  253. this.unfinished = unfinished || this.unfinished;
  254. };
  255. Scheduler.prototype.plan = function () {
  256. // Travel pipelines, check block.
  257. this._pipelineMap.each(function (pipeline) {
  258. var task = pipeline.tail;
  259. do {
  260. if (task.__block) {
  261. pipeline.blockIndex = task.__idxInPipeline;
  262. break;
  263. }
  264. task = task.getUpstream();
  265. } while (task);
  266. });
  267. };
  268. Scheduler.prototype.updatePayload = function (task, payload) {
  269. payload !== 'remain' && (task.context.payload = payload);
  270. };
  271. Scheduler.prototype._createSeriesStageTask = function (stageHandler, stageHandlerRecord, ecModel, api) {
  272. var scheduler = this;
  273. var oldSeriesTaskMap = stageHandlerRecord.seriesTaskMap; // The count of stages are totally about only several dozen, so
  274. // do not need to reuse the map.
  275. var newSeriesTaskMap = stageHandlerRecord.seriesTaskMap = createHashMap();
  276. var seriesType = stageHandler.seriesType;
  277. var getTargetSeries = stageHandler.getTargetSeries; // If a stageHandler should cover all series, `createOnAllSeries` should be declared mandatorily,
  278. // to avoid some typo or abuse. Otherwise if an extension do not specify a `seriesType`,
  279. // it works but it may cause other irrelevant charts blocked.
  280. if (stageHandler.createOnAllSeries) {
  281. ecModel.eachRawSeries(create);
  282. } else if (seriesType) {
  283. ecModel.eachRawSeriesByType(seriesType, create);
  284. } else if (getTargetSeries) {
  285. getTargetSeries(ecModel, api).each(create);
  286. }
  287. function create(seriesModel) {
  288. var pipelineId = seriesModel.uid; // Init tasks for each seriesModel only once.
  289. // Reuse original task instance.
  290. var task = newSeriesTaskMap.set(pipelineId, oldSeriesTaskMap && oldSeriesTaskMap.get(pipelineId) || createTask({
  291. plan: seriesTaskPlan,
  292. reset: seriesTaskReset,
  293. count: seriesTaskCount
  294. }));
  295. task.context = {
  296. model: seriesModel,
  297. ecModel: ecModel,
  298. api: api,
  299. // PENDING: `useClearVisual` not used?
  300. useClearVisual: stageHandler.isVisual && !stageHandler.isLayout,
  301. plan: stageHandler.plan,
  302. reset: stageHandler.reset,
  303. scheduler: scheduler
  304. };
  305. scheduler._pipe(seriesModel, task);
  306. }
  307. };
  308. Scheduler.prototype._createOverallStageTask = function (stageHandler, stageHandlerRecord, ecModel, api) {
  309. var scheduler = this;
  310. var overallTask = stageHandlerRecord.overallTask = stageHandlerRecord.overallTask // For overall task, the function only be called on reset stage.
  311. || createTask({
  312. reset: overallTaskReset
  313. });
  314. overallTask.context = {
  315. ecModel: ecModel,
  316. api: api,
  317. overallReset: stageHandler.overallReset,
  318. scheduler: scheduler
  319. };
  320. var oldAgentStubMap = overallTask.agentStubMap; // The count of stages are totally about only several dozen, so
  321. // do not need to reuse the map.
  322. var newAgentStubMap = overallTask.agentStubMap = createHashMap();
  323. var seriesType = stageHandler.seriesType;
  324. var getTargetSeries = stageHandler.getTargetSeries;
  325. var overallProgress = true;
  326. var shouldOverallTaskDirty = false; // FIXME:TS never used, so comment it
  327. // let modifyOutputEnd = stageHandler.modifyOutputEnd;
  328. // An overall task with seriesType detected or has `getTargetSeries`, we add
  329. // stub in each pipelines, it will set the overall task dirty when the pipeline
  330. // progress. Moreover, to avoid call the overall task each frame (too frequent),
  331. // we set the pipeline block.
  332. var errMsg = '';
  333. if (process.env.NODE_ENV !== 'production') {
  334. errMsg = '"createOnAllSeries" is not supported for "overallReset", ' + 'because it will block all streams.';
  335. }
  336. assert(!stageHandler.createOnAllSeries, errMsg);
  337. if (seriesType) {
  338. ecModel.eachRawSeriesByType(seriesType, createStub);
  339. } else if (getTargetSeries) {
  340. getTargetSeries(ecModel, api).each(createStub);
  341. } // Otherwise, (usually it is legacy case), the overall task will only be
  342. // executed when upstream is dirty. Otherwise the progressive rendering of all
  343. // pipelines will be disabled unexpectedly. But it still needs stubs to receive
  344. // dirty info from upstream.
  345. else {
  346. overallProgress = false;
  347. each(ecModel.getSeries(), createStub);
  348. }
  349. function createStub(seriesModel) {
  350. var pipelineId = seriesModel.uid;
  351. var stub = newAgentStubMap.set(pipelineId, oldAgentStubMap && oldAgentStubMap.get(pipelineId) || ( // When the result of `getTargetSeries` changed, the overallTask
  352. // should be set as dirty and re-performed.
  353. shouldOverallTaskDirty = true, createTask({
  354. reset: stubReset,
  355. onDirty: stubOnDirty
  356. })));
  357. stub.context = {
  358. model: seriesModel,
  359. overallProgress: overallProgress // FIXME:TS never used, so comment it
  360. // modifyOutputEnd: modifyOutputEnd
  361. };
  362. stub.agent = overallTask;
  363. stub.__block = overallProgress;
  364. scheduler._pipe(seriesModel, stub);
  365. }
  366. if (shouldOverallTaskDirty) {
  367. overallTask.dirty();
  368. }
  369. };
  370. Scheduler.prototype._pipe = function (seriesModel, task) {
  371. var pipelineId = seriesModel.uid;
  372. var pipeline = this._pipelineMap.get(pipelineId);
  373. !pipeline.head && (pipeline.head = task);
  374. pipeline.tail && pipeline.tail.pipe(task);
  375. pipeline.tail = task;
  376. task.__idxInPipeline = pipeline.count++;
  377. task.__pipeline = pipeline;
  378. };
  379. Scheduler.wrapStageHandler = function (stageHandler, visualType) {
  380. if (isFunction(stageHandler)) {
  381. stageHandler = {
  382. overallReset: stageHandler,
  383. seriesType: detectSeriseType(stageHandler)
  384. };
  385. }
  386. stageHandler.uid = getUID('stageHandler');
  387. visualType && (stageHandler.visualType = visualType);
  388. return stageHandler;
  389. };
  390. ;
  391. return Scheduler;
  392. }();
  393. function overallTaskReset(context) {
  394. context.overallReset(context.ecModel, context.api, context.payload);
  395. }
  396. function stubReset(context) {
  397. return context.overallProgress && stubProgress;
  398. }
  399. function stubProgress() {
  400. this.agent.dirty();
  401. this.getDownstream().dirty();
  402. }
  403. function stubOnDirty() {
  404. this.agent && this.agent.dirty();
  405. }
  406. function seriesTaskPlan(context) {
  407. return context.plan ? context.plan(context.model, context.ecModel, context.api, context.payload) : null;
  408. }
  409. function seriesTaskReset(context) {
  410. if (context.useClearVisual) {
  411. context.data.clearAllVisual();
  412. }
  413. var resetDefines = context.resetDefines = normalizeToArray(context.reset(context.model, context.ecModel, context.api, context.payload));
  414. return resetDefines.length > 1 ? map(resetDefines, function (v, idx) {
  415. return makeSeriesTaskProgress(idx);
  416. }) : singleSeriesTaskProgress;
  417. }
  418. var singleSeriesTaskProgress = makeSeriesTaskProgress(0);
  419. function makeSeriesTaskProgress(resetDefineIdx) {
  420. return function (params, context) {
  421. var data = context.data;
  422. var resetDefine = context.resetDefines[resetDefineIdx];
  423. if (resetDefine && resetDefine.dataEach) {
  424. for (var i = params.start; i < params.end; i++) {
  425. resetDefine.dataEach(data, i);
  426. }
  427. } else if (resetDefine && resetDefine.progress) {
  428. resetDefine.progress(params, data);
  429. }
  430. };
  431. }
  432. function seriesTaskCount(context) {
  433. return context.data.count();
  434. }
  435. /**
  436. * Only some legacy stage handlers (usually in echarts extensions) are pure function.
  437. * To ensure that they can work normally, they should work in block mode, that is,
  438. * they should not be started util the previous tasks finished. So they cause the
  439. * progressive rendering disabled. We try to detect the series type, to narrow down
  440. * the block range to only the series type they concern, but not all series.
  441. */
  442. function detectSeriseType(legacyFunc) {
  443. seriesType = null;
  444. try {
  445. // Assume there is no async when calling `eachSeriesByType`.
  446. legacyFunc(ecModelMock, apiMock);
  447. } catch (e) {}
  448. return seriesType;
  449. }
  450. var ecModelMock = {};
  451. var apiMock = {};
  452. var seriesType;
  453. mockMethods(ecModelMock, GlobalModel);
  454. mockMethods(apiMock, ExtensionAPI);
  455. ecModelMock.eachSeriesByType = ecModelMock.eachRawSeriesByType = function (type) {
  456. seriesType = type;
  457. };
  458. ecModelMock.eachComponent = function (cond) {
  459. if (cond.mainType === 'series' && cond.subType) {
  460. seriesType = cond.subType;
  461. }
  462. };
  463. function mockMethods(target, Clz) {
  464. /* eslint-disable */
  465. for (var name_1 in Clz.prototype) {
  466. // Do not use hasOwnProperty
  467. target[name_1] = noop;
  468. }
  469. /* eslint-enable */
  470. }
  471. export default Scheduler;