sparkle-react 0.0.4 → 0.0.5
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 +4 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
2
|
|
|
3
3
|
interface WebSocketContextType {
|
|
4
4
|
socket: WebSocket | undefined;
|
|
5
5
|
}
|
|
6
|
-
declare const SparkleContext:
|
|
6
|
+
declare const SparkleContext: React.Context<WebSocketContextType>;
|
|
7
7
|
interface SparkleProviderProps {
|
|
8
|
-
children:
|
|
8
|
+
children: ReactNode;
|
|
9
9
|
}
|
|
10
|
-
declare const SparkleProvider: ({ children }: SparkleProviderProps) =>
|
|
10
|
+
declare const SparkleProvider: ({ children }: SparkleProviderProps) => React.JSX.Element;
|
|
11
11
|
|
|
12
12
|
declare function useDatabase<T extends string>(...keys: T[]): Record<T, any>;
|
|
13
13
|
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{createContext as
|
|
1
|
+
import S from"react";import{createContext as d,useEffect as f,useState as k}from"react";var c=d({socket:void 0}),v=({children:t})=>{let[r,a]=k();return f(()=>{let s=new WebSocket("ws://localhost:8000");return a(s),()=>{s.close()}},[]),S.createElement(c.Provider,{value:{socket:r}},t)};import{useCallback as u,useState as x,useContext as m,useEffect as b}from"react";function T(...t){let r=m(c),[a,s]=x(t.reduce((n,o)=>({...n,[o]:""}),{}));if(!r)throw new Error("You must use useDatabase inside a SparkleProvider");let{socket:e}=r,p=u(()=>{console.log("Connexion \xE9tablie"),e?.send(JSON.stringify({type:"subscribe",keys:t}))},[e,t]),i=u(n=>{let o=JSON.parse(n.data.toString());console.log("Message re\xE7u : "+n.data),console.log("salut",o),s(l=>({...l,[o.key]:o.value}))},[]);return b(()=>{e&&(e.onopen=p,e.onmessage=i)},[i,p,e]),a}export{c as SparkleContext,v as SparkleProvider,T as useDatabase};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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:
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/hooks/useDatabase.ts"],"sourcesContent":["import React from \"react\"\nimport { type ReactNode, 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: 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, useEffect } from \"react\"\nimport { SparkleContext } from \"../provider\"\n\nexport function useDatabase<T extends string>(...keys: T[]) {\n type State = Record<T, any>\n\n const context = useContext(SparkleContext)\n\n const [state, setState] = useState<State>(\n keys.reduce((acc, key) => ({ ...acc, [key]: \"\" }), {} as State)\n )\n\n if (!context) {\n throw new Error(\"You must use useDatabase inside a SparkleProvider\")\n }\n\n const { socket } = context\n\n const openOpen = useCallback(() => {\n console.log(\"Connexion établie\")\n socket?.send(JSON.stringify({ type: \"subscribe\", keys }))\n }, [socket, keys])\n\n const openMessage = 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 useEffect(() => {\n if (!socket) return\n\n socket.onopen = openOpen\n socket.onmessage = openMessage\n }, [openMessage, openOpen, socket])\n\n return state\n}\n"],"mappings":"AAAA,OAAOA,MAAW,QAClB,OAAyB,iBAAAC,EAAe,aAAAC,EAAW,YAAAC,MAAgB,QAM5D,IAAMC,EAAiBH,EAAoC,CAChE,OAAQ,MACV,CAAC,EAMYI,EAAkB,CAAC,CAAE,SAAAC,CAAS,IAA4B,CACrE,GAAM,CAACC,EAAQC,CAAS,EAAIL,EAAoB,EAEhD,OAAAD,EAAU,IAAM,CACd,IAAMO,EAAK,IAAI,UAAU,qBAAqB,EAC9C,OAAAD,EAAUC,CAAE,EAEL,IAAM,CACXA,EAAG,MAAM,CACX,CACF,EAAG,CAAC,CAAC,EAGHT,EAAA,cAACI,EAAe,SAAf,CACC,MAAO,CACL,OAAAG,CACF,GAECD,CACH,CAEJ,ECpCA,OAAS,eAAAI,EAAa,YAAAC,EAAU,cAAAC,EAAY,aAAAC,MAAiB,QAGtD,SAASC,KAAiCC,EAAW,CAG1D,IAAMC,EAAUC,EAAWC,CAAc,EAEnC,CAACC,EAAOC,CAAQ,EAAIC,EACxBN,EAAK,OAAO,CAACO,EAAKC,KAAS,CAAE,GAAGD,EAAK,CAACC,CAAG,EAAG,EAAG,GAAI,CAAC,CAAU,CAChE,EAEA,GAAI,CAACP,EACH,MAAM,IAAI,MAAM,mDAAmD,EAGrE,GAAM,CAAE,OAAAQ,CAAO,EAAIR,EAEbS,EAAWC,EAAY,IAAM,CACjC,QAAQ,IAAI,sBAAmB,EAC/BF,GAAQ,KAAK,KAAK,UAAU,CAAE,KAAM,YAAa,KAAAT,CAAK,CAAC,CAAC,CAC1D,EAAG,CAACS,EAAQT,CAAI,CAAC,EAEXY,EAAcD,EAAaE,GAAwB,CACvD,IAAMC,EAAO,KAAK,MAAMD,EAAM,KAAK,SAAS,CAAC,EAE7C,QAAQ,IAAI,qBAAoBA,EAAM,IAAI,EAC1C,QAAQ,IAAI,QAASC,CAAI,EAEzBT,EAAUD,IAAW,CACnB,GAAGA,EACH,CAACU,EAAK,GAAG,EAAGA,EAAK,KACnB,EAAE,CACJ,EAAG,CAAC,CAAC,EAEL,OAAAC,EAAU,IAAM,CACTN,IAELA,EAAO,OAASC,EAChBD,EAAO,UAAYG,EACrB,EAAG,CAACA,EAAaF,EAAUD,CAAM,CAAC,EAE3BL,CACT","names":["React","createContext","useEffect","useState","SparkleContext","SparkleProvider","children","socket","setSocket","ws","useCallback","useState","useContext","useEffect","useDatabase","keys","context","useContext","SparkleContext","state","setState","useState","acc","key","socket","openOpen","useCallback","openMessage","event","data","useEffect"]}
|