Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:paths ["src" "resources"]
:deps {}}
8 changes: 0 additions & 8 deletions project.clj

This file was deleted.

35 changes: 22 additions & 13 deletions resources/ring/js/refresh.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
var pageLoadTime = new Date().getTime()
var pageLoadTime = new Date().getTime();

function reloadIfSourceChanged() {
var request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.responseText == 'true') {
window.location.reload()
}
else {
setTimeout(reloadIfSourceChanged, 200)

async function reloadIfSourceChanged() {
try {
const response = await fetch('/__source_changed', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
const text = await response.text()
const sourceChanged = parseInt(text);

if (sourceChanged >= pageLoadTime) {
window.location.reload();
}
} catch (error) {
console.error('Error:', error);
}
request.open('GET', '/__source_changed?since=' + pageLoadTime, true)
request.send()
}

window.onload = reloadIfSourceChanged
async function reloadIfSourceChangedJob() {
await reloadIfSourceChanged()
setTimeout(reloadIfSourceChangedJob, 200);
}

window.onload = reloadIfSourceChangedJob;
73 changes: 26 additions & 47 deletions src/ring/middleware/refresh.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
(ns ring.middleware.refresh
(:refer-clojure :exclude [random-uuid])
(:use [compojure.core :only (routes GET)]
[watchtower.core :only (watcher rate on-change)]
ring.middleware.params)
(:require [clojure.string :as str]
[clojure.java.io :as io])
(:import [java.util Date UUID]))
[clojure.java.io :as io]))

(defn- dir-last-modified-ts [dir]
(when-let [files (file-seq (io/file dir))]
(apply max (map #(.lastModified %) files))))

(defn- dirs-last-modified-ts [dir & dirs]
(->> (cons dir dirs)
(mapv dir-last-modified-ts)
(reduce max)))

(defn- get-request? [request]
(= (:request-method request) :get))

(defn- html-content? [response]
(if-let [content-type (get-in response [:headers "Content-Type"])]
(when-let [content-type (get-in response [:headers "Content-Type"])]
(re-find #"text/html" content-type)))

(def ^:private refresh-script
Expand All @@ -33,45 +37,22 @@
(as-str [_] nil))

(defn- add-script [body script]
(if-let [body-str (as-str body)]
(when-let [body-str (as-str body)]
(str/replace
body-str
#"<head\s*(?:\s[^\/>]+)?>"
#(str % "<script type=\"text/javascript\">" script "</script>"))))

(def ^:private last-modified
(atom (Date.)))

(defn- watch-dirs! [dirs]
(watcher dirs
(rate 100)
(on-change
(fn [_] (reset! last-modified (Date.))))))

(defn- random-uuid []
(str (UUID/randomUUID)))
#"</head>"
#(str "\n<script type=\"text/javascript\" async>\n" script "</script>\n" %))))

(defn- watch-until [reference pred timeout-ms]
(let [result (promise)
watch-key (random-uuid)]
(try
(add-watch reference
watch-key
(fn [_ _ _ value]
(when (pred value)
(deliver result true))))
(or (pred @reference)
(deref result timeout-ms false))
(finally
(remove-watch reference watch-key)))))
(defn- source-changed-handler [_ dirs]
{:status 200
:headers {"Content-Type" "application/json"}
:body (str (apply dirs-last-modified-ts dirs))})

(def ^:private source-changed-route
(GET "/__source_changed" [since]
(let [timestamp (Long. since)]
(str (watch-until
last-modified
#(> (.getTime %) timestamp)
60000)))))
(defn- intercept-source-changed-route [handler dirs]
(fn [{:keys [uri] :as request}]
(if (= uri "/__source_changed")
(source-changed-handler request dirs)
(handler request))))

(defn- wrap-with-script [handler script]
(fn [request]
Expand All @@ -91,8 +72,6 @@
([handler]
(wrap-refresh handler ["src" "resources"]))
([handler dirs]
(watch-dirs! dirs)
(wrap-params
(routes
source-changed-route
(wrap-with-script handler refresh-script)))))
(fn [request]
(let [wrapped (-> handler (intercept-source-changed-route dirs) (wrap-with-script refresh-script))]
(wrapped request)))))