-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added leaflet service to manage loading and initialization of leaflet…
… to prevent duplicate initializations
- Loading branch information
Showing
7 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Service from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { debug } from '@ember/debug'; | ||
|
||
export default class LeafletService extends Service { | ||
@tracked instances = []; | ||
@tracked initialized = false; | ||
@tracked instance; | ||
@tracked initializationId; | ||
|
||
load() { | ||
let intervals = 0; | ||
this.initializationId = setInterval(() => { | ||
const Leaflet = window.L || window.leaflet; | ||
// Check if Leaflet global object `L` is present | ||
if (Leaflet && typeof Leaflet === 'object') { | ||
if (!this.initialized) { | ||
// First initialization | ||
debug('Leaflet has been initialized.'); | ||
if (this.instance === undefined) { | ||
this.instance = Leaflet; | ||
window.L = Leaflet; | ||
} | ||
this.initialized = true; | ||
} else if (Leaflet !== this.instance && !this.instances.includes(Leaflet)) { | ||
// Subsequent re-initializations | ||
debug('Leaflet has been re-initialized!'); | ||
this.instances.push(window.L); | ||
} | ||
} | ||
|
||
intervals++; | ||
if (intervals === 5) { | ||
clearTimeout(this.initializationId); | ||
} | ||
}, 100); | ||
} | ||
|
||
getInstance() { | ||
return this.instance || window.L || window.leaflet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import loadAssets from './load-assets'; | ||
|
||
export default function loadLeafletPlugins(assets = { basePath: null, scripts: [], stylesheets: [], globalIndicatorKey: null }) { | ||
export default function loadLeafletPlugins(assets = { basePath: null, scripts: [], stylesheets: [], globalIndicatorKey: null }, callback = null) { | ||
const basePath = assets.basePath ?? 'engines-dist/leaflet'; | ||
loadAssets({ basePath, ...assets }); | ||
loadAssets({ basePath, ...assets }, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@fleetbase/ember-ui/services/leaflet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'dummy/tests/helpers'; | ||
|
||
module('Unit | Service | leaflet', function (hooks) { | ||
setupTest(hooks); | ||
|
||
// TODO: Replace this with your real tests. | ||
test('it exists', function (assert) { | ||
let service = this.owner.lookup('service:leaflet'); | ||
assert.ok(service); | ||
}); | ||
}); |