-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial support for defplugin with respo.caches underneath; alpha rel…
…ease
- Loading branch information
Showing
11 changed files
with
1,488 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 +1,4 @@ | ||
{:version "0.12.1" | ||
{:version "0.12.2-a1" | ||
:group-id "respo" | ||
:artifact-id "respo" | ||
:skip-tag true | ||
|
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 |
---|---|---|
@@ -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)))) |
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 |
---|---|---|
@@ -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)))) |
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 |
---|---|---|
@@ -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})))))) |
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
Oops, something went wrong.