aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2022-07-06 22:49:06 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2022-07-06 22:49:06 +0200
commitc30e688113e317ff6aca8a63f16b202cbfff3820 (patch)
tree36967ba64c9803fb5179e496cc455f29e78f68b9 /src/lib
parent2df937df1372359babd123d1361e702a1a4b8168 (diff)
Connect sensor to database
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/components/sensor.svelte3
-rw-r--r--src/lib/config.js5
-rw-r--r--src/lib/stores/widgets.js31
3 files changed, 38 insertions, 1 deletions
diff --git a/src/lib/components/sensor.svelte b/src/lib/components/sensor.svelte
index da968a3..e98b740 100644
--- a/src/lib/components/sensor.svelte
+++ b/src/lib/components/sensor.svelte
@@ -1,6 +1,7 @@
<script>
import { browser } from '$app/env';
import { maxSize } from '$lib/config';
+ import { sensor } from '$lib/stores/widgets';
export let x;
export let y;
@@ -16,7 +17,7 @@
const fetchItems = function fetchItems( left, top, right, bottom ) {
clearTimeout(timer);
timer = setTimeout(() => {
- console.log('fetching', left, top, right, bottom);
+ sensor.set({ left, top, right, bottom });
}, 50);
}
diff --git a/src/lib/config.js b/src/lib/config.js
index 3d243ed..b450d84 100644
--- a/src/lib/config.js
+++ b/src/lib/config.js
@@ -1,2 +1,7 @@
export const coordinateLength = 6;
export const maxSize = Math.pow(16, coordinateLength);
+
+export const supabase = {
+ url: import.meta.env.VITE_SUPABASE_URL,
+ key: import.meta.env.VITE_SUPABASE_KEY
+};
diff --git a/src/lib/stores/widgets.js b/src/lib/stores/widgets.js
new file mode 100644
index 0000000..ff5c53e
--- /dev/null
+++ b/src/lib/stores/widgets.js
@@ -0,0 +1,31 @@
+import { derived, writable } from 'svelte/store';
+import { createClient } from '@supabase/supabase-js';
+import { supabase } from '$lib/config';
+import { maxSize } from '$lib/config';
+
+const client = createClient(supabase.url, supabase.key);
+
+export const sensor = writable({left: 0, top: 0, right: 0, bottom: 0});
+
+const getBoxes = function getBoxes ({left, top, right, bottom}) {
+ return [
+ `box.ov."(${left},${top},${right},${bottom})"`,
+ `box.ov."(${left+maxSize},${top+maxSize},${right+maxSize},${bottom+maxSize})"`,
+ `box.ov."(${left+maxSize},${top},${right+maxSize},${bottom})"`,
+ `box.ov."(${left},${top+maxSize},${right},${bottom+maxSize})"`
+ ]
+};
+
+let ac = null;
+export const widgets = derived(sensor, async function ($sensor, set) {
+
+ const boxes = getBoxes($sensor);
+ ac && ac.abort()
+ ac = new AbortController();
+ const { data } = await client
+ .from('widgets')
+ .select()
+ .or(boxes.join(','))
+ .abortSignal(ac.signal)
+ return set(data);
+});