httpstate 0.0.11__tar.gz → 0.0.13__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpstate
3
- Version: 0.0.11
3
+ Version: 0.0.13
4
4
  Summary: HTTP State, httpstate.com
5
5
  Author-email: "Alex Morales, HTTP State" <alex@httpstate.com>
6
6
  License-Expression: AGPL-3.0
@@ -74,6 +74,9 @@ That's it! 🐙
74
74
 
75
75
  - `HttpState(uuid)`
76
76
  Create a reactive state instance of UUIDv4.
77
+
78
+ <br>
79
+
77
80
  - `<HttpState>.get()`
78
81
  Get state.
79
82
  - `<HttpState>.read()`
@@ -60,6 +60,9 @@ That's it! 🐙
60
60
 
61
61
  - `HttpState(uuid)`
62
62
  Create a reactive state instance of UUIDv4.
63
+
64
+ <br>
65
+
63
66
  - `<HttpState>.get()`
64
67
  Get state.
65
68
  - `<HttpState>.read()`
@@ -16,7 +16,7 @@ license-files = ["LICEN[CS]E*"]
16
16
  name = "httpstate"
17
17
  readme = "README.md"
18
18
  requires-python = ">=3.10"
19
- version = "0.0.11"
19
+ version = "0.0.13"
20
20
 
21
21
  [project.url]
22
22
  Homepage = "https://httpstate.com"
@@ -52,19 +52,51 @@ def write(uuid:str, data:str) -> None|int:
52
52
  class HttpState:
53
53
  def __init__(self, uuid:str):
54
54
  self.data:None|str = None
55
+ self.el:None|asyncio.AbstractEventLoop = None
55
56
  self.et:Dict[str, List[Callable[[None|str], None]]] = {}
56
57
  self.uuid:str = uuid
57
58
  self.ws:None|websockets.WebSocketClientProtocol = None
58
59
 
60
+ threading.Thread(
61
+ daemon=True,
62
+ target=self._el
63
+ ).start()
64
+
59
65
  threading.Thread(
60
66
  daemon=True,
61
67
  target=lambda : asyncio.run(self._ws())
62
68
  ).start()
69
+
70
+ def _el(self):
71
+ self.el = asyncio.new_event_loop()
72
+
73
+ asyncio.set_event_loop(self.el)
74
+
75
+ self.el.call_soon_threadsafe(lambda : self.get())
76
+
77
+ self.el.run_forever()
63
78
 
64
79
  async def _ws(self):
65
80
  self.ws = await websockets.connect(f"wss://httpstate.com/{self.uuid}")
66
81
 
67
82
  await self.ws.send(f'{{"open":"{self.uuid}"}}')
83
+ self.emit('open')
84
+
85
+ async def data():
86
+ async for data in self.ws:
87
+ data = data.decode()
88
+
89
+ if(
90
+ data
91
+ and len(data) > 32
92
+ and data[:32] == self.uuid
93
+ and data[45] == '1'
94
+ ):
95
+ self.data = data[46:]
96
+
97
+ self.emit('change', self.data)
98
+
99
+ asyncio.create_task(data())
68
100
 
69
101
  async def interval():
70
102
  while True:
@@ -76,26 +108,30 @@ class HttpState:
76
108
  break
77
109
 
78
110
  asyncio.create_task(interval())
111
+
112
+ await asyncio.Event().wait()
113
+
114
+ def destroy(self):
115
+ pass
79
116
 
80
- async for data in self.ws:
81
- self.data = data.decode()
82
-
83
- if(
84
- self.data
85
- and len(self.data) > 32
86
- and self.data[:32] == self.uuid
87
- and self.data[45] == '1'
88
- ):
89
- self.emit('change', self.data[46:])
90
-
91
- def emit(self, type:str, data:None|str) -> None:
117
+ def emit(self, type:str, data:None|str = None):
92
118
  for callback in self.et.get(type, []):
93
- callback(data)
119
+ if(data is None):
120
+ callback()
121
+ else:
122
+ callback(data)
94
123
 
95
124
  return self
96
125
 
97
126
  def get(self) -> None|str:
98
- return get(self.uuid)
127
+ data = get(self.uuid)
128
+
129
+ if(data != self.data):
130
+ self.el.call_soon_threadsafe(lambda : self.emit('change', self.data))
131
+
132
+ self.data = data
133
+
134
+ return self.data
99
135
 
100
136
  def off(self, type:str, callback:Callable[[None|str], None]):
101
137
  if type in self.et:
@@ -109,7 +145,7 @@ class HttpState:
109
145
 
110
146
  return self
111
147
 
112
- def on(self, type:str, callback:Callable[[None|str], None]) -> None:
148
+ def on(self, type:str, callback:Callable[[None|str], None]):
113
149
  if type not in self.et:
114
150
  self.et[type] = []
115
151
 
@@ -118,10 +154,10 @@ class HttpState:
118
154
  return self
119
155
 
120
156
  def read(self) -> None|str:
121
- return read(self.uuid)
157
+ return self.get()
122
158
 
123
159
  def set(self, data:str) -> None|int:
124
160
  return set(self.uuid, data)
125
161
 
126
162
  def write(self, data:str) -> None|int:
127
- return write(self.uuid, data)
163
+ return self.set(data)
File without changes
File without changes