From 4aa140c264412c5c2da9dc13215523371ba622ff Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 9 Jun 2025 13:30:11 +0100 Subject: [PATCH] always use callback in tiles.fetch (formerly polylines) --- pkgs/maps/main.fnl | 13 ++++++++++--- pkgs/maps/tiles.fnl | 33 +++++++++++++-------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/pkgs/maps/main.fnl b/pkgs/maps/main.fnl index 8bd73bd..2a1183f 100644 --- a/pkgs/maps/main.fnl +++ b/pkgs/maps/main.fnl @@ -67,6 +67,7 @@ label.readout { :zoom 17 :course 0 ; direction of travel :orientation 0 ; map rotation angle from north + :tiles {} } ) @@ -198,6 +199,13 @@ label.readout { (var map-surface nil) +(fn fetch-tiles [bounds tbl zoom] + (for [x bounds.min.x bounds.max.x] + (for [y bounds.min.y bounds.max.y] + (let [k (tiles.name x y zoom)] + (when (not (. tbl k)) + (tiles.fetch cq x y zoom #(tset tbl k $1))))))) + (fn draw-onto-map-surface [surface bounds zoom] (let [{ : num-tiles-x : num-tiles-y } bounds road-width 14 @@ -205,9 +213,7 @@ label.readout { (for [x bounds.min.x bounds.max.x] (for [y bounds.min.y bounds.max.y] - (merge lines (tiles.polylines cq x y zoom - #(set map-surface nil) - )))) + (merge lines (or (. app-state.tiles (tiles.name x y zoom)) {})))) (let [seen-road-names {} g (cairo.Context.create surface)] @@ -314,6 +320,7 @@ label.readout { (let [bounds (map-bounds app-state.lat app-state.lon app-state.zoom)] (when (not (bounds= old-bounds bounds)) + (fetch-tiles bounds app-state.tiles app-state.zoom) (set map-surface nil))) (set app-state.orientation (+ app-state.orientation diff --git a/pkgs/maps/tiles.fnl b/pkgs/maps/tiles.fnl index 436bf6e..d269b26 100644 --- a/pkgs/maps/tiles.fnl +++ b/pkgs/maps/tiles.fnl @@ -112,33 +112,26 @@ (fn tile-name [x y zoom] (.. x "_" y "_" zoom)) -(fn polylines [cq x y zoom cb] - (let [k (tile-name x y zoom) +(fn fetch [cq x y zoom cb] + (let [k (tile-name x y zoom) pathname (.. "/tmp/tiles/" k ".json")] (if (file-exists? pathname) - (let [data (with-open [i (io.open pathname :r)] (i:read "*a"))] - (if (= data "") - [] - (canvas (. (json.decode data) :elements) zoom))) + (let [payload (with-open [i (io.open pathname :r)] (i:read "*a"))] + (when (not (= payload "")) + (cb (canvas (. (json.decode payload) :elements) zoom)))) (let [out (io.open pathname :w)] (cq:wrap (fn [] (print "getting " k) - (var json nil) + (var payload nil) (with-open [f out] - (while (not json) - (set json (unparsed-for-xyz x y zoom)) - (when (not json) + (while (not payload) + (set payload (unparsed-for-xyz x y zoom)) + (when (not payload) (print "sleeping " k) (cqueues.sleep (math.random 2 6)))) (print "got " k) - (f:write json) - (cb) - true))) - [] ; return no lines for now - )))) + (f:write payload) + (cb (canvas (. (json.decode payload) :elements) zoom)) + true))))))) - - - - -{ : polylines : latlon->tile :name tile-name } +{ : fetch : latlon->tile :name tile-name }