Add protocol worker#431
Conversation
|
I now see that my branch was not in an ideal state, let me know if I need to do some git magic to solve that. |
|
I think this looks good! Since it's just repackaging the code in a different distribution format and doesn't change bundle size much I think it makes sense to just keep in this repo. Would you mind adding instructions for how to use this to the readme? |
| getOptionsForZoom(options, z), | ||
| abortController, | ||
| ); | ||
| return { data: data.arrayBuffer }; |
There was a problem hiding this comment.
If this integrating more directly with maplibre, any chance we could let it return the raw vector tile without having to encode it first (since it gets immediately decoded right afterwards by maplibre) ?
There was a problem hiding this comment.
I don't think there's an easy way to do that right now. But I can look into it as a future improvement.
|
Sure, I'll add those, wanted a quick look before I invest time in the docs. |
|
Do you think this should be the recommended way to integrate with mapibre and replace the setupMaplibre utility? |
|
That's a good question. I'm not sure to be honest. |
|
I've added the instructions. Also let me know if you think I should change the output file name, maybe it should be maplibre-controur-worker.js instead of add-protocol-worker? I don't know... In any case, changing the name is fairly simple... I think it's a good addition, even if it's a bit rough, I'll be happy to support any issues related to it obviously, just tag me in the future. It's now live in production in Mapeak.com and the Mapeak app, it has great performance, thanks to you! |
|
Nice! Thanks! Let me know when a new version is available 😎 |
|
Hi and thank you for this great feature, // add-protocol-worker.ts
(self as any).addProtocol(
"dem-shared",
async (request: any, abortController: AbortController) => {
const [z, x, y] = parseUrl(request.url);
const data = await localDemManager.fetchTile(z, x, y, abortController);
return { data: data.data.arrayBuffer() };
},
); |
|
I'm not too familiar with all the details of this library, but So if you register a protocol for shared DEM, and you point your contour source and other sources to use it, I think it should be working as expected. |
|
I don't think the code above is using dem-shared correctly: await dispatcher.broadcast("contour-worker" as any, {
demUrlPattern: "https://tiles.mapterhorn.com/{z}/{x}/{y}.webp",
encoding: "terrarium",
maxzoom: 12,
});In order to utilize So you can configure a localdemsource on the main thread for caching, but again, I think it might be an overkill, IDK... |
Completely agree, I thought they were fetched from the worker but in the test above there is an unresolved request made on the main thread with the dem-shared protocol. My example above was wrong and using 2 localDemSource removes all the benefit we could have gained from it. |
|
@msbarry note that maplibre-gl-js 6 is now in pre-release and has dropped support for commonjs in favor of esm, so it might be worth waiting a bit with the release of this and make it esm (if it's not already). |
|
Re: shared DEM source, I think to make that work we'd need the request to bounce back to the main thread so they could be cached there, and we'd also need to install maplibre contour code on both the main and worker threads...
Sounds good, I might end up making the current version (0.1.7) compatible with maplibre <6 and a new 0.2.0 version compatible >= 6, unless I'm able to get 0.2.0 to be compabile with both ... |
|
I've updated the code in maplibre to avoid the error in case of unknown worker-main-thread message type here: I haven't tested it, but I hope it would work. I need to see if I can try out version 6 with my code somehow. Note that maplibre-gl 6 uses rolldown and the build is almost instantaneous. We have dropped the wiring of the worker code inside the package and now there's a two step requirement to initialize maplirbre (we'll see how much heat we'll get on that once we release this as non pre-release). |

Hi @msbarry
I've finished the implementation of this approach.
I thought about creating a different repo and different npm package for this, but due to how maplibre-conrour package is bundled (shared worker code etc), it's a bit problematic.
Also I think having it here would make sense as this use some internal utilities methods.
The idea here is similar to how RTL plugin works, where you host a js file and import it in a worker.
The code around how I use it in my website is the following, it's fairly simple I think:
So I added a target in the rollup to take the code from add-protocol-worker and bundle it separately.
This requires the latest additions I added to maplibre-gl-js in version 5.23.
I've also moved the
parseUrlto the utils file.I can add instructions on how to use it to readme with the above code to help people use it.
Do let me know what you want to do.