]> git.r.bdr.sh - rbdr/canvas/blob - src/lib/stores/widgets.js
ff5c53ecd8f60d9cb12076753e633070e31613ea
[rbdr/canvas] / src / lib / stores / widgets.js
1 import { derived, writable } from 'svelte/store';
2 import { createClient } from '@supabase/supabase-js';
3 import { supabase } from '$lib/config';
4 import { maxSize } from '$lib/config';
5
6 const client = createClient(supabase.url, supabase.key);
7
8 export const sensor = writable({left: 0, top: 0, right: 0, bottom: 0});
9
10 const getBoxes = function getBoxes ({left, top, right, bottom}) {
11 return [
12 `box.ov."(${left},${top},${right},${bottom})"`,
13 `box.ov."(${left+maxSize},${top+maxSize},${right+maxSize},${bottom+maxSize})"`,
14 `box.ov."(${left+maxSize},${top},${right+maxSize},${bottom})"`,
15 `box.ov."(${left},${top+maxSize},${right},${bottom+maxSize})"`
16 ]
17 };
18
19 let ac = null;
20 export const widgets = derived(sensor, async function ($sensor, set) {
21
22 const boxes = getBoxes($sensor);
23 ac && ac.abort()
24 ac = new AbortController();
25 const { data } = await client
26 .from('widgets')
27 .select()
28 .or(boxes.join(','))
29 .abortSignal(ac.signal)
30 return set(data);
31 });