sparkle-react 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/dist/index.d.ts +15 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
|
|
4
|
+
interface WebSocketContextType {
|
|
5
|
+
socket: WebSocket | undefined;
|
|
6
|
+
}
|
|
7
|
+
declare const SparkleContext: react.Context<WebSocketContextType>;
|
|
8
|
+
interface SparkleProviderProps {
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
}
|
|
11
|
+
declare const SparkleProvider: ({ children }: SparkleProviderProps) => react_jsx_runtime.JSX.Element;
|
|
12
|
+
|
|
13
|
+
declare function useDatabase<T extends string>(...keys: T[]): Record<T, any>;
|
|
14
|
+
|
|
15
|
+
export { SparkleContext, SparkleProvider, useDatabase };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createContext as l,useEffect as p,useState as S}from"react";import{jsx as u}from"react/jsx-runtime";var n=l({socket:void 0}),x=({children:o})=>{let[e,a]=S();return p(()=>{let r=new WebSocket("ws://localhost:8000");return a(r),()=>{r.close()}},[]),u(n.Provider,{value:{socket:e},children:o})};import{useCallback as c,useState as d,useContext as k}from"react";function v(...o){let{socket:e}=k(n);if(!e)throw new Error("You must use useDatabase inside a SparkleProvider");let[a,r]=d(o.reduce((s,t)=>({...s,[t]:""}),{}));return e.onopen=c(()=>{console.log("Connexion \xE9tablie"),e.send(JSON.stringify({type:"subscribe",keys:o}))},[]),e.onmessage=c(s=>{let t=JSON.parse(s.data.toString());console.log("Message re\xE7u : "+s.data),console.log("salut",t),r(i=>({...i,[t.key]:t.value}))},[]),a}export{n as SparkleContext,x as SparkleProvider,v as useDatabase};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/hooks/useDatabase.ts"],"sourcesContent":["import { createContext, useEffect, useState } from \"react\"\n\ninterface WebSocketContextType {\n socket: WebSocket | undefined\n}\n\nexport const SparkleContext = createContext<WebSocketContextType>({\n socket: undefined,\n})\n\ninterface SparkleProviderProps {\n children: React.ReactNode\n}\n\nexport const SparkleProvider = ({ children }: SparkleProviderProps) => {\n const [socket, setSocket] = useState<WebSocket>()\n\n useEffect(() => {\n const ws = new WebSocket(\"ws://localhost:8000\")\n setSocket(ws)\n\n return () => {\n ws.close()\n }\n }, [])\n\n return (\n <SparkleContext.Provider\n value={{\n socket,\n }}\n >\n {children}\n </SparkleContext.Provider>\n )\n}\n","import { useCallback, useState, useContext } from \"react\"\nimport { SparkleContext } from \"../provider\"\n\nexport function useDatabase<T extends string>(...keys: T[]) {\n const { socket } = useContext(SparkleContext)\n\n if (!socket) {\n throw new Error(\"You must use useDatabase inside a SparkleProvider\")\n }\n\n type State = Record<T, any>\n\n const [state, setState] = useState<State>(\n keys.reduce((acc, key) => ({ ...acc, [key]: \"\" }), {} as State)\n )\n\n socket.onopen = useCallback(() => {\n console.log(\"Connexion établie\")\n socket.send(JSON.stringify({ type: \"subscribe\", keys }))\n }, [])\n\n socket.onmessage = useCallback((event: MessageEvent) => {\n const data = JSON.parse(event.data.toString())\n\n console.log(\"Message reçu : \" + event.data)\n console.log(\"salut\", data)\n\n setState((state) => ({\n ...state,\n [data.key]: data.value,\n }))\n }, [])\n\n return state\n}\n"],"mappings":"AAAA,OAAS,iBAAAA,EAAe,aAAAC,EAAW,YAAAC,MAAgB,QA2B/C,cAAAC,MAAA,oBArBG,IAAMC,EAAiBJ,EAAoC,CAChE,OAAQ,MACV,CAAC,EAMYK,EAAkB,CAAC,CAAE,SAAAC,CAAS,IAA4B,CACrE,GAAM,CAACC,EAAQC,CAAS,EAAIN,EAAoB,EAEhD,OAAAD,EAAU,IAAM,CACd,IAAMQ,EAAK,IAAI,UAAU,qBAAqB,EAC9C,OAAAD,EAAUC,CAAE,EAEL,IAAM,CACXA,EAAG,MAAM,CACX,CACF,EAAG,CAAC,CAAC,EAGHN,EAACC,EAAe,SAAf,CACC,MAAO,CACL,OAAAG,CACF,EAEC,SAAAD,EACH,CAEJ,ECnCA,OAAS,eAAAI,EAAa,YAAAC,EAAU,cAAAC,MAAkB,QAG3C,SAASC,KAAiCC,EAAW,CAC1D,GAAM,CAAE,OAAAC,CAAO,EAAIC,EAAWC,CAAc,EAE5C,GAAI,CAACF,EACH,MAAM,IAAI,MAAM,mDAAmD,EAKrE,GAAM,CAACG,EAAOC,CAAQ,EAAIC,EACxBN,EAAK,OAAO,CAACO,EAAKC,KAAS,CAAE,GAAGD,EAAK,CAACC,CAAG,EAAG,EAAG,GAAI,CAAC,CAAU,CAChE,EAEA,OAAAP,EAAO,OAASQ,EAAY,IAAM,CAChC,QAAQ,IAAI,sBAAmB,EAC/BR,EAAO,KAAK,KAAK,UAAU,CAAE,KAAM,YAAa,KAAAD,CAAK,CAAC,CAAC,CACzD,EAAG,CAAC,CAAC,EAELC,EAAO,UAAYQ,EAAaC,GAAwB,CACtD,IAAMC,EAAO,KAAK,MAAMD,EAAM,KAAK,SAAS,CAAC,EAE7C,QAAQ,IAAI,qBAAoBA,EAAM,IAAI,EAC1C,QAAQ,IAAI,QAASC,CAAI,EAEzBN,EAAUD,IAAW,CACnB,GAAGA,EACH,CAACO,EAAK,GAAG,EAAGA,EAAK,KACnB,EAAE,CACJ,EAAG,CAAC,CAAC,EAEEP,CACT","names":["createContext","useEffect","useState","jsx","SparkleContext","SparkleProvider","children","socket","setSocket","ws","useCallback","useState","useContext","useDatabase","keys","socket","useContext","SparkleContext","state","setState","useState","acc","key","useCallback","event","data"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sparkle-react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": "./dist/index.js",
|
|
9
|
+
"bin": "./dist/index.js",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"rimraf": "^5.0.5",
|
|
13
|
+
"tsup": "^8.0.1",
|
|
14
|
+
"typescript": "^5.3.3"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"react": "^18.2.0",
|
|
18
|
+
"zod": "^3.22.4"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"dev": "tsup --watch",
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"clean": "rimraf dist",
|
|
25
|
+
"release": "changeset version",
|
|
26
|
+
"pub:beta": "pnpm build && pnpm publish --no-git-checks --access public --tag beta",
|
|
27
|
+
"pub:next": "pnpm build && pnpm publish --no-git-checks --access public --tag next",
|
|
28
|
+
"pub:release": "pnpm build && pnpm publish --access public"
|
|
29
|
+
}
|
|
30
|
+
}
|