Skip to content

Commit

Permalink
refactor: expose a semantically sound getting serpenta context function
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakrok09 committed Aug 26, 2024
1 parent 003080f commit ee5d3f9
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 55 deletions.
12 changes: 3 additions & 9 deletions Writerside/topics/Configuration-Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ All Serpenta components shall get data that shall be configurable from this cont
```html
<!-- Component.svelte -->
<script lang="ts">
import { getContext } from "svelte";
import type { FinalizedConfig }
from "$lib/appShell/SerpentaConfig";
const config = getContext<FinalizedConfig>("serpenta-config");
import { getSerpentaContext } from "$lib";
const context = getSerpentaContext();
</script>
```

The config itself has a describing interface:

```Typescript
const
```
2 changes: 1 addition & 1 deletion packages/serpenta/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@delft-hyperloop/serpenta",
"version": "0.3.0",
"version": "0.3.1",
"private": false,
"description": "UI Component Library of Delft Hyperloop's Serpenta GUI Standard.",
"keywords": [
Expand Down
13 changes: 11 additions & 2 deletions packages/serpenta/src/lib/appShell/SerpentaConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writable, type Writable } from "svelte/store";
import { ErrorStatus, PlotBuffer } from "$lib";
import type { DataDistributor, WindowEngine, CommandInvocation } from "$lib";
import { getContext } from "svelte";

