Skip to content

Redux and Sagas

HaileyJang edited this page Aug 20, 2020 · 1 revision

File Locations Mapped

State Container: Redux and redux-saga

In short, Redux imagines all data will reside in a store, which basically an object (similar to Python dictionaries, Java Maps, Ruby Hashes). Actions are called to initiate changes, and reducers actually create the changes. redux-saga creates threads to deal better with asynchronous reducers.

Actions define the information to be transmitted to the store, and they are defined as objects. A related concept is an Action Creator, which are functions that create Actions (i.e. fill in values for object attributes). Normally, Actions need to be dispatched to actually make changes. However, thanks to redux-react, action creation can implicitly call dispatch; this is done for Dawn's actions.

Note: In Dawn colloquially uses the word action for both regular Actions and Action Creators, given how most Actions in Dawn are created by Action Creators.

Note 2: Actions is also the main way the Main process can communicate to the renderer. This is done through RendererBridge.js.

Reducers handle state/store changes. In Dawn, we use TYPE attributes in actions to allow for switch statements, and reducers are combined in dawnApp.js for easier usage. Reducers take in (state, action) as its input and return a new state. This new state is not a mutated version of the old one; it does copy everything from the old state using ...state syntax. All normal reducers are pure functions.

Any changes to the state can be monitored with listeners, though that is not currently used in Dawn. The state can be directly accessed with getState(), though Dawn normally uses React props to pass in information for code cleanliness.

Sagas provide more features to Redux; in particular, it allows for impure functions and asynchronous operations. It actually spins off as a separate process and intercepts actions. For Dawn, we use it to deal with Main process communications, Runtime heartbeats, and File operations.

Clone this wiki locally