Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync package #864

Open
emil14 opened this issue Feb 5, 2025 · 0 comments
Open

sync package #864

emil14 opened this issue Feb 5, 2025 · 0 comments
Labels

Comments

@emil14
Copy link
Collaborator

emil14 commented Feb 5, 2025

WARNING: Depends on #862

Problem

Nodes are asynchronous by default. It means they can receive next messages before previous are processed. Sometimes this lead to race conditions, especially when working with side-effects (see #857 for more details).

Because of that, we need to manually introduce locks, which is boilerplate and makes code harder to reason about. For example, For component does that. However, we going to need this kind of logic more and, which is most important, we need to empower user to synchronize nodes easily when needed.

Solution

At the moment sync package only contains one component WaitGroup. We need generic component Sync that will turn async node into a sync one. Here "sync" means "will not receive next message before previous one is fully processed".

  1. Add code
  2. Add e2e test (e.g. print stream of numbers using sync.Sync instead of For asserting specific order)
import { state }

// Sync prevents node from receiving next message before previous one is processed.
def Sync<T, Y>(data T) (res Y, err error) {
    is_first state.Bool
    lock Lock<T>
    handler ISyncHandler<T, Y>?
    ---
    false -> is_first:init
    :data -> [lock:data, is_first:get]
    [is_first:then, handler] -> lock:sig
    lock -> handler -> :res
}

// ISyncHandler defines async node that needs to be synchronized.
pub interface ISyncHandler<T, Y>(T) (res Y, err error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant