-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added delay. Fixed timefiles example
- Loading branch information
Showing
8 changed files
with
118 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[flake8] | ||
ignore = E731, T484, T400 # Do not assign a lambda expression, use a def | ||
ignore = E731, T484, T400, W503 | ||
max-line-length = 120 |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
from .eventloop import VirtualTimeEventLoop | ||
"""Testing module. | ||
Contains utilities for unit testing async observables. | ||
""" | ||
from .observer import AsyncTestObserver | ||
from .subject import AsyncSingleSubject, AsyncSubject | ||
from .virtual_events import VirtualTimeEventLoop | ||
|
||
__all__ = ["VirtualTimeEventLoop", "AsyncAnonymousObserver", "AsyncSingleSubject", "AsyncSubject"] | ||
__all__ = ["VirtualTimeEventLoop", "AsyncTestObserver", "AsyncSingleSubject", "AsyncSubject"] |
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,72 @@ | ||
import asyncio | ||
from datetime import datetime, timedelta | ||
from typing import Tuple, TypeVar | ||
|
||
from expression.core import MailboxProcessor, pipe | ||
from expression.system import CancellationTokenSource | ||
|
||
from .notification import Notification, OnError, OnNext | ||
from .observables import AsyncAnonymousObservable | ||
from .observers import AsyncAnonymousObserver, AsyncNotificationObserver | ||
from .types import AsyncDisposable, AsyncObservable, AsyncObserver, Stream | ||
|
||
TSource = TypeVar("TSource") | ||
|
||
|
||
def delay(seconds: float) -> Stream[TSource, TSource]: | ||
"""Delay observable. | ||
Time shifts the observable sequence by the given timeout. The | ||
relative time intervals between the values are preserved. | ||
Args: | ||
seconds (float): Number of seconds to delay. | ||
Returns: | ||
Stream[TSource, TSource]: Delayed stream. | ||
""" | ||
|
||
def _delay(source: AsyncObservable[TSource]) -> AsyncObservable[TSource]: | ||
cts = CancellationTokenSource() | ||
|
||
async def subscribe_async(aobv: AsyncObserver[TSource]) -> AsyncDisposable: | ||
async def worker(inbox: MailboxProcessor[Tuple[Notification, datetime]]) -> None: | ||
async def message_loop() -> None: | ||
ns, due_time = await inbox.receive() | ||
|
||
diff = due_time - datetime.utcnow() | ||
seconds = diff.total_seconds() | ||
if seconds > 0: | ||
await asyncio.sleep(seconds) | ||
|
||
if isinstance(ns, OnNext): | ||
x = ns.value | ||
await aobv.asend(x) | ||
elif isinstance(ns, OnError): | ||
err = ns.exception | ||
await aobv.athrow(err) | ||
else: | ||
await aobv.aclose() | ||
|
||
await message_loop() | ||
|
||
await message_loop() | ||
|
||
agent = MailboxProcessor.start(worker, cts.token) | ||
|
||
async def fn(ns: Notification) -> None: | ||
due_time = datetime.utcnow() + timedelta(seconds=seconds) | ||
agent.post((ns, due_time)) | ||
|
||
obv: AsyncNotificationObserver[TSource] = AsyncNotificationObserver(fn) | ||
subscription = await pipe(obv, source.subscribe_async) | ||
|
||
async def cancel() -> None: | ||
cts.cancel() | ||
await subscription.dispose_async() | ||
|
||
return AsyncDisposable.create(cancel) | ||
|
||
return AsyncAnonymousObservable(subscribe_async) | ||
|
||
return _delay |
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