Files
pytchat-fork/pytchat/core_async/buffer.py
taizan-hokouto 16df63c14e Fix comments
2020-10-24 16:10:04 +09:00

39 lines
862 B
Python

import asyncio
class Buffer(asyncio.Queue):
'''
Buffer for storing chat data.
Parameter
---------
maxsize : int
Maximum number of chat blocks to be stored.
If it exceeds the maximum, the oldest chat block will be discarded.
'''
def __init__(self, maxsize=0):
super().__init__(maxsize)
async def put(self, item):
if item is None:
return
if super().full():
super().get_nowait()
await super().put(item)
def put_nowait(self, item):
if item is None:
return
if super().full():
super().get_nowait()
super().put_nowait(item)
async def get(self):
ret = []
ret.append(await super().get())
while not super().empty():
ret.append(super().get_nowait())
return ret