Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State change debounce #50

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Declarative DOM tree composition, reusable components, reactive state binding -
* Convert HTML snippet to **VanJS** code with our online [HTML to **VanJS** Converter](https://vanjs.org/convert)
* Want server-side rendering? Check out [Mini-Van](https://github.com/vanjs-org/mini-van) (the entire vanjs.org site is built on top of Mini-Van)
* For questions, feedback or general discussions, visit our [Discussions](https://github.com/vanjs-org/van/discussions) page
* [How did **VanJS** get its name?](https://vanjs.org/about#name)

## Support & Feedback

Expand Down
31 changes: 5 additions & 26 deletions src/van.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
// Auto-Append feature
let _base; // Global element stores current insert position. If empty, elements are added at the end of document.
let _baseStack = []; // Stack for storing _base positions

// set a new basepoint for append, save previous on baseStack
function begin(ID) {
_baseStack.push(_base);// Save old base
if (_baseStack.length > 100) throw new Error("_baseStackOverflow in begin()");

_base = (typeof (ID) === 'string') ? document.getElementById(ID) : ID
return _base
}
// restore last base, cnt: call multiple times
function end(cnt = 1) {
if (cnt > 1) end(cnt - 1)
if (_baseStack.length <= 0) throw new Error("_baseStack empty in end()")
return (_base = _baseStack.pop()) // restore old stack
}

// This file consistently uses `let` keyword instead of `const` for reducing the bundle size.

// Aliasing some builtin symbols to reduce the bundle size.
Expand All @@ -25,7 +6,7 @@ let Obj = Object, _undefined, protoOf = Object.getPrototypeOf
let addAndScheduleOnFirst = (set, s, func, waitMs) =>
(set ?? (setTimeout(func, waitMs), new Set)).add(s)

let changedStates
let changedStates, ur

let stateProto = {
get "val"() { return this._val },
Expand All @@ -35,7 +16,7 @@ let stateProto = {
let s = this, curV = s._val
if (v !== curV) {
if (s.oldVal === curV)
changedStates = addAndScheduleOnFirst(changedStates, s, updateDoms)
changedStates = addAndScheduleOnFirst(changedStates, s, updateDoms, ur)
else if (v === s.oldVal)
changedStates.delete(s)
s._val = v
Expand Down Expand Up @@ -74,9 +55,7 @@ let tags = new Proxy((name, ...args) => {
else if (protoOf(v) === objProto) bind(...v["deps"], (...deps) => (setter(v["f"](...deps)), dom))
else setter(v)
}
let r = add(dom, ...children)
if (_base) add(_base, r) // auto-Append, if base not empty
return r
return add(dom, ...children)
}, {get: (tag, name) => tag.bind(_undefined, name)})

let filterBindings = s => s.bindings = s.bindings.filter(b => b.dom?.isConnected)
Expand Down Expand Up @@ -110,5 +89,5 @@ let bind = (...deps) => {
}
return binding.dom
}

export default {add, tags, state, bind, begin, end}
let rate = (ms) => ur = ms
export default {add, tags, state, bind, rate}