Skip to content

Commit

Permalink
initial support for defplugin with respo.caches underneath; alpha rel…
Browse files Browse the repository at this point in the history
…ease
  • Loading branch information
tiye committed Jun 14, 2020
1 parent 9cd3128 commit a30fd65
Show file tree
Hide file tree
Showing 11 changed files with 1,488 additions and 166 deletions.
1,177 changes: 1,177 additions & 0 deletions calcit.cirru

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions macros/respo/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@
~@(if (empty? body)
`((js/console.warn (str "WARNING: " '~effect-name " has no code for handling effects!")))
body))})))

(defmacro defplugin [x params & body]
(let [plugin-name (gensym "plugin-")]
`(do
(defn ~plugin-name [~@params] ~@body)
(defn ~x [~@params] (respo.core/call-plugin-func ~plugin-name [~@params])))))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"homepage": "https://github.com/Respo/respo#readme",
"dependencies": {},
"devDependencies": {
"shadow-cljs": "^2.9.10",
"shadow-cljs": "^2.10.7",
"source-map-support": "^0.5.19",
"ws": "^7.3.0"
}
Expand Down
2 changes: 1 addition & 1 deletion release.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:version "0.12.1"
{:version "0.12.2-a1"
:group-id "respo"
:artifact-id "respo"
:skip-tag true
Expand Down
8 changes: 8 additions & 0 deletions scripts/plugin.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

(defmacro defplugin [x params & body]
(let [plugin-name (gensym "plugin-")]
`(do
(defn ~plugin-name [~@params] ~@body)
(defn ~x [~@params] (respo.core/call-plugin-func ~plugin-name ~params)))))

(println (macroexpand-1 '(defplugin g [a b] (+ a b))))
52 changes: 52 additions & 0 deletions src/respo/app/comp/caches.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

(ns respo.app.comp.caches
(:require [respo.core :refer [defcomp defplugin div button span >>]]
[respo.comp.space :refer [=<]]
[respo.app.style.widget :as widget]
[respo.caches :as caches]))

(defplugin
use-demo
(states)
(let [cursor (states :cursor), state (or (:data states) {:status true})]
{:ui (span {:inner-text (str "status: " (state :status))}),
:toggle (fn [d!] (d! cursor (update state :status not)))}))

(defcomp
comp-caches
(states)
(let [value-plugin (use-demo (>> states :count))]
(div
{:style {:padding 8}}
(div
{}
(div
{:inner-text "Loop",
:style widget/button,
:on-click (fn [e d!] (caches/new-loop!) (println @caches/*cache-states))})
(=< 8 nil)
(div
{:inner-text "Add cache",
:style widget/button,
:on-click (fn [e d!] (caches/write-cache! [1 2 3] 6))})
(=< 8 nil)
(div
{:inner-text "Access",
:style widget/button,
:on-click (fn [e d!] (println (caches/access-cache [1 2 3])))})
(=< 8 nil)
(div
{:inner-text "Reset",
:style widget/button,
:on-click (fn [e d!] (caches/reset-caches!))})
(=< 8 nil)
(div
{:inner-text "GC", :style widget/button, :on-click (fn [e d!] (caches/perform-gc!))}))
(=< nil 8)
(div
{}
(div
{:inner-text "Trigger",
:style widget/button,
:on-click (fn [e d!] ((:toggle value-plugin) d!))}))
(:ui value-plugin))))
8 changes: 6 additions & 2 deletions src/respo/app/comp/container.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

(ns respo.app.comp.container
(:require [respo.core :refer [defcomp div span <> >>]]
[respo.app.comp.todolist :refer [comp-todolist]]))
[respo.app.comp.todolist :refer [comp-todolist]]
[respo.app.comp.caches :refer [comp-caches]]
[respo.comp.space :refer [=<]]))

(def style-global {:font-family "Avenir,Verdana"})

Expand All @@ -14,4 +16,6 @@
(div
{:style style-global}
(comp-todolist states (:tasks store))
(div {:style style-states} (<> (str "states: " (pr-str (:states store))))))))
(div {:style style-states} (<> (str "states: " (pr-str (:states store)))))
(=< nil 40)
(comp-caches (>> states :caches)))))
65 changes: 65 additions & 0 deletions src/respo/caches.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

(ns respo.caches )

(defonce *cache-states
(atom {:loop 0, :caches {}, :gc {:cold-duration 400, :trigger-loop 100, :elapse-loop 50}}))

(defn access-cache [params]
(let [caches (@*cache-states :caches), the-loop (@*cache-states :loop)]
(if (contains? caches params)
(do
(swap!
*cache-states
update-in
[:caches params]
(fn [info] (-> info (assoc :last-hit the-loop) (update :hit-times inc))))
(:value (get caches params)))
nil)))

(defn perform-gc! []
(let [states-0 @*cache-states, gc (states-0 :gc)]
(swap!
*cache-states
update
:caches
(fn [caches]
(->> caches
(remove
(fn [[params info]]
(cond
(zero? (info :hit-times)) true
(> (- (states-0 :loop) (info :hit-loop)) (gc :elapse-loop)) true
:else false)))
(into {}))))
(println
"[Respo Caches] Performed GC, from "
(count (states-0 :caches))
" to "
(count (@*cache-states :caches)))))

(defn new-loop! []
(swap! *cache-states update :loop inc)
(let [loop-count (@*cache-states :loop), gc (@*cache-states :gc)]
(when (and (> loop-count (gc :cold-duration)) (zero? (rem loop-count (gc :trigger-loop))))
(perform-gc!))))

(defn reset-caches! [] (swap! *cache-states assoc :loop 0 :caches {}))

(defn write-cache! [params value]
(let [the-loop (@*cache-states :loop)]
(swap!
*cache-states
update
:caches
(fn [caches]
(if (contains? caches params)
(do
(println "[Respo Caches] already exisits" params)
(update
caches
params
(fn [info] (-> info (assoc :last-hit the-loop) (update :hit-times inc)))))
(assoc
caches
params
{:value value, :initial-loop the-loop, :last-hit the-loop, :hit-times 0}))))))
12 changes: 10 additions & 2 deletions src/respo/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
[respo.util.detect :refer [component? element?]]
[respo.util.dom :refer [compare-to-dom!]]
[respo.schema :as schema]
[respo.util.comparator :refer [compare-xy]])
[respo.util.comparator :refer [compare-xy]]
[respo.caches :as caches])
(:require-macros [respo.core]))

(defonce *changes-logger (atom nil))
Expand All @@ -23,7 +24,13 @@
(let [parent-cursor (or (:cursor states) []), branch (get states k)]
(assoc branch :cursor (conj parent-cursor k))))

(defn clear-cache! [] (reset! *dom-element nil))
(defn call-plugin-func [f params]
(if (some fn? params)
(apply f params)
(let [xs (concat [f] params), v (caches/access-cache xs)]
(if (some? v) v (let [result (apply f params)] (caches/write-cache! xs result) result)))))

(defn clear-cache! [] (reset! *dom-element nil) (caches/reset-caches!))

(defn confirm-child [x]
(when-not (or (nil? x) (element? x) (component? x))
Expand Down Expand Up @@ -102,6 +109,7 @@
(reset! *dom-element element)))

(defn rerender-app! [target markup dispatch!]
(caches/new-loop!)
(let [element (render-element markup)
deliver-event (build-deliver-event *global-element dispatch!)
*changes (atom [])
Expand Down
2 changes: 2 additions & 0 deletions src/respo/schema.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

(ns respo.schema )

(def cache-info {:value nil, :initial-loop nil, :last-hit nil, :hit-times 0})

(def component
{:name nil,
:respo-node :component,
Expand Down
Loading

0 comments on commit a30fd65

Please sign in to comment.