📡 Adds reactive data from socket connection
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
import { onMount } from "svelte";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
onMount(() => {
|
||||
window.addEventListener("", () => {
|
||||
screen.orientation.addEventListener("change", _handleOrientationChange);
|
||||
})
|
||||
})
|
||||
|
||||
export type OrientationType = 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary'
|
||||
|
||||
export const orientation:Writable<OrientationType> = writable('portrait-primary');
|
||||
|
||||
export const isPortrait = writable(true);
|
||||
|
||||
const _isPortrait = (orientation:OrientationType | undefined):boolean => {
|
||||
return orientation === "portrait-primary" || orientation === "portrait-secondary";
|
||||
}
|
||||
|
||||
const _handleOrientationChange = () => {
|
||||
orientation.set(screen.orientation.type)
|
||||
isPortrait.set(_isPortrait(screen.orientation.type))
|
||||
}
|
||||
@@ -4,6 +4,8 @@ export type WebSocketStatus = 'OPEN' | 'CONNECTING' | 'CLOSED'
|
||||
|
||||
export const isConnected = writable(false)
|
||||
|
||||
export const data = writable(new Float32Array(13))
|
||||
|
||||
export const status:Writable<WebSocketStatus> = writable('CLOSED')
|
||||
|
||||
export const socket = writable(null)
|
||||
@@ -11,8 +13,10 @@ export const socket = writable(null)
|
||||
export const connect = (url:string) => {
|
||||
status.set('CONNECTING')
|
||||
let _socket = new WebSocket(url);
|
||||
_socket.binaryType = "arraybuffer";
|
||||
_socket.onopen = _connected;
|
||||
_socket.onclose = _disconnected;
|
||||
_socket.onmessage = _message;
|
||||
socket.set(_socket)
|
||||
}
|
||||
|
||||
@@ -26,3 +30,9 @@ const _disconnected = () => {
|
||||
isConnected.set(false)
|
||||
}
|
||||
|
||||
const _message = (event) => {
|
||||
if (event.data instanceof ArrayBuffer) {
|
||||
let buffer = new Uint8Array(event.data);
|
||||
data.set(new Float32Array(buffer.buffer));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
export class throttler {
|
||||
private _throttlePause: boolean;
|
||||
constructor() {
|
||||
this._throttlePause = false;
|
||||
}
|
||||
throttle = (callback:Function, time:number) => {
|
||||
if (this._throttlePause) return;
|
||||
|
||||
this._throttlePause = true;
|
||||
setTimeout(() => {
|
||||
callback();
|
||||
this._throttlePause = false;
|
||||
}, time);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user