Skip to content

Commit

Permalink
fix: Delta typing
Browse files Browse the repository at this point in the history
Fix Delta typing to allow meta and values in updates at the
same time.
  • Loading branch information
tkurki committed Sep 20, 2024
1 parent cf2e365 commit d9f8d2b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
59 changes: 59 additions & 0 deletions packages/server-api/src/deltas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Delta, Path } from './deltas'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const metaDelta: Delta = {
updates: [
{
meta: [
{
path: 'foo.bar' as Path,
value: {
displayName: 'Foo Bar'
}
}
]
}
]
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const valuesDelta: Delta = {
updates: [
{
values: [
{
path: 'foo.bar' as Path,
value: {
displayName: 'Foo Bar'
}
}
]
}
]
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const combinedDelta: Delta = {
updates: [
{
meta: [
{
path: 'foo.bar' as Path,
value: {
displayName: 'Foo Bar'
}
}
]
},
{
values: [
{
path: 'foo.bar' as Path,
value: {
displayName: 'Foo Bar'
}
}
]
}
]
}
35 changes: 23 additions & 12 deletions packages/server-api/src/deltas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ export interface DeltaSubscription {
}>
}

// "Classic" Delta with values
export interface ValuesDelta {
export interface Delta {
context?: Context
updates: Update[]
}

export interface MetaDelta {
metas: Array<{ values: Meta[] }>
}

// Delta Message
export type Delta = ValuesDelta | MetaDelta
/**
* @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate
*/
export type ValuesDelta = Delta
/**
* @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate
*/
export type MetaDelta = Delta

export interface Update {
export type Update = {
timestamp?: Timestamp
source?: Source
$source?: SourceRef
values: PathValue[]
}
} & ({ values: PathValue[] } | { meta: Meta[] }) // require either values or meta or both

// Update delta
export interface PathValue {
Expand All @@ -67,7 +67,7 @@ export interface Notification {

// MetaMessage
export interface Meta {
path: string
path: Path
value: MetaValue
}

Expand All @@ -76,6 +76,17 @@ export interface MetaValue {
description?: string
units?: string
example?: string
timeout?: number
displayName?: string
displayScale?: {
lower: number
upper: number
}
zones?: {
upper: number
lower: number
state: string
}[]
}

// Notification attribute types
Expand Down
3 changes: 2 additions & 1 deletion packages/server-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
},
"ts-node": {
"files": true
}
},
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit d9f8d2b

Please sign in to comment.