sparkle-react 0.0.3 → 0.0.4

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 CHANGED
@@ -1,4 +1,3 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
1
  import * as react from 'react';
3
2
 
4
3
  interface WebSocketContextType {
@@ -8,7 +7,7 @@ declare const SparkleContext: react.Context<WebSocketContextType>;
8
7
  interface SparkleProviderProps {
9
8
  children: React.ReactNode;
10
9
  }
11
- declare const SparkleProvider: ({ children }: SparkleProviderProps) => react_jsx_runtime.JSX.Element;
10
+ declare const SparkleProvider: ({ children }: SparkleProviderProps) => react.JSX.Element;
12
11
 
13
12
  declare function useDatabase<T extends string>(...keys: T[]): Record<T, any>;
14
13
 
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{createContext as S,useEffect as d,useState as f}from"react";import{jsx as k}from"react/jsx-runtime";var c=S({socket:void 0}),C=({children:t})=>{let[r,a]=f();return d(()=>{let s=new WebSocket("ws://localhost:8000");return a(s),()=>{s.close()}},[]),k(c.Provider,{value:{socket:r},children:t})};import{useCallback as l,useState as x,useContext as b,useEffect as g}from"react";function T(...t){let r=b(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=l(()=>{console.log("Connexion \xE9tablie"),e?.send(JSON.stringify({type:"subscribe",keys:t}))},[e,t]),i=l(n=>{let o=JSON.parse(n.data.toString());console.log("Message re\xE7u : "+n.data),console.log("salut",o),s(u=>({...u,[o.key]:o.value}))},[]);return g(()=>{e&&(e.onopen=p,e.onmessage=i)},[i,p,e]),a}export{c as SparkleContext,C as SparkleProvider,T as useDatabase};
1
+ import{createContext as S,useEffect as d,useState as f}from"react";var c=S({socket:void 0}),m=({children:t})=>{let[r,a]=f();return d(()=>{let s=new WebSocket("ws://localhost:8000");return a(s),()=>{s.close()}},[]),React.createElement(c.Provider,{value:{socket:r}},t)};import{useCallback as u,useState as k,useContext as x,useEffect as b}from"react";function w(...t){let r=x(c),[a,s]=k(t.reduce((n,o)=>({...n,[o]:""}),{}));if(!r)throw new Error("You must use useDatabase inside a SparkleProvider");let{socket:e}=r,i=u(()=>{console.log("Connexion \xE9tablie"),e?.send(JSON.stringify({type:"subscribe",keys:t}))},[e,t]),p=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=i,e.onmessage=p)},[p,i,e]),a}export{c as SparkleContext,m as SparkleProvider,w 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: 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, 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,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,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":["createContext","useEffect","useState","jsx","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"]}
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, 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,OAAS,iBAAAA,EAAe,aAAAC,EAAW,YAAAC,MAAgB,QAM5C,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,EAGH,oBAACL,EAAe,SAAf,CACC,MAAO,CACL,OAAAG,CACF,GAECD,CACH,CAEJ,ECnCA,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":["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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sparkle-react",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "files": [
5
5
  "dist"
6
6
  ],