@cybermp/rpc-react 0.1.0-rc.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 +21 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.mjs +44 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CyberMP-RPC
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const react = require('react');
|
|
4
|
+
const jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
const rpcContext = react.createContext({});
|
|
7
|
+
|
|
8
|
+
const useRpc = () => {
|
|
9
|
+
const rpc = react.useContext(rpcContext);
|
|
10
|
+
if (!rpc) {
|
|
11
|
+
throw new Error("You did not provided rpc");
|
|
12
|
+
}
|
|
13
|
+
return rpc;
|
|
14
|
+
};
|
|
15
|
+
const useRegister = (method, handler, deps = []) => {
|
|
16
|
+
const rpc = useRpc();
|
|
17
|
+
react.useEffect(() => {
|
|
18
|
+
rpc.register(
|
|
19
|
+
method,
|
|
20
|
+
...Array.isArray(handler) ? handler : [handler]
|
|
21
|
+
);
|
|
22
|
+
return () => {
|
|
23
|
+
rpc.unregister(method);
|
|
24
|
+
};
|
|
25
|
+
}, deps);
|
|
26
|
+
};
|
|
27
|
+
const useOn = (method, handler, deps = [], cleanAll = true) => {
|
|
28
|
+
const rpc = useRpc();
|
|
29
|
+
react.useEffect(() => {
|
|
30
|
+
const handlerArr = Array.isArray(handler) ? handler : [handler];
|
|
31
|
+
rpc.on(method, ...handlerArr);
|
|
32
|
+
return () => {
|
|
33
|
+
if (cleanAll) {
|
|
34
|
+
rpc.offAll(method);
|
|
35
|
+
} else {
|
|
36
|
+
rpc.off(method, handlerArr[handlerArr.length - 1]);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}, deps);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const RpcProvider = ({ rpc, children }) => {
|
|
43
|
+
return /* @__PURE__ */ jsxRuntime.jsx(rpcContext.Provider, { value: rpc, children });
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
exports.RpcProvider = RpcProvider;
|
|
47
|
+
exports.useOn = useOn;
|
|
48
|
+
exports.useRegister = useRegister;
|
|
49
|
+
exports.useRpc = useRpc;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as _cybermp_rpc_browser from '@cybermp/rpc-browser';
|
|
2
|
+
import { RpcBrowserContext, RpcHandler, RpcBrowser } from '@cybermp/rpc-browser';
|
|
3
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
declare const useRpc: () => _cybermp_rpc_browser.RpcBrowser;
|
|
7
|
+
declare const useRegister: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList) => void;
|
|
8
|
+
declare const useOn: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList, cleanAll?: boolean) => void;
|
|
9
|
+
|
|
10
|
+
type RpcProviderProps = PropsWithChildren<{
|
|
11
|
+
rpc: RpcBrowser;
|
|
12
|
+
}>;
|
|
13
|
+
declare const RpcProvider: ({ rpc, children }: RpcProviderProps) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
export { RpcProvider, useOn, useRegister, useRpc };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as _cybermp_rpc_browser from '@cybermp/rpc-browser';
|
|
2
|
+
import { RpcBrowserContext, RpcHandler, RpcBrowser } from '@cybermp/rpc-browser';
|
|
3
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
declare const useRpc: () => _cybermp_rpc_browser.RpcBrowser;
|
|
7
|
+
declare const useRegister: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList) => void;
|
|
8
|
+
declare const useOn: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList, cleanAll?: boolean) => void;
|
|
9
|
+
|
|
10
|
+
type RpcProviderProps = PropsWithChildren<{
|
|
11
|
+
rpc: RpcBrowser;
|
|
12
|
+
}>;
|
|
13
|
+
declare const RpcProvider: ({ rpc, children }: RpcProviderProps) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
export { RpcProvider, useOn, useRegister, useRpc };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as _cybermp_rpc_browser from '@cybermp/rpc-browser';
|
|
2
|
+
import { RpcBrowserContext, RpcHandler, RpcBrowser } from '@cybermp/rpc-browser';
|
|
3
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
4
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
declare const useRpc: () => _cybermp_rpc_browser.RpcBrowser;
|
|
7
|
+
declare const useRegister: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList) => void;
|
|
8
|
+
declare const useOn: <TContext extends RpcBrowserContext = RpcBrowserContext>(method: string, handler: RpcHandler<TContext> | RpcHandler<TContext>[], deps?: DependencyList, cleanAll?: boolean) => void;
|
|
9
|
+
|
|
10
|
+
type RpcProviderProps = PropsWithChildren<{
|
|
11
|
+
rpc: RpcBrowser;
|
|
12
|
+
}>;
|
|
13
|
+
declare const RpcProvider: ({ rpc, children }: RpcProviderProps) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
export { RpcProvider, useOn, useRegister, useRpc };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect } from 'react';
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
const rpcContext = createContext({});
|
|
5
|
+
|
|
6
|
+
const useRpc = () => {
|
|
7
|
+
const rpc = useContext(rpcContext);
|
|
8
|
+
if (!rpc) {
|
|
9
|
+
throw new Error("You did not provided rpc");
|
|
10
|
+
}
|
|
11
|
+
return rpc;
|
|
12
|
+
};
|
|
13
|
+
const useRegister = (method, handler, deps = []) => {
|
|
14
|
+
const rpc = useRpc();
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
rpc.register(
|
|
17
|
+
method,
|
|
18
|
+
...Array.isArray(handler) ? handler : [handler]
|
|
19
|
+
);
|
|
20
|
+
return () => {
|
|
21
|
+
rpc.unregister(method);
|
|
22
|
+
};
|
|
23
|
+
}, deps);
|
|
24
|
+
};
|
|
25
|
+
const useOn = (method, handler, deps = [], cleanAll = true) => {
|
|
26
|
+
const rpc = useRpc();
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const handlerArr = Array.isArray(handler) ? handler : [handler];
|
|
29
|
+
rpc.on(method, ...handlerArr);
|
|
30
|
+
return () => {
|
|
31
|
+
if (cleanAll) {
|
|
32
|
+
rpc.offAll(method);
|
|
33
|
+
} else {
|
|
34
|
+
rpc.off(method, handlerArr[handlerArr.length - 1]);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}, deps);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const RpcProvider = ({ rpc, children }) => {
|
|
41
|
+
return /* @__PURE__ */ jsx(rpcContext.Provider, { value: rpc, children });
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { RpcProvider, useOn, useRegister, useRpc };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cybermp/rpc-react",
|
|
3
|
+
"version": "0.1.0-rc.1",
|
|
4
|
+
"keywords": [],
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"import": "./dist/index.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"unbuild": {
|
|
17
|
+
"failOnWarn": false,
|
|
18
|
+
"declaration": true,
|
|
19
|
+
"rollup": {
|
|
20
|
+
"esbuild": {
|
|
21
|
+
"jsx": "automatic"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "^19.2.7"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": ">=18.0.0",
|
|
30
|
+
"@cybermp/browser-types": "^1.0.0",
|
|
31
|
+
"@cybermp/rpc-browser": "0.1.0-rc.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"dev": "pnpm run build --watch",
|
|
35
|
+
"build": "unbuild",
|
|
36
|
+
"type:check": "tsc -b"
|
|
37
|
+
}
|
|
38
|
+
}
|