Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from XMLHttpRequest to fetch #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const map = L.map('map').setView([0, 0], 2);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

const markers = L.geoJson(null, {
Expand Down
43 changes: 19 additions & 24 deletions demo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@
importScripts('../dist/supercluster.js');

const now = Date.now();

let index;

getJSON('../test/fixtures/places.json', (geojson) => {
console.log(`loaded ${ geojson.length } points JSON in ${ (Date.now() - now) / 1000 }s`);

index = new Supercluster({
const url = '../test/fixtures/places.json';
let index = new Supercluster({
log: true,
radius: 60,
extent: 256,
maxZoom: 17
}).load(geojson.features);

console.log(index.getTile(0, 0, 0));

postMessage({ready: true});
});

self.onmessage = function (e) {
Expand All @@ -30,17 +20,22 @@ self.onmessage = function (e) {
} else if (e.data) {
postMessage(index.getClusters(e.data.bbox, e.data.zoom));
}
};
}

function getJSON(url) {
fetch(url)
.then(response => response.json())
.then(geojson => {

console.log(`Loaded ${ geojson.features.length } points JSON in ${ (Date.now() - now) / 1000 }s`);

index.load(geojson.features);

console.log(index.getTile(0, 0, 0));

function getJSON(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.setRequestHeader('Accept', 'application/json');
xhr.onload = function () {
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300 && xhr.response) {
callback(xhr.response);
}
};
xhr.send();
postMessage({ready: true});
})
.catch(error => {console.log('Request failed', error);});
}

getJSON(url);