-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pauli Jaakkola
committed
Aug 5, 2020
1 parent
ca2449f
commit c59f918
Showing
5 changed files
with
53 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,46 @@ | ||
(ns fell.error | ||
"Error effect." | ||
(:require [cats.monad.either :refer [left right]] | ||
[fell.core :refer [pure request-eff handle-relay]])) | ||
(:require [cats.core :refer [mlet fmap extract]] | ||
[cats.monad.either :as either :refer [left right #?@(:cljs [Left Right])]] | ||
[fell.eff :refer [Effect weave #?@(:cljs [Pure Impure])]] | ||
[fell.queue :as q :refer [singleton-queue apply-queue]] | ||
[fell.core :refer [pure impure request-eff]]) | ||
#?(:clj (:import [cats.monad.either Left Right] | ||
[fell.eff Pure Impure]))) | ||
|
||
(defn make | ||
"Given the request keyword `tag`, return {:raise [[raise]]-for-`tag`, :run [[run-error]]-for-`tag`}." | ||
[tag] | ||
{:raise (fn [err] (request-eff [tag err])) | ||
:run (fn [eff] | ||
(handle-relay tag | ||
(comp pure right) | ||
(fn [[_ err] _] (pure (left err))) | ||
eff))}) | ||
(defrecord Raise [error] | ||
Effect | ||
(weave [self _ _] self)) | ||
|
||
(let [{:keys [raise run]} (make ::error)] | ||
(def raise | ||
"`(raise err)` is an Eff that raises an Error effect `err`." | ||
raise) | ||
(defrecord Handle [body on-error] | ||
Effect | ||
(weave [_ suspension handler] | ||
(Handle. (handler (fmap (constantly body) suspension)) | ||
(fn [error] (handler (fmap (constantly (on-error error)) suspension)))))) | ||
|
||
(def run-error | ||
"`(run-error eff)` runs the Error effect in `eff`, returning [[cats.monad.either/left]] | ||
if there was an error and [[cats.monad.either/right]] otherwise." | ||
run)) | ||
(defn raise [error] (request-eff (Raise. error))) | ||
|
||
(defn handle [body on-error] (request-eff (Handle. body on-error))) | ||
|
||
(declare run-error) | ||
|
||
(defn- resume-error [suspension] | ||
(condp instance? suspension | ||
Left (pure suspension) | ||
Right (run-error (extract suspension)))) | ||
|
||
(defn run-error [eff] | ||
(condp instance? eff | ||
Pure (pure (right (extract eff))) | ||
Impure (let [request (.-request eff) | ||
cont (.-cont eff)] | ||
(condp instance? request | ||
Raise (pure (left (.-error request))) | ||
Handle (mlet [status (run-error (.-body request))] | ||
(condp instance? status | ||
Left (mlet [status (run-error ((.-on_error request) (extract status)))] | ||
(condp instance? status | ||
Left (pure status) | ||
Right (run-error (apply-queue cont (extract status))))) | ||
Right (run-error (apply-queue cont (extract status))))) | ||
(fell.core/weave eff (right nil) resume-error))))) |
This file contains 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
This file contains 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