UNPKG

iripo

Version:

A mutation observer based livequery replacement.

3 lines (2 loc) 3.13 kB
(function(e){typeof define=="function"&&define.amd?define(e):e()})(function(){"use strict";window.requestIdleCallback=window.requestIdleCallback||function(u){var t=Date.now();return setTimeout(function(){u({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-t))}})},1)},window.cancelIdleCallback=window.cancelIdleCallback||function(u){clearTimeout(u)};const e=window.iripo={paused:!1,allFns:new Map,pausedFns:new Map,inWatchers:new Map,outWatchers:new Map,processedElems:new WeakMap,processingQueued:!1,outElems:new Map,getSymbol:function(t,n){const s=Array.from(e.allFns.entries()).find(([i,o])=>{if(i.description===t&&n.toString()===o.toString())return!0});return s&&s[0]},getFn:function(t){return e.allFns.get(t)},setAction:function(t,n,s){const i=s.get(t)||new Set;let o=e.getSymbol(t,n);return o||(o=Symbol(t),e.allFns.set(o,n),s.set(t,i.add(o))),o},in:function(t,n,s){const i=e.setAction(t,n,e.inWatchers);return s&&e.processInFns(),i},out:function(t,n){return e.setAction(t,n,e.outWatchers)},clear:function(t){e.allFns.delete(t),e.pausedFns.delete(t),[e.inWatchers,e.outWatchers].forEach(function(n){n.forEach(function(s){s.delete(t)})})},pause:function(t){return e.pausedFns.set(t,!0),t},pauseAll:function(){this.paused=!0,e.allFns.forEach(function(t,n){e.pause(n)})},resume:function(t,n){return typeof n>"u"&&(n=!0),this.paused=!1,e.pausedFns.delete(t),n&&e.processInFns(),t},resumeAll:function(){this.paused=!1,e.allFns.forEach(function(t,n){e.resume(n,!1)}),e.processInFns()},buildOutFn:function({elem:t,selector:n,fn:s}){e.outElems.has(t)?e.outElems.get(t).has(n)?e.outElems.get(t).get(n).add(s):e.outElems.get(t).set(n,new Set([s])):e.outElems.set(t,new Map([[n,new Set([s])]]))},processInFns:function(t){if(e.inWatchers.size>0){const n=Array.from(e.inWatchers.keys()).join(",");document.querySelectorAll(n).forEach(function(s){e.inWatchers.forEach(function(i,o){s.matches(o)&&i.forEach(function(a){if(!e.pausedFns.get(a)){const r=e.processedElems.get(s);if(!r||!r.has(a)){const f=e.getFn(a),d=r||new Set;e.processedElems.set(s,d.add(a)),e.buildOutFn({elem:s,selector:o,fn:()=>{var c;(c=e.processedElems.get(s))==null||c.delete(a)}}),f(s)}}})})})}if(e.outWatchers.size>0){const n=Array.from(e.outWatchers.keys()).join(",");document.querySelectorAll(n).forEach(function(s){e.outWatchers.forEach(function(i,o){s.matches(o)&&i.forEach(function(a){const r=e.getFn(a);e.buildOutFn({elem:s,selector:o,fn:r})})})})}},processOutFns:function(t){Array.from(e.outElems.entries()).forEach(([n,s])=>{Array.from(s.entries()).forEach(([i,o])=>{(!n.isConnected||!n.matches(i))&&(o.forEach(a=>a(n)),e.outElems.get(n).delete(i)),e.outElems.get(n).size||e.outElems.delete(n)})})}};window.addEventListener("DOMContentLoaded",function(t){e.paused||e.processInFns();function n(s){e.paused||e.processingQueued||(e.processingQueued=!0,requestIdleCallback(function(){e.processInFns(s),e.processOutFns(s),e.processingQueued=!1},{timeout:1e3}))}new MutationObserver(n).observe(document.documentElement||document.body,{attributes:!0,attributeOldValue:!0,childList:!0,subtree:!0})})}); //# sourceMappingURL=iripo.umd.js.map