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

Re-use url object in cache #183

Merged
merged 3 commits into from
Sep 12, 2024
Merged
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });

Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.4.2/radar.min.js"></script>
<script src="https://js.radar.com/v4.4.3-beta.1/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.2/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.2/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.3-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.3-beta.1/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.2/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.2/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.3-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.3-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.2/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.2/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.3-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.3-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.4.2",
"version": "4.4.3-beta.1",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
25 changes: 13 additions & 12 deletions src/ui/RadarMarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ interface ImageOptions {
}

// cache URL loaded markers
const IMAGE_CACHE = new Map<string, 'pending' | 'failed' | Blob>();
const IMAGE_CACHE = new Map<string, string>();

const useCachedImage = (url: string, timeoutMS: number = 5000): Promise<Blob> => new Promise((resolve, reject) => {
const useCachedImage = (url: string, timeoutMS: number = 5000): Promise<string> => new Promise((resolve, reject) => {
if (!IMAGE_CACHE.has(url)) { // nothing in cache
IMAGE_CACHE.set(url, 'pending'); // request in flight
return reject('miss');
Expand All @@ -50,9 +50,9 @@ const useCachedImage = (url: string, timeoutMS: number = 5000): Promise<Blob> =>
clearInterval(interval);
reject('failed');

} else { // return data
} else if (cachedData !== undefined) { // return data
clearInterval(interval);
resolve(cachedData as Blob);
resolve(cachedData);
}
}, 100);
});
Expand Down Expand Up @@ -114,12 +114,11 @@ class RadarMarker extends maplibregl.Marker {
child.remove();
});

const onSuccess = (blob: Blob) => {
const markerObject = URL.createObjectURL(blob);
const onSuccess = (url: string) => {
this._element.replaceChildren(createImageElement({
width: markerOptions.width,
height: markerOptions.height,
url: markerObject,
url,
}));
};

Expand All @@ -137,8 +136,9 @@ class RadarMarker extends maplibregl.Marker {
if (res.status === 200) {
res.blob()
.then((data) => {
IMAGE_CACHE.set(markerOptions.url as string, data); // cache data
onSuccess(data);
const url = URL.createObjectURL(data);
IMAGE_CACHE.set(markerOptions.url as string, url); // cache data
onSuccess(url);
})
.catch(onError);
} else {
Expand Down Expand Up @@ -169,8 +169,9 @@ class RadarMarker extends maplibregl.Marker {
responseType: 'blob',
})
.then(({ data }) => {
IMAGE_CACHE.set(markerOptions.marker as string, data); // cache data
onSuccess(data)
const url = URL.createObjectURL(data);
IMAGE_CACHE.set(markerOptions.marker as string, url); // cache data
onSuccess(url)
})
.catch(onError);
};
Expand Down Expand Up @@ -226,7 +227,7 @@ class RadarMarker extends maplibregl.Marker {
if (this.getPopup()) {
// close any other open popups
(this._map.getMarkers() || []).forEach((otherMarker) => {
if (otherMarker.getPopup().isOpen()) {
if (otherMarker.getPopup()?.isOpen()) {
otherMarker.togglePopup();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.4.2';
export default '4.4.3-beta.1';
Loading