From 37f94fed127bb89e0510b0ff664262eadc91ee40 Mon Sep 17 00:00:00 2001 From: phi Date: Wed, 5 Nov 2025 19:48:17 +0900 Subject: [PATCH] feat: configure --- src/socketio/packet.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/socketio/packet.py b/src/socketio/packet.py index 3deba7f..dea5a8e 100644 --- a/src/socketio/packet.py +++ b/src/socketio/packet.py @@ -1,4 +1,5 @@ import functools +import warnings from engineio import json as _json (CONNECT, DISCONNECT, EVENT, ACK, CONNECT_ERROR, BINARY_EVENT, BINARY_ACK) = \ @@ -21,6 +22,8 @@ class Packet: uses_binary_events = True json = _json + _configure_args = ((),()) + _subclass_registry = {} def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None, binary=None, encoded_packet=None): @@ -192,3 +195,23 @@ class Packet: if self.id is not None: d['id'] = self.id return d + + @classmethod + def configure(cls, *args, **kwargs): + configure_args = (args, tuple(sorted(kwargs.items()))) + try: + args_hash = hash(configure_args) + except TypeError: + warnings.warn('Packet.configure() called with unhashable ' + 'arguments; subclass caching will not work.', + RuntimeWarning) + args_hash = None + + if args_hash in cls._subclass_registry: + return cls._subclass_registry[args_hash] + return cls._configure(*args, **kwargs) + + @classmethod + def _configure(cls, *args, **kwargs): + raise NotImplementedError('Packet._configure() must be implemented ' + 'by subclasses.') \ No newline at end of file