Set up project structure, source files, and architecture docs
- Copy core source from alkhub_ts/packages/core/pubsub/ with import path fixups (typed_event_target.ts → types.ts, .ts → .js extensions) - Make PubSubPublishArgsByKey exported (was private type, needed by barrel) - Add package.json with sub-path exports and optional peer deps (ioredis) - Add tsup.config.ts with multi-entry + splitting for tree-shaking - Add tsconfig.json, vitest.config.ts, .gitignore - Add AGENTS.md with project conventions and adapter checklist - Add architecture docs following taskgraph/alkhub pattern: docs/architecture/README.md, api-surface.md, event-targets.md, iroh-transport.md, build-distribution.md - Add ADRs: 001-graphql-yoga-fork, 002-tree-shake-pattern - Copy migration research doc to docs/research/migration.md - Dual-license MIT OR Apache-2.0 (matching taskgraph)
This commit is contained in:
117
src/event-target-redis.ts
Normal file
117
src/event-target-redis.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Adapted from @graphql-yoga/redis-event-target
|
||||
* Original source: https://github.com/graphql-hive/graphql-yoga
|
||||
* License: MIT
|
||||
*
|
||||
* Copyright (c) 2024 The Guild, GraphQL Yoga Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* Changes from original:
|
||||
* - Uses native CustomEvent instead of @whatwg-node/events ponyfill (Deno has CustomEvent)
|
||||
* - Uses our TypedEventTarget/TypedEvent types from types.ts
|
||||
* - Removed tslib dependency
|
||||
* - Uses ioredis types directly (already a dependency)
|
||||
*/
|
||||
|
||||
import type { Cluster, Redis } from "ioredis";
|
||||
import type { TypedEventTarget, TypedEvent } from "./types.js";
|
||||
|
||||
export type CreateRedisEventTargetArgs = {
|
||||
publishClient: Redis | Cluster;
|
||||
subscribeClient: Redis | Cluster;
|
||||
serializer?: {
|
||||
stringify: (message: unknown) => string;
|
||||
parse: (message: string) => unknown;
|
||||
};
|
||||
};
|
||||
|
||||
export function createRedisEventTarget<TEvent extends TypedEvent>(
|
||||
args: CreateRedisEventTargetArgs,
|
||||
): TypedEventTarget<TEvent> {
|
||||
const { publishClient, subscribeClient } = args;
|
||||
|
||||
const serializer = args.serializer ?? JSON;
|
||||
|
||||
const callbacksForTopic = new Map<string, Set<EventListener>>();
|
||||
|
||||
function onMessage(channel: string, message: string) {
|
||||
const callbacks = callbacksForTopic.get(channel);
|
||||
if (callbacks === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const event = new CustomEvent(channel, {
|
||||
detail: message === "" ? null : serializer.parse(message),
|
||||
}) as TEvent;
|
||||
for (const callback of callbacks) {
|
||||
callback(event);
|
||||
}
|
||||
}
|
||||
|
||||
(subscribeClient as Redis).on("message", onMessage);
|
||||
|
||||
function addCallback(topic: string, callback: EventListener) {
|
||||
let callbacks = callbacksForTopic.get(topic);
|
||||
if (callbacks === undefined) {
|
||||
callbacks = new Set();
|
||||
callbacksForTopic.set(topic, callbacks);
|
||||
|
||||
subscribeClient.subscribe(topic);
|
||||
}
|
||||
callbacks.add(callback);
|
||||
}
|
||||
|
||||
function removeCallback(topic: string, callback: EventListener) {
|
||||
const callbacks = callbacksForTopic.get(topic);
|
||||
if (callbacks === undefined) {
|
||||
return;
|
||||
}
|
||||
callbacks.delete(callback);
|
||||
if (callbacks.size > 0) {
|
||||
return;
|
||||
}
|
||||
callbacksForTopic.delete(topic);
|
||||
subscribeClient.unsubscribe(topic);
|
||||
}
|
||||
|
||||
return {
|
||||
addEventListener(topic, callbackOrOptions: EventListenerOrEventListenerObject) {
|
||||
if (callbackOrOptions != null) {
|
||||
const callback =
|
||||
"handleEvent" in callbackOrOptions ? callbackOrOptions.handleEvent : callbackOrOptions;
|
||||
addCallback(topic, callback);
|
||||
}
|
||||
},
|
||||
dispatchEvent(event: TEvent) {
|
||||
publishClient.publish(
|
||||
event.type,
|
||||
event.detail === undefined ? "" : serializer.stringify(event.detail),
|
||||
);
|
||||
return true;
|
||||
},
|
||||
removeEventListener(topic, callbackOrOptions: EventListenerOrEventListenerObject) {
|
||||
if (callbackOrOptions != null) {
|
||||
const callback =
|
||||
"handleEvent" in callbackOrOptions ? callbackOrOptions.handleEvent : callbackOrOptions;
|
||||
removeCallback(topic, callback);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user