-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.min.js
1 lines (1 loc) · 2.92 KB
/
index.min.js
1
(function(){'use strict';const h=(obj,proto=null)=>Object.assign(Object.create(proto),obj),i=1,j=-1,k=h({addEventListener:{callbackIndex:1,type:i},removeEventListener:{callbackIndex:1,type:j},setTimeout:{callbackIndex:0,type:i},clearTimeout:{callbackIndex:0,type:j},setInterval:{callbackIndex:0,type:i},clearInterval:{callbackIndex:0,type:j},on:{callbackIndex:1,type:i},off:{callbackIndex:1,type:j},removeListener:{callbackIndex:1,type:j}}),l=name=>!!k[name],m=name=>k[name],n=(name,config)=>!k[name]?!!(k[name]=config):!1,o=h({add:'addEventListener',remove:'removeEventListener'}),p=alias=>o[alias]?o[alias]:alias,q=(alias,name)=>!o[alias]?!!(o[alias]=name):!1,r=/^\[object/,s=obj=>r.test(Object.prototype.toString.call(obj)),t=fn=>typeof fn=='function',u=number=>number===Number(number),v=obj=>t(obj[Symbol.iterator]),w=Symbol('stroxy-stream');function a(A,B){B=B.slice();const C=B.indexOf(A);B.splice(C,1);return B}function b(A,B){A=A.slice();return A.map(C=>{C._value=C.pipes.reduce((val,fn)=>fn(val,B),B);return C})}function c(A){A.valueListeners.forEach(({listener:B,instance:C})=>{B(C._value)})}function d(A){return A.parent?A.parent:A}const y={[w]:!0,instances:[],valueListeners:[],pipe(A){if(!this.parent){const B=h({_value:this._value,parent:this,pipes:[A],valueOf:null},this);this.instances.push(B);return B}this.pipes.push(A);return this},onValue(A){const B=d(this);B.valueListeners.push({listener:A,instance:this})},offValue(A){const B=d(this);B.valueListeners=a(A,B.valueListeners)},remove(A){if(this.parent){console.warn('Cannot remove child stream from another child stream.Did you want to remove it from the parent stream?');return!1}this.instances=a(A,this.instances);this.valueListeners=this.valueListeners.filter(({instance:listenerInstance})=>listenerInstance!==A)},event(){if(this.eventFunction){return this.eventFunction}return this.eventFunction=A=>{if(this.parent){console.warn(`Child streams can't have their own event listeners.`);return!1}this._value=A;this.instances=b(this.instances,A);c(this)}}};function e(){return h({instances:[],valueListeners:[]},y)}const z=Symbol('stroxy');function f(A,B){const C=v(A),D=C?A[0][B]:A[B];if(!t(D)){return A[B]}return(...E)=>{let F,G;const{type:H,callbackIndex:I}=m(B);if(u(I)){if(H===i){F=e()}else{[F]=E.splice(I,1);if(F.parent){F.parent.remove(F);return}}const J=F.event().hasOwnProperty('valueOf')?F.event().valueOf():F.event();E.splice(I,null,J)}if(C){Array.from(A).forEach(J=>{Reflect.apply(J[B],J,E)})}else{G=Reflect.apply(A[B],A,E);if(F&&G!=null){F.event().valueOf=x=>G}}return F}}function g(A,B){return new Proxy(B?A[B]:A,{get(C,D){const E=C[D],F=p(D);if(l(F)){return f(C,F)}if(D===z){return!0}if(s(E)){return g(C,D)}return E},apply(C,D,E){const F=Reflect.apply(C,A,E);if(s(F)){return g(F)}return F}})}g=Object.assign(g,{addStreamable:n,addAlias:q,ADD:i,REMOVE:j});if(typeof module=='object'&&typeof module.exports=='object'){module.exports=exports.default=g}else if(typeof window=='object'){window.stroxy=g}})();