Git Product home page Git Product logo

Comments (3)

nardi avatar nardi commented on July 22, 2024 1

I created my own helper class, turned out it wasn't too difficult :) I've posted the code below. Probably not conformant to your operator API, but fine for my needs ATM.

from aioreactive import AsyncAnonymousObserver
from aioreactive.subject import AsyncSubject
from asyncinit import asyncinit # External package
from .utils import dropargs # Wrapper function which drops arguments

@asyncinit
class BufferOperator():
    async def __init__(self, boundaries):
        self.emitter = AsyncSubject()
        self.buffer = []

        async def emit_buffer():
            buffer = self.buffer
            self.buffer = []
            await self.emitter.asend(buffer)

        self.subs = [
            await boundaries.subscribe_async(AsyncAnonymousObserver(dropargs(emit_buffer)))
        ]

    async def __call__(self, receiver):
        async def add_to_buffer(x):
            self.buffer.append(x)

        self.subs.append(await receiver.subscribe_async(AsyncAnonymousObserver(add_to_buffer)))
        return self

    async def subscribe_async(self, *args, **kwargs):
        return await self.emitter.subscribe_async(*args, **kwargs)

    async def dispose_async(self):
        for sub in self.subs:
            await sub.dispose_async()
        self.buffer.clear()

async def buffer(boundaries):
    return await BufferOperator(boundaries)

# Used as follows:
values = AsyncSubject()
boundaries = AsyncSubject()
grouped_values = await pipe(
    values,
    await buffer(boundaries)
)
# Then grouped_values can be subscribed to.

from aioreactive.

dbrattli avatar dbrattli commented on July 22, 2024

Thanks. I'll have a look and see if I can re-use it!

from aioreactive.

austinnichols101 avatar austinnichols101 commented on July 22, 2024

I just came across this while looking for buffer functions (specifically rx.operators.buffer_with_time_or_count) for aioreactive.

Would be nice to have buffer parity with rxpy, but sadly I am not (currently) smart enough to write it...

from aioreactive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.