Batch Procedures together. The elements are evaluated in order.
Construct a Procedure instance that modifies shared memory state.
The update operation, passed as the first argument, is performed atomically. It means the state of shared memory read by a particular thread with modify is not updated by another thread until it is updated by the thread.
Construct a Procedure instance that pushes a local Cmd.
Construct a Procedure instance that awaits the local events for the thread.
If it returns Nothing, it awaits again.
Otherwise, it evaluates the given Procedure.
Note1: The shared memory state passed to the first argument function may become outdated during running the thread for the Procedure generated by that function, so it is safe to use this shared memory state only to determine whether to accept or miss events.
Note2: Technically, all the modify push fork written before the await will be executed internally as a single operation. This avoids the situation where a local event triggered by a push occurs while processing tons of subsequent modifys and pushs, thus ensuring that the await always can catch the local event caused by the previous Procedures.
Note3: pushs written before an await will not necessarily cause local events in the order written. For example, if the first push sends a request to the server and it fires a local event with its result, and the second push sleeps for 0.1 seconds and then returns a local event, the first local event can fire later if the server is slow to respond. To avoid this situation, after using one push, catch it with await and use the next push, or use syncAll.
Construct a Procedure instance that awaits global events.
If it returns Nothing, it awaits again.
Otherwise, it evaluates the given Procedure.
Note that the shared memory state passed to the first argument function may become outdated during running the thread for the Procedure generated by that function, so it is safe to use this shared memory state only to determine whether to accept or miss events.
Construct a Procedure instance that evaluates the given Procedure in a forked thread.
The forked thread runs independently of the original thread.
i.e., The subsequent Procedures in the original thread are evaluated immediately.
Construct a Procedure instance that wait for all given Procedures to be completed.
Given Procedures are evaluated in the independent thread, but the subsequent Procedures in the original thread are not evaluated immediately.
Quit the thread immediately.
Subsequent Procedures are not evaluated and are discarded.
Modify the shared memory atomically, creating the intermediate value in the process, and pass the value to another Procedure.
The intermediate value is supposed to be the identifier of the certain resource at a particular time. e.g., In an application that allows the user to select and edit the goat profile from a list of goat profiles, the information about "which goat was selected" can be the intermediate value.
For a sample, see sample/src/Advanced.elm and its live demo.
Sometimes you have Procedure with recursive structure, like awaiting events again.
You can use lazy to make sure your procedure unrolls lazily.
procedure : Procedure Shared Global Local
procedure =
Procedure.batch
[ putLog "Thread started."
, Procedure.await <|
\global _ ->
case global of
ChangeInput str ->
Just <|
Procedure.batch
[ Procedure.modify <|
\shared ->
{ shared | input = str }
, Procedure.lazy <| \_ -> procedure
]
ClickSave ->
Just <|
Procedure.modify <|
\shared ->
{ shared
| input = ""
, saved = shared.saved ++ [ shared.input ]
}
_ ->
Nothing
]You can use fork for a similar purpose, but whereas fork creates a new thread for the given Procedure and the original thread proceeds immediately to the next Procedure, lazy does the given Procedure in the original thread and then proceeds to the subsequent Procedure after it is finished.
This property can be used to create threads that do not complete until the user input is complete. In combination with modifyAndThen, such threads are convenient for resource management. See sample/src/Advanced.elm and its live demo for more information.
These items are needed when you try to build a hierarchy of shared memory and events in an SPA. Note that the pattern often unnecessarily increases complexity, so you should first consider using monolithic shared memory and events.
For a sample, see sample/src/SPA.elm.
It is recommended to use Thread.Browser for normal use.
Procedures to be processed in a thread.