domVirtualCreateQueue.mjs

  1. import pmQueue from './pmQueue.mjs'
  2. import domVirtualCreate from './domVirtualCreate.mjs'
  3. /**
  4. * 前端產生臨時DOM元素為對象並進行客製化處理,並通過佇列管控,限定一次處理一個。初始化後將回傳封裝後的domVirtualCreate,輸入與輸出皆與其相同,詳見domVirtualCreate
  5. *
  6. * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/domVirtualCreateQueue.test.mjs Github}
  7. * @memberOf wsemi
  8. * @returns {Function} 回傳domVirtualCreate函數
  9. * @example
  10. * need test in browser
  11. *
  12. * let dpq = domVirtualCreateQueue()
  13. *
  14. * //先給予圖片寬高與產製函數, 內部通過佇列逐次運行產圖與回傳base64
  15. * let fun = async (ele) => {
  16. *
  17. * //chart
  18. * let highchartsOpt = {...} //給予highcharts設定物件
  19. * window.Highcharts.chart(ele, highchartsOpt)
  20. *
  21. * //html2picDyn, 預設轉出base64
  22. * let html2canvasOpt = { scale: 3 } //放大3倍, 提高解析度
  23. * b64 = await html2picDyn(ele, html2canvasOpt)
  24. *
  25. * return b64
  26. * }
  27. *
  28. * let b64 = await dpq(fun, { width: 500, height: 350 })
  29. *
  30. */
  31. function domVirtualCreateQueue() {
  32. function Core() {
  33. //pmq
  34. let pmq = pmQueue(1) //因處理dom為瀏覽器當前執行緒, 無法使用web worker, 故僅限定同時處理1組
  35. async function pushQueue(fun, opt = {}) {
  36. return pmq(domVirtualCreate, fun, opt)
  37. }
  38. return pushQueue
  39. }
  40. return new Core()
  41. }
  42. export default domVirtualCreateQueue