11import { fromBinary } from "@bufbuild/protobuf" ;
2- import { type Tile , TileSchema } from "./gen/vector_tile_pb.js" ;
3- import type { Source } from "./types.js" ;
2+ import { type Tile , TileSchema } from ".. /gen/vector_tile_pb.js" ;
3+ import type { Source } from ".. /types.js" ;
44
55const TILES_URL = "https://tiles.versatiles.org/tiles/osm/{z}/{x}/{y}" ;
66
7- export const fetchTile = async (
8- tilesUrl : string ,
9- x : number ,
10- y : number ,
11- z : number ,
12- ) => {
7+ const fetchTile = async ( tilesUrl : string , x : number , y : number , z : number ) => {
138 const url = tilesUrl
149 . replace ( "{x}" , x . toString ( ) )
1510 . replace ( "{y}" , y . toString ( ) )
@@ -19,21 +14,26 @@ export const fetchTile = async (
1914 return bytes ;
2015} ;
2116
22- export const parseTile = ( bytes : Uint8Array ) => {
17+ const parseTile = ( bytes : Uint8Array ) => {
2318 const tile = fromBinary ( TileSchema , bytes ) ;
2419 return tile ;
2520} ;
2621
27- export const source = ( ) : Source => {
28- const tileCache = new Map < string , Tile > ( ) ;
22+ export const vector = ( { name : sourceName } : { name : string } ) : Source => {
23+ const tileCache = new Map < string , { type : "vector" } & Tile > ( ) ;
2924
3025 return {
31- async fetch ( x , y , z ) {
26+ get name ( ) {
27+ return sourceName ;
28+ } ,
29+ async fetch ( { name, tile : { x, y, z } } ) {
30+ if ( name !== sourceName ) return null ;
31+
3232 const cached = tileCache . get ( `${ x } -${ y } -${ z } ` ) ;
3333 if ( cached ) return cached ;
3434
3535 const bytes = await fetchTile ( TILES_URL , x , y , z ) ;
36- const tile = parseTile ( bytes ) ;
36+ const tile = { type : "vector" as const , ... parseTile ( bytes ) } ;
3737
3838 tileCache . set ( `${ x } -${ y } -${ z } ` , tile ) ;
3939
0 commit comments