-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use rxjs to coordinate events and pipe state to back end using …
…web socket
- Loading branch information
Showing
5 changed files
with
163 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,56 @@ | ||
import { onMounted, onUnmounted, reactive, ref } from "vue"; | ||
import { onUnmounted, readonly, ref } from "vue"; | ||
import { BehaviorSubject, fromEvent } from "rxjs"; | ||
import { map, share, take } from "rxjs/operators"; | ||
|
||
const orientation = reactive<{ alpha: number | null; beta: number | null }>({ | ||
alpha: 0, | ||
beta: 0, | ||
}); | ||
export interface DeviceOrientation { | ||
alpha: number | null; | ||
beta: number | null; | ||
} | ||
|
||
export const isDeviceOrientationGranted = ref<boolean>( | ||
const _isDeviceOrientationGranted = ref<boolean>( | ||
typeof DeviceOrientationEvent.requestPermission !== "function" | ||
); | ||
|
||
export const isDeviceOrientationGranted = readonly(_isDeviceOrientationGranted); | ||
|
||
export async function requestDeviceOrientation(): Promise<boolean> { | ||
if (typeof DeviceOrientationEvent.requestPermission !== "function") { | ||
return true; | ||
} | ||
|
||
const response = await DeviceOrientationEvent.requestPermission(); | ||
if (response === "granted") { | ||
isDeviceOrientationGranted.value = true; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
_isDeviceOrientationGranted.value = response === "granted"; | ||
|
||
function onDeviceOrientation(e: DeviceOrientationEvent) { | ||
orientation.alpha = e.alpha; | ||
orientation.beta = e.beta; | ||
return _isDeviceOrientationGranted.value; | ||
} | ||
|
||
function checkDeviceOrientationGranted() { | ||
isDeviceOrientationGranted.value = true; | ||
} | ||
export function getDeviceOrientationSubject(): BehaviorSubject<DeviceOrientation> { | ||
const deviceOrientationSubject = new BehaviorSubject<DeviceOrientation>({ | ||
alpha: 0, | ||
beta: 0, | ||
}); | ||
|
||
export function useDeviceOrientation() { | ||
onMounted(() => { | ||
window.addEventListener("deviceorientation", onDeviceOrientation); | ||
window.addEventListener( | ||
"deviceorientation", | ||
checkDeviceOrientationGranted, | ||
{ | ||
once: true, | ||
} | ||
); | ||
const deviceOrientation$ = fromEvent<DeviceOrientationEvent>( | ||
window, | ||
"deviceorientation" | ||
).pipe( | ||
map(({ alpha, beta }) => ({ | ||
alpha, | ||
beta, | ||
})), | ||
share() | ||
); | ||
|
||
const sub1 = deviceOrientation$.pipe(take(1)).subscribe(() => { | ||
_isDeviceOrientationGranted.value = true; | ||
}); | ||
|
||
const sub2 = deviceOrientation$.subscribe(deviceOrientationSubject); | ||
|
||
onUnmounted(() => { | ||
window.removeEventListener("deviceorientation", onDeviceOrientation); | ||
window.removeEventListener( | ||
"deviceorientation", | ||
checkDeviceOrientationGranted | ||
); | ||
sub1.unsubscribe(); | ||
sub2.unsubscribe(); | ||
}); | ||
|
||
return orientation; | ||
return deviceOrientationSubject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { onUnmounted } from "vue"; | ||
import { BehaviorSubject, merge } from "rxjs"; | ||
import { DeviceOrientation } from "@/deviceOrientation"; | ||
import { webSocket } from "rxjs/webSocket"; | ||
import { map } from "rxjs/operators"; | ||
|
||
export function pipeToPi( | ||
velocity: BehaviorSubject<number>, | ||
orientation: BehaviorSubject<DeviceOrientation> | ||
) { | ||
const socketSubject = webSocket("wss:https://localhost:7777"); | ||
|
||
merge( | ||
velocity.pipe( | ||
map((velocity) => ({ | ||
velocity, | ||
})) | ||
), | ||
orientation.pipe( | ||
map((orientation) => ({ | ||
orientation, | ||
})) | ||
) | ||
).subscribe(socketSubject); | ||
|
||
const subscription = socketSubject.subscribe( | ||
() => {}, | ||
(error) => { | ||
console.log(error); | ||
} | ||
); | ||
|
||
onUnmounted(() => { | ||
socketSubject.error({ code: 1000, reason: "Stack.vue is unmounted" }); | ||
subscription.unsubscribe(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BehaviorSubject } from "rxjs"; | ||
import { onUnmounted, ref, Ref, readonly } from "vue"; | ||
import { DeepReadonly, UnwrapRef } from "@vue/reactivity"; | ||
|
||
export function behaviorSubjectToRef<T>( | ||
s: BehaviorSubject<T> | ||
): DeepReadonly<Ref<UnwrapRef<T>>> { | ||
const reference = ref<T>(s.value); | ||
|
||
const subscription = s.subscribe((v: T) => { | ||
reference.value = v as UnwrapRef<T>; | ||
}); | ||
onUnmounted(() => subscription.unsubscribe()); | ||
|
||
return readonly(reference); | ||
} |