繁体   English   中英

在 Clojure doseq function 中间暂停?

[英]Pause in the middle of a Clojure doseq function?

我的程序允许用户将 select 文件导入数据库。 在此之前,它一次只允许他们导入一个文件。 让他们一次导入多个文件很容易。 但我遇到的问题是,如果数据库已经包含一个具有相同标题的页面,我想在使用正在导入的版本覆盖数据库中的版本之前显示警告并获得确认。

这是我到目前为止所拥有的。 它在很大程度上遵循了我导入单个文件的做法。

;; Handler for the POST "/import" route.
(defn- post-import-page
  "Import the file(s) specified in the upload dialog. Checks the page
  title of the import file against existing pages in the database. If
  a page of the same name already exists, asks for confirmation before
  importing."
  [{{file-info "file-info"
     referer   "referer"} :multipart-params :as req}]
  (let [file-vec (if (map? file-info)
                   (conj [] file-info)
                   file-info)]
    (doseq [fm file-vec]
      (let [file-name (:filename fm)
            import-map (files/load-markdown-from-file (:tempfile fm))
            page-title (get-in import-map [:meta :title])
            id-exists? (db/title->page-id page-title)]
        (if id-exists?
          (build-response
            (layout/compose-import-existing-page-warning
              import-map file-name referer) req)
          (do-the-import import-map file-name req))))))

此 function 导入数据库中尚不存在的任何文件,但不会导入任何会覆盖具有相同标题的现有数据库条目的任何文件。 它也从不显示要求确认的警告页面。

警告页面的构造如下:

(defn compose-import-existing-page-warning
  "Return a page stating that a page with the same title already exists 
  in the wiki. Ask the user to proceed or cancel the import."
  [import-map file-name referer]
  (short-form-template
    [:div {:class "cwiki-form"}
     (form-to {:enctype      "multipart/form-data"
               :autocomplete "off"}
              [:post "proceed-with-import"]
              (hidden-field "import-map" import-map)
              (hidden-field "file-name" file-name)
              (hidden-field "referer" referer)
              [:p {:class "form-title"} "Page Already Exists"]
              [:div {:class "form-group"}
               [:p (str "A page with the title \"" (get-in import-map [:meta :title])
                        "\" already exists in the wiki.")]
               [:p (str "Click \"Proceed\" to delete the existing page and "
                        "replace it with the contents of the imported file.")]
               [:div {:class "button-bar-container"}
                (submit-button {:id    "proceed-with-import-button"
                                :class "form-button button-bar-item"}
                               "Proceed")
                [:input {:type      "button" :name "cancel-button"
                         :value     "Cancel"
                         :class     "form-button button-bar-item"
                         :autofocus "autofocus"
                         :onclick   "window.history.back();"}]]])]))

程序如何在doseq (或其他循环函数)中间暂停以显示确认页面并等待用户做出选择?

只需在循环中间使用read-line ,然后使用if来选择所需的分支。 以下是您可能会发现有用的其他文档列表,尤其是Clojure CheatSheet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM