Skip to content

Commit

Permalink
use args and kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Feb 6, 2025
1 parent d38a518 commit 3fccf0f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions js_tests/native.dict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ test("native_dict_length", async () => {
expect(await dct.length()).toBe(2);
await dct.set("7", "A7");
expect(await dct.length()).toBe(3);

const dct2 = new Dict({ client: client, key: "dict:test2" });
expect(await dct2.length()).toBe(0);
});


Expand Down
3 changes: 3 additions & 0 deletions js_tests/native.list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ test("native_list_length", async () => {
expect(await lst.length()).toBe(1);
await lst.push(5);
expect(await lst.length()).toBe(2);

const lst2 = new List({ client: client, key: "list:test2" });
expect(await lst2.length()).toBe(0);
});

test("native_list_push_socket_callback_self", async () => {
Expand Down
10 changes: 5 additions & 5 deletions znsocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def refresh(data: RefreshDataTypeDict):
if not self.decode_responses:
raise NotImplementedError("decode_responses=False is not supported yet")

def call(self, event: str, data: t.Any) -> t.Any:
def call(self, event: str, *args, **kwargs) -> t.Any:
"""Call an event on the server."""
if self.delay_between_calls:
time_since_last_call = datetime.datetime.now() - self._last_call
Expand All @@ -134,7 +134,7 @@ def call(self, event: str, data: t.Any) -> t.Any:

for idx in reversed(range(self.retry + 1)):
try:
return self.sio.call(event, data, namespace=self.namespace)
return self.sio.call(event, [args, kwargs], namespace=self.namespace)
except socketio.exceptions.TimeoutError:
if idx == 0:
raise
Expand All @@ -143,7 +143,7 @@ def call(self, event: str, data: t.Any) -> t.Any:

def _redis_command(self, command, *args, **kwargs):
"""Generic handler for Redis commands."""
result = self.call(command, [args, kwargs])
result = self.call(command, *args, **kwargs)

if result is None:
raise exceptions.ZnSocketError("No response from server")
Expand Down Expand Up @@ -201,7 +201,7 @@ def _send_message(self, message) -> list:
"""Send a message to the server and process the response."""
result = self.client.call(
"pipeline",
{"pipeline": message},
message=message,
)

if result is None:
Expand All @@ -225,7 +225,7 @@ def execute(self):
)
results.extend(self._send_message(message))
message = []
if message:
if len(message) > 0:
results.extend(self._send_message(message))

return results
8 changes: 5 additions & 3 deletions znsocket/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ def attach_events(
@sio.on("*", namespace=namespace)
def handle_all_events(event, sid, data):
"""Handle any event dynamically by mapping event name to storage method."""
args, kwargs = data
if hasattr(storage, event):
try:
result = {"data": getattr(storage, event)(*data[0], **data[1])}
result = {"data": getattr(storage, event)(*args, **kwargs)}
if isinstance(result["data"], set):
result["data"] = list(result["data"])
result["type"] = "set"
Expand All @@ -324,9 +325,10 @@ def refresh(sid, data: RefreshDataTypeDict) -> None:

@sio.event(namespace=namespace)
def pipeline(sid, data):
commands = data.pop("pipeline")
args, kwargs = data
message = kwargs.pop("message")
results = []
for cmd in commands:
for cmd in message:
event = cmd[0]
args = cmd[1][0]
kwargs = cmd[1][1]
Expand Down

0 comments on commit 3fccf0f

Please sign in to comment.