-
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.
Merge pull request #10 from observerly/feature/composables/useStatus
Added the useStatus() composable.
- Loading branch information
Showing
7 changed files
with
184 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
--- | ||
category: Integration | ||
--- | ||
|
||
# useStatus | ||
|
||
Reactive Planewave API status client. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { watch } from 'vue' | ||
|
||
import { useStatus } from '@observerly/useaestrium | ||
|
||
const { status } = useStatus({ url: 'http://127.0.0.1:5000' }) | ||
|
||
// watch for a change in the status: | ||
watch(status, () => { | ||
console.log(status) | ||
}) | ||
``` | ||
|
||
See the [Type Declarations](#type-declarations) for more options. |
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,15 @@ | ||
import { | ||
describe, | ||
expect, | ||
it | ||
} from '@jest/globals' | ||
|
||
import { | ||
useStatus | ||
} from './' | ||
|
||
describe('useStatus', () => { | ||
it('Should Be Defined', () => { | ||
expect(useStatus).toBeDefined() | ||
}) | ||
}) |
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,79 @@ | ||
import { | ||
onUnmounted, ref, Ref, watch | ||
} from 'vue' | ||
|
||
import { | ||
useEventSource | ||
} from '@vueuse/core' | ||
|
||
import { | ||
UseStatusResponse | ||
} from '../../types' | ||
|
||
export interface UseStatusOptions { | ||
/** | ||
* | ||
* URL/ip:port/host to the remote observatory NAT | ||
* @default 'http://0.0.0.0:8220' | ||
*/ | ||
url: string | ||
} | ||
|
||
export type UseStatusReturn = { | ||
status: Ref<UseStatusResponse> | ||
} | ||
|
||
export interface UseStatusProps extends Partial<UseStatusReturn> {} | ||
|
||
// Default status: | ||
const defaultStatus = { | ||
isOffline: true, | ||
isConnected: false, | ||
isSlewing: false, | ||
isTracking: false, | ||
alt: Infinity, | ||
az: Infinity | ||
} | ||
|
||
/** | ||
* Reactive useTelescopeStatus() | ||
* Returns the reactive status of a PlaneWave Astrograph Telescope | ||
* | ||
* @param options of type UseTelescopeStatusOptions | ||
* @output reactive status | ||
*/ | ||
export const useStatus = ( | ||
options: UseStatusOptions | ||
): UseStatusReturn => { | ||
const { url } = options | ||
|
||
// Connect to the Telescope Status over server-sent events: | ||
const { data, close, error } = useEventSource(`${url}/status`) | ||
|
||
const status = ref<UseStatusResponse>(defaultStatus) | ||
|
||
// Close the event source onUnmounted lifecycle hook: | ||
onUnmounted(() => { | ||
close() | ||
}) | ||
|
||
// Watch for an error signal from the status server-sent event: | ||
watch(error, () => { | ||
if (error.value) { | ||
if (error.value.type === 'error') { | ||
status.value = defaultStatus | ||
} | ||
} | ||
}) | ||
|
||
// Watch for an change of response data from the status server-sent event: | ||
watch(data, () => { | ||
if (data.value) { | ||
status.value = JSON.parse(data.value) | ||
} | ||
}) | ||
|
||
return { | ||
status | ||
} | ||
} |
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