feat: add deployment reload controls
This commit is contained in:
+21
-2
@@ -19,8 +19,16 @@ type SubscriptionOptions = {
|
||||
onopen?: (event: Event) => void;
|
||||
};
|
||||
|
||||
type StreamOptions = {
|
||||
type StreamMessage<T extends EventMap> = {
|
||||
[K in Extract<keyof T, string>]: {
|
||||
event: K;
|
||||
data: z.input<T[K]>;
|
||||
};
|
||||
}[Extract<keyof T, string>];
|
||||
|
||||
type StreamOptions<T extends EventMap> = {
|
||||
signal?: AbortSignal;
|
||||
motd?: StreamMessage<T> | StreamMessage<T>[];
|
||||
};
|
||||
|
||||
const redisEnvelopeSchema = z.object({
|
||||
@@ -154,7 +162,11 @@ export class SseEndpoint<T extends EventMap> {
|
||||
}
|
||||
}
|
||||
|
||||
async stream({ signal }: StreamOptions = {}) {
|
||||
async stream({ signal, motd }: StreamOptions<T> = {}) {
|
||||
const initialMessages = (Array.isArray(motd) ? motd : motd ? [motd] : [])
|
||||
.map(({ event, data }) =>
|
||||
encodeEvent(event, this.events[event].parse(data)),
|
||||
);
|
||||
const subscriber = await getRedisSubscriber();
|
||||
const topic = this.topic;
|
||||
const channel = channelName(this.topic);
|
||||
@@ -223,6 +235,7 @@ export class SseEndpoint<T extends EventMap> {
|
||||
start(streamController) {
|
||||
controller = streamController;
|
||||
write(`retry: ${DEFAULT_RETRY_MS}\n\n: connected ${topic}\n\n`);
|
||||
for (const message of initialMessages) write(message);
|
||||
|
||||
receive = handleMessage;
|
||||
for (const message of pendingMessages.splice(0)) {
|
||||
@@ -287,6 +300,12 @@ export function createSseEndpoints<
|
||||
const updateActionSchema = z.enum(["created", "updated", "deleted"]);
|
||||
|
||||
export const sse = createSseEndpoints({
|
||||
active: {
|
||||
events: {
|
||||
version: z.string().min(1),
|
||||
refresh: z.string().min(1).nullable(),
|
||||
},
|
||||
},
|
||||
forms: {
|
||||
events: {
|
||||
update: z.object({
|
||||
|
||||
Reference in New Issue
Block a user