-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
999316f
commit cfb0f4a
Showing
4 changed files
with
122 additions
and
2 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
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,43 @@ | ||
import { CallbackRateLimiter } from './callback-rate-limiter' | ||
|
||
/** | ||
* Current version of the Cockpit Widget API | ||
*/ | ||
export const COCKPIT_WIDGET_API_VERSION = '0.0.0' | ||
|
||
/** | ||
* Listens to updates for a specific datalake variable. | ||
* This function sets up a message listener that receives updates from the parent window | ||
* and forwards them to the callback function, respecting the specified rate limit. | ||
* @param {string} variable - The name of the datalake variable to listen to | ||
* @param {Function} callback - The function to call when the variable is updated | ||
* @param {number} maxRateHz - The maximum rate (in Hz) at which updates should be received. Default is 10 Hz | ||
* @example | ||
* ```typescript | ||
* // Listen to updates at 5Hz | ||
* listenToDatalakeVariable('temperature', (value) => { | ||
* console.log('Temperature:', value); | ||
* }, 5); | ||
* ``` | ||
*/ | ||
export function listenToDatalakeVariable(variable: string, callback: (data: any) => void, maxRateHz = 10): void { | ||
// Convert Hz to minimum interval in milliseconds | ||
const minIntervalMs = 1000 / maxRateHz | ||
const rateLimiter = new CallbackRateLimiter(minIntervalMs) | ||
|
||
const message = { | ||
type: 'cockpit:listenToDatalakeVariables', | ||
variable: variable, | ||
maxRateHz: maxRateHz, | ||
} | ||
window.parent.postMessage(message, '*') | ||
|
||
window.addEventListener('message', function handler(event) { | ||
if (event.data.type === 'cockpit:datalakeVariable' && event.data.variable === variable) { | ||
// Only call callback if we haven't exceeded the rate limit | ||
if (rateLimiter.canCall(variable)) { | ||
callback(event.data.value) | ||
} | ||
} | ||
}) | ||
} |
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,30 @@ | ||
/** | ||
* A simple rate limiter for callbacks that ensures a minimum time interval between calls | ||
*/ | ||
export class CallbackRateLimiter { | ||
private lastCallTimes = new Map<string, number>() | ||
|
||
/** | ||
* Creates a new CallbackRateLimiter | ||
* @param {number} minIntervalMs - The minimum time (in milliseconds) that must pass between calls | ||
*/ | ||
constructor(private minIntervalMs: number) {} | ||
|
||
/** | ||
* Checks if enough time has passed to allow another call | ||
* @param {string} key - Unique identifier for the callback being rate limited | ||
* @returns {boolean} true if enough time has passed since the last call, false otherwise | ||
*/ | ||
public canCall(key: string): boolean { | ||
const now = Date.now() | ||
const lastCall = this.lastCallTimes.get(key) || 0 | ||
const timeSinceLastCall = now - lastCall | ||
|
||
if (timeSinceLastCall >= this.minIntervalMs) { | ||
this.lastCallTimes.set(key, now) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} |
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