5b84c70150018a595d651b7d4974ea24c0c4c13e.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import * as maptalks from 'maptalks'
  2. import FnTask from './Task.js'
  3. import Event from './Event'
  4. // 从远程中获取数据
  5. function getRemoteData (url) {
  6. return new Promise((resolve, reject) => {
  7. maptalks.Ajax.getJSON(url, (err, data) => {
  8. if (err || !data || !data.features.length) {
  9. reject()
  10. } else {
  11. resolve(data.features)
  12. }
  13. })
  14. })
  15. }
  16. const LOADDING = 1;
  17. const SUCCESS = 2;
  18. class Reveal extends Event {
  19. constructor() {
  20. super()
  21. this.urlStatue = {}
  22. this.cache = {}
  23. this.loaddingGeos = {}
  24. this.remoteTask = new FnTask({maxParallel: 3})
  25. this.loadTask = new FnTask({maxParallel: 5})
  26. }
  27. // 获取数据先从内存,再从远程获取
  28. preloadingFeatures(url) {
  29. this.urlStatue[url] = LOADDING
  30. this.remoteTask.push(
  31. async () => {
  32. try {
  33. return await getRemoteData(url)
  34. } catch (e) {
  35. delete this.urlStatue[url]
  36. return null
  37. }
  38. },
  39. data => {
  40. this.emit(url, data)
  41. }
  42. )
  43. }
  44. checkFeatureState (id) {
  45. for (let key in this.loaddingGeos) {
  46. if (~this.loaddingGeos[key].indexOf(id)) return true
  47. }
  48. }
  49. // 分批加载
  50. async inBatches(url, features, addArchitecture) {
  51. if (this.urlStatue[url] === LOADDING) {
  52. for (let i = 0, len = features.length; i < len; i++) {
  53. if (this.checkFeatureState(features[i].id)) {
  54. features.splice(i, 1)
  55. i--
  56. len--
  57. } else {
  58. }
  59. }
  60. this.loaddingGeos[url] = features.map(f => f.id)
  61. this.cache[url] = await addArchitecture(features)
  62. this.urlStatue[url] = SUCCESS
  63. }
  64. }
  65. // 加载模型
  66. async loaddingFeatures(url, cb) {
  67. this.loadTask.push(async () => {
  68. this.preloadingFeatures(url)
  69. this.off(url)
  70. this.once(url, features => {
  71. if (features) {
  72. if (document.hidden) {
  73. this.once('visibilityshow', () => this.inBatches(url, features, cb))
  74. } else {
  75. this.inBatches(url, features, cb)
  76. }
  77. }
  78. })
  79. })
  80. }
  81. async loadRegion(urls, loadCb) {
  82. for (let key of urls) {
  83. if (this.urlStatue[key]) continue
  84. this.loaddingFeatures(key, loadCb)
  85. }
  86. }
  87. eliminate (urls) {
  88. if (!Array.isArray(urls)) {
  89. urls = [urls]
  90. }
  91. urls.forEach(url => {
  92. delete this.urlStatue[url]
  93. delete this.cache[url]
  94. delete this.loaddingGeos[url]
  95. })
  96. }
  97. }
  98. const reveal = new Reveal()
  99. document.addEventListener('visibilitychange', function () {
  100. if (!document.hidden) {
  101. reveal.emit('visibilityshow')
  102. }
  103. });
  104. export default reveal