From af3d1b51f9e504f6be4cfc2958458cfb7c049633 Mon Sep 17 00:00:00 2001 From: Christopher J Baker Date: Fri, 12 Jul 2024 14:33:35 -0700 Subject: [PATCH] remove pub-sub --- pub-sub/index.ts | 3 -- pub-sub/pub-sub.ts | 72 --------------------------------------- pub-sub/usePubSub.ts | 24 ------------- pub-sub/usePubSubState.ts | 32 ----------------- 4 files changed, 131 deletions(-) delete mode 100644 pub-sub/index.ts delete mode 100644 pub-sub/pub-sub.ts delete mode 100644 pub-sub/usePubSub.ts delete mode 100644 pub-sub/usePubSubState.ts diff --git a/pub-sub/index.ts b/pub-sub/index.ts deleted file mode 100644 index baa7df4..0000000 --- a/pub-sub/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./pub-sub"; -export * from "./usePubSub"; -export * from "./usePubSubState"; diff --git a/pub-sub/pub-sub.ts b/pub-sub/pub-sub.ts deleted file mode 100644 index 63126ec..0000000 --- a/pub-sub/pub-sub.ts +++ /dev/null @@ -1,72 +0,0 @@ -type ChannelListeners = { __listeners: Array> } -type ChannelFragments = { [topic: string]: Channel | undefined } -type Channel = ChannelListeners & ChannelFragments - -type Unsubscribe = () => void - -export type Topic = string[] -export type Listener = (message: Message) => void -export type BaseMessage = { type: string } - -const symbol = Symbol.for("r2wc.pubsub") - -declare global { - interface Window { - [symbol]: Channel - } -} - -function createChannel(): Channel { - return { __listeners: [] } as unknown as Channel -} - -export const subscribe = ( - topic: Topic, - listener?: Listener, -): Unsubscribe | undefined => { - if (!listener) return - - if (!window[symbol]) { - window[symbol] = createChannel() - } - - let channel: Channel = window[symbol] - - for (const fragment of topic) { - if (!channel[fragment]) channel[fragment] = createChannel() - channel = channel[fragment] as Channel - } - - channel.__listeners.push(listener as Listener) - - return () => { - const index = channel.__listeners.indexOf(listener as Listener) - if (index > -1) { - channel.__listeners.splice(index, 1) - } - } -} - -export const publish = ( - topic: Topic, - message: Message, -): void => { - if (!window[symbol]) { - window[symbol] = createChannel() - } - - const listeners: Array> = [] - - let channel: Channel = window[symbol] - - for (const fragment of topic) { - listeners.unshift(...channel.__listeners) - - if (!channel[fragment]) channel[fragment] = createChannel() - - channel = channel[fragment] as Channel - } - - channel.__listeners.forEach((subscriber) => subscriber(message)) - listeners.forEach((subscriber) => subscriber(message)) -} diff --git a/pub-sub/usePubSub.ts b/pub-sub/usePubSub.ts deleted file mode 100644 index 3aada93..0000000 --- a/pub-sub/usePubSub.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { useEffect, useCallback, useRef } from "react"; - -import { BaseMessage, publish, subscribe } from "./pub-sub"; - -const _internalTopicStabilizer = "-||-||-"; - -export const usePubSub = ( - topics: string[], - handler?: (data: TMessage) => void -): ((data: TMessage) => void) => { - const stableHandler = useRef(handler); - - // eslint-disable-next-line react-hooks/exhaustive-deps - useEffect( - () => subscribe(topics, stableHandler?.current), - [topics.join(_internalTopicStabilizer)] - ); - - // eslint-disable-next-line react-hooks/exhaustive-deps - return useCallback( - (data: TMessage) => publish(topics, data), - [topics.join(_internalTopicStabilizer)] - ); -}; diff --git a/pub-sub/usePubSubState.ts b/pub-sub/usePubSubState.ts deleted file mode 100644 index 4c3e85e..0000000 --- a/pub-sub/usePubSubState.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { BaseMessage } from "./pub-sub"; - -import { useState, useCallback } from "react"; - -import { usePubSub } from "./usePubSub"; - -export const usePubSubState = ( - topics: string[], - initialValue?: TMessage, - filter?: (data: TMessage) => boolean -): [TMessage | undefined, (data: TMessage) => void] => { - const [state, setState] = useState(initialValue); - - const filteredSetter = useCallback( - (data: TMessage) => { - if (!filter) { - setState(data); - return; - } - - if (filter(data)) { - setState(data); - return; - } - }, - [filter] - ); - - const publish = usePubSub(topics, filteredSetter); - - return [state, publish]; -};