kotlineum
Version:
React implementation of Kotlin patterns and features, including Flow patterns (SharedFlow and StateFlow) and MVVM architecture with ViewModels
3 lines (2 loc) • 38.6 kB
JavaScript
import{useRef as t,useState as e,useEffect as n}from"react";function r(){var t={current:new Map},e={subscribe:function(n,r){return t.current.set(n,r),function(){return e.unsubscribe(n)}},unsubscribe:function(e){t.current.delete(e)},emit:function(e){t.current.forEach((function(t){return t(e)}))},getSubscriberCount:function(){return t.current.size}};return e}function i(){var e=t(new Map),n={subscribe:function(t,r){return e.current.set(t,r),function(){return n.unsubscribe(t)}},unsubscribe:function(t){e.current.delete(t)},emit:function(t){e.current.forEach((function(e){return e(t)}))},getSubscriberCount:function(){return e.current.size}},r=[function(t){return n.emit(t)},function(t){var e="sub-".concat(Math.random());return n.subscribe(e,t)}];return Object.assign(r,n),r}function o(n){var r=e(n),o=r[0],s=r[1],c=i();return t((function(){c.subscribe("internal-state",(function(t){return s(t)}))})).current(),[o,c.emit,function(t){return c.subscribe("sub-".concat(Math.random()),t)}]}var s=function(t,e){return s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},s(t,e)};function c(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}s(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var u,a=function(){return a=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},a.apply(this,arguments)};function l(t,e,n,r){return new(n||(n=Promise))((function(i,o){function s(t){try{u(r.next(t))}catch(t){o(t)}}function c(t){try{u(r.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,c)}u((r=r.apply(t,e||[])).next())}))}function f(t,e){var n,r,i,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=c(0),s.throw=c(1),s.return=c(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,c[0]&&(o=0)),o;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return o.label++,{value:c[1],done:!1};case 5:o.label++,r=c[1],c=[0];continue;case 7:c=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){o=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){o.label=c[1];break}if(6===c[0]&&o.label<i[1]){o.label=i[1],i=c;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(c);break}i[2]&&o.ops.pop(),o.trys.pop();continue}c=e.call(t,o)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}function p(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,function(t){t.DEBUG="DEBUG",t.INFO="INFO",t.WARN="WARN",t.ERROR="ERROR"}(u||(u={}));var h={minLevel:u.ERROR,includeTimestamp:!0,prefix:"[Kotlineum]",enabled:!0},d=a({},h);function v(t,e){var n=[];return d.includeTimestamp&&n.push("".concat((new Date).toISOString())),n.push("".concat(d.prefix||"[Kotlineum]"," [").concat(e,"]")),n.push(t),n.join(" ")}function b(t){if(!d.enabled)return!1;var e=[u.DEBUG,u.INFO,u.WARN,u.ERROR],n=e.indexOf(d.minLevel||u.INFO);return e.indexOf(t)>=n}var m,g,w={debug:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];b(u.DEBUG)&&console.debug.apply(console,p([v(t,u.DEBUG)],e,!1))},info:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];b(u.INFO)&&console.info.apply(console,p([v(t,u.INFO)],e,!1))},warn:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];b(u.WARN)&&console.warn.apply(console,p([v(t,u.WARN)],e,!1))},error:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];b(u.ERROR)&&console.error.apply(console,p([v(t,u.ERROR)],e,!1))},configure:function(t){d=a(a({},d),t)},getConfig:function(){return a({},d)}};!function(t){t.LOCAL_STORAGE="localStorage",t.INDEXED_DB="indexedDB"}(m||(m={})),function(t){t.INITIAL_LOAD_COMPLETE="INITIAL_LOAD_COMPLETE",t.VALUE_UPDATED="VALUE_UPDATED",t.PERSISTED="PERSISTED",t.LOADED_FROM_STORAGE="LOADED_FROM_STORAGE",t.ERROR="ERROR"}(g||(g={}));var y=function(){function t(){}return t.openDatabase=function(t,e){var n=this;return new Promise((function(r,i){if(n.openConnections.has(t))r(n.openConnections.get(t));else if(window.indexedDB){var o=indexedDB.open(t,1);o.onerror=function(t){var e=new Error("IndexedDB error: ".concat(t.target.error));w.error("Error saving to IndexedDB:",e),w.error("IndexedDB error:",e),i(e)},o.onupgradeneeded=function(t){var n=t.target.result;n.objectStoreNames.contains(e)||n.createObjectStore(e)},o.onsuccess=function(e){var i=e.target.result;n.openConnections.set(t,i),window.addEventListener("beforeunload",(function(){i.close(),n.openConnections.delete(t)}),{once:!0}),r(i)}}else i(new Error("IndexedDB is not supported in this browser"))}))},t.getValue=function(t,e,n){var r=this;return new Promise((function(i,o){return l(r,void 0,void 0,(function(){var r,s,c,u,a;return f(this,(function(l){switch(l.label){case 0:return l.trys.push([0,2,,3]),[4,this.openDatabase(t,e)];case 1:return r=l.sent(),s=r.transaction(e,"readonly"),c=s.objectStore(e),(u=c.get(n)).onsuccess=function(){i(u.result)},u.onerror=function(){o(u.error)},[3,3];case 2:return a=l.sent(),o(a),[3,3];case 3:return[2]}}))}))}))},t.setValue=function(t,e,n,r){var i=this;return new Promise((function(o,s){return l(i,void 0,void 0,(function(){var i,c,u,a,l;return f(this,(function(f){switch(f.label){case 0:return f.trys.push([0,2,,3]),[4,this.openDatabase(t,e)];case 1:return i=f.sent(),c=i.transaction(e,"readwrite"),u=c.objectStore(e),(a=u.put(r,n)).onsuccess=function(){o()},a.onerror=function(){s(a.error)},[3,3];case 2:return l=f.sent(),s(l),[3,3];case 3:return[2]}}))}))}))},t.openConnections=new Map,t}();function E(t,e){var n=null;return function(){for(var r=[],i=0;i<arguments.length;i++)r[i]=arguments[i];null!==n&&clearTimeout(n),n=setTimeout((function(){n=null,t.apply(void 0,r)}),e)}}var S=function(){function t(){this.flows=new Map,this.persistOptions=new Map,this.debouncedSaveFunctions=new Map,this.eventSubscribers=new Map,this.debounce=E}return t.getInstance=function(){return t.instance||(t.instance=new t),t.instance},t.prototype.subscribeToEvents=function(t,e,n){var r=this;return this.eventSubscribers.has(t)||this.eventSubscribers.set(t,new Map),this.eventSubscribers.get(t).set(e,n),function(){var n=r.eventSubscribers.get(t);n&&(n.delete(e),0===n.size&&r.eventSubscribers.delete(t))}},t.prototype.emitEvent=function(t,e){var n=this.eventSubscribers.get(t);n&&n.forEach((function(t){try{t(e)}catch(t){w.error("Error in StateFlow event subscriber:",t)}}))},t.prototype.getOrCreate=function(e,n,r){var i=this;if(!this.flows.has(e)){var o=new Map;if(r&&this.persistOptions.set(e,{storageKey:r.storageKey||e,enabled:!1!==r.enabled,storageType:r.storageType||m.LOCAL_STORAGE,dbName:r.dbName||"kotlineum_state",storeName:r.storeName||"state_store",serialize:r.serialize||JSON.stringify,deserialize:r.deserialize||JSON.parse,debounceTime:r.debounceTime||300,emitEvents:!1!==r.emitEvents}),r&&!1!==r.enabled){var s=this.persistOptions.get(e),c=this.debounce((function(t){i.saveToStorage(e,t,s),s.emitEvents&&i.emitEvent(e,{type:g.PERSISTED,key:e,value:null!=t?t:void 0,timestamp:Date.now()})}),s.debounceTime||300);this.debouncedSaveFunctions.set(e,c)}var u=n;this.persistOptions.get(e),r&&!1!==r.enabled&&this.loadFromStorage(e,this.persistOptions.get(e)).then((function(t){if(null!=t){i.flows.get(e).update(t);var n=i.persistOptions.get(e);n&&n.emitEvents&&i.emitEvent(e,{type:g.LOADED_FROM_STORAGE,key:e,value:t,timestamp:Date.now()})}var r=i.persistOptions.get(e);r&&r.emitEvents&&i.emitEvent(e,{type:g.INITIAL_LOAD_COMPLETE,key:e,value:u,timestamp:Date.now()})}));var a={getValue:function(){return u},subscribe:function(t,e){return o.set(t,e),e(u),function(){return a.unsubscribe(t)}},unsubscribe:function(t){o.delete(t)},update:function(n){u=n,o.forEach((function(t){return t(n)}));var r=t.getInstance().persistOptions.get(e);if((null==r?void 0:r.enabled)&&"undefined"!=typeof window){var i=t.getInstance().debouncedSaveFunctions.get(e);i&&i(n)}},dispose:function(){o.clear()},getSubscriberCount:function(){return o.size}};this.flows.set(e,a)}return this.flows.get(e)},t.prototype.get=function(t){return this.flows.get(t)},t.prototype.has=function(t){return this.flows.has(t)},t.prototype.saveToStorage=function(t,e,n){try{if(n.storageType===m.INDEXED_DB){var r=n.serialize(e);y.setValue(n.dbName,n.storeName,n.storageKey,r).catch((function(e){console.warn("Failed to persist state to IndexedDB for key ".concat(t,":"),e)}))}else{r=n.serialize(e);localStorage.setItem(n.storageKey,r)}}catch(e){console.warn("Failed to persist state to storage for key ".concat(t,":"),e)}},t.prototype.loadFromStorage=function(t,e){return l(this,void 0,void 0,(function(){var n,r;return f(this,(function(i){switch(i.label){case 0:return i.trys.push([0,4,,5]),e.storageType!==m.INDEXED_DB?[3,2]:[4,y.getValue(e.dbName||"kotlineum_state",e.storeName||"state_store",e.storageKey||t)];case 1:return(n=i.sent())?[2,e.deserialize?e.deserialize(n):n]:[3,3];case 2:if(r=localStorage.getItem(e.storageKey))return[2,e.deserialize(r)];i.label=3;case 3:return[3,5];case 4:throw i.sent();case 5:return[2,void 0]}}))}))},t}();function F(t,e,n){return S.getInstance().getOrCreate(t,e,n)}function I(t,r,i){var o=e(r),s=o[0],c=o[1],u=F(t,r,i);return n((function(){var e=u.subscribe("hook-".concat(t,"-").concat(Math.random()),(function(t){c(t)}));return c(u.getValue()),e}),[t,r]),[s,function(t){return u.update(t)}]}function _(t,e){var n={current:new Map},r={current:new Map},i=t,o=!1!==(null==e?void 0:e.emitEvents),s={getValue:function(){return i},subscribe:function(t,e){return n.current.set(t,e),e(i),w.debug("New subscription to StateFlow with ID: ".concat(t)),function(){return s.unsubscribe(t)}},unsubscribe:function(t){n.current.delete(t),w.debug("Unsubscribed from StateFlow with ID: ".concat(t))},update:function(t){i=t,n.current.forEach((function(e){try{e(t)}catch(t){w.error("Error in StateFlow subscriber:",t)}})),o&&s.emitEvent&&s.emitEvent({type:g.VALUE_UPDATED,key:"stateflow",value:t,timestamp:Date.now()})},getSubscriberCount:function(){return n.current.size},dispose:function(){n.current.clear(),r.current.clear(),w.debug("StateFlow disposed")},subscribeToEvents:function(t,e){return r.current.set(t,e),w.debug("New event subscription to StateFlow with ID: ".concat(t)),function(){r.current.delete(t),w.debug("Unsubscribed from StateFlow events with ID: ".concat(t))}},emitEvent:function(t){r.current.forEach((function(e){try{e(t)}catch(t){w.error("Error in StateFlow event subscriber:",t)}}))}};return s}function D(n,r){var i=e(n),o=i[0],s=i[1],c=t(new Map),u=t(new Map),a=!1!==(null==r?void 0:r.emitEvents),l={getValue:function(){return o},subscribe:function(t,e){return c.current.set(t,e),e(o),function(){return l.unsubscribe(t)}},unsubscribe:function(t){c.current.delete(t),w.debug("Unsubscribed from StateFlow hook with ID: ".concat(t))},update:function(t){s(t),c.current.forEach((function(e){try{e(t)}catch(t){w.error("Error in StateFlow subscriber:",t)}})),a&&l.emitEvent&&l.emitEvent({type:g.VALUE_UPDATED,key:"stateflow-hook",value:t,timestamp:Date.now()})},getSubscriberCount:function(){return c.current.size},dispose:function(){c.current.clear(),u.current.clear(),w.debug("StateFlow hook disposed")},subscribeToEvents:function(t,e){return u.current.set(t,e),w.debug("New event subscription to StateFlow hook with ID: ".concat(t)),function(){u.current.delete(t),w.debug("Unsubscribed from StateFlow hook events with ID: ".concat(t))}},emitEvent:function(t){u.current.forEach((function(e){try{e(t)}catch(t){w.error("Error in StateFlow hook event subscriber:",t)}}))}},f=[o,l.update];return Object.assign(f,l),f}var O,T=function(){function t(){this.flows=new Map}return t.getInstance=function(){return t.instance||(t.instance=new t),t.instance},t.prototype.getOrCreate=function(t){if(!this.flows.has(t)){var e=new Map,n={subscribe:function(t,r){return e.set(t,r),function(){return n.unsubscribe(t)}},unsubscribe:function(t){e.delete(t)},emit:function(t){e.forEach((function(e){return e(t)}))},getSubscriberCount:function(){return e.size}};this.flows.set(t,n)}return this.flows.get(t)},t.prototype.get=function(t){return this.flows.get(t)},t.prototype.has=function(t){return this.flows.has(t)},t}();function C(t){return T.getInstance().getOrCreate(t)}function M(t,e){var r=C(t);n((function(){var n;return e&&(n=r.subscribe("hook-".concat(t,"-").concat(Math.random()),e)),function(){n&&n()}}),[t,e]);return[function(t){return r.emit(t)},function(e){return r.subscribe("sub-".concat(t,"-").concat(Math.random()),e)}]}function A(t,r){var i=e(r),o=i[0],s=i[1],c=C(t);return n((function(){return c.subscribe("state-".concat(t,"-").concat(Math.random()),(function(t){return s(t)}))}),[t]),[o,function(t){return c.emit(t)}]}!function(t){t.INITIAL_LOAD_COMPLETE="INITIAL_LOAD_COMPLETE",t.ITEM_ADDED="ITEM_ADDED",t.ITEM_UPDATED="ITEM_UPDATED",t.ITEM_REMOVED="ITEM_REMOVED"}(O||(O={}));var k=function(){function t(t,e,n){void 0===e&&(e=[]),void 0===n&&(n={}),this.itemStateFlows=new Map,this.pendingItemSubscriptions=new Map,this.itemAdditionCallbacks=new Map,this.eventSubscribers=new Map,this.key=t,this.idField=n.idField||"id",this.emitEvents=void 0===n.emitEvents||n.emitEvents,this.stateFlow=F(t,e,n.persistOptions),this.initializeItemFlows(e),this.emitEvents&&this.emitEvent({type:O.INITIAL_LOAD_COMPLETE,items:e,timestamp:Date.now()})}return t.prototype.initializeItemFlows=function(t){var e=this;this.itemStateFlows.clear(),t.forEach((function(t){var n=String(t[e.idField]),r=F("".concat(e.key,"_item_").concat(n),t);e.itemStateFlows.set(n,r)}))},t.prototype.getItems=function(){return this.stateFlow.getValue()},t.prototype.getItem=function(t){var e=this,n=String(t),r=this.itemStateFlows.get(n);if(r)return r.getValue();var i=this.stateFlow.getValue().find((function(t){return String(t[e.idField])===n}));if(i){w.debug("[Kotlineum] ListStateFlow.getItem: Creating missing flow for item ".concat(n));var o=F("".concat(this.key,"_item_").concat(n),i);return this.itemStateFlows.set(n,o),i}},t.prototype.updateItems=function(t){this.stateFlow.update(t),this.initializeItemFlows(t)},t.prototype.updateItem=function(t,e){var n=this,r=String(t),i=this.itemStateFlows.get(r);if(i){var o=e(i.getValue());i.update(o);var s=this.stateFlow.getValue().map((function(t){return String(t[n.idField])===r?o:t}));this.stateFlow.update(s)}},t.prototype.addItem=function(t){var e=this,n=String(t[this.idField]),r=this.itemStateFlows.get(n),i=this.stateFlow.getValue(),o=i.some((function(t){return String(t[e.idField])===n}));if(r&&o)r.update(t),this.emitEvents&&this.emitEvent({type:O.ITEM_UPDATED,item:t,timestamp:Date.now()});else{var s=F("".concat(this.key,"_item_").concat(n),t);this.itemStateFlows.set(n,s),this.stateFlow.update(p(p([],i,!0),[t],!1)),this.itemAdditionCallbacks.forEach((function(e){e.onItemAdded(t)})),this.emitEvents&&this.emitEvent({type:O.ITEM_ADDED,item:t,timestamp:Date.now()})}var c=this.pendingItemSubscriptions.get(n);c&&(c.forEach((function(t,r){var i=e.itemStateFlows.get(n);i&&i.subscribe(r,t)})),this.pendingItemSubscriptions.delete(n))},t.prototype.addItemWithCallback=function(t,e,n){this.addItem(t),e&&n&&(this.itemAdditionCallbacks.set(e,n),n.onItemAdded(t))},t.prototype.removeItem=function(t){var e=this,n=String(t);this.itemStateFlows.delete(n);var r=this.stateFlow.getValue(),i=r.find((function(n){return String(n[e.idField])===t}));this.stateFlow.update(r.filter((function(n){return String(n[e.idField])!==t}))),this.emitEvents&&i&&this.emitEvent({type:O.ITEM_REMOVED,item:i,timestamp:Date.now()})},t.prototype.subscribeToList=function(t,e){return this.stateFlow.subscribe(t,e)},t.prototype.subscribeToItemsByIds=function(t,e,n){var r=this;if(!t||0===t.length)return w.debug("[Kotlineum] ListStateFlow.subscribeToItemsByIds: No IDs provided"),function(){};w.debug("[Kotlineum] ListStateFlow.subscribeToItemsByIds: Subscribing to ".concat(t.length," items"));var i=[];return t.forEach((function(t,o){var s="".concat(e,"_").concat(o),c=r.subscribeToItem(t,s,n);i.push(c)})),function(){i.forEach((function(t){return t()}))}},t.prototype.subscribeToItem=function(t,e,n){var r=this,i=String(t),o=this.itemStateFlows.get(i);return o?o.subscribe(e,n):(this.pendingItemSubscriptions.has(i)||this.pendingItemSubscriptions.set(i,new Map),this.pendingItemSubscriptions.get(i).set(e,n),function(){var t=r.pendingItemSubscriptions.get(i);t&&(t.delete(e),0===t.size&&r.pendingItemSubscriptions.delete(i))})},t.prototype.onItemAdded=function(t,e){var n=this;return this.itemAdditionCallbacks.set(t,e),function(){n.itemAdditionCallbacks.delete(t)}},t.prototype.addItems=function(t){var e=this;if(t&&0!==t.length){w.debug("[Kotlineum] ListStateFlow.addItems: Adding ".concat(t.length," items"));var n=p([],this.stateFlow.getValue(),!0),r=[],i=[],o=[];t.forEach((function(t){var s=String(t[e.idField]);o.push(s);var c=n.findIndex((function(t){return String(t[e.idField])===s})),u=e.itemStateFlows.get(s);if(c>=0){if(u)u.update(t);else{var a=F("".concat(e.key,"_item_").concat(s),t);e.itemStateFlows.set(s,a)}n[c]=t,i.push(t)}else{a=F("".concat(e.key,"_item_").concat(s),t);e.itemStateFlows.set(s,a),n.push(t),r.push(t)}})),this.stateFlow.update(n),o.forEach((function(t){var n=e.pendingItemSubscriptions.get(t);if(n){var r=e.itemStateFlows.get(t);r&&(n.forEach((function(t,e){r.subscribe(e,t)})),e.pendingItemSubscriptions.delete(t))}})),r.length>0&&(r.forEach((function(t){e.itemAdditionCallbacks.forEach((function(e){e.onItemAdded(t)}))})),this.emitEvents&&this.emitEvent({type:O.ITEM_ADDED,items:r,timestamp:Date.now()})),i.length>0&&this.emitEvents&&this.emitEvent({type:O.ITEM_UPDATED,items:i,timestamp:Date.now()})}else w.debug("[Kotlineum] ListStateFlow.addItems: No items to add")},t.prototype.addItemsWithCallback=function(t,e,n){e&&n&&this.itemAdditionCallbacks.set(e,n),this.addItems(t)},t.prototype.batchUpdate=function(t){var e=this;if(t&&0!==t.length){w.debug("[Kotlineum] ListStateFlow.batchUpdate: Updating ".concat(t.length," items"));var n=this.stateFlow.getValue(),r=p([],n,!0),i=[],o=[],s=new Map;t.forEach((function(t){var c=t.id,u=t.update,a=String(c);o.push(a);var l=e.itemStateFlows.get(a);if(l){var f=u(l.getValue());l.update(f),r=r.map((function(t){return String(t[e.idField])===a?f:t})),i.push(f)}else{var p=n.find((function(t){return String(t[e.idField])===a}));if(p){var h=u(p),d=F("".concat(e.key,"_item_").concat(a),h);e.itemStateFlows.set(a,d),s.set(a,d),r=r.map((function(t){return String(t[e.idField])===a?h:t})),i.push(h)}}})),this.stateFlow.update(r),o.forEach((function(t){var n=e.pendingItemSubscriptions.get(t);if(n){var r=e.itemStateFlows.get(t);r&&(n.forEach((function(t,e){r.subscribe(e,t)})),e.pendingItemSubscriptions.delete(t))}})),i.length>0&&this.emitEvents&&this.emitEvent({type:O.ITEM_UPDATED,items:i,timestamp:Date.now()})}else w.debug("[Kotlineum] ListStateFlow.batchUpdate: No updates to apply")},t.prototype.filter=function(t){return this.stateFlow.getValue().filter(t)},t.prototype.find=function(t){return this.stateFlow.getValue().find(t)},t.prototype.some=function(t){return this.stateFlow.getValue().some(t)},t.prototype.every=function(t){return this.stateFlow.getValue().every(t)},t.prototype.reduce=function(t,e){return this.stateFlow.getValue().reduce((function(e,n){return t(e,n)}),e)},t.prototype.sort=function(t){return p([],this.stateFlow.getValue(),!0).sort(t)},t.prototype.slice=function(t,e){return this.stateFlow.getValue().slice(t,e)},t.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var e;return f(this,(function(n){switch(n.label){case 0:return e=this.stateFlow.getValue(),[4,t(e)];case 1:return n.sent(),[2,Promise.resolve()]}}))}))},t.prototype.mapAndCollect=function(t,e){return l(this,void 0,void 0,(function(){var n;return f(this,(function(r){switch(r.label){case 0:return n=this.stateFlow.getValue().map(t),[4,e(n)];case 1:return r.sent(),[2,Promise.resolve()]}}))}))},t.prototype.collectFlow=function(t,e){return this.subscribeToList(t,e)},t.prototype.map=function(t){return this.stateFlow.getValue().map(t)},Object.defineProperty(t.prototype,"size",{get:function(){return this.stateFlow.getValue().length},enumerable:!1,configurable:!0}),t.prototype.getStateFlow=function(){return this.stateFlow},t.prototype.subscribeToEvents=function(t,e){var n=this;return this.eventSubscribers.set(t,e),function(){n.eventSubscribers.delete(t)}},t.prototype.emitEvent=function(t){this.eventSubscribers.forEach((function(e){try{e(t)}catch(t){w.error("Error in event subscriber:",t)}}))},t.prototype.dispose=function(){this.itemStateFlows.forEach((function(t){return t.dispose()})),this.itemStateFlows.clear(),this.pendingItemSubscriptions.clear(),this.itemAdditionCallbacks.clear(),this.eventSubscribers.clear(),this.stateFlow.dispose()},t}();function V(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new k(t,e,n)}function P(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new k(t,e,n)}function L(t,r,i){void 0===r&&(r=[]),void 0===i&&(i={});var o=P(t,r,i),s=e(o.getItems()),c=s[0],u=s[1];return n((function(){var e=o.subscribeToList("hook-".concat(t,"-").concat(Math.random()),(function(t){u(t)}));return u(o.getItems()),e}),[t,o]),[c,o]}function R(t,r){var i=e(t.getItem(r)),o=i[0],s=i[1];return n((function(){var e=t.subscribeToItem(r,"item-hook-".concat(r,"-").concat(Math.random()),(function(t){s(t)}));return s(t.getItem(r)),e}),[t,r]),[o,function(e){o&&t.updateItem(r,e)}]}var N=function(){function t(t){this.isDisposed=!1,this.stateFlow=_({data:t,loading:!1,error:null}),this.eventsFlow=r()}return t.prototype.dispatch=function(t){this.processIntent(t)},t.prototype.getState=function(){return this.stateFlow.getValue()},t.prototype.getData=function(){return this.stateFlow.getValue().data},t.prototype.getStateFlow=function(){return this.stateFlow},t.prototype.getEventsFlow=function(){return this.eventsFlow},t.prototype.subscribeToState=function(t,e){return this.stateFlow.subscribe(t,e)},t.prototype.subscribeToEvents=function(t,e){return this.eventsFlow.subscribe(t,e)},t.prototype.setLoading=function(t){this.stateFlow.update(a(a({},this.stateFlow.getValue()),{loading:t}))},t.prototype.setError=function(t){this.stateFlow.update(a(a({},this.stateFlow.getValue()),{error:t}))},t.prototype.updateData=function(t){this.stateFlow.update(a(a({},this.stateFlow.getValue()),{data:t}))},t.prototype.updateState=function(t){this.stateFlow.update(a(a({},this.stateFlow.getValue()),t))},t.prototype.emitEvent=function(t){this.eventsFlow.emit(t)},t.prototype.dispose=function(){this.isDisposed||(this.onDispose(),this.isDisposed=!0)},t.prototype.onDispose=function(){},t}(),j=function(){function t(){this.viewModels=new Map}return t.getInstance=function(){return t.instance||(t.instance=new t),t.instance},t.prototype.get=function(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];if(!this.viewModels.has(t)){var i=new(e.bind.apply(e,p([void 0],n,!1)));return this.viewModels.set(t,i),i}return this.viewModels.get(t)},t.prototype.has=function(t){return this.viewModels.has(t)},t.prototype.remove=function(t){if(this.viewModels.has(t)){var e=this.viewModels.get(t);null==e||e.dispose(),this.viewModels.delete(t)}},t.prototype.clear=function(){this.viewModels.forEach((function(t){return t.dispose()})),this.viewModels.clear()},t}();function U(r,i){for(var o=[],s=2;s<arguments.length;s++)o[s-2]=arguments[s];var c=j.getInstance(),u=c.get.apply(c,p([r,i],o,!1)),a=e(u.getState()),l=a[0],f=a[1],h=t("vm-".concat(r,"-").concat(Math.random()));return n((function(){var t=u.subscribeToState(h.current,f);return function(){t()}}),[u]),[l,u]}function x(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var i="global-".concat(t);return U.apply(void 0,p([i,e],n,!1))}var z=function(){function t(){var t=this;this._completed=!1,this.promise=new Promise((function(e,n){t.resolveCallback=function(n){t._completed=!0,e(n)},t.rejectCallback=function(e){t._completed=!0,n(e)}}))}return Object.defineProperty(t.prototype,"completed",{get:function(){return this._completed},enumerable:!1,configurable:!0}),t.prototype.resolve=function(t){this.resolveCallback(t)},t.prototype.reject=function(t){this.rejectCallback(t)},t.prototype.asPromise=function(){return this.promise},t.prototype.await=function(){return l(this,void 0,void 0,(function(){return f(this,(function(t){return[2,this.promise]}))}))},t}(),B=function(){function t(){this._cancelled=!1,this._completed=!1,this._children=new Set,this._onCancelCallbacks=[],this._onCompleteCallbacks=[]}return Object.defineProperty(t.prototype,"isActive",{get:function(){return!this._cancelled&&!this._completed},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isCancelled",{get:function(){return this._cancelled},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isCompleted",{get:function(){return this._completed},enumerable:!1,configurable:!0}),t.prototype.addChild=function(t){var e=this;this._cancelled?t.cancel():(this._children.add(t),t.onComplete((function(){e._children.delete(t)})))},t.prototype.cancel=function(){this._cancelled||this._completed||(this._cancelled=!0,this._children.forEach((function(t){return t.cancel()})),this._children.clear(),this._onCancelCallbacks.forEach((function(t){return t()})),this._onCancelCallbacks=[])},t.prototype.complete=function(){this._cancelled||this._completed||(this._completed=!0,this._onCompleteCallbacks.forEach((function(t){return t()})),this._onCompleteCallbacks=[])},t.prototype.onCancel=function(t){this._cancelled?t():this._onCancelCallbacks.push(t)},t.prototype.onComplete=function(t){this._completed?t():this._onCompleteCallbacks.push(t)},t.prototype.join=function(){return l(this,void 0,void 0,(function(){var t=this;return f(this,(function(e){return this._completed||this._cancelled?[2]:[2,new Promise((function(e){t.onComplete((function(){return e()})),t.onCancel((function(){return e()}))}))]}))}))},t}(),K=function(){function t(t){this._active=!0,this._job=t||new B}return Object.defineProperty(t.prototype,"job",{get:function(){return this._job},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isActive",{get:function(){return this._active&&this._job.isActive},enumerable:!1,configurable:!0}),t.prototype.launch=function(t){if(!this.isActive)throw new Error("Cannot launch in an inactive scope");var e=new B;return this._job.addChild(e),t().then((function(t){return e.complete(),t})).catch((function(t){throw e.complete(),t})),e.onCancel((function(){})),e},t.prototype.async=function(){var t=new z;return this._job.onCancel((function(){t.completed||t.reject(new Error("Cancelled"))})),t},t.prototype.cancel=function(){this._active=!1,this._job.cancel()},t}();function G(){return new K}function W(t){return new Promise((function(e){return setTimeout(e,t)}))}function X(t,e){return l(this,void 0,void 0,(function(){var n,r;return f(this,(function(i){switch(i.label){case 0:r=new Promise((function(e,r){n=setTimeout((function(){r(new Error("Timed out after ".concat(t,"ms")))}),t)})),i.label=1;case 1:return i.trys.push([1,,3,4]),[4,Promise.race([e(),r])];case 2:return[2,i.sent()];case 3:return clearTimeout(n),[7];case 4:return[2]}}))}))}function J(t){return l(this,void 0,void 0,(function(){return f(this,(function(e){return[2,Promise.all(t)]}))}))}function q(t){return l(this,void 0,void 0,(function(){var e;return f(this,(function(n){switch(n.label){case 0:e=new K,n.label=1;case 1:return n.trys.push([1,,3,4]),[4,t(e)];case 2:return[2,n.sent()];case 3:return e.cancel(),[7];case 4:return[2]}}))}))}var H=function(){function t(){this.dependencies=new Map,this.factories=new Map,this.singletons=new Set}return t.getInstance=function(){return t.instance||(t.instance=new t),t.instance},t.prototype.register=function(t,e){this.dependencies.set(t,e)},t.prototype.registerFactory=function(t,e,n){void 0===n&&(n=!1),this.factories.set(t,e),n&&this.singletons.add(t)},t.prototype.get=function(t){if(this.dependencies.has(t))return this.dependencies.get(t);if(this.factories.has(t)){var e=this.factories.get(t)();return this.singletons.has(t)&&(this.dependencies.set(t,e),this.factories.delete(t)),e}throw new Error("Dependency ".concat(t," not found"))},t.prototype.has=function(t){return this.dependencies.has(t)||this.factories.has(t)},t.prototype.remove=function(t){this.dependencies.delete(t),this.factories.delete(t),this.singletons.delete(t)},t.prototype.clear=function(){this.dependencies.clear(),this.factories.clear(),this.singletons.clear()},t.prototype.resolve=function(t){return this.get(t)},t}();function Q(t){return void 0===t&&(t={}),function(e){var n=t.token||e.name,r=!1!==t.singleton;return H.getInstance().registerFactory(n,(function(){return new e}),r),e}}function Y(t){return function(e,n){var r=t||n;Object.defineProperty(e,n,{get:function(){return H.getInstance().get(r)},enumerable:!0,configurable:!0})}}function Z(t){return H.getInstance().get(t)}function $(t,e){H.getInstance().register(t,e)}function tt(t,e,n){void 0===n&&(n=!1),H.getInstance().registerFactory(t,e,n)}var et=function(){function t(t){void 0===t&&(t={}),this.providers=[],this.imports=[],t.providers&&(this.providers=t.providers),t.imports&&(this.imports=t.imports)}return t.prototype.register=function(){for(var t=H.getInstance(),e=0,n=this.imports;e<n.length;e++){n[e].register()}for(var r=function(e){"useClass"in e?t.registerFactory(e.provide,(function(){return new e.useClass}),!1!==e.singleton):"useFactory"in e?t.registerFactory(e.provide,e.useFactory,!1!==e.singleton):"useValue"in e&&t.register(e.provide,e.useValue)},i=0,o=this.providers;i<o.length;i++){r(o[i])}},t}();function nt(t){var r=e((function(){return H.getInstance().get(t)})),i=r[0],o=r[1];return n((function(){var e=H.getInstance().get(t);e!==i&&o(e)}),[t,i]),i}function rt(t,r,i){void 0===i&&(i=[]);var o=e((function(){var e=r();return H.getInstance().register(t,e),e})),s=o[0],c=o[1];return n((function(){var e=r();return H.getInstance().register(t,e),c(e),function(){H.getInstance().remove(t)}}),i),s}function it(){return H.getInstance()}var ot=function(){function t(){}return t.prototype.map=function(e){var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return c(r,t),r.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var r=this;return f(this,(function(i){switch(i.label){case 0:return[4,n.collect({emit:function(n){return l(r,void 0,void 0,(function(){var r;return f(this,(function(i){switch(i.label){case 0:return[4,e(n)];case 1:return r=i.sent(),[4,t.emit(r)];case 2:return i.sent(),[2]}}))}))}})];case 1:return i.sent(),[2]}}))}))},r}(t))},t.prototype.filter=function(e){var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return c(r,t),r.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var r=this;return f(this,(function(i){switch(i.label){case 0:return[4,n.collect({emit:function(n){return l(r,void 0,void 0,(function(){return f(this,(function(r){switch(r.label){case 0:return[4,e(n)];case 1:return r.sent()?[4,t.emit(n)]:[3,3];case 2:r.sent(),r.label=3;case 3:return[2]}}))}))}})];case 1:return i.sent(),[2]}}))}))},r}(t))},t.prototype.flatMap=function(e){var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return c(r,t),r.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var r=this;return f(this,(function(i){switch(i.label){case 0:return[4,n.collect({emit:function(n){return l(r,void 0,void 0,(function(){var r=this;return f(this,(function(i){switch(i.label){case 0:return[4,e(n).collect({emit:function(e){return l(r,void 0,void 0,(function(){return f(this,(function(n){switch(n.label){case 0:return[4,t.emit(e)];case 1:return n.sent(),[2]}}))}))}})];case 1:return i.sent(),[2]}}))}))}})];case 1:return i.sent(),[2]}}))}))},r}(t))},t.prototype.take=function(e){if(e<=0)return ut();var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return c(r,t),r.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var r,i=this;return f(this,(function(o){switch(o.label){case 0:return r=e,[4,n.collect({emit:function(e){return l(i,void 0,void 0,(function(){return f(this,(function(n){switch(n.label){case 0:return r>0?[4,t.emit(e)]:[3,2];case 1:n.sent(),r--,n.label=2;case 2:if(r<=0)throw new st;return[2]}}))}))}}).catch((function(t){if(!(t instanceof st))throw t}))];case 1:return o.sent(),[2]}}))}))},r}(t))},t.prototype.onEach=function(e){var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return c(r,t),r.prototype.collect=function(t){return l(this,void 0,void 0,(function(){var r=this;return f(this,(function(i){switch(i.label){case 0:return[4,n.collect({emit:function(n){return l(r,void 0,void 0,(function(){return f(this,(function(r){switch(r.label){case 0:return[4,e(n)];case 1:return r.sent(),[4,t.emit(n)];case 2:return r.sent(),[2]}}))}))}})];case 1:return i.sent(),[2]}}))}))},r}(t))},t.prototype.toArray=function(){return l(this,void 0,void 0,(function(){var t,e=this;return f(this,(function(n){switch(n.label){case 0:return t=[],[4,this.collect({emit:function(n){return l(e,void 0,void 0,(function(){return f(this,(function(e){return t.push(n),[2]}))}))}})];case 1:return n.sent(),[2,t]}}))}))},t.prototype.first=function(){return l(this,void 0,void 0,(function(){var t,e=this;return f(this,(function(n){switch(n.label){case 0:return t=null,[4,this.take(1).collect({emit:function(n){return l(e,void 0,void 0,(function(){return f(this,(function(e){return t=n,[2]}))}))}})];case 1:return n.sent(),[2,t]}}))}))},t.prototype.reduce=function(t,e){return l(this,void 0,void 0,(function(){var n,r=this;return f(this,(function(i){switch(i.label){case 0:return n=t,[4,this.collect({emit:function(t){return l(r,void 0,void 0,(function(){return f(this,(function(r){switch(r.label){case 0:return[4,e(n,t)];case 1:return n=r.sent(),[2]}}))}))}})];case 1:return i.sent(),[2,n]}}))}))},t.prototype.collectValues=function(t){return l(this,void 0,void 0,(function(){var e=this;return f(this,(function(n){switch(n.label){case 0:return[4,this.collect({emit:function(n){return l(e,void 0,void 0,(function(){return f(this,(function(e){switch(e.label){case 0:return[4,t(n)];case 1:return e.sent(),[2]}}))}))}})];case 1:return n.sent(),[2]}}))}))},t}(),st=function(t){function e(){var e=t.call(this,"Flow collection was cancelled")||this;return e.name="FlowCancellationException",e}return c(e,t),e}(Error);function ct(t){return new(function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return c(n,e),n.prototype.collect=function(e){return l(this,void 0,void 0,(function(){return f(this,(function(n){switch(n.label){case 0:return[4,t(e)];case 1:return n.sent(),[2]}}))}))},n}(ot))}function ut(){for(var t=this,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return ct((function(n){return l(t,void 0,void 0,(function(){var t,r,i;return f(this,(function(o){switch(o.label){case 0:t=0,r=e,o.label=1;case 1:return t<r.length?(i=r[t],[4,n.emit(i)]):[3,4];case 2:o.sent(),o.label=3;case 3:return t++,[3,1];case 4:return[2]}}))}))}))}function at(t){return ut.apply(void 0,t)}function lt(t){var e=this;return ct((function(n){return l(e,void 0,void 0,(function(){var e;return f(this,(function(r){switch(r.label){case 0:return[4,t];case 1:return e=r.sent(),[4,n.emit(e)];case 2:return r.sent(),[2]}}))}))}))}function ft(t,e){var n=this;return void 0===e&&(e=0),ct((function(r){return l(n,void 0,void 0,(function(){var n;return f(this,(function(i){switch(i.label){case 0:return e>0?[4,new Promise((function(t){return setTimeout(t,e)}))]:[3,2];case 1:i.sent(),i.label=2;case 2:n=0,i.label=3;case 3:return[4,r.emit(n++)];case 4:return i.sent(),[4,new Promise((function(e){return setTimeout(e,t)}))];case 5:return i.sent(),[3,3];case 6:return[2]}}))}))}))}function pt(t,e,n){var r=this;return t.launch((function(){return l(r,void 0,void 0,(function(){var t=this;return f(this,(function(r){switch(r.label){case 0:return[4,e.collect({emit:function(e){return l(t,void 0,void 0,(function(){return f(this,(function(t){switch(t.label){case 0:return[4,n(e)];case 1:return t.sent(),[2]}}))}))}})];case 1:return r.sent(),[2]}}))}))}))}function ht(t){var e=this;return ct((function(n){return l(e,void 0,void 0,(function(){var e=this;return f(this,(function(r){switch(r.label){case 0:return[4,n.emit(t.getValue())];case 1:return r.sent(),[4,new Promise((function(r){var i=t.subscribe("flow-subscription",(function(t){return l(e,void 0,void 0,(function(){var e;return f(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,n.emit(t)];case 1:return o.sent(),[3,3];case 2:if(!((e=o.sent())instanceof st))throw e;return i(),r(),[3,3];case 3:return[2]}}))}))}))}))];case 2:return r.sent(),[2]}}))}))}))}export{ot as AbstractFlow,K as CoroutineScope,H as DIContainer,z as Deferred,st as FlowCancellationException,P as GlobalListStateFlow,C as GlobalSharedFlow,F as GlobalStateFlow,Y as Inject,Q as Injectable,B as Job,k as ListStateFlow,O as ListStateFlowEventType,et as Module,r as SharedFlow,_ as StateFlow,g as StateFlowEventType,m as StorageType,N as ViewModel,j as ViewModelProvider,ht as asFlow,J as awaitAll,q as coroutineScope,G as createCoroutineScope,W as delay,ct as flow,at as flowFromArray,lt as flowFromPromise,ut as flowOf,Z as inject,pt as launchFlowCollection,$ as provide,tt as provideFactory,ft as ticker,it as useContainer,nt as useDependency,L as useGlobalListStateFlow,M as useGlobalSharedFlow,A as useGlobalSharedFlowWithState,I as useGlobalStateFlow,x as useGlobalViewModel,R as useListItem,V as useListStateFlow,rt as useProvider,i as useSharedFlow,o as useSharedFlowWithState,D as useStateFlow,U as useViewModel,X as withTimeout};
//# sourceMappingURL=index.esm.js.map