Scheduler.js 20 KB

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