Created
March 4, 2026 13:12
-
-
Save rolandpeelen/030a42bb695317febc2b7d010d689358 to your computer and use it in GitHub Desktop.
lensnest
This file contains hidden or 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
| // Nesting | |
| type action = | |
| | Auth(Auth.action) | |
| let reduce = ... | |
| | Auth(action) => | |
| nestByLens( | |
| Auth.reduce, | |
| stateAuthLens, | |
| subaction => Auth(subaction), | |
| state, | |
| action, | |
| ) | |
| // nestByLens | |
| let nestByLens: | |
| type substate subaction state action. | |
| ( | |
| (substate, subaction) => update(subaction, substate), | |
| Lens.t(state, substate), | |
| subaction => action, | |
| state, | |
| subaction | |
| ) => | |
| update(action, state) = | |
| (reduce, lens, liftAction, state, subaction) => { | |
| let substate = Lens.view(lens, state); | |
| let result = reduce(substate, subaction); | |
| let liftSideEffects = | |
| Array.map((fn, {dispatch, state}) => { | |
| let subdispatch = subAction => liftAction(subAction) |> dispatch; | |
| let substate = Lens.view(lens, state); | |
| fn({ | |
| dispatch: subdispatch, | |
| state: substate, | |
| }); | |
| }); | |
| switch (result) { | |
| | Update(substate) => Update(Lens.set(lens, substate, state)) | |
| | UpdateWithSideEffects(substate, sideEffects) => | |
| UpdateWithSideEffects( | |
| Lens.set(lens, substate, state), | |
| liftSideEffects(sideEffects), | |
| ) | |
| | SideEffects(xs) => SideEffects(liftSideEffects(xs)) | |
| | NoUpdate => NoUpdate | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment