task.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 { assert, isArray } from 'zrender/lib/core/util.js';
  41. ;
  42. /**
  43. * @param {Object} define
  44. * @return See the return of `createTask`.
  45. */
  46. export function createTask(define) {
  47. return new Task(define);
  48. }
  49. var Task =
  50. /** @class */
  51. function () {
  52. function Task(define) {
  53. define = define || {};
  54. this._reset = define.reset;
  55. this._plan = define.plan;
  56. this._count = define.count;
  57. this._onDirty = define.onDirty;
  58. this._dirty = true;
  59. }
  60. /**
  61. * @param step Specified step.
  62. * @param skip Skip customer perform call.
  63. * @param modBy Sampling window size.
  64. * @param modDataCount Sampling count.
  65. * @return whether unfinished.
  66. */
  67. Task.prototype.perform = function (performArgs) {
  68. var upTask = this._upstream;
  69. var skip = performArgs && performArgs.skip; // TODO some refactor.
  70. // Pull data. Must pull data each time, because context.data
  71. // may be updated by Series.setData.
  72. if (this._dirty && upTask) {
  73. var context = this.context;
  74. context.data = context.outputData = upTask.context.outputData;
  75. }
  76. if (this.__pipeline) {
  77. this.__pipeline.currentTask = this;
  78. }
  79. var planResult;
  80. if (this._plan && !skip) {
  81. planResult = this._plan(this.context);
  82. } // Support sharding by mod, which changes the render sequence and makes the rendered graphic
  83. // elements uniformed distributed when progress, especially when moving or zooming.
  84. var lastModBy = normalizeModBy(this._modBy);
  85. var lastModDataCount = this._modDataCount || 0;
  86. var modBy = normalizeModBy(performArgs && performArgs.modBy);
  87. var modDataCount = performArgs && performArgs.modDataCount || 0;
  88. if (lastModBy !== modBy || lastModDataCount !== modDataCount) {
  89. planResult = 'reset';
  90. }
  91. function normalizeModBy(val) {
  92. !(val >= 1) && (val = 1); // jshint ignore:line
  93. return val;
  94. }
  95. var forceFirstProgress;
  96. if (this._dirty || planResult === 'reset') {
  97. this._dirty = false;
  98. forceFirstProgress = this._doReset(skip);
  99. }
  100. this._modBy = modBy;
  101. this._modDataCount = modDataCount;
  102. var step = performArgs && performArgs.step;
  103. if (upTask) {
  104. if (process.env.NODE_ENV !== 'production') {
  105. assert(upTask._outputDueEnd != null);
  106. }
  107. this._dueEnd = upTask._outputDueEnd;
  108. } // DataTask or overallTask
  109. else {
  110. if (process.env.NODE_ENV !== 'production') {
  111. assert(!this._progress || this._count);
  112. }
  113. this._dueEnd = this._count ? this._count(this.context) : Infinity;
  114. } // Note: Stubs, that its host overall task let it has progress, has progress.
  115. // If no progress, pass index from upstream to downstream each time plan called.
  116. if (this._progress) {
  117. var start = this._dueIndex;
  118. var end = Math.min(step != null ? this._dueIndex + step : Infinity, this._dueEnd);
  119. if (!skip && (forceFirstProgress || start < end)) {
  120. var progress = this._progress;
  121. if (isArray(progress)) {
  122. for (var i = 0; i < progress.length; i++) {
  123. this._doProgress(progress[i], start, end, modBy, modDataCount);
  124. }
  125. } else {
  126. this._doProgress(progress, start, end, modBy, modDataCount);
  127. }
  128. }
  129. this._dueIndex = end; // If no `outputDueEnd`, assume that output data and
  130. // input data is the same, so use `dueIndex` as `outputDueEnd`.
  131. var outputDueEnd = this._settedOutputEnd != null ? this._settedOutputEnd : end;
  132. if (process.env.NODE_ENV !== 'production') {
  133. // ??? Can not rollback.
  134. assert(outputDueEnd >= this._outputDueEnd);
  135. }
  136. this._outputDueEnd = outputDueEnd;
  137. } else {
  138. // (1) Some overall task has no progress.
  139. // (2) Stubs, that its host overall task do not let it has progress, has no progress.
  140. // This should always be performed so it can be passed to downstream.
  141. this._dueIndex = this._outputDueEnd = this._settedOutputEnd != null ? this._settedOutputEnd : this._dueEnd;
  142. }
  143. return this.unfinished();
  144. };
  145. Task.prototype.dirty = function () {
  146. this._dirty = true;
  147. this._onDirty && this._onDirty(this.context);
  148. };
  149. Task.prototype._doProgress = function (progress, start, end, modBy, modDataCount) {
  150. iterator.reset(start, end, modBy, modDataCount);
  151. this._callingProgress = progress;
  152. this._callingProgress({
  153. start: start,
  154. end: end,
  155. count: end - start,
  156. next: iterator.next
  157. }, this.context);
  158. };
  159. Task.prototype._doReset = function (skip) {
  160. this._dueIndex = this._outputDueEnd = this._dueEnd = 0;
  161. this._settedOutputEnd = null;
  162. var progress;
  163. var forceFirstProgress;
  164. if (!skip && this._reset) {
  165. progress = this._reset(this.context);
  166. if (progress && progress.progress) {
  167. forceFirstProgress = progress.forceFirstProgress;
  168. progress = progress.progress;
  169. } // To simplify no progress checking, array must has item.
  170. if (isArray(progress) && !progress.length) {
  171. progress = null;
  172. }
  173. }
  174. this._progress = progress;
  175. this._modBy = this._modDataCount = null;
  176. var downstream = this._downstream;
  177. downstream && downstream.dirty();
  178. return forceFirstProgress;
  179. };
  180. Task.prototype.unfinished = function () {
  181. return this._progress && this._dueIndex < this._dueEnd;
  182. };
  183. /**
  184. * @param downTask The downstream task.
  185. * @return The downstream task.
  186. */
  187. Task.prototype.pipe = function (downTask) {
  188. if (process.env.NODE_ENV !== 'production') {
  189. assert(downTask && !downTask._disposed && downTask !== this);
  190. } // If already downstream, do not dirty downTask.
  191. if (this._downstream !== downTask || this._dirty) {
  192. this._downstream = downTask;
  193. downTask._upstream = this;
  194. downTask.dirty();
  195. }
  196. };
  197. Task.prototype.dispose = function () {
  198. if (this._disposed) {
  199. return;
  200. }
  201. this._upstream && (this._upstream._downstream = null);
  202. this._downstream && (this._downstream._upstream = null);
  203. this._dirty = false;
  204. this._disposed = true;
  205. };
  206. Task.prototype.getUpstream = function () {
  207. return this._upstream;
  208. };
  209. Task.prototype.getDownstream = function () {
  210. return this._downstream;
  211. };
  212. Task.prototype.setOutputEnd = function (end) {
  213. // This only happens in dataTask, dataZoom, map, currently.
  214. // where dataZoom do not set end each time, but only set
  215. // when reset. So we should record the set end, in case
  216. // that the stub of dataZoom perform again and earse the
  217. // set end by upstream.
  218. this._outputDueEnd = this._settedOutputEnd = end;
  219. };
  220. return Task;
  221. }();
  222. export { Task };
  223. var iterator = function () {
  224. var end;
  225. var current;
  226. var modBy;
  227. var modDataCount;
  228. var winCount;
  229. var it = {
  230. reset: function (s, e, sStep, sCount) {
  231. current = s;
  232. end = e;
  233. modBy = sStep;
  234. modDataCount = sCount;
  235. winCount = Math.ceil(modDataCount / modBy);
  236. it.next = modBy > 1 && modDataCount > 0 ? modNext : sequentialNext;
  237. }
  238. };
  239. return it;
  240. function sequentialNext() {
  241. return current < end ? current++ : null;
  242. }
  243. function modNext() {
  244. var dataIndex = current % winCount * modBy + Math.ceil(current / winCount);
  245. var result = current >= end ? null : dataIndex < modDataCount ? dataIndex // If modDataCount is smaller than data.count() (consider `appendData` case),
  246. // Use normal linear rendering mode.
  247. : current;
  248. current++;
  249. return result;
  250. }
  251. }(); // -----------------------------------------------------------------------------
  252. // For stream debug (Should be commented out after used!)
  253. // @usage: printTask(this, 'begin');
  254. // @usage: printTask(this, null, {someExtraProp});
  255. // @usage: Use `__idxInPipeline` as conditional breakpiont.
  256. //
  257. // window.printTask = function (task: any, prefix: string, extra: { [key: string]: unknown }): void {
  258. // window.ecTaskUID == null && (window.ecTaskUID = 0);
  259. // task.uidDebug == null && (task.uidDebug = `task_${window.ecTaskUID++}`);
  260. // task.agent && task.agent.uidDebug == null && (task.agent.uidDebug = `task_${window.ecTaskUID++}`);
  261. // let props = [];
  262. // if (task.__pipeline) {
  263. // let val = `${task.__idxInPipeline}/${task.__pipeline.tail.__idxInPipeline} ${task.agent ? '(stub)' : ''}`;
  264. // props.push({text: '__idxInPipeline/total', value: val});
  265. // } else {
  266. // let stubCount = 0;
  267. // task.agentStubMap.each(() => stubCount++);
  268. // props.push({text: 'idx', value: `overall (stubs: ${stubCount})`});
  269. // }
  270. // props.push({text: 'uid', value: task.uidDebug});
  271. // if (task.__pipeline) {
  272. // props.push({text: 'pipelineId', value: task.__pipeline.id});
  273. // task.agent && props.push(
  274. // {text: 'stubFor', value: task.agent.uidDebug}
  275. // );
  276. // }
  277. // props.push(
  278. // {text: 'dirty', value: task._dirty},
  279. // {text: 'dueIndex', value: task._dueIndex},
  280. // {text: 'dueEnd', value: task._dueEnd},
  281. // {text: 'outputDueEnd', value: task._outputDueEnd}
  282. // );
  283. // if (extra) {
  284. // Object.keys(extra).forEach(key => {
  285. // props.push({text: key, value: extra[key]});
  286. // });
  287. // }
  288. // let args = ['color: blue'];
  289. // let msg = `%c[${prefix || 'T'}] %c` + props.map(item => (
  290. // args.push('color: green', 'color: red'),
  291. // `${item.text}: %c${item.value}`
  292. // )).join('%c, ');
  293. // console.log.apply(console, [msg].concat(args));
  294. // // console.log(this);
  295. // };
  296. // window.printPipeline = function (task: any, prefix: string) {
  297. // const pipeline = task.__pipeline;
  298. // let currTask = pipeline.head;
  299. // while (currTask) {
  300. // window.printTask(currTask, prefix);
  301. // currTask = currTask._downstream;
  302. // }
  303. // };
  304. // window.showChain = function (chainHeadTask) {
  305. // var chain = [];
  306. // var task = chainHeadTask;
  307. // while (task) {
  308. // chain.push({
  309. // task: task,
  310. // up: task._upstream,
  311. // down: task._downstream,
  312. // idxInPipeline: task.__idxInPipeline
  313. // });
  314. // task = task._downstream;
  315. // }
  316. // return chain;
  317. // };
  318. // window.findTaskInChain = function (task, chainHeadTask) {
  319. // let chain = window.showChain(chainHeadTask);
  320. // let result = [];
  321. // for (let i = 0; i < chain.length; i++) {
  322. // let chainItem = chain[i];
  323. // if (chainItem.task === task) {
  324. // result.push(i);
  325. // }
  326. // }
  327. // return result;
  328. // };
  329. // window.printChainAEachInChainB = function (chainHeadTaskA, chainHeadTaskB) {
  330. // let chainA = window.showChain(chainHeadTaskA);
  331. // for (let i = 0; i < chainA.length; i++) {
  332. // console.log('chainAIdx:', i, 'inChainB:', window.findTaskInChain(chainA[i].task, chainHeadTaskB));
  333. // }
  334. // };