Fixes multiple client connection data freshness.
This commit is contained in:
@@ -3,8 +3,7 @@ import { getServiceRecord } from '$lib/services/services';
|
||||
import { readable } from 'svelte/store';
|
||||
import { serverConfig } from './config';
|
||||
import { rootLogger } from './logger';
|
||||
|
||||
export type MayAsyncServiceData = Partial<ServiceData> | Promise<Partial<ServiceData>>;
|
||||
import type { Unsubscriber } from 'svelte/motion';
|
||||
|
||||
const logger = rootLogger.child({ name: 'ServiceData' });
|
||||
|
||||
@@ -14,15 +13,22 @@ export interface ServiceDataEvent {
|
||||
data: ServiceData;
|
||||
}
|
||||
|
||||
export const serviceDataEvents = readable<ServiceDataEvent>(undefined, (set) => {
|
||||
export function subscribeToDataEvent(onEvent: (event: ServiceDataEvent) => void): Unsubscriber {
|
||||
for (const [group, items] of serviceData.entries()) {
|
||||
for (const [item, data] of items.entries()) {
|
||||
onEvent({ group, item, data: data as ServiceData });
|
||||
}
|
||||
}
|
||||
return serviceDataEvents.subscribe(onEvent);
|
||||
}
|
||||
|
||||
const serviceDataEvents = readable<ServiceDataEvent>(undefined, (set) => {
|
||||
logger.debug({ interval: pollInterval }, 'starting polling loop');
|
||||
|
||||
pushServiceData = set;
|
||||
|
||||
if (serviceData.length == 0) {
|
||||
logger.debug('polling for initial state');
|
||||
pollAllServices();
|
||||
}
|
||||
logger.debug('polling for initial state');
|
||||
pollAllServices();
|
||||
const timer = setInterval(() => {
|
||||
logger.debug('polling for service update');
|
||||
pollAllServices();
|
||||
@@ -47,9 +53,9 @@ let pushServiceData: ((value: ServiceDataEvent) => void) | undefined = undefined
|
||||
|
||||
const pollInterval = 30000;
|
||||
|
||||
let serviceData: Array<Array<MayAsyncServiceData>> = [];
|
||||
let serviceData: Array<Array<Partial<ServiceData>>> = [];
|
||||
|
||||
function pollAllServices(): Array<Array<MayAsyncServiceData>> {
|
||||
function pollAllServices() {
|
||||
logger.trace('pollAllServices()');
|
||||
const config = serverConfig();
|
||||
|
||||
@@ -61,7 +67,8 @@ function pollAllServices(): Array<Array<MayAsyncServiceData>> {
|
||||
for (const [idxItem, service] of group.items.entries()) {
|
||||
const poller: ServicePoller = getServiceRecord(service.type || '').poll;
|
||||
const serviceLogger = logger.child({ group: idxGroup, item: idxItem, service });
|
||||
serviceData[idxGroup][idxItem] = poller(service)
|
||||
serviceData[idxGroup][idxItem] = {};
|
||||
poller(service)
|
||||
.then((data: ServiceData) => {
|
||||
serviceLogger.trace({ data }, 'pollAllService:result()');
|
||||
serviceData[idxGroup][idxItem] = data;
|
||||
@@ -76,6 +83,4 @@ function pollAllServices(): Array<Array<MayAsyncServiceData>> {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return serviceData;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { serviceDataEvents } from '$lib/server/dataPolling';
|
||||
import { requestLogger, rootLogger } from '$lib/server/logger';
|
||||
import { subscribeToDataEvent } from '$lib/server/dataPolling';
|
||||
import { requestLogger } from '$lib/server/logger';
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import type { Unsubscriber } from 'svelte/store';
|
||||
|
||||
@@ -17,7 +17,7 @@ export const GET: RequestHandler = ({ getClientAddress }) => {
|
||||
start: (controller) => {
|
||||
logger.info('new update request stream started');
|
||||
|
||||
unsubscribe = serviceDataEvents.subscribe((event) => {
|
||||
unsubscribe = subscribeToDataEvent((event) => {
|
||||
logger.trace({ event: event }, 'sending new event');
|
||||
const data = `event:message\ndata:${JSON.stringify(event)}\n\n`;
|
||||
controller.enqueue(data);
|
||||
|
||||
Reference in New Issue
Block a user