Skip to content

Commit 2a07c23

Browse files
author
Christopher J Baker
committed
move into namespace
1 parent 4de034a commit 2a07c23

File tree

10 files changed

+147
-45
lines changed

10 files changed

+147
-45
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/pubsub/package.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@r2wc/pubsub",
3+
"version": "0.1.0",
4+
"description": "Easily integrate Emotion with R2WC.",
5+
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
6+
"author": "Bitovi",
7+
"license": "MIT",
8+
"keywords": [
9+
"React",
10+
"Web Component",
11+
"Emotion"
12+
],
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/bitovi/react-to-web-component.git"
16+
},
17+
"type": "module",
18+
"main": "./dist/pubsub.cjs",
19+
"module": "./dist/pubsub.js",
20+
"types": "./dist/pubsub.d.ts",
21+
"exports": {
22+
".": {
23+
"require": "./dist/pubsub.cjs",
24+
"import": "./dist/pubsub.js",
25+
"types": "./dist/pubsub.d.ts"
26+
},
27+
"./package.json": "./package.json"
28+
},
29+
"files": [
30+
"dist",
31+
"src"
32+
],
33+
"scripts": {
34+
"typecheck": "tsc --noEmit",
35+
"eslint": "eslint .",
36+
"prettier": "prettier --check .",
37+
"depcheck": "depcheck .",
38+
"dev": "vite",
39+
"test": "vitest",
40+
"test:ci": "vitest run",
41+
"test:coverage": "vitest run --coverage",
42+
"clean": "rm -rf tsconfig.tsbuildinfo dist",
43+
"build": "vite build"
44+
},
45+
"devDependencies": {
46+
"@types/react": "^18.0.0",
47+
"@types/react-dom": "^18.0.0"
48+
},
49+
"peerDependencies": {
50+
"react": "^18.0.0",
51+
"react-dom": "^18.0.0"
52+
}
53+
}

packages/pubsub/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./pubsub"
2+
export * from "./usePubSub"
3+
export * from "./usePubSubState"
File renamed without changes.
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { useEffect, useCallback, useRef } from "react";
1+
import { useEffect, useCallback, useRef } from "react"
22

3-
import { BaseMessage, publish, subscribe } from "./pub-sub";
3+
import { BaseMessage, publish, subscribe } from "./pubsub"
44

5-
const _internalTopicStabilizer = "-||-||-";
5+
const _internalTopicStabilizer = "-||-||-"
66

77
export const usePubSub = <TMessage extends BaseMessage>(
88
topics: string[],
9-
handler?: (data: TMessage) => void
9+
handler?: (data: TMessage) => void,
1010
): ((data: TMessage) => void) => {
11-
const stableHandler = useRef(handler);
11+
const stableHandler = useRef(handler)
1212

1313
// eslint-disable-next-line react-hooks/exhaustive-deps
1414
useEffect(
1515
() => subscribe(topics, stableHandler?.current),
16-
[topics.join(_internalTopicStabilizer)]
17-
);
16+
[topics.join(_internalTopicStabilizer)],
17+
)
1818

1919
// eslint-disable-next-line react-hooks/exhaustive-deps
2020
return useCallback(
2121
(data: TMessage) => publish(topics, data),
22-
[topics.join(_internalTopicStabilizer)]
23-
);
24-
};
22+
[topics.join(_internalTopicStabilizer)],
23+
)
24+
}

packages/pubsub/src/usePubSubState.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useState, useCallback } from "react"
2+
3+
import { BaseMessage } from "./pubsub"
4+
import { usePubSub } from "./usePubSub"
5+
6+
export const usePubSubState = <TMessage extends BaseMessage>(
7+
topics: string[],
8+
initialValue?: TMessage,
9+
filter?: (data: TMessage) => boolean,
10+
): [TMessage | undefined, (data: TMessage) => void] => {
11+
const [state, setState] = useState(initialValue)
12+
13+
const filteredSetter = useCallback(
14+
(data: TMessage) => {
15+
if (!filter) {
16+
setState(data)
17+
return
18+
}
19+
20+
if (filter(data)) {
21+
setState(data)
22+
return
23+
}
24+
},
25+
[filter],
26+
)
27+
28+
const publish = usePubSub(topics, filteredSetter)
29+
30+
return [state, publish]
31+
}

packages/pubsub/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.react.json",
3+
"include": ["src"]
4+
}

packages/pubsub/vite.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference types="vitest" />
2+
import react from "@vitejs/plugin-react"
3+
import { defineConfig } from "vite"
4+
import dts from "vite-plugin-dts"
5+
6+
// https://vitejs.dev/config/
7+
export default defineConfig({
8+
build: {
9+
lib: {
10+
entry: "src/pubsub.tsx",
11+
formats: ["es", "cjs"],
12+
},
13+
rollupOptions: {
14+
external: ["react", "react/jsx-runtime"],
15+
},
16+
},
17+
plugins: [
18+
dts({
19+
exclude: ["src/**/*.test.*"],
20+
rollupTypes: true,
21+
}),
22+
react(),
23+
],
24+
test: {
25+
environment: "jsdom",
26+
restoreMocks: true,
27+
mockReset: true,
28+
},
29+
})

pub-sub/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

pub-sub/usePubSubState.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)