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

Implement task schedule #202

Open
ArtiomTr opened this issue Mar 22, 2022 · 0 comments
Open

Implement task schedule #202

ArtiomTr opened this issue Mar 22, 2022 · 0 comments
Assignees
Labels
breaking changes Changes causing the next version to be incompatible with the previous ones

Comments

@ArtiomTr
Copy link
Member

The problem

There is an issues with current reactive-forms architecture, because it not enforces task sequence.

Currently, it follows this flow:

sequenceDiagram
    autonumber
    app ->> reactive-forms: setFieldValue
    reactive-forms ->> values stock: setValue
    values stock ->> reactive-forms: batchUpdate
    par 
        reactive-forms ->> reactive-forms: validators (async)
    and
        values stock ->> reactive-forms: State updates
    and
        values stock ->> app: External watches, from app
    end
    Note over values stock, app: All watch updates are running in parallel, after batch update
Loading

As you can see, tasks validators, state updates and external watches are running in parallel. So the issue appears, when some external watch calls setFieldValue - this will eventually make two different validateBranch calls, where first one will try to validate old values copy, and second one will try to validate new values.

Possible solution

This architecture could be used, to prevent "racing condition":

flowchart LR
subgraph stocked
notifySubtree --> |while notification queue is not empty|notifySubtree
notifySubtree --> batchedUpdate
stateUpdate
end
subgraph scheduler
enqueueNotifications --> |on next frame|notifySubtree
enqueueStateUpdates --> |on next frame, if notification queue is empty|stateUpdate
batchedUpdate --> enqueueStateUpdates
end
subgraph reactive-forms
setFieldValue --> enqueueNotifications
batchedUpdate --> validators
end
Loading

Alternative solutions

React team develops scheduler package. However, this package has experimental, undocumented API. Also, this package is relatively large, 4.83 kb production minified build.

@ArtiomTr ArtiomTr added the breaking changes Changes causing the next version to be incompatible with the previous ones label Mar 22, 2022
@ArtiomTr ArtiomTr self-assigned this Mar 22, 2022
@ArtiomTr ArtiomTr changed the title Implement task schedulement Implement task schedule Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking changes Changes causing the next version to be incompatible with the previous ones
Projects
None yet
Development

No branches or pull requests

1 participant