task.js 12 KB

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