-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The idea is to track depth and break complex markup into chunks and continue rendering in body if over budget.
- Loading branch information
Showing
8 changed files
with
176 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
(ns devtools.formatters.budgeting | ||
(:require-macros [devtools.util :refer [oget oset ocall oapply safe-call]]) | ||
(:require [devtools.formatters.templating :refer [render-markup]] | ||
[devtools.formatters.state :refer [get-depth-budget set-depth-budget]] | ||
[devtools.formatters.markup :refer [<header-expander>]])) | ||
|
||
; This functionality provides a workaround to issue #22 (https://github.com/binaryage/cljs-devtools/issues/22). | ||
; The idea is to track hierarchy depth for json-ml(s) we are generating. | ||
; If we are about to cross the depth limit hardcoded in WebKit we instead | ||
; render simple expandable placeholders which resume full rendering in their bodies (when expanded by user). | ||
|
||
; this is hardcoded in InjectedScriptSource.js in WebKit, look for maxCustomPreviewRecursionDepth | ||
(def initial-hierarchy-depth-budget (dec 20)) | ||
|
||
; we need to reserve some levels for our :header-expander-tag | ||
(def header-expander-cost 3) | ||
|
||
; -- tracking over-budget values ------------------------------------------------------------------------------------------- | ||
; note: phantomjs does not have WeakSet, so we have to emulate it when testing | ||
|
||
(def over-budget-values (if (exists? js/WeakSet) | ||
(js/WeakSet.) | ||
(volatile! #{}))) | ||
|
||
(defn add-over-budget-value! [value] | ||
(if (volatile? over-budget-values) | ||
(vreset! over-budget-values (conj @over-budget-values value)) | ||
(ocall over-budget-values "add" value))) | ||
|
||
(defn delete-over-budget-value! [value] | ||
(if (volatile? over-budget-values) | ||
(vreset! over-budget-values (disj @over-budget-values value)) | ||
(ocall over-budget-values "delete" value))) | ||
|
||
(defn has-over-budget-value? [value] | ||
(if (volatile? over-budget-values) | ||
(contains? @over-budget-values value) | ||
(ocall over-budget-values "has" value))) | ||
|
||
; -- helpers ---------------------------------------------------------------------------------------------------------------- | ||
|
||
(defn object-reference? [v] | ||
(= (first v) "object")) | ||
|
||
(defn determine-depth [v] | ||
(if (array? v) | ||
(if (object-reference? v) | ||
(+ 1 header-expander-cost) | ||
(inc (apply max (map determine-depth v)))) | ||
0)) | ||
|
||
(defn transfer-remaining-depth-budget! [object-reference depth-budget] | ||
{:pre [(pos? depth-budget)]} | ||
(let [data (second object-reference) | ||
_ (assert (object? data)) | ||
config (oget data "config")] | ||
(oset data ["config"] (set-depth-budget config depth-budget)))) | ||
|
||
(defn distribute-budget! [json-ml depth-budget] | ||
{:pre [(pos? depth-budget)]} | ||
(if (array? json-ml) | ||
(let [new-depth-budget (dec depth-budget)] | ||
(if (object-reference? json-ml) | ||
(transfer-remaining-depth-budget! json-ml (- new-depth-budget header-expander-cost)) | ||
(doseq [item json-ml] | ||
(distribute-budget! item new-depth-budget))))) | ||
json-ml) | ||
|
||
; -- api -------------------------------------------------------------------------------------------------------------------- | ||
|
||
(defn was-over-budget?! [value] | ||
(when (has-over-budget-value? value) | ||
(delete-over-budget-value! value) | ||
true)) | ||
|
||
(defn alter-json-ml-to-fit-in-remaining-budget! [value json-ml] | ||
(let [remaining-depth-budget (or (get-depth-budget) initial-hierarchy-depth-budget) | ||
depth (determine-depth json-ml)] | ||
#_(.log js/console "D" depth remaining-depth-budget (- remaining-depth-budget depth) | ||
(>= (- remaining-depth-budget depth) header-expander-cost) (binding [*print-level* 3] | ||
(pr-str json-ml))) | ||
(if (pos? (- remaining-depth-budget depth)) | ||
(distribute-budget! json-ml remaining-depth-budget) | ||
(let [expander-ml (render-markup (<header-expander> value))] | ||
(add-over-budget-value! value) | ||
;(.log js/console "Z" (determine-depth expander-ml) expander-ml) | ||
expander-ml)))) |
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
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