@event-chat/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 快乐的小萌新
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Rslib project
2
+
3
+ ## Setup
4
+
5
+ Install the dependencies:
6
+
7
+ ```bash
8
+ npm install
9
+ ```
10
+
11
+ ## Get started
12
+
13
+ Build the library:
14
+
15
+ ```bash
16
+ npm run build
17
+ ```
18
+
19
+ Build the library in watch mode:
20
+
21
+ ```bash
22
+ npm run dev
23
+ ```
@@ -0,0 +1,14 @@
1
+ import { EventDetailType } from './utils';
2
+ declare class EventBus {
3
+ private events;
4
+ constructor();
5
+ clear(): void;
6
+ emit(eventName: string, args: EventDetailType): void;
7
+ has(eventName: string, callback?: (args: EventDetailType) => void): boolean | undefined;
8
+ off(eventName: string, callback?: (args: EventDetailType) => void): void;
9
+ on(eventName: string, callback: (data: EventDetailType) => void): void;
10
+ once(eventName: string, callback: (data: EventDetailType) => void): void;
11
+ }
12
+ declare const _default: EventBus;
13
+ export default _default;
14
+ //# sourceMappingURL=eventBus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventBus.d.ts","sourceRoot":"","sources":["../src/eventBus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,cAAM,QAAQ;IACZ,OAAO,CAAC,MAAM,CAAkE;;IAKhF,KAAK,IAAI,IAAI;IAIb,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe;IAM7C,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI;IAOjE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI;IAajE,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI;IAO/D,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI;CAOlE;;AAED,wBAA8B"}
@@ -0,0 +1,36 @@
1
+ class EventBus {
2
+ events;
3
+ constructor(){
4
+ this.events = {};
5
+ }
6
+ clear() {
7
+ this.events = {};
8
+ }
9
+ emit(eventName, args) {
10
+ this.events[eventName]?.forEach((callback)=>{
11
+ callback(args);
12
+ });
13
+ }
14
+ has(eventName, callback) {
15
+ return (this.events[eventName] ? void 0 : false) ?? (callback ? this.events[eventName]?.includes(callback) : true);
16
+ }
17
+ off(eventName, callback) {
18
+ if (!Array.isArray(this.events[eventName])) return;
19
+ if (callback) this.events[eventName] = this.events[eventName].filter((cb)=>cb !== callback);
20
+ else this.events[eventName] = [];
21
+ if (0 === this.events[eventName].length) Reflect.deleteProperty(this.events, eventName);
22
+ }
23
+ on(eventName, callback) {
24
+ this.events[eventName] ??= [];
25
+ if (!this.events[eventName].includes(callback)) this.events[eventName].push(callback);
26
+ }
27
+ once(eventName, callback) {
28
+ const wrapper = (args)=>{
29
+ callback(args);
30
+ this.off(eventName, wrapper);
31
+ };
32
+ this.on(eventName, wrapper);
33
+ }
34
+ }
35
+ const eventBus = new EventBus();
36
+ export { eventBus as default };
@@ -0,0 +1,8 @@
1
+ import { ZodType } from 'zod';
2
+ import { EventChatOptions, EventDetailType, NamepathType } from './utils';
3
+ export declare const useMemoFn: <T>(fn: T) => import("react").MutableRefObject<T>;
4
+ export declare function useEventChat<Name extends NamepathType, Schema extends ZodType | undefined = undefined, Group extends string | undefined = undefined, Type extends string | undefined = undefined, Token extends boolean | undefined = undefined>(name: Name, ops?: EventChatOptions<Name, Schema, Group, Type, Token>): Readonly<{
5
+ token: string;
6
+ emit: <Detail, CustomName extends NamepathType>(detail: Omit<EventDetailType<Detail, CustomName>, "group" | "id" | "origin" | "type">) => void;
7
+ }>;
8
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAE9B,OAAO,EACL,gBAAgB,EAChB,eAAe,EAEf,YAAY,EAQb,MAAM,SAAS,CAAC;AAGjB,eAAO,MAAM,SAAS,GAAI,CAAC,EAAE,IAAI,CAAC,wCAOjC,CAAC;AAEF,wBAAgB,YAAY,CAC1B,IAAI,SAAS,YAAY,EACzB,MAAM,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAC9C,KAAK,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC5C,IAAI,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC3C,KAAK,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAC7C,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC;;WA4CjE,MAAM,EAAE,UAAU,SAAS,YAAY,UAC9B,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,QAAQ,GAAG,MAAM,CAAC;GA8B1F"}
package/dist/hooks.js ADDED
@@ -0,0 +1,92 @@
1
+ import { useCallback, useEffect, useId, useMemo, useRef } from "react";
2
+ import eventBus from "./eventBus.js";
3
+ import { EventName, createEvent, createToken, getConditionKey, getEventName, isResultType, isSafetyType, mountEvent } from "./utils.js";
4
+ import { checkLiteral, validate } from "./validate.js";
5
+ const useMemoFn = (fn)=>{
6
+ const methodRef = useRef(fn);
7
+ useEffect(()=>{
8
+ methodRef.current = fn;
9
+ }, [
10
+ fn
11
+ ]);
12
+ return methodRef;
13
+ };
14
+ function useEventChat(name, ops) {
15
+ const eventName = useMemo(()=>getEventName(name), [
16
+ name
17
+ ]);
18
+ const id = useId();
19
+ const token = useMemo(()=>createToken(getConditionKey(eventName, id, ops?.type)), [
20
+ eventName,
21
+ id,
22
+ ops?.type
23
+ ]);
24
+ const nameRc = useMemoFn(name);
25
+ const options = useMemoFn(ops);
26
+ const tokenRc = useMemoFn(token);
27
+ const errorHandle = useCallback((error, data)=>{
28
+ if (error instanceof Error && options.current?.debug) options.current.debug(isResultType(error.cause) ? {
29
+ ...error.cause,
30
+ data
31
+ } : void 0);
32
+ }, [
33
+ options
34
+ ]);
35
+ const callbackHandle = useCallback((data)=>{
36
+ const { name: subName, ...args } = data;
37
+ const opitem = options.current;
38
+ if (!opitem || !isSafetyType(subName, nameRc.current)) return;
39
+ const upRecord = {
40
+ ...args,
41
+ name: subName
42
+ };
43
+ if (void 0 !== opitem.schema) validate(upRecord, {
44
+ ...opitem,
45
+ schema: opitem.schema
46
+ }, tokenRc.current).then(opitem.callback).catch((error)=>errorHandle(error, data.detail));
47
+ else checkLiteral(upRecord, {
48
+ ...opitem,
49
+ schema: void 0
50
+ }, tokenRc.current).then(opitem.callback).catch((error)=>errorHandle(error, data.detail));
51
+ }, [
52
+ nameRc,
53
+ options,
54
+ tokenRc,
55
+ errorHandle
56
+ ]);
57
+ const emit = useCallback((detail)=>{
58
+ const event = createEvent({
59
+ ...detail,
60
+ group: ops?.group,
61
+ origin: nameRc.current,
62
+ type: ops?.type,
63
+ id
64
+ });
65
+ document.body.dispatchEvent(event);
66
+ }, [
67
+ id,
68
+ nameRc,
69
+ ops?.group,
70
+ ops?.type
71
+ ]);
72
+ useEffect(()=>{
73
+ if (!document.body.dataset.globalIsListened) {
74
+ document.body.addEventListener(EventName, mountEvent);
75
+ document.body.dataset.globalIsListened = '1';
76
+ }
77
+ }, []);
78
+ useEffect(()=>{
79
+ if (eventName) eventBus.on(eventName, callbackHandle);
80
+ return ()=>{
81
+ if (eventName) eventBus.off(eventName, callbackHandle);
82
+ };
83
+ }, [
84
+ eventName,
85
+ callbackHandle
86
+ ]);
87
+ return Object.freeze({
88
+ token,
89
+ emit
90
+ });
91
+ }
92
+ export { useEventChat, useMemoFn };
@@ -0,0 +1,4 @@
1
+ export { useEventChat } from './hooks';
2
+ export { createToken } from './utils';
3
+ export type { EventChatOptions, EventDetailType, NamepathType } from './utils';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { useEventChat } from "./hooks.js";
2
+ import { createToken } from "./utils.js";
3
+ export { createToken, useEventChat };
@@ -0,0 +1,50 @@
1
+ import { ZodType, z } from 'zod';
2
+ export declare const EventName = "custom-event-chat-11.18";
3
+ export declare const createEvent: <Detail, Name extends NamepathType = string>(detail: EventDetailType<Detail, Name>) => CustomEvent<EventDetailType<Detail, Name>>;
4
+ export declare const createToken: (key: string) => string;
5
+ export declare const getConditionKey: (name: string, id: string, type?: string) => string;
6
+ export declare const getEventName: (name: NamepathType) => string;
7
+ export declare const isResultType: (data: unknown) => data is ResultType;
8
+ export declare const isSafetyType: <T>(target: unknown, origin: T) => target is T;
9
+ export declare function mountEvent(event: CustomDetailEvent): void;
10
+ export interface EventChatOptions<Name extends NamepathType, Schema extends ZodType | undefined = undefined, Group extends string | undefined = undefined, Type extends string | undefined = undefined, Token extends boolean | undefined = undefined> {
11
+ async?: boolean;
12
+ group?: Group;
13
+ schema?: Schema;
14
+ token?: Token;
15
+ type?: Type;
16
+ callback?: (target: DetailType<Name, Schema, Group, Type, Token>) => void;
17
+ debug?: (result?: ResultType) => void;
18
+ }
19
+ export type DetailType<Name extends NamepathType, Schema extends ZodType | undefined = undefined, Group extends string | undefined = undefined, Type extends string | undefined = undefined, Token extends boolean | undefined = undefined> = {
20
+ id: string;
21
+ name: Name;
22
+ detail: WasProvided<Schema> extends true ? z.output<Exclude<Schema, undefined>> : unknown;
23
+ group: WasProvided<Group> extends true ? Exclude<Group, undefined> : undefined;
24
+ origin: string;
25
+ type: WasProvided<Type> extends true ? Exclude<Type, undefined> : undefined;
26
+ token: Token extends true ? string : undefined;
27
+ global?: boolean;
28
+ };
29
+ export type EventDetailType<Detail = unknown, Name extends NamepathType = NamepathType> = {
30
+ id: string;
31
+ name: Name;
32
+ origin: NamepathType;
33
+ detail?: Detail;
34
+ global?: boolean;
35
+ group?: string;
36
+ type?: string;
37
+ token?: string;
38
+ };
39
+ export type NamepathType = number | string | Array<string | number> | Readonly<Array<string | number>>;
40
+ export type ResultType<Schema = unknown> = Omit<z.ZodSafeParseError<Schema>, 'data'> & {
41
+ data: unknown;
42
+ };
43
+ export type WasProvided<T, Default = undefined> = [
44
+ T
45
+ ] extends [Default] ? false : [T] extends [undefined] ? false : true;
46
+ interface CustomDetailEvent extends Event {
47
+ detail?: EventDetailType;
48
+ }
49
+ export {};
50
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"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"}
package/dist/utils.js ADDED
@@ -0,0 +1,38 @@
1
+ import eventBus from "./eventBus.js";
2
+ const EventName = 'custom-event-chat-11.18';
3
+ const createEvent = (detail)=>new CustomEvent(EventName, {
4
+ bubbles: true,
5
+ cancelable: true,
6
+ detail
7
+ });
8
+ const createToken = (key)=>window.btoa(`${key}:${Math.random()}:${Date.now()}`);
9
+ const getConditionKey = (name, id, type)=>[
10
+ name,
11
+ id,
12
+ type
13
+ ].filter(Boolean).join('-');
14
+ const getEventName = (name)=>{
15
+ const path = Array.isArray(name) ? name : [
16
+ name
17
+ ];
18
+ try {
19
+ return `event-chart_${JSON.stringify(path)}`;
20
+ } catch {
21
+ return "event-chart_[]";
22
+ }
23
+ };
24
+ const isResultType = (data)=>'object' == typeof data && null !== data && 'success' in data && !data.success;
25
+ const isSafetyType = (target, origin)=>{
26
+ if (target && origin && 'object' == typeof origin) try {
27
+ return JSON.stringify(target) === JSON.stringify(origin);
28
+ } catch {
29
+ return false;
30
+ }
31
+ return target === origin;
32
+ };
33
+ function mountEvent(event) {
34
+ const { name: detailName } = event.detail ?? {};
35
+ const currentName = detailName ? getEventName(detailName) : void 0;
36
+ if (currentName && event.detail) eventBus.emit(currentName, event.detail);
37
+ }
38
+ export { EventName, createEvent, createToken, getConditionKey, getEventName, isResultType, isSafetyType, mountEvent };
@@ -0,0 +1,14 @@
1
+ import { ZodType, z } from 'zod';
2
+ import { EventChatOptions, EventDetailType, NamepathType } from './utils';
3
+ export declare const checkLiteral: <Name extends NamepathType, Schema extends ZodType, Group extends string | undefined = undefined, Type extends string | undefined = undefined, Token extends boolean | undefined = undefined>(data: EventDetailType, { group, token }: EventChatOptions<Name, Schema, Group, Type, Token>, currentToken?: string) => Promise<import("./utils").DetailType<Name, Schema, Group, Type, Token>>;
4
+ export declare const validate: <Name extends NamepathType, Schema extends ZodType, Group extends string | undefined = undefined, Type extends string | undefined = undefined, Token extends boolean | undefined = undefined>(data: EventDetailType, record: EventChatOptions<Name, Schema, Group, Type, Token>, currentToken?: string) => Promise<{
5
+ detail: z.core.output<Schema>;
6
+ id: string;
7
+ name: Name;
8
+ group: import("./utils").WasProvided<Group, undefined> extends true ? Exclude<Group, undefined> : undefined;
9
+ origin: string;
10
+ type: import("./utils").WasProvided<Type, undefined> extends true ? Exclude<Type, undefined> : undefined;
11
+ token: Token extends true ? string : undefined;
12
+ global?: boolean;
13
+ }>;
14
+ //# sourceMappingURL=validate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACjC,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAgC1E,eAAO,MAAM,YAAY,GACvB,IAAI,SAAS,YAAY,EACzB,MAAM,SAAS,OAAO,EACtB,KAAK,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC5C,IAAI,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC3C,KAAK,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAE7C,MAAM,eAAe,EACrB,kBAAkB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,EACpE,eAAe,MAAM,4EA2BtB,CAAC;AAEF,eAAO,MAAM,QAAQ,GACnB,IAAI,SAAS,YAAY,EACzB,MAAM,SAAS,OAAO,EACtB,KAAK,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC5C,IAAI,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC3C,KAAK,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAE7C,MAAM,eAAe,EACrB,QAAQ,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,EAC1D,eAAe,MAAM;;;;;;;;;EAOpB,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ const checkDetail = (detail, { async, schema })=>{
3
+ if (schema) {
4
+ const result = async ? schema.safeParseAsync(detail) : Promise.resolve(schema.safeParse(detail));
5
+ return result.then((cause)=>cause.success ? cause : Promise.reject(new Error('validate faild', {
6
+ cause
7
+ })));
8
+ }
9
+ return Promise.reject(new Error('validate faild'));
10
+ };
11
+ const literalCondition = (value, error, empty)=>('string' == typeof value && '' !== value ? z.literal(value, error ? {
12
+ error
13
+ } : void 0) : void 0) ?? (value ? z.any() : z.undefined(empty ? {
14
+ error: empty
15
+ } : void 0));
16
+ const checkLiteral = (data, { group, token }, currentToken)=>{
17
+ const schema = z.object({
18
+ group: literalCondition(group ?? data.global, 'Non group members.', 'Do not accept record with group.'),
19
+ token: literalCondition(token ? currentToken : data.global, 'Not providing tokens as expected.', 'Do not accept record with token.')
20
+ });
21
+ const cause = schema.safeParse(data);
22
+ return cause.success ? Promise.resolve().then(()=>{
23
+ const resultData = {
24
+ ...data,
25
+ token: currentToken,
26
+ group
27
+ };
28
+ return resultData;
29
+ }) : Promise.reject(new Error('validate faild', {
30
+ cause
31
+ }));
32
+ };
33
+ const validate = (data, record, currentToken)=>checkLiteral(data, record, currentToken).then((detail)=>checkDetail(data.detail, record).then((result)=>({
34
+ ...detail,
35
+ detail: result.data
36
+ })));
37
+ export { checkLiteral, validate };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@event-chat/core",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "devDependencies": {
17
+ "@rslib/core": "^0.18.4",
18
+ "@rstest/core": "^0.7.1",
19
+ "@rstest/coverage-istanbul": "^0.1.3",
20
+ "@storybook/addon-docs": "^10.1.4",
21
+ "@storybook/addon-onboarding": "^10.1.4",
22
+ "@storybook/react": "^10.1.4",
23
+ "@testing-library/jest-dom": "^6.9.1",
24
+ "@testing-library/react": "^16.3.0",
25
+ "jsdom": "^26.1.0",
26
+ "storybook": "^10.1.4",
27
+ "storybook-addon-rslib": "^3.1.0",
28
+ "storybook-react-rsbuild": "^3.1.0",
29
+ "tsd": "^0.33.0",
30
+ "typescript-eslint": "^8.48.0"
31
+ },
32
+ "peerDependencies": {
33
+ "react": ">=18.0.0",
34
+ "react-dom": ">=18.0.0",
35
+ "zod": "^3.0.0 || ^4.0.0"
36
+ },
37
+ "scripts": {
38
+ "build": "rslib build",
39
+ "build:storybook": "storybook build",
40
+ "dev": "rslib build --watch",
41
+ "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\"",
42
+ "format:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx}\"",
43
+ "lint": "eslint \"src/**/*.{ts,tsx,js,jsx}\"",
44
+ "lint:fix": "eslint \"src/**/*.{ts,tsx,js,jsx}\" --fix",
45
+ "storybook": "storybook dev",
46
+ "test": "rstest",
47
+ "test:all": "tsd && rstest --coverage",
48
+ "test:coverage": "rstest --coverage",
49
+ "test:type": "tsd"
50
+ }
51
+ }