aboutsummaryrefslogtreecommitdiff
path: root/src/lib/stores/widgets.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/stores/widgets.js')
-rw-r--r--src/lib/stores/widgets.js57
1 files changed, 32 insertions, 25 deletions
diff --git a/src/lib/stores/widgets.js b/src/lib/stores/widgets.js
index 372cad1..2796a7b 100644
--- a/src/lib/stores/widgets.js
+++ b/src/lib/stores/widgets.js
@@ -1,25 +1,23 @@
import { derived, readable, writable } from 'svelte/store';
-import { createClient } from '@supabase/supabase-js';
-import { supabase } from '$lib/config';
+import { queryWithAbort } from '$lib/db';
import { maxSize } from '$lib/config';
const boxParser = /\(([0-9]+),([0-9]+)\),\(([0-9]+),([0-9]+)\)/;
-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 }) {
- const results = [
- `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}))"`,
- `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}))"`
+const getBoxQuery = function getBoxQuery({ left, top, right, bottom }) {
+ const boxes = [
+ `box && box '((${left},${top}),(${right},${bottom}))'`,
+ `box && box '((${left + maxSize},${top + maxSize}),(${right + maxSize},${bottom + maxSize}))'`,
+ `box && box '((${left + maxSize},${top}),(${right + maxSize},${bottom}))'`,
+ `box && box '((${left},${top + maxSize}),(${right},${bottom + maxSize}))'`,
+ `box && box '((${left - maxSize},${top - maxSize}),(${right - maxSize},${bottom - maxSize}))'`,
+ `box && box '((${left - maxSize},${top}),(${right - maxSize},${bottom}))'`,
+ `box && box '((${left},${top - maxSize}),(${right},${bottom - maxSize}))'`
];
- return results;
+ return `SELECT * FROM widgets WHERE ${boxes.join(' OR ')}`;
};
const serialize = function serialize(widget) {
@@ -35,13 +33,19 @@ const serialize = function serialize(widget) {
};
let ac = null;
-export const widgets = derived(sensor, async function ($sensor, set) {
- const boxes = getBoxes($sensor);
+export const widgets = derived(sensor, async ($sensor, set) => {
+ const query = getBoxQuery($sensor);
ac && ac.abort();
ac = new AbortController();
- const { data } = await client.from('widgets').select().or(boxes.join(',')).abortSignal(ac.signal);
- if (data) {
- return set(data.map(serialize));
+ try {
+ const { rows } = await queryWithAbort(query, [], ac.signal);
+ if (rows) {
+ return set(rows.map(serialize));
+ }
+ } catch (error) {
+ if (error.message !== 'Query aborted') {
+ console.error('Error fetching widgets:', error);
+ }
}
});
@@ -51,13 +55,16 @@ export const countElements = function countElements(left, top, right, bottom) {
(async function () {
countAc && countAc.abort();
countAc = new AbortController();
- const { data } = await client
- .from('widgets')
- .select('*', { head: true, count: 'estimated' })
- .or(`box.ov."((${left},${top}),(${right},${bottom}))"`)
- .abortSignal(countAc.signal);
- if (data) {
- return set(data);
+ try {
+ const countQuery = `SELECT COUNT(*) FROM widgets WHERE box && box '((${left},${top}),(${right},${bottom}))'`;
+ const { rows } = await queryWithAbort(countQuery, [], countAc.signal);
+ if (rows && rows[0]) {
+ return set(parseInt(rows[0].count));
+ }
+ } catch (error) {
+ if (error.message !== 'Query aborted') {
+ console.error('Error counting widgets:', error);
+ }
}
})();
});