/**
* Serpenta configuration object
Expand All @@ -23,7 +24,7 @@ export interface SerpentaConfig {
};
}

export interface FinalizedConfig {
export interface FinalizedContext {
appWindow: any;
pod_name: string;

Expand All @@ -41,7 +42,7 @@ export interface FinalizedConfig {
};
}

export function defineConfig(config: SerpentaConfig): FinalizedConfig {
export function defineConfig(config: SerpentaConfig): FinalizedContext {
return {
appWindow: config.appWindow,
pod_name: config.pod_name,
Expand All @@ -60,3 +61,11 @@ export function defineConfig(config: SerpentaConfig): FinalizedConfig {
}
};
}

/**
* Get the Serpenta context from within the `<SerpentaShell>` base component.
* @note this will return null if not called within the serpenta shell.
*/
export function getSerpentaContext(): FinalizedContext {
return getContext<FinalizedContext>("serpenta-context");
}
6 changes: 3 additions & 3 deletions packages/serpenta/src/lib/appShell/SerpentaShell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { initializeStores, Modal, Toast } from "@skeletonlabs/skeleton";
import { BottomBar, TitleBar } from "$lib";
import { onDestroy, setContext } from "svelte";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import type { FinalizedContext } from "$lib/appShell/SerpentaConfig";
export let config: FinalizedConfig;
setContext<FinalizedConfig>("serpenta-config", config);
export let config: FinalizedContext;
setContext<FinalizedContext>("serpenta-context", config);
config.grand_data_distributor.start(50);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import { util } from "$lib";
import { getContext } from "svelte";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { getSerpentaContext, util } from "$lib";
export let className: string = "";
export let cmd: string;
Expand All @@ -11,11 +9,11 @@
export let errorCallback: (error: string) => void = () => {};
export let textOverride: string = "";
const config = getContext<FinalizedConfig>("serpenta-config");
const commandInvoker = config.command_invocation;
const context = getSerpentaContext();
const commandInvoker = context.command_invocation;
let send = async () => {
await commandInvoker.invokeCommand<void>(config.generic_command_name, { cmdName: cmd, val })
await commandInvoker.invokeCommand<void>(context.generic_command_name, { cmdName: cmd, val })
.then(returned => {
console.log(`Command ${cmd} sent with val: ${val}`);
successCallback(returned);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import { util } from "$lib";
import { getContext } from "svelte";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { getSerpentaContext, util } from "$lib";
export let className: string = "";
export let cmd: string;
Expand All @@ -11,8 +9,7 @@
export let errorCallback: (error: string) => void = () => {};
export let textOverride: string = "";
const config = getContext<FinalizedConfig>("serpenta-config");
const commandInvoker = config.command_invocation;
const commandInvoker = getSerpentaContext().command_invocation;
export let send = async () => {
console.log(`Sending command: ${cmd} with options ${options}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { getContext } from "svelte";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { getSerpentaContext } from "$lib/appShell/SerpentaConfig";
export let offCmd: string;
export let onCmd: string;
Expand All @@ -11,8 +10,7 @@
// for binding
export let status: boolean = false;
const config = getContext<FinalizedConfig>("serpenta-config");
const commandInvoker = config.command_invocation;
const commandInvoker = getSerpentaContext().command_invocation;
const toggleOff = () => {
commandInvoker.invokeCommand("send_command", { cmdName: offCmd, val }).then(r => {
Expand Down
11 changes: 5 additions & 6 deletions packages/serpenta/src/lib/components/data/Chart.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script lang="ts">
import { getContext, onDestroy, onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import "uplot/dist/uPlot.min.css";
import { PlotBuffer, PopupIcon } from "$lib";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { getSerpentaContext, PlotBuffer, PopupIcon } from "$lib";
const config: FinalizedConfig = getContext<FinalizedConfig>("serpenta-config");
const grandCharter = config.grand_charter;
const windowEngine = config.window_engine;
const context = getSerpentaContext();
const grandCharter = context.grand_charter;
const windowEngine = context.window_engine;
function popoutWindow() {
windowEngine.spawnWindow(title, `/view/chart/${title}`);
Expand Down
9 changes: 4 additions & 5 deletions packages/serpenta/src/lib/components/data/Store.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script lang="ts">
import { getContext } from "svelte";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { getSerpentaContext } from "$lib/appShell/SerpentaConfig";
import { type DataDistributor } from "$lib";
const config: FinalizedConfig = getContext<FinalizedConfig>("serpenta-config");
const latestTimestamp = config.latest_timestamp;
const gdd: DataDistributor = config.grand_data_distributor;
const context = getSerpentaContext();
const latestTimestamp = context.latest_timestamp;
const gdd: DataDistributor = context.grand_data_distributor;
const STALE_DATA_TICKS = 10_000;
export let datatype: string;
Expand Down
8 changes: 3 additions & 5 deletions packages/serpenta/src/lib/components/graphic/FSM.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<script lang="ts">
import { getContext, onDestroy, onMount } from "svelte";
import { type DataDistributor } from "$lib";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
import { onDestroy, onMount } from "svelte";
import { getSerpentaContext } from "$lib";
const config: FinalizedConfig = getContext<FinalizedConfig>("serpenta-config");
const gdd: DataDistributor = config.grand_data_distributor;
const gdd = getSerpentaContext().grand_data_distributor;
let boot_state: SVGGElement;
let est_con_state: SVGGElement;
Expand Down
15 changes: 7 additions & 8 deletions packages/serpenta/src/lib/components/page/BottomBar.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<script lang="ts">
import { getContext, onMount } from "svelte";
import { ErrorStatus, type DataDistributor } from "$lib";
import { onMount } from "svelte";
import { ErrorStatus, type DataDistributor, getSerpentaContext } from "$lib";
import type { Writable } from "svelte/store";
import type { FinalizedConfig } from "$lib/appShell/SerpentaConfig";
const config: FinalizedConfig = getContext<FinalizedConfig>("serpenta-config");
const gdd: DataDistributor = config.grand_data_distributor;
const bigErrorStatus: Writable<ErrorStatus> = config.big_error;
const fsmStateName: string = config.stores.fsm_name;
const podName: string = config.pod_name;
const context = getSerpentaContext();
const gdd: DataDistributor = context.grand_data_distributor;
const bigErrorStatus: Writable<ErrorStatus> = context.big_error;
const fsmStateName: string = context.stores.fsm_name;
const podName: string = context.pod_name;
let time = new Date().toLocaleTimeString([], {
hour: "2-digit",
Expand Down
3 changes: 2 additions & 1 deletion packages/serpenta/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PlotBuffer, StrokePresets } from "$lib/PlotBuffer";
import type { DataDistributor } from "$lib/middleware/DataDistributor";
import type { WindowEngine } from "$lib/middleware/WindowControl";
import util from "$lib/util";
import { defineConfig, type SerpentaConfig } from "$lib/appShell/SerpentaConfig";
import { defineConfig, getSerpentaContext, type SerpentaConfig } from "$lib/appShell/SerpentaConfig";
import type { CommandInvocation } from "./middleware/CommandInvocation";

/**
Expand Down Expand Up @@ -56,6 +56,7 @@ export {
util,
SerpentaShell,
defineConfig,
getSerpentaContext,
StrokePresets,
PopupIcon
};
Expand Down

0 comments on commit ee5d3f9

Please sign in to comment.