(state: State, action: Action)
| 112 | const init: State = { step: "url", url: "" }; |
| 113 | |
| 114 | function reducer(state: State, action: Action): State { |
| 115 | return Match.value(action).pipe( |
| 116 | Match.discriminator("type")("set-url", (a): State => ({ step: "url", url: a.url })), |
| 117 | Match.discriminator("type")( |
| 118 | "probe-start", |
| 119 | (): State => ({ |
| 120 | step: "probing", |
| 121 | url: state.url, |
| 122 | probe: "probe" in state ? state.probe : null, |
| 123 | }), |
| 124 | ), |
| 125 | Match.discriminator("type")( |
| 126 | "probe-ok", |
| 127 | (a): State => ({ step: "probed", url: state.url, probe: a.probe }), |
| 128 | ), |
| 129 | Match.discriminator("type")( |
| 130 | "probe-fail", |
| 131 | (a): State => ({ |
| 132 | step: "error", |
| 133 | url: state.url, |
| 134 | probe: null, |
| 135 | error: a.error, |
| 136 | }), |
| 137 | ), |
| 138 | Match.discriminator("type")("add-start", (): State => { |
| 139 | const probe = "probe" in state ? state.probe : null; |
| 140 | if (!probe) return state; |
| 141 | return { step: "adding", url: state.url, probe }; |
| 142 | }), |
| 143 | Match.discriminator("type")("add-fail", (a): State => { |
| 144 | if (state.step !== "adding") return state; |
| 145 | return { |
| 146 | step: "error", |
| 147 | url: state.url, |
| 148 | probe: state.probe, |
| 149 | error: a.error, |
| 150 | }; |
| 151 | }), |
| 152 | Match.discriminator("type")("retry", (): State => { |
| 153 | if (state.step !== "error") return state; |
| 154 | return state.probe |
| 155 | ? { step: "probed", url: state.url, probe: state.probe } |
| 156 | : { step: "url", url: state.url }; |
| 157 | }), |
| 158 | Match.exhaustive, |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | // --------------------------------------------------------------------------- |
| 163 | // Component |
nothing calls this directly
no outgoing calls
no test coverage detected