task.js 11 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. var _util = require("zrender/lib/core/util");
  20. var assert = _util.assert;
  21. var isArray = _util.isArray;
  22. var _config = require("../config");
  23. var __DEV__ = _config.__DEV__;
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. /**
  43. * @param {Object} define
  44. * @return See the return of `createTask`.
  45. */
  46. function createTask(define) {
  47. return new Task(define);
  48. }
  49. /**
  50. * @constructor
  51. * @param {Object} define
  52. * @param {Function} define.reset Custom reset
  53. * @param {Function} [define.plan] Returns 'reset' indicate reset immediately.
  54. * @param {Function} [define.count] count is used to determin data task.
  55. * @param {Function} [define.onDirty] count is used to determin data task.
  56. */
  57. function Task(define) {
  58. define = define || {};
  59. this._reset = define.reset;
  60. this._plan = define.plan;
  61. this._count = define.count;
  62. this._onDirty = define.onDirty;
  63. this._dirty = true; // Context must be specified implicitly, to
  64. // avoid miss update context when model changed.
  65. this.context;
  66. }
  67. var taskProto = Task.prototype;
  68. /**
  69. * @param {Object} performArgs
  70. * @param {number} [performArgs.step] Specified step.
  71. * @param {number} [performArgs.skip] Skip customer perform call.
  72. * @param {number} [performArgs.modBy] Sampling window size.
  73. * @param {number} [performArgs.modDataCount] Sampling count.
  74. */
  75. taskProto.perform = function (performArgs) {
  76. var upTask = this._upstream;
  77. var skip = performArgs && performArgs.skip; // TODO some refactor.
  78. // Pull data. Must pull data each time, because context.data
  79. // may be updated by Series.setData.
  80. if (this._dirty && upTask) {
  81. var context = this.context;
  82. context.data = context.outputData = upTask.context.outputData;
  83. }
  84. if (this.__pipeline) {
  85. this.__pipeline.currentTask = this;
  86. }
  87. var planResult;
  88. if (this._plan && !skip) {
  89. planResult = this._plan(this.context);
  90. } // Support sharding by mod, which changes the render sequence and makes the rendered graphic
  91. // elements uniformed distributed when progress, especially when moving or zooming.
  92. var lastModBy = normalizeModBy(this._modBy);
  93. var lastModDataCount = this._modDataCount || 0;
  94. var modBy = normalizeModBy(performArgs && performArgs.modBy);
  95. var modDataCount = performArgs && performArgs.modDataCount || 0;
  96. if (lastModBy !== modBy || lastModDataCount !== modDataCount) {
  97. planResult = 'reset';
  98. }
  99. function normalizeModBy(val) {
  100. !(val >= 1) && (val = 1); // jshint ignore:line
  101. return val;
  102. }
  103. var forceFirstProgress;
  104. if (this._dirty || planResult === 'reset') {
  105. this._dirty = false;
  106. forceFirstProgress = reset(this, skip);
  107. }
  108. this._modBy = modBy;
  109. this._modDataCount = modDataCount;
  110. var step = performArgs && performArgs.step;
  111. if (upTask) {
  112. this._dueEnd = upTask._outputDueEnd;
  113. } // DataTask or overallTask
  114. else {
  115. this._dueEnd = this._count ? this._count(this.context) : Infinity;
  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. doProgress(this, progress[i], start, end, modBy, modDataCount);
  126. }
  127. } else {
  128. doProgress(this, progress, start, end, modBy, modDataCount);
  129. }
  130. }
  131. this._dueIndex = end; // If no `outputDueEnd`, assume that output data and
  132. // input data is the same, so use `dueIndex` as `outputDueEnd`.
  133. var outputDueEnd = this._settedOutputEnd != null ? this._settedOutputEnd : end;
  134. this._outputDueEnd = outputDueEnd;
  135. } else {
  136. // (1) Some overall task has no progress.
  137. // (2) Stubs, that its host overall task do not let it has progress, has no progress.
  138. // This should always be performed so it can be passed to downstream.
  139. this._dueIndex = this._outputDueEnd = this._settedOutputEnd != null ? this._settedOutputEnd : this._dueEnd;
  140. }
  141. return this.unfinished();
  142. };
  143. var iterator = function () {
  144. var end;
  145. var current;
  146. var modBy;
  147. var modDataCount;
  148. var winCount;
  149. var it = {
  150. reset: function (s, e, sStep, sCount) {
  151. current = s;
  152. end = e;
  153. modBy = sStep;
  154. modDataCount = sCount;
  155. winCount = Math.ceil(modDataCount / modBy);
  156. it.next = modBy > 1 && modDataCount > 0 ? modNext : sequentialNext;
  157. }
  158. };
  159. return it;
  160. function sequentialNext() {
  161. return current < end ? current++ : null;
  162. }
  163. function modNext() {
  164. var dataIndex = current % winCount * modBy + Math.ceil(current / winCount);
  165. var result = current >= end ? null : dataIndex < modDataCount ? dataIndex // If modDataCount is smaller than data.count() (consider `appendData` case),
  166. // Use normal linear rendering mode.
  167. : current;
  168. current++;
  169. return result;
  170. }
  171. }();
  172. taskProto.dirty = function () {
  173. this._dirty = true;
  174. this._onDirty && this._onDirty(this.context);
  175. };
  176. function doProgress(taskIns, progress, start, end, modBy, modDataCount) {
  177. iterator.reset(start, end, modBy, modDataCount);
  178. taskIns._callingProgress = progress;
  179. taskIns._callingProgress({
  180. start: start,
  181. end: end,
  182. count: end - start,
  183. next: iterator.next
  184. }, taskIns.context);
  185. }
  186. function reset(taskIns, skip) {
  187. taskIns._dueIndex = taskIns._outputDueEnd = taskIns._dueEnd = 0;
  188. taskIns._settedOutputEnd = null;
  189. var progress;
  190. var forceFirstProgress;
  191. if (!skip && taskIns._reset) {
  192. progress = taskIns._reset(taskIns.context);
  193. if (progress && progress.progress) {
  194. forceFirstProgress = progress.forceFirstProgress;
  195. progress = progress.progress;
  196. } // To simplify no progress checking, array must has item.
  197. if (isArray(progress) && !progress.length) {
  198. progress = null;
  199. }
  200. }
  201. taskIns._progress = progress;
  202. taskIns._modBy = taskIns._modDataCount = null;
  203. var downstream = taskIns._downstream;
  204. downstream && downstream.dirty();
  205. return forceFirstProgress;
  206. }
  207. /**
  208. * @return {boolean}
  209. */
  210. taskProto.unfinished = function () {
  211. return this._progress && this._dueIndex < this._dueEnd;
  212. };
  213. /**
  214. * @param {Object} downTask The downstream task.
  215. * @return {Object} The downstream task.
  216. */
  217. taskProto.pipe = function (downTask) {
  218. // If already downstream, do not dirty downTask.
  219. if (this._downstream !== downTask || this._dirty) {
  220. this._downstream = downTask;
  221. downTask._upstream = this;
  222. downTask.dirty();
  223. }
  224. };
  225. taskProto.dispose = function () {
  226. if (this._disposed) {
  227. return;
  228. }
  229. this._upstream && (this._upstream._downstream = null);
  230. this._downstream && (this._downstream._upstream = null);
  231. this._dirty = false;
  232. this._disposed = true;
  233. };
  234. taskProto.getUpstream = function () {
  235. return this._upstream;
  236. };
  237. taskProto.getDownstream = function () {
  238. return this._downstream;
  239. };
  240. taskProto.setOutputEnd = function (end) {
  241. // This only happend in dataTask, dataZoom, map, currently.
  242. // where dataZoom do not set end each time, but only set
  243. // when reset. So we should record the setted end, in case
  244. // that the stub of dataZoom perform again and earse the
  245. // setted end by upstream.
  246. this._outputDueEnd = this._settedOutputEnd = end;
  247. }; ///////////////////////////////////////////////////////////
  248. // For stream debug (Should be commented out after used!)
  249. // Usage: printTask(this, 'begin');
  250. // Usage: printTask(this, null, {someExtraProp});
  251. // function printTask(task, prefix, extra) {
  252. // window.ecTaskUID == null && (window.ecTaskUID = 0);
  253. // task.uidDebug == null && (task.uidDebug = `task_${window.ecTaskUID++}`);
  254. // task.agent && task.agent.uidDebug == null && (task.agent.uidDebug = `task_${window.ecTaskUID++}`);
  255. // var props = [];
  256. // if (task.__pipeline) {
  257. // var val = `${task.__idxInPipeline}/${task.__pipeline.tail.__idxInPipeline} ${task.agent ? '(stub)' : ''}`;
  258. // props.push({text: 'idx', value: val});
  259. // } else {
  260. // var stubCount = 0;
  261. // task.agentStubMap.each(() => stubCount++);
  262. // props.push({text: 'idx', value: `overall (stubs: ${stubCount})`});
  263. // }
  264. // props.push({text: 'uid', value: task.uidDebug});
  265. // if (task.__pipeline) {
  266. // props.push({text: 'pid', value: task.__pipeline.id});
  267. // task.agent && props.push(
  268. // {text: 'stubFor', value: task.agent.uidDebug}
  269. // );
  270. // }
  271. // props.push(
  272. // {text: 'dirty', value: task._dirty},
  273. // {text: 'dueIndex', value: task._dueIndex},
  274. // {text: 'dueEnd', value: task._dueEnd},
  275. // {text: 'outputDueEnd', value: task._outputDueEnd}
  276. // );
  277. // if (extra) {
  278. // Object.keys(extra).forEach(key => {
  279. // props.push({text: key, value: extra[key]});
  280. // });
  281. // }
  282. // var args = ['color: blue'];
  283. // var msg = `%c[${prefix || 'T'}] %c` + props.map(item => (
  284. // args.push('color: black', 'color: red'),
  285. // `${item.text}: %c${item.value}`
  286. // )).join('%c, ');
  287. // console.log.apply(console, [msg].concat(args));
  288. // // console.log(this);
  289. // }
  290. exports.createTask = createTask;