qsa-observer
Advanced tools
Comparing version 0.1.1 to 1.0.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
module.exports = options => { | ||
const live = new WeakMap; | ||
const callback = records => { | ||
@@ -20,9 +21,25 @@ const {query} = options; | ||
const loop = (elements, connected, query, set = new Set) => { | ||
for (let element, i = 0, {length} = elements; i < length; i++) { | ||
for (let selectors, element, i = 0, {length} = elements; i < length; i++) { | ||
if (!set.has(element = elements[i])) { | ||
set.add(element); | ||
for (let m = matches(element), i = 0, {length} = query; i < length; i++) { | ||
if (m.call(element, query[i])) | ||
options.handle(element, connected, i); | ||
if (connected) { | ||
for (let q, m = matches(element), i = 0, {length} = query; i < length; i++) { | ||
if (m.call(element, q = query[i])) { | ||
if (!live.has(element)) | ||
live.set(element, new Set); | ||
selectors = live.get(element); | ||
if (!selectors.has(q)) { | ||
selectors.add(q); | ||
options.handle(element, connected, q); | ||
} | ||
} | ||
} | ||
} | ||
else if (live.has(element)) { | ||
selectors = live.get(element); | ||
live.delete(element); | ||
selectors.forEach(q => { | ||
options.handle(element, connected, selectors[i]); | ||
}); | ||
} | ||
loop(element.querySelectorAll(query), connected, query, set); | ||
@@ -29,0 +46,0 @@ } |
@@ -1,1 +0,1 @@ | ||
self.qsaObserver=function(e){"use strict";const t=e=>"querySelectorAll"in e,{filter:r}=[];return e.default=e=>{const l=l=>{const{query:s}=e;if(s.length)for(let e=0,{length:c}=l;e<c;e++)o(r.call(l[e].addedNodes,t),!0,s),o(r.call(l[e].removedNodes,t),!1,s)},o=(t,r,l,c=new Set)=>{for(let n,a=0,{length:u}=t;a<u;a++)if(!c.has(n=t[a])){c.add(n);for(let t=s(n),o=0,{length:c}=l;o<c;o++)t.call(n,l[o])&&e.handle(n,r,o);o(n.querySelectorAll(l),r,l,c)}},s=e=>e.matches||e.webkitMatchesSelector||e.msMatchesSelector,c=(t,r=!0)=>{o(t,r,e.query)},n=new MutationObserver(l),a=e.root||document,{query:u}=e;return n.observe(a,{childList:!0,subtree:!0}),u.length&&c(a.querySelectorAll(u)),{flush:()=>{l(n.takeRecords())},observer:n,parse:c}},e}({}).default; | ||
self.qsaObserver=function(e){"use strict";const t=e=>"querySelectorAll"in e,{filter:r}=[];return e.default=e=>{const l=new WeakMap,s=l=>{const{query:s}=e;if(s.length)for(let e=0,{length:o}=l;e<o;e++)a(r.call(l[e].addedNodes,t),!0,s),a(r.call(l[e].removedNodes,t),!1,s)},a=(t,r,s,n=new Set)=>{for(let c,d,h=0,{length:u}=t;h<u;h++)if(!n.has(d=t[h])){if(n.add(d),r)for(let t,a=o(d),n=0,{length:h}=s;n<h;n++)a.call(d,t=s[n])&&(l.has(d)||l.set(d,new Set),c=l.get(d),c.has(t)||(c.add(t),e.handle(d,r,t)));else l.has(d)&&(c=l.get(d),l.delete(d),c.forEach(t=>{e.handle(d,r,c[h])}));a(d.querySelectorAll(s),r,s,n)}},o=e=>e.matches||e.webkitMatchesSelector||e.msMatchesSelector,n=(t,r=!0)=>{a(t,r,e.query)},c=new MutationObserver(s),d=e.root||document,{query:h}=e;return c.observe(d,{childList:!0,subtree:!0}),h.length&&n(d.querySelectorAll(h)),{flush:()=>{s(c.takeRecords())},observer:c,parse:n}},e}({}).default; |
@@ -5,2 +5,3 @@ const elements = element => 'querySelectorAll' in element; | ||
export default options => { | ||
const live = new WeakMap; | ||
const callback = records => { | ||
@@ -19,9 +20,25 @@ const {query} = options; | ||
const loop = (elements, connected, query, set = new Set) => { | ||
for (let element, i = 0, {length} = elements; i < length; i++) { | ||
for (let selectors, element, i = 0, {length} = elements; i < length; i++) { | ||
if (!set.has(element = elements[i])) { | ||
set.add(element); | ||
for (let m = matches(element), i = 0, {length} = query; i < length; i++) { | ||
if (m.call(element, query[i])) | ||
options.handle(element, connected, i); | ||
if (connected) { | ||
for (let q, m = matches(element), i = 0, {length} = query; i < length; i++) { | ||
if (m.call(element, q = query[i])) { | ||
if (!live.has(element)) | ||
live.set(element, new Set); | ||
selectors = live.get(element); | ||
if (!selectors.has(q)) { | ||
selectors.add(q); | ||
options.handle(element, connected, q); | ||
} | ||
} | ||
} | ||
} | ||
else if (live.has(element)) { | ||
selectors = live.get(element); | ||
live.delete(element); | ||
selectors.forEach(q => { | ||
options.handle(element, connected, q); | ||
}); | ||
} | ||
loop(element.querySelectorAll(query), connected, query, set); | ||
@@ -28,0 +45,0 @@ } |
39
index.js
@@ -10,2 +10,4 @@ self.qsaObserver = (function (exports) { | ||
var index = (function (options) { | ||
var live = new WeakMap(); | ||
var callback = function callback(records) { | ||
@@ -29,12 +31,37 @@ var query = options.query; | ||
for (var element, i = 0, length = elements.length; i < length; i++) { | ||
if (!set.has(element = elements[i])) { | ||
set.add(element); | ||
var _loop = function _loop(_selectors, _element, i, length) { | ||
if (!set.has(_element = elements[i])) { | ||
set.add(_element); | ||
for (var m = matches(element), _i = 0, _length = query.length; _i < _length; _i++) { | ||
if (m.call(element, query[_i])) options.handle(element, connected, _i); | ||
if (connected) { | ||
for (var q, m = matches(_element), _i = 0, _length = query.length; _i < _length; _i++) { | ||
if (m.call(_element, q = query[_i])) { | ||
if (!live.has(_element)) live.set(_element, new Set()); | ||
_selectors = live.get(_element); | ||
if (!_selectors.has(q)) { | ||
_selectors.add(q); | ||
options.handle(_element, connected, q); | ||
} | ||
} | ||
} | ||
} else if (live.has(_element)) { | ||
_selectors = live.get(_element); | ||
live["delete"](_element); | ||
_selectors.forEach(function (q) { | ||
options.handle(_element, connected, _selectors[i]); | ||
}); | ||
} | ||
loop(element.querySelectorAll(query), connected, query, set); | ||
loop(_element.querySelectorAll(query), connected, query, set); | ||
} | ||
selectors = _selectors; | ||
element = _element; | ||
}; | ||
for (var selectors, element, i = 0, length = elements.length; i < length; i++) { | ||
_loop(selectors, element, i); | ||
} | ||
@@ -41,0 +68,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
self.qsaObserver=function(e){"use strict";var r=function(e){return"querySelectorAll"in e},t=[].filter;return e.default=function(e){var n=function(n){var o=e.query;if(o.length)for(var u=0,c=n.length;u<c;u++)l(t.call(n[u].addedNodes,r),!0,o),l(t.call(n[u].removedNodes,r),!1,o)},l=function r(t,n,l){for(var u,c=arguments.length>3&&void 0!==arguments[3]?arguments[3]:new Set,a=0,s=t.length;a<s;a++)if(!c.has(u=t[a])){c.add(u);for(var i=o(u),f=0,d=l.length;f<d;f++)i.call(u,l[f])&&e.handle(u,n,f);r(u.querySelectorAll(l),n,l,c)}},o=function(e){return e.matches||e.webkitMatchesSelector||e.msMatchesSelector},u=function(r){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];l(r,t,e.query)},c=new MutationObserver(n),a=e.root||document,s=e.query;return c.observe(a,{childList:!0,subtree:!0}),s.length&&u(a.querySelectorAll(s)),{flush:function(){n(c.takeRecords())},observer:c,parse:u}},e}({}).default; | ||
self.qsaObserver=function(e){"use strict";var t=function(e){return"querySelectorAll"in e},r=[].filter;return e.default=function(e){var n=new WeakMap,l=function(n){var l=e.query;if(l.length)for(var a=0,c=n.length;a<c;a++)o(r.call(n[a].addedNodes,t),!0,l),o(r.call(n[a].removedNodes,t),!1,l)},o=function t(r,l,o){for(var c,u,s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:new Set,i=function(i,f,d,h){if(!s.has(f=r[d])){if(s.add(f),l)for(var v,g=a(f),q=0,S=o.length;q<S;q++)g.call(f,v=o[q])&&(n.has(f)||n.set(f,new Set),(i=n.get(f)).has(v)||(i.add(v),e.handle(f,l,v)));else n.has(f)&&(i=n.get(f),n.delete(f),i.forEach((function(t){e.handle(f,l,i[d])})));t(f.querySelectorAll(o),l,o,s)}c=i,u=f},f=0,d=r.length;f<d;f++)i(c,u,f)},a=function(e){return e.matches||e.webkitMatchesSelector||e.msMatchesSelector},c=function(t){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];o(t,r,e.query)},u=new MutationObserver(l),s=e.root||document,i=e.query;return u.observe(s,{childList:!0,subtree:!0}),i.length&&c(s.querySelectorAll(i)),{flush:function(){l(u.takeRecords())},observer:u,parse:c}},e}({}).default; |
{ | ||
"name": "qsa-observer", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "handle elements lifecycle through CSS selectors", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
@@ -9,3 +9,3 @@ # qsa-observer | ||
* each element is granted to be handled, whenever it's observed via one, or more, selectors | ||
* all observed elements will pass through `handle(element, selector)` utility, per each matching selector | ||
* all observed elements will pass through `handle(element, connected, selector)` utility, per each matching selector | ||
* elements injected through `innerHTML`, or created offline, will be handled too once live | ||
@@ -36,5 +36,5 @@ | ||
// selectors in the query, and are either connected or disconnected | ||
handle(element, connected, index) { | ||
handle(element, connected, selector) { | ||
const event = connected ? 'connected' : 'disconnected'; | ||
console.log(element, event, query[index]); | ||
console.log(element, event, selector); | ||
} | ||
@@ -41,0 +41,0 @@ }); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12277
201
0