@context-action/core 0.0.1
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 +187 -0
- package/README.ko.md +328 -0
- package/README.md +195 -0
- package/dist/index.cjs +225 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +223 -0
- package/dist/index.js.map +1 -0
- package/package.json +83 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
13
|
+
key = keys[i];
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
22
|
+
value: mod,
|
|
23
|
+
enumerable: true
|
|
24
|
+
}) : target, mod));
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
const react = __toESM(require("react"));
|
|
28
|
+
const react_jsx_runtime = __toESM(require("react/jsx-runtime"));
|
|
29
|
+
|
|
30
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js
|
|
31
|
+
var require_typeof = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js"(exports, module) {
|
|
32
|
+
function _typeof$2(o) {
|
|
33
|
+
"@babel/helpers - typeof";
|
|
34
|
+
return module.exports = _typeof$2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
|
|
35
|
+
return typeof o$1;
|
|
36
|
+
} : function(o$1) {
|
|
37
|
+
return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
|
|
38
|
+
}, module.exports.__esModule = true, module.exports["default"] = module.exports, _typeof$2(o);
|
|
39
|
+
}
|
|
40
|
+
module.exports = _typeof$2, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
41
|
+
} });
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js
|
|
45
|
+
var require_toPrimitive = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js"(exports, module) {
|
|
46
|
+
var _typeof$1 = require_typeof()["default"];
|
|
47
|
+
function toPrimitive$1(t, r) {
|
|
48
|
+
if ("object" != _typeof$1(t) || !t) return t;
|
|
49
|
+
var e = t[Symbol.toPrimitive];
|
|
50
|
+
if (void 0 !== e) {
|
|
51
|
+
var i = e.call(t, r || "default");
|
|
52
|
+
if ("object" != _typeof$1(i)) return i;
|
|
53
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
54
|
+
}
|
|
55
|
+
return ("string" === r ? String : Number)(t);
|
|
56
|
+
}
|
|
57
|
+
module.exports = toPrimitive$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
58
|
+
} });
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js
|
|
62
|
+
var require_toPropertyKey = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js"(exports, module) {
|
|
63
|
+
var _typeof = require_typeof()["default"];
|
|
64
|
+
var toPrimitive = require_toPrimitive();
|
|
65
|
+
function toPropertyKey$1(t) {
|
|
66
|
+
var i = toPrimitive(t, "string");
|
|
67
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
68
|
+
}
|
|
69
|
+
module.exports = toPropertyKey$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
70
|
+
} });
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js
|
|
74
|
+
var require_defineProperty = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js"(exports, module) {
|
|
75
|
+
var toPropertyKey = require_toPropertyKey();
|
|
76
|
+
function _defineProperty$1(e, r, t) {
|
|
77
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
78
|
+
value: t,
|
|
79
|
+
enumerable: !0,
|
|
80
|
+
configurable: !0,
|
|
81
|
+
writable: !0
|
|
82
|
+
}) : e[r] = t, e;
|
|
83
|
+
}
|
|
84
|
+
module.exports = _defineProperty$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
85
|
+
} });
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/core/ActionRegister.ts
|
|
89
|
+
var import_defineProperty = __toESM(require_defineProperty(), 1);
|
|
90
|
+
var ActionRegister = class {
|
|
91
|
+
constructor() {
|
|
92
|
+
(0, import_defineProperty.default)(this, "pipelines", /* @__PURE__ */ new Map());
|
|
93
|
+
(0, import_defineProperty.default)(this, "atomSetters", /* @__PURE__ */ new Map());
|
|
94
|
+
}
|
|
95
|
+
register(action, handler, config = {}) {
|
|
96
|
+
if (!this.pipelines.has(action)) this.pipelines.set(action, /* @__PURE__ */ new Map());
|
|
97
|
+
const pipeline = this.pipelines.get(action);
|
|
98
|
+
const handlerId = config.id || `handler_${Date.now()}_${Math.random()}`;
|
|
99
|
+
if (pipeline.has(handlerId)) {
|
|
100
|
+
console.warn(`Handler with id ${handlerId} already exists`);
|
|
101
|
+
return () => {};
|
|
102
|
+
}
|
|
103
|
+
pipeline.set(handlerId, {
|
|
104
|
+
handler,
|
|
105
|
+
config
|
|
106
|
+
});
|
|
107
|
+
this.sortPipeline(action);
|
|
108
|
+
return () => {
|
|
109
|
+
pipeline.delete(handlerId);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
registerAtomSetter(name, setter) {
|
|
113
|
+
this.atomSetters.set(name, setter);
|
|
114
|
+
}
|
|
115
|
+
async dispatch(action, payload) {
|
|
116
|
+
const pipeline = this.pipelines.get(action);
|
|
117
|
+
if (!pipeline || pipeline.size === 0) {
|
|
118
|
+
console.warn(`No handlers registered for action: ${String(action)}`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
let modifiedPayload = payload;
|
|
122
|
+
const handlers = Array.from(pipeline.values());
|
|
123
|
+
for (const { handler, config } of handlers) {
|
|
124
|
+
let shouldContinue = true;
|
|
125
|
+
const controller = {
|
|
126
|
+
next: () => {
|
|
127
|
+
shouldContinue = true;
|
|
128
|
+
},
|
|
129
|
+
abort: (reason) => {
|
|
130
|
+
shouldContinue = false;
|
|
131
|
+
console.log(`Pipeline aborted: ${reason}`);
|
|
132
|
+
},
|
|
133
|
+
modifyPayload: (modifier) => {
|
|
134
|
+
modifiedPayload = modifier(modifiedPayload);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
try {
|
|
138
|
+
if (config.blocking) await handler(modifiedPayload, controller);
|
|
139
|
+
else handler(modifiedPayload, controller);
|
|
140
|
+
if (!shouldContinue) break;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error(`Error in pipeline handler:`, error);
|
|
143
|
+
if (config.blocking) throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
sortPipeline(action) {
|
|
148
|
+
const pipeline = this.pipelines.get(action);
|
|
149
|
+
if (!pipeline) return;
|
|
150
|
+
const sorted = Array.from(pipeline.entries()).sort(([, a], [, b]) => {
|
|
151
|
+
const priorityA = a.config.priority ?? 0;
|
|
152
|
+
const priorityB = b.config.priority ?? 0;
|
|
153
|
+
return priorityB - priorityA;
|
|
154
|
+
});
|
|
155
|
+
pipeline.clear();
|
|
156
|
+
sorted.forEach(([id, data]) => pipeline.set(id, data));
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/core/types.ts
|
|
162
|
+
function createAction(type, payload) {
|
|
163
|
+
return {
|
|
164
|
+
type,
|
|
165
|
+
payload
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function isAction(action, type) {
|
|
169
|
+
return action?.type === type;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/react/ActionContext.tsx
|
|
174
|
+
/**
|
|
175
|
+
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
176
|
+
* @returns Provider, hooks를 포함한 객체
|
|
177
|
+
*/
|
|
178
|
+
function createActionContext() {
|
|
179
|
+
const ActionContext = (0, react.createContext)(null);
|
|
180
|
+
const Provider = ({ children }) => {
|
|
181
|
+
const actionRegisterRef = (0, react.useRef)(new ActionRegister());
|
|
182
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ActionContext.Provider, {
|
|
183
|
+
value: { actionRegisterRef },
|
|
184
|
+
children
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
const useActionContext = () => {
|
|
188
|
+
const context = (0, react.useContext)(ActionContext);
|
|
189
|
+
if (!context) throw new Error("useActionContext must be used within Provider");
|
|
190
|
+
return context;
|
|
191
|
+
};
|
|
192
|
+
const useAction = () => {
|
|
193
|
+
const { actionRegisterRef } = useActionContext();
|
|
194
|
+
return actionRegisterRef.current;
|
|
195
|
+
};
|
|
196
|
+
const useActionHandler = (action, handler, config) => {
|
|
197
|
+
const actionRegister = useAction();
|
|
198
|
+
const componentId = (0, react.useId)();
|
|
199
|
+
(0, react.useEffect)(() => {
|
|
200
|
+
const unregister = actionRegister.register(action, handler, {
|
|
201
|
+
...config,
|
|
202
|
+
id: config?.id || componentId
|
|
203
|
+
});
|
|
204
|
+
return unregister;
|
|
205
|
+
}, [
|
|
206
|
+
action,
|
|
207
|
+
handler,
|
|
208
|
+
config,
|
|
209
|
+
componentId,
|
|
210
|
+
actionRegister
|
|
211
|
+
]);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
Provider,
|
|
215
|
+
useActionContext,
|
|
216
|
+
useAction,
|
|
217
|
+
useActionHandler
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//#endregion
|
|
222
|
+
exports.ActionRegister = ActionRegister;
|
|
223
|
+
exports.createAction = createAction;
|
|
224
|
+
exports.createActionContext = createActionContext;
|
|
225
|
+
exports.isAction = isAction;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/core/ActionRegister.d.ts
|
|
4
|
+
type PipelineController<T = any> = {
|
|
5
|
+
next: () => void;
|
|
6
|
+
abort: (reason?: string) => void;
|
|
7
|
+
modifyPayload: (modifier: (payload: T) => T) => void;
|
|
8
|
+
};
|
|
9
|
+
type ActionHandler<T = any> = (payload: T, controller: PipelineController<T>) => void | Promise<void>;
|
|
10
|
+
type HandlerConfig = {
|
|
11
|
+
priority?: number;
|
|
12
|
+
id?: string;
|
|
13
|
+
blocking?: boolean;
|
|
14
|
+
};
|
|
15
|
+
interface ActionPayloadMap {}
|
|
16
|
+
declare class ActionRegister<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
17
|
+
private pipelines;
|
|
18
|
+
private atomSetters;
|
|
19
|
+
register<K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig): () => void;
|
|
20
|
+
registerAtomSetter(name: string, setter: Function): void;
|
|
21
|
+
dispatch<K extends keyof T>(action: T[K] extends void ? K : never): Promise<void>;
|
|
22
|
+
dispatch<K extends keyof T>(action: K, payload: T[K]): Promise<void>;
|
|
23
|
+
private sortPipeline;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=ActionRegister.d.ts.map
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/core/types.d.ts
|
|
28
|
+
interface BaseActionPayloadMap {}
|
|
29
|
+
type ActionType<T extends Record<string, any>> = keyof T;
|
|
30
|
+
type ActionPayload<T extends Record<string, any>, K extends keyof T> = T[K];
|
|
31
|
+
type ActionHandlerMap<T extends Record<string, any>> = { [K in keyof T]?: (payload: T[K]) => void | Promise<void> };
|
|
32
|
+
declare function createAction<T extends Record<string, any>, K extends keyof T>(type: K, payload: T[K]): {
|
|
33
|
+
type: K;
|
|
34
|
+
payload: T[K];
|
|
35
|
+
};
|
|
36
|
+
declare function isAction<T extends Record<string, any>, K extends keyof T>(action: any, type: K): action is {
|
|
37
|
+
type: K;
|
|
38
|
+
payload: T[K];
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=types.d.ts.map
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/react/ActionContext.d.ts
|
|
43
|
+
/**
|
|
44
|
+
* Context type for ActionRegister
|
|
45
|
+
*/
|
|
46
|
+
interface ActionContextType<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
47
|
+
actionRegisterRef: React.RefObject<ActionRegister<T>>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Return type for createActionContext
|
|
51
|
+
*/
|
|
52
|
+
interface ActionContextReturn<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
53
|
+
Provider: React.FC<{
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}>;
|
|
56
|
+
useActionContext: () => ActionContextType<T>;
|
|
57
|
+
useAction: () => ActionRegister<T>;
|
|
58
|
+
useActionHandler: <K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig) => void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
62
|
+
* @returns Provider, hooks를 포함한 객체
|
|
63
|
+
*/
|
|
64
|
+
declare function createActionContext<T extends ActionPayloadMap = ActionPayloadMap>(): ActionContextReturn<T>;
|
|
65
|
+
//# sourceMappingURL=ActionContext.d.ts.map
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { ActionHandler, ActionHandlerMap, ActionPayload, ActionPayloadMap, ActionRegister, ActionType, BaseActionPayloadMap, HandlerConfig, PipelineController, createAction, createActionContext, isAction };
|
|
69
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/core/ActionRegister.ts","../src/core/types.ts","../src/react/ActionContext.tsx"],"sourcesContent":[],"mappings":";;;KACY;;;EAAA,aAAA,EAAA,CAAA,QAAkB,EAAA,CAAA,OAAA,EAGQ,CAHR,EAAA,GAGc,CAHd,EAAA,GAAA,IAAA;CAAA;AAGQ,KAG1B,aAH0B,CAAA,IAAA,GAAA,CAAA,GAAA,CAAA,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EPf,CFOe,EAAA,OAAA,EENd,aFMc,CENA,CFMA,CENE,CFMF,CAAA,CAAA,EAAA,MAAA,CAAA,EELd,aFKc,EAAA,GAAA,IAAA;;;;;;AAWA,iBERX,mBFQW,CAAA,UERmB,gBFQnB,GERsC,gBFQtC,CAAA,CAAA,CAAA,EER2D,mBFQ3D,CER+E,CFQ/E,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/core/ActionRegister.d.ts
|
|
4
|
+
type PipelineController<T = any> = {
|
|
5
|
+
next: () => void;
|
|
6
|
+
abort: (reason?: string) => void;
|
|
7
|
+
modifyPayload: (modifier: (payload: T) => T) => void;
|
|
8
|
+
};
|
|
9
|
+
type ActionHandler<T = any> = (payload: T, controller: PipelineController<T>) => void | Promise<void>;
|
|
10
|
+
type HandlerConfig = {
|
|
11
|
+
priority?: number;
|
|
12
|
+
id?: string;
|
|
13
|
+
blocking?: boolean;
|
|
14
|
+
};
|
|
15
|
+
interface ActionPayloadMap {}
|
|
16
|
+
declare class ActionRegister<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
17
|
+
private pipelines;
|
|
18
|
+
private atomSetters;
|
|
19
|
+
register<K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig): () => void;
|
|
20
|
+
registerAtomSetter(name: string, setter: Function): void;
|
|
21
|
+
dispatch<K extends keyof T>(action: T[K] extends void ? K : never): Promise<void>;
|
|
22
|
+
dispatch<K extends keyof T>(action: K, payload: T[K]): Promise<void>;
|
|
23
|
+
private sortPipeline;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=ActionRegister.d.ts.map
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/core/types.d.ts
|
|
28
|
+
interface BaseActionPayloadMap {}
|
|
29
|
+
type ActionType<T extends Record<string, any>> = keyof T;
|
|
30
|
+
type ActionPayload<T extends Record<string, any>, K extends keyof T> = T[K];
|
|
31
|
+
type ActionHandlerMap<T extends Record<string, any>> = { [K in keyof T]?: (payload: T[K]) => void | Promise<void> };
|
|
32
|
+
declare function createAction<T extends Record<string, any>, K extends keyof T>(type: K, payload: T[K]): {
|
|
33
|
+
type: K;
|
|
34
|
+
payload: T[K];
|
|
35
|
+
};
|
|
36
|
+
declare function isAction<T extends Record<string, any>, K extends keyof T>(action: any, type: K): action is {
|
|
37
|
+
type: K;
|
|
38
|
+
payload: T[K];
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=types.d.ts.map
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/react/ActionContext.d.ts
|
|
43
|
+
/**
|
|
44
|
+
* Context type for ActionRegister
|
|
45
|
+
*/
|
|
46
|
+
interface ActionContextType<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
47
|
+
actionRegisterRef: React.RefObject<ActionRegister<T>>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Return type for createActionContext
|
|
51
|
+
*/
|
|
52
|
+
interface ActionContextReturn<T extends ActionPayloadMap = ActionPayloadMap> {
|
|
53
|
+
Provider: React.FC<{
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}>;
|
|
56
|
+
useActionContext: () => ActionContextType<T>;
|
|
57
|
+
useAction: () => ActionRegister<T>;
|
|
58
|
+
useActionHandler: <K extends keyof T>(action: K, handler: ActionHandler<T[K]>, config?: HandlerConfig) => void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
62
|
+
* @returns Provider, hooks를 포함한 객체
|
|
63
|
+
*/
|
|
64
|
+
declare function createActionContext<T extends ActionPayloadMap = ActionPayloadMap>(): ActionContextReturn<T>;
|
|
65
|
+
//# sourceMappingURL=ActionContext.d.ts.map
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { ActionHandler, ActionHandlerMap, ActionPayload, ActionPayloadMap, ActionRegister, ActionType, BaseActionPayloadMap, HandlerConfig, PipelineController, createAction, createActionContext, isAction };
|
|
69
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/core/ActionRegister.ts","../src/core/types.ts","../src/react/ActionContext.tsx"],"sourcesContent":[],"mappings":";;;KACY;;;EAAA,aAAA,EAAA,CAAA,QAAkB,EAAA,CAAA,OAAA,EAGQ,CAHR,EAAA,GAGc,CAHd,EAAA,GAAA,IAAA;CAAA;AAGQ,KAG1B,aAH0B,CAAA,IAAA,GAAA,CAAA,GAAA,CAAA,OAAA,EAI3B,CAJ2B,EAAA,UAAA,EAKxB,kBALwB,CAKL,CALK,CAAA,EAAA,GAAA,IAAA,GAM1B,OAN0B,CAAA,IAAA,CAAA;AAAM,KAQhC,aAAA,GARgC;EAAC,QAAA,CAAA,EAAA,MAAA;EAGjC,EAAA,CAAA,EAAA,MAAA;EAAa,QAAA,CAAA,EAAA,OAAA;CAAA;AAEQ,UAUhB,gBAAA,CAVgB;AACrB,cAeC,cAfD,CAAA,UAe0B,gBAf1B,GAe6C,gBAf7C,CAAA,CAAA;EAAO,QAAA,SAAA;EAEP,QAAA,WAAa;EAOR,QAAA,CAAA,UAAgB,MAeN,CAfM,CAAA,CAAA,MAAA,EAgBrB,CAhBqB,EAAA,OAAA,EAiBpB,aAjBoB,CAiBN,CAjBM,CAiBJ,CAjBI,CAAA,CAAA,EAAA,MAAA,CAAA,EAkBrB,aAlBqB,CAAA,EAAA,GAAA,GAAA,IAAA;EAMpB,kBAAc,CAAA,IAAA,EAAA,MAAA,EAAA,MAAA,EAuCgB,QAvChB,CAAA,EAAA,IAAA;EAAA,QAAA,CAAA,UAAA,MA4CM,CA5CN,CAAA,CAAA,MAAA,EA6Cf,CA7Ce,CA6Cb,CA7Ca,CAAA,SAAA,IAAA,GA6CK,CA7CL,GAAA,KAAA,CAAA,EA8CtB,OA9CsB,CAAA,I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KAAA,CAAM,SFHiB,CEGP,cFHO,CEGQ,CFHR,CAAA,CAAA;AAAC;AAG7C;;;AAEiC,UEIhB,mBFJgB,CAAA,UEIc,gBFJd,GEIiC,gBFJjC,CAAA,CAAA;EAAC,QAApB,EEKF,KAAA,CAAM,EFLJ,CAAA;IACF,QAAA,EEIqB,SFJrB;EAAO,CAAA,CAAA;EAEP,gBAAa,EAAA,GAAA,GEGC,iBFHD,CEGmB,CFHnB,CAAA;EAOR,SAAA,EAAA,GAAA,GEHE,cFGc,CEHC,CFGD,CAAA;EAMpB,gBAAA,EAAc,CAAA,UAAA,MERU,CFQV,CAAA,CAAA,MAAA,EEPf,CFOe,EAAA,OAAA,EENd,aFMc,CENA,CFMA,CENE,CFMF,CAAA,CAAA,EAAA,MAAA,CAAA,EELd,aFKc,EAAA,GAAA,IAAA;;;;;;AAWA,iBERX,mBFQW,CAAA,UERmB,gBFQnB,GERsC,gBFQtC,CAAA,CAAA,CAAA,EER2D,mBFQ3D,CER+E,CFQ/E,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import React, { createContext, useContext, useEffect, useId, useRef } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region rolldown:runtime
|
|
5
|
+
var __create = Object.create;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
8
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __commonJS = (cb, mod) => function() {
|
|
12
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
16
|
+
key = keys[i];
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
18
|
+
get: ((k) => from[k]).bind(null, key),
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
+
value: mod,
|
|
26
|
+
enumerable: true
|
|
27
|
+
}) : target, mod));
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js
|
|
31
|
+
var require_typeof = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js"(exports, module) {
|
|
32
|
+
function _typeof$2(o) {
|
|
33
|
+
"@babel/helpers - typeof";
|
|
34
|
+
return module.exports = _typeof$2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
|
|
35
|
+
return typeof o$1;
|
|
36
|
+
} : function(o$1) {
|
|
37
|
+
return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
|
|
38
|
+
}, module.exports.__esModule = true, module.exports["default"] = module.exports, _typeof$2(o);
|
|
39
|
+
}
|
|
40
|
+
module.exports = _typeof$2, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
41
|
+
} });
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js
|
|
45
|
+
var require_toPrimitive = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js"(exports, module) {
|
|
46
|
+
var _typeof$1 = require_typeof()["default"];
|
|
47
|
+
function toPrimitive$1(t, r) {
|
|
48
|
+
if ("object" != _typeof$1(t) || !t) return t;
|
|
49
|
+
var e = t[Symbol.toPrimitive];
|
|
50
|
+
if (void 0 !== e) {
|
|
51
|
+
var i = e.call(t, r || "default");
|
|
52
|
+
if ("object" != _typeof$1(i)) return i;
|
|
53
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
54
|
+
}
|
|
55
|
+
return ("string" === r ? String : Number)(t);
|
|
56
|
+
}
|
|
57
|
+
module.exports = toPrimitive$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
58
|
+
} });
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js
|
|
62
|
+
var require_toPropertyKey = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js"(exports, module) {
|
|
63
|
+
var _typeof = require_typeof()["default"];
|
|
64
|
+
var toPrimitive = require_toPrimitive();
|
|
65
|
+
function toPropertyKey$1(t) {
|
|
66
|
+
var i = toPrimitive(t, "string");
|
|
67
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
68
|
+
}
|
|
69
|
+
module.exports = toPropertyKey$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
70
|
+
} });
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js
|
|
74
|
+
var require_defineProperty = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js"(exports, module) {
|
|
75
|
+
var toPropertyKey = require_toPropertyKey();
|
|
76
|
+
function _defineProperty$1(e, r, t) {
|
|
77
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
78
|
+
value: t,
|
|
79
|
+
enumerable: !0,
|
|
80
|
+
configurable: !0,
|
|
81
|
+
writable: !0
|
|
82
|
+
}) : e[r] = t, e;
|
|
83
|
+
}
|
|
84
|
+
module.exports = _defineProperty$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
85
|
+
} });
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/core/ActionRegister.ts
|
|
89
|
+
var import_defineProperty = __toESM(require_defineProperty(), 1);
|
|
90
|
+
var ActionRegister = class {
|
|
91
|
+
constructor() {
|
|
92
|
+
(0, import_defineProperty.default)(this, "pipelines", /* @__PURE__ */ new Map());
|
|
93
|
+
(0, import_defineProperty.default)(this, "atomSetters", /* @__PURE__ */ new Map());
|
|
94
|
+
}
|
|
95
|
+
register(action, handler, config = {}) {
|
|
96
|
+
if (!this.pipelines.has(action)) this.pipelines.set(action, /* @__PURE__ */ new Map());
|
|
97
|
+
const pipeline = this.pipelines.get(action);
|
|
98
|
+
const handlerId = config.id || `handler_${Date.now()}_${Math.random()}`;
|
|
99
|
+
if (pipeline.has(handlerId)) {
|
|
100
|
+
console.warn(`Handler with id ${handlerId} already exists`);
|
|
101
|
+
return () => {};
|
|
102
|
+
}
|
|
103
|
+
pipeline.set(handlerId, {
|
|
104
|
+
handler,
|
|
105
|
+
config
|
|
106
|
+
});
|
|
107
|
+
this.sortPipeline(action);
|
|
108
|
+
return () => {
|
|
109
|
+
pipeline.delete(handlerId);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
registerAtomSetter(name, setter) {
|
|
113
|
+
this.atomSetters.set(name, setter);
|
|
114
|
+
}
|
|
115
|
+
async dispatch(action, payload) {
|
|
116
|
+
const pipeline = this.pipelines.get(action);
|
|
117
|
+
if (!pipeline || pipeline.size === 0) {
|
|
118
|
+
console.warn(`No handlers registered for action: ${String(action)}`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
let modifiedPayload = payload;
|
|
122
|
+
const handlers = Array.from(pipeline.values());
|
|
123
|
+
for (const { handler, config } of handlers) {
|
|
124
|
+
let shouldContinue = true;
|
|
125
|
+
const controller = {
|
|
126
|
+
next: () => {
|
|
127
|
+
shouldContinue = true;
|
|
128
|
+
},
|
|
129
|
+
abort: (reason) => {
|
|
130
|
+
shouldContinue = false;
|
|
131
|
+
console.log(`Pipeline aborted: ${reason}`);
|
|
132
|
+
},
|
|
133
|
+
modifyPayload: (modifier) => {
|
|
134
|
+
modifiedPayload = modifier(modifiedPayload);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
try {
|
|
138
|
+
if (config.blocking) await handler(modifiedPayload, controller);
|
|
139
|
+
else handler(modifiedPayload, controller);
|
|
140
|
+
if (!shouldContinue) break;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error(`Error in pipeline handler:`, error);
|
|
143
|
+
if (config.blocking) throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
sortPipeline(action) {
|
|
148
|
+
const pipeline = this.pipelines.get(action);
|
|
149
|
+
if (!pipeline) return;
|
|
150
|
+
const sorted = Array.from(pipeline.entries()).sort(([, a], [, b]) => {
|
|
151
|
+
const priorityA = a.config.priority ?? 0;
|
|
152
|
+
const priorityB = b.config.priority ?? 0;
|
|
153
|
+
return priorityB - priorityA;
|
|
154
|
+
});
|
|
155
|
+
pipeline.clear();
|
|
156
|
+
sorted.forEach(([id, data]) => pipeline.set(id, data));
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/core/types.ts
|
|
162
|
+
function createAction(type, payload) {
|
|
163
|
+
return {
|
|
164
|
+
type,
|
|
165
|
+
payload
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function isAction(action, type) {
|
|
169
|
+
return action?.type === type;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/react/ActionContext.tsx
|
|
174
|
+
/**
|
|
175
|
+
* ActionRegister를 Context로 공유할 수 있는 헬퍼 함수
|
|
176
|
+
* @returns Provider, hooks를 포함한 객체
|
|
177
|
+
*/
|
|
178
|
+
function createActionContext() {
|
|
179
|
+
const ActionContext = createContext(null);
|
|
180
|
+
const Provider = ({ children }) => {
|
|
181
|
+
const actionRegisterRef = useRef(new ActionRegister());
|
|
182
|
+
return /* @__PURE__ */ jsx(ActionContext.Provider, {
|
|
183
|
+
value: { actionRegisterRef },
|
|
184
|
+
children
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
const useActionContext = () => {
|
|
188
|
+
const context = useContext(ActionContext);
|
|
189
|
+
if (!context) throw new Error("useActionContext must be used within Provider");
|
|
190
|
+
return context;
|
|
191
|
+
};
|
|
192
|
+
const useAction = () => {
|
|
193
|
+
const { actionRegisterRef } = useActionContext();
|
|
194
|
+
return actionRegisterRef.current;
|
|
195
|
+
};
|
|
196
|
+
const useActionHandler = (action, handler, config) => {
|
|
197
|
+
const actionRegister = useAction();
|
|
198
|
+
const componentId = useId();
|
|
199
|
+
useEffect(() => {
|
|
200
|
+
const unregister = actionRegister.register(action, handler, {
|
|
201
|
+
...config,
|
|
202
|
+
id: config?.id || componentId
|
|
203
|
+
});
|
|
204
|
+
return unregister;
|
|
205
|
+
}, [
|
|
206
|
+
action,
|
|
207
|
+
handler,
|
|
208
|
+
config,
|
|
209
|
+
componentId,
|
|
210
|
+
actionRegister
|
|
211
|
+
]);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
Provider,
|
|
215
|
+
useActionContext,
|
|
216
|
+
useAction,
|
|
217
|
+
useActionHandler
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//#endregion
|
|
222
|
+
export { ActionRegister, createAction, createActionContext, isAction };
|
|
223
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_typeof","o","_typeof","toPrimitive","toPropertyKey","_defineProperty","action: K","handler: ActionHandler<T[K]>","config: HandlerConfig","name: string","setter: Function","payload?: T[K]","controller: PipelineController<T[K]>","type: K","payload: T[K]","action: any","action: K","handler: ActionHandler<T[K]>","config?: HandlerConfig"],"sources":["../../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/typeof.js","../../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js","../../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js","../../../node_modules/.pnpm/@oxc-project+runtime@0.77.3/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js","../src/core/ActionRegister.ts","../src/core/types.ts","../src/react/ActionContext.tsx"],"sourcesContent":["function _typeof(o) {\n \"@babel/helpers - typeof\";\n\n return module.exports = _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (o) {\n return typeof o;\n } : function (o) {\n return o && \"function\" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? \"symbol\" : typeof o;\n }, module.exports.__esModule = true, module.exports[\"default\"] = module.exports, _typeof(o);\n}\nmodule.exports = _typeof, module.exports.__esModule = true, module.exports[\"default\"] = module.exports;","var _typeof = require(\"./typeof.js\")[\"default\"];\nfunction toPrimitive(t, r) {\n if (\"object\" != _typeof(t) || !t) return t;\n var e = t[Symbol.toPrimitive];\n if (void 0 !== e) {\n var i = e.call(t, r || \"default\");\n if (\"object\" != _typeof(i)) return i;\n throw new TypeError(\"@@toPrimitive must return a primitive value.\");\n }\n return (\"string\" === r ? String : Number)(t);\n}\nmodule.exports = toPrimitive, module.exports.__esModule = true, module.exports[\"default\"] = module.exports;","var _typeof = require(\"./typeof.js\")[\"default\"];\nvar toPrimitive = require(\"./toPrimitive.js\");\nfunction toPropertyKey(t) {\n var i = toPrimitive(t, \"string\");\n return \"symbol\" == _typeof(i) ? i : i + \"\";\n}\nmodule.exports = toPropertyKey, module.exports.__esModule = true, module.exports[\"default\"] = module.exports;","var toPropertyKey = require(\"./toPropertyKey.js\");\nfunction _defineProperty(e, r, t) {\n return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {\n value: t,\n enumerable: !0,\n configurable: !0,\n writable: !0\n }) : e[r] = t, e;\n}\nmodule.exports = _defineProperty, module.exports.__esModule = true, module.exports[\"default\"] = module.exports;","// 타입 정의\nexport type PipelineController<T = any> = {\n next: () => void;\n abort: (reason?: string) => void;\n modifyPayload: (modifier: (payload: T) => T) => void;\n};\n\nexport type ActionHandler<T = any> = (\n payload: T,\n controller: PipelineController<T>\n) => void | Promise<void>;\n\nexport type HandlerConfig = {\n priority?: number;\n id?: string;\n blocking?: boolean;\n};\n\n// 선언적 타입 정의를 위한 인터페이스\nexport interface ActionPayloadMap {\n // 확장 가능한 구조\n // 'actionName': PayloadType;\n}\n\n// Action Register 클래스\nexport class ActionRegister<T extends ActionPayloadMap = ActionPayloadMap> {\n private pipelines = new Map<keyof T, Map<string, {\n handler: ActionHandler<any>;\n config: HandlerConfig;\n }>>();\n \n private atomSetters = new Map<string, Function>();\n\n // 파이프라인에 핸들러 등록\n register<K extends keyof T>(\n action: K,\n handler: ActionHandler<T[K]>,\n config: HandlerConfig = {}\n ): () => void {\n if (!this.pipelines.has(action)) {\n this.pipelines.set(action, new Map());\n }\n \n const pipeline = this.pipelines.get(action)!;\n const handlerId = config.id || `handler_${Date.now()}_${Math.random()}`;\n \n // 중복 등록 방지\n if (pipeline.has(handlerId)) {\n console.warn(`Handler with id ${handlerId} already exists`);\n return () => {};\n }\n \n pipeline.set(handlerId, { handler, config });\n \n // 우선순위로 정렬\n this.sortPipeline(action);\n \n // unregister 함수 반환\n return () => {\n pipeline.delete(handlerId);\n };\n }\n\n // Atom setter 등록\n registerAtomSetter(name: string, setter: Function) {\n this.atomSetters.set(name, setter);\n }\n\n // 파이프라인 실행 - void 타입을 위한 오버로드\n async dispatch<K extends keyof T>(\n action: T[K] extends void ? K : never\n ): Promise<void>;\n async dispatch<K extends keyof T>(\n action: K,\n payload: T[K]\n ): Promise<void>;\n async dispatch<K extends keyof T>(\n action: K,\n payload?: T[K]\n ): Promise<void> {\n const pipeline = this.pipelines.get(action);\n \n if (!pipeline || pipeline.size === 0) {\n console.warn(`No handlers registered for action: ${String(action)}`);\n return;\n }\n \n let modifiedPayload = payload as T[K];\n const handlers = Array.from(pipeline.values());\n \n for (const { handler, config } of handlers) {\n let shouldContinue = true;\n \n const controller: PipelineController<T[K]> = {\n next: () => { shouldContinue = true; },\n abort: (reason) => {\n shouldContinue = false;\n console.log(`Pipeline aborted: ${reason}`);\n },\n modifyPayload: (modifier) => {\n modifiedPayload = modifier(modifiedPayload);\n }\n };\n \n try {\n if (config.blocking) {\n await handler(modifiedPayload, controller);\n } else {\n handler(modifiedPayload, controller);\n }\n \n if (!shouldContinue) break;\n } catch (error) {\n console.error(`Error in pipeline handler:`, error);\n if (config.blocking) throw error;\n }\n }\n }\n\n private sortPipeline<K extends keyof T>(action: K) {\n const pipeline = this.pipelines.get(action);\n if (!pipeline) return;\n \n const sorted = Array.from(pipeline.entries())\n .sort(([, a], [, b]) => {\n const priorityA = a.config.priority ?? 0;\n const priorityB = b.config.priority ?? 0;\n return priorityB - priorityA; // 높은 우선순위가 먼저\n });\n \n pipeline.clear();\n sorted.forEach(([id, data]) => pipeline.set(id, data));\n }\n}","// 기본 액션 타입 정의\nexport interface BaseActionPayloadMap {\n // 기본 액션들은 여기에 정의\n}\n\n// 액션 타입 추출 헬퍼\nexport type ActionType<T extends Record<string, any>> = keyof T;\nexport type ActionPayload<\n T extends Record<string, any>,\n K extends keyof T\n> = T[K];\n\n// 액션 핸들러 타입\nexport type ActionHandlerMap<T extends Record<string, any>> = {\n [K in keyof T]?: (payload: T[K]) => void | Promise<void>;\n};\n\n// 액션 생성 헬퍼\nexport function createAction<T extends Record<string, any>, K extends keyof T>(\n type: K,\n payload: T[K]\n): { type: K; payload: T[K] } {\n return { type, payload };\n}\n\n// 타입 가드\nexport function isAction<T extends Record<string, any>, K extends keyof T>(\n action: any,\n type: K\n): action is { type: K; payload: T[K] } {\n return action?.type === type;\n}","import React, { createContext, ReactNode, useContext, useRef, useEffect, useId } from 'react';\nimport { ActionPayloadMap, ActionRegister, ActionHandler, HandlerConfig } from '../core';\n\n/**\n * Context type for ActionRegister\n */\nexport interface ActionContextType<T extends ActionPayloadMap = ActionPayloadMap> {\n actionRegisterRef: React.RefObject<ActionRegister<T>>;\n}\n\n/**\n * Return type for createActionContext\n */\nexport interface ActionContextReturn<T extends ActionPayloadMap = ActionPayloadMap> {\n Provider: React.FC<{ children: ReactNode }>;\n useActionContext: () => ActionContextType<T>;\n useAction: () => ActionRegister<T>;\n useActionHandler: <K extends keyof T>(\n action: K,\n handler: ActionHandler<T[K]>,\n config?: HandlerConfig\n ) => void;\n}\n\n/**\n * ActionRegister를 Context로 공유할 수 있는 헬퍼 함수\n * @returns Provider, hooks를 포함한 객체\n */\nexport function createActionContext<T extends ActionPayloadMap = ActionPayloadMap>(): ActionContextReturn<T> {\n\n const ActionContext = createContext<ActionContextType<T> | null>(null);\n\n // Provider 컴포넌트\n const Provider = ({ children }: { children: ReactNode }) => {\n const actionRegisterRef = useRef(new ActionRegister<T>());\n return (\n <ActionContext.Provider value={{ actionRegisterRef }}>\n {children}\n </ActionContext.Provider>\n );\n };\n\n // Context 접근 hook\n const useActionContext = () => {\n const context = useContext(ActionContext);\n if (!context) {\n throw new Error('useActionContext must be used within Provider');\n }\n return context;\n };\n\n // ActionRegister 인스턴스 반환 hook\n const useAction = () => {\n const { actionRegisterRef } = useActionContext();\n return actionRegisterRef.current!;\n };\n\n // Action Handler 등록 hook\n const useActionHandler = <K extends keyof T>(\n action: K,\n handler: ActionHandler<T[K]>,\n config?: HandlerConfig\n ) => {\n const actionRegister = useAction();\n const componentId = useId();\n \n useEffect(() => {\n const unregister = actionRegister.register(\n action,\n handler,\n { ...config, id: config?.id || componentId }\n );\n \n return unregister;\n }, [action, handler, config, componentId, actionRegister]);\n };\n\n return {\n Provider,\n useActionContext,\n useAction,\n useActionHandler,\n };\n}\n"],"x_google_ignoreList":[0,1,2,3],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAA,SAASA,UAAQ,GAAG;AAClB;AAEA,SAAO,OAAO,UAAUA,YAAU,cAAc,OAAO,UAAU,YAAY,OAAO,OAAO,WAAW,SAAUC,KAAG;AACjH,UAAO,OAAOA;EACf,IAAG,SAAUA,KAAG;AACf,UAAOA,OAAK,cAAc,OAAO,UAAUA,IAAE,gBAAgB,UAAUA,QAAM,OAAO,YAAY,WAAW,OAAOA;EACnH,GAAE,OAAO,QAAQ,aAAa,MAAM,OAAO,QAAQ,aAAa,OAAO,SAASD,UAAQ,EAAE;CAC5F;CACD,OAAO,UAAUA,WAAS,OAAO,QAAQ,aAAa,MAAM,OAAO,QAAQ,aAAa,OAAO;;;;;;CCT/F,IAAIE,6BAAiC;CACrC,SAASC,cAAY,GAAG,GAAG;AACzB,MAAI,YAAYD,UAAQ,EAAE,IAAI,CAAC,EAAG,QAAO;EACzC,IAAI,IAAI,EAAE,OAAO;AACjB,MAAI,KAAK,MAAM,GAAG;GAChB,IAAI,IAAI,EAAE,KAAK,GAAG,KAAK,UAAU;AACjC,OAAI,YAAYA,UAAQ,EAAE,CAAE,QAAO;AACnC,SAAM,IAAI,UAAU;EACrB;AACD,UAAQ,aAAa,IAAI,SAAS,QAAQ,EAAE;CAC7C;CACD,OAAO,UAAUC,eAAa,OAAO,QAAQ,aAAa,MAAM,OAAO,QAAQ,aAAa,OAAO;;;;;;CCXnG,IAAI,2BAAiC;CACrC,IAAI;CACJ,SAASC,gBAAc,GAAG;EACxB,IAAI,IAAI,YAAY,GAA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