-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-ghaction
- Loading branch information
Showing
12 changed files
with
121 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__version__ = "0.0.4" | ||
|
||
from realtime.channel import CallbackListener, Channel | ||
from realtime.connection import Socket | ||
from realtime.exceptions import NotConnectedError | ||
from realtime.message import * | ||
from realtime.transformers import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,79 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import json | ||
from collections import namedtuple | ||
from typing import List | ||
from typing import Any, List, Dict, TYPE_CHECKING, NamedTuple | ||
|
||
from realtime.types import Callback | ||
|
||
if TYPE_CHECKING: | ||
from realtime.connection import Socket | ||
|
||
""" | ||
Callback Listener is a tuple with `event` and `callback` | ||
""" | ||
CallbackListener = namedtuple("CallbackListener", "event callback") | ||
|
||
class CallbackListener(NamedTuple): | ||
"""A tuple with `event` and `callback` """ | ||
event: str | ||
callback: Callback | ||
|
||
|
||
class Channel: | ||
""" | ||
`Channel` is an abstraction for a topic listener for an existing socket connection. | ||
Each Channel has its own topic and a list of event-callbacks that responds to messages. | ||
Should only be instantiated through `connection.Socket().set_chanel(topic)` | ||
Should only be instantiated through `connection.Socket().set_channel(topic)` | ||
Topic-Channel has a 1-many relationship. | ||
""" | ||
|
||
def __init__(self, socket, topic: str, params: dict = {}): | ||
def __init__(self, socket: Socket, topic: str, params: Dict[str, Any] = {}) -> None: | ||
""" | ||
:param socket: Socket object | ||
:param topic: Topic that it subscribes to on the realtime server | ||
:param params: | ||
""" | ||
self.socket = socket | ||
self.topic: str = topic | ||
self.params: dict = params | ||
self.params = params | ||
self.topic = topic | ||
self.listeners: List[CallbackListener] = [] | ||
self.joined: bool = False | ||
self.joined = False | ||
|
||
def join(self): | ||
def join(self) -> Channel: | ||
""" | ||
Wrapper for async def _join() to expose a non-async interface | ||
Essentially gets the only event loop and attempt joining a topic | ||
:return: None | ||
:return: Channel | ||
""" | ||
loop = asyncio.get_event_loop() | ||
loop = asyncio.get_event_loop() # TODO: replace with get_running_loop | ||
loop.run_until_complete(self._join()) | ||
return self | ||
|
||
async def _join(self): | ||
async def _join(self) -> None: | ||
""" | ||
Coroutine that attempts to join Phoenix Realtime server via a certain topic | ||
:return: Channel.channel | ||
:return: None | ||
""" | ||
join_req = dict(topic=self.topic, event="phx_join", payload={}, ref=None) | ||
join_req = dict(topic=self.topic, event="phx_join", | ||
payload={}, ref=None) | ||
|
||
try: | ||
await self.socket.ws_connection.send(json.dumps(join_req)) | ||
|
||
except Exception as e: | ||
print(str(e)) | ||
print(str(e)) # TODO: better error propagation | ||
return | ||
|
||
def on(self, event: str, callback): | ||
def on(self, event: str, callback: Callback) -> Channel: | ||
""" | ||
:param event: A specific event will have a specific callback | ||
:param callback: Callback that takes msg payload as its first argument | ||
:return: None | ||
:return: Channel | ||
""" | ||
|
||
cl = CallbackListener(event=event, callback=callback) | ||
self.listeners.append(cl) | ||
return self | ||
|
||
def off(self, event: str): | ||
def off(self, event: str) -> None: | ||
""" | ||
:param event: Stop responding to a certain event | ||
:return: None | ||
""" | ||
self.listeners = [callback for callback in self.listeners if callback.event != event] | ||
self.listeners = [ | ||
callback for callback in self.listeners if callback.event != event] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.