]> git.r.bdr.sh - rbdr/canvas/blame - src/lib/stores/widgets.js
Save WIP
[rbdr/canvas] / src / lib / stores / widgets.js
CommitLineData
587d8fe6 1import { derived, readable, writable } from 'svelte/store';
c30e6881
RBR
2import { createClient } from '@supabase/supabase-js';
3import { supabase } from '$lib/config';
4import { maxSize } from '$lib/config';
5
587d8fe6 6const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/;
c30e6881
RBR
7const client = createClient(supabase.url, supabase.key);
8
587d8fe6 9export const sensor = writable({ left: 0, top: 0, right: 0, bottom: 0 });
c30e6881 10
587d8fe6
RBR
11const getBoxes = function getBoxes({ left, top, right, bottom }) {
12 const results = [
13 `box.ov."((${left},${top}),(${right},${bottom}))"`,
14 `box.ov."((${left + maxSize},${top + maxSize}),(${right + maxSize},${bottom + maxSize}))"`,
15 `box.ov."((${left + maxSize},${top}),(${right + maxSize},${bottom}))"`,
16 `box.ov."((${left},${top + maxSize}),(${right},${bottom + maxSize}))"`,
17 `box.ov."((${left - maxSize},${top - maxSize}),(${right - maxSize},${bottom - maxSize}))"`,
18 `box.ov."((${left - maxSize},${top}),(${right - maxSize},${bottom}))"`,
19 `box.ov."((${left},${top - maxSize}),(${right},${bottom - maxSize}))"`
20 ];
d7cea969 21
587d8fe6 22 return results;
d7cea969
RBR
23};
24
25const serialize = function serialize(widget) {
587d8fe6
RBR
26 const boxComponents = widget.box.match(boxParser).slice(1, 5).map(Number);
27 const box = {
28 left: Math.min(boxComponents[0], boxComponents[2]),
29 right: Math.max(boxComponents[0], boxComponents[2]),
30 top: Math.min(boxComponents[1], boxComponents[3]),
31 bottom: Math.max(boxComponents[1], boxComponents[3])
32 };
d7cea969 33
587d8fe6 34 return { ...widget, box };
c30e6881
RBR
35};
36
37let ac = null;
38export const widgets = derived(sensor, async function ($sensor, set) {
587d8fe6
RBR
39 const boxes = getBoxes($sensor);
40 ac && ac.abort();
41 ac = new AbortController();
42 const { data } = await client.from('widgets').select().or(boxes.join(',')).abortSignal(ac.signal);
43 if (data) {
44 return set(data.map(serialize));
45 }
c30e6881 46});
587d8fe6
RBR
47
48export const countElements = function countElements(left, top, right, bottom) {
49 let countAc = null;
50 return readable(0, (set) => {
51 (async function () {
52 countAc && countAc.abort();
53 countAc = new AbortController();
54 const { data } = await client
55 .from('widgets')
56 .select('*', { head: true, count: 'estimated' })
57 .or(`box.ov."((${left},${top}),(${right},${bottom}))"`)
58 .abortSignal(countAc.signal);
59 if (data) {
60 return set(data);
61 }
62 })();
63 });
64};