UNPKG

kotlineum

Version:

React implementation of Kotlin patterns and features, including Flow patterns (SharedFlow and StateFlow) and MVVM architecture with ViewModels

3 lines (2 loc) 39.4 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");function e(){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 n(){var e=t.useRef(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}var r=function(t,e){return r=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])},r(t,e)};function o(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}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var i,s=function(){return s=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},s.apply(this,arguments)};function u(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{c(r.next(t))}catch(t){i(t)}}function u(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,u)}c((r=r.apply(t,e||[])).next())}))}function c(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=u(0),s.throw=u(1),s.return=u(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function u(u){return function(c){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,u[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,r=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=e.call(t,i)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,c])}}}function a(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);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"}(i||(i={}));var l={minLevel:i.ERROR,includeTimestamp:!0,prefix:"[Kotlineum]",enabled:!0},f=s({},l);function p(t,e){var n=[];return f.includeTimestamp&&n.push("".concat((new Date).toISOString())),n.push("".concat(f.prefix||"[Kotlineum]"," [").concat(e,"]")),n.push(t),n.join(" ")}function h(t){if(!f.enabled)return!1;var e=[i.DEBUG,i.INFO,i.WARN,i.ERROR],n=e.indexOf(f.minLevel||i.INFO);return e.indexOf(t)>=n}var d,v,b={debug:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];h(i.DEBUG)&&console.debug.apply(console,a([p(t,i.DEBUG)],e,!1))},info:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];h(i.INFO)&&console.info.apply(console,a([p(t,i.INFO)],e,!1))},warn:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];h(i.WARN)&&console.warn.apply(console,a([p(t,i.WARN)],e,!1))},error:function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];h(i.ERROR)&&console.error.apply(console,a([p(t,i.ERROR)],e,!1))},configure:function(t){f=s(s({},f),t)},getConfig:function(){return s({},f)}};exports.StorageType=void 0,(d=exports.StorageType||(exports.StorageType={})).LOCAL_STORAGE="localStorage",d.INDEXED_DB="indexedDB",exports.StateFlowEventType=void 0,(v=exports.StateFlowEventType||(exports.StateFlowEventType={})).INITIAL_LOAD_COMPLETE="INITIAL_LOAD_COMPLETE",v.VALUE_UPDATED="VALUE_UPDATED",v.PERSISTED="PERSISTED",v.LOADED_FROM_STORAGE="LOADED_FROM_STORAGE",v.ERROR="ERROR";var w=function(){function t(){}return t.openDatabase=function(t,e){var n=this;return new Promise((function(r,o){if(n.openConnections.has(t))r(n.openConnections.get(t));else if(window.indexedDB){var i=indexedDB.open(t,1);i.onerror=function(t){var e=new Error("IndexedDB error: ".concat(t.target.error));b.error("Error saving to IndexedDB:",e),b.error("IndexedDB error:",e),o(e)},i.onupgradeneeded=function(t){var n=t.target.result;n.objectStoreNames.contains(e)||n.createObjectStore(e)},i.onsuccess=function(e){var o=e.target.result;n.openConnections.set(t,o),window.addEventListener("beforeunload",(function(){o.close(),n.openConnections.delete(t)}),{once:!0}),r(o)}}else o(new Error("IndexedDB is not supported in this browser"))}))},t.getValue=function(t,e,n){var r=this;return new Promise((function(o,i){return u(r,void 0,void 0,(function(){var r,s,u,a,l;return c(this,(function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,this.openDatabase(t,e)];case 1:return r=c.sent(),s=r.transaction(e,"readonly"),u=s.objectStore(e),(a=u.get(n)).onsuccess=function(){o(a.result)},a.onerror=function(){i(a.error)},[3,3];case 2:return l=c.sent(),i(l),[3,3];case 3:return[2]}}))}))}))},t.setValue=function(t,e,n,r){var o=this;return new Promise((function(i,s){return u(o,void 0,void 0,(function(){var o,u,a,l,f;return c(this,(function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,this.openDatabase(t,e)];case 1:return o=c.sent(),u=o.transaction(e,"readwrite"),a=u.objectStore(e),(l=a.put(r,n)).onsuccess=function(){i()},l.onerror=function(){s(l.error)},[3,3];case 2:return f=c.sent(),s(f),[3,3];case 3:return[2]}}))}))}))},t.openConnections=new Map,t}();function m(t,e){var n=null;return function(){for(var r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];null!==n&&clearTimeout(n),n=setTimeout((function(){n=null,t.apply(void 0,r)}),e)}}var g=function(){function t(){this.flows=new Map,this.persistOptions=new Map,this.debouncedSaveFunctions=new Map,this.eventSubscribers=new Map,this.debounce=m}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){b.error("Error in StateFlow event subscriber:",t)}}))},t.prototype.getOrCreate=function(e,n,r){var o=this;if(!this.flows.has(e)){var i=new Map;if(r&&this.persistOptions.set(e,{storageKey:r.storageKey||e,enabled:!1!==r.enabled,storageType:r.storageType||exports.StorageType.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),u=this.debounce((function(t){o.saveToStorage(e,t,s),s.emitEvents&&o.emitEvent(e,{type:exports.StateFlowEventType.PERSISTED,key:e,value:null!=t?t:void 0,timestamp:Date.now()})}),s.debounceTime||300);this.debouncedSaveFunctions.set(e,u)}var c=n;this.persistOptions.get(e),r&&!1!==r.enabled&&this.loadFromStorage(e,this.persistOptions.get(e)).then((function(t){if(null!=t){o.flows.get(e).update(t);var n=o.persistOptions.get(e);n&&n.emitEvents&&o.emitEvent(e,{type:exports.StateFlowEventType.LOADED_FROM_STORAGE,key:e,value:t,timestamp:Date.now()})}var r=o.persistOptions.get(e);r&&r.emitEvents&&o.emitEvent(e,{type:exports.StateFlowEventType.INITIAL_LOAD_COMPLETE,key:e,value:c,timestamp:Date.now()})}));var a={getValue:function(){return c},subscribe:function(t,e){return i.set(t,e),e(c),function(){return a.unsubscribe(t)}},unsubscribe:function(t){i.delete(t)},update:function(n){c=n,i.forEach((function(t){return t(n)}));var r=t.getInstance().persistOptions.get(e);if((null==r?void 0:r.enabled)&&"undefined"!=typeof window){var o=t.getInstance().debouncedSaveFunctions.get(e);o&&o(n)}},dispose:function(){i.clear()},getSubscriberCount:function(){return i.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===exports.StorageType.INDEXED_DB){var r=n.serialize(e);w.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 u(this,void 0,void 0,(function(){var n,r;return c(this,(function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),e.storageType!==exports.StorageType.INDEXED_DB?[3,2]:[4,w.getValue(e.dbName||"kotlineum_state",e.storeName||"state_store",e.storageKey||t)];case 1:return(n=o.sent())?[2,e.deserialize?e.deserialize(n):n]:[3,3];case 2:if(r=localStorage.getItem(e.storageKey))return[2,e.deserialize(r)];o.label=3;case 3:return[3,5];case 4:throw o.sent();case 5:return[2,void 0]}}))}))},t}();function y(t,e,n){return g.getInstance().getOrCreate(t,e,n)}function E(t,e){var n={current:new Map},r={current:new Map},o=t,i=!1!==(null==e?void 0:e.emitEvents),s={getValue:function(){return o},subscribe:function(t,e){return n.current.set(t,e),e(o),b.debug("New subscription to StateFlow with ID: ".concat(t)),function(){return s.unsubscribe(t)}},unsubscribe:function(t){n.current.delete(t),b.debug("Unsubscribed from StateFlow with ID: ".concat(t))},update:function(t){o=t,n.current.forEach((function(e){try{e(t)}catch(t){b.error("Error in StateFlow subscriber:",t)}})),i&&s.emitEvent&&s.emitEvent({type:exports.StateFlowEventType.VALUE_UPDATED,key:"stateflow",value:t,timestamp:Date.now()})},getSubscriberCount:function(){return n.current.size},dispose:function(){n.current.clear(),r.current.clear(),b.debug("StateFlow disposed")},subscribeToEvents:function(t,e){return r.current.set(t,e),b.debug("New event subscription to StateFlow with ID: ".concat(t)),function(){r.current.delete(t),b.debug("Unsubscribed from StateFlow events with ID: ".concat(t))}},emitEvent:function(t){r.current.forEach((function(e){try{e(t)}catch(t){b.error("Error in StateFlow event subscriber:",t)}}))}};return s}var S,F=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 I(t){return F.getInstance().getOrCreate(t)}exports.ListStateFlowEventType=void 0,(S=exports.ListStateFlowEventType||(exports.ListStateFlowEventType={})).INITIAL_LOAD_COMPLETE="INITIAL_LOAD_COMPLETE",S.ITEM_ADDED="ITEM_ADDED",S.ITEM_UPDATED="ITEM_UPDATED",S.ITEM_REMOVED="ITEM_REMOVED";var _=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=y(t,e,n.persistOptions),this.initializeItemFlows(e),this.emitEvents&&this.emitEvent({type:exports.ListStateFlowEventType.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=y("".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 o=this.stateFlow.getValue().find((function(t){return String(t[e.idField])===n}));if(o){b.debug("[Kotlineum] ListStateFlow.getItem: Creating missing flow for item ".concat(n));var i=y("".concat(this.key,"_item_").concat(n),o);return this.itemStateFlows.set(n,i),o}},t.prototype.updateItems=function(t){this.stateFlow.update(t),this.initializeItemFlows(t)},t.prototype.updateItem=function(t,e){var n=this,r=String(t),o=this.itemStateFlows.get(r);if(o){var i=e(o.getValue());o.update(i);var s=this.stateFlow.getValue().map((function(t){return String(t[n.idField])===r?i:t}));this.stateFlow.update(s)}},t.prototype.addItem=function(t){var e=this,n=String(t[this.idField]),r=this.itemStateFlows.get(n),o=this.stateFlow.getValue(),i=o.some((function(t){return String(t[e.idField])===n}));if(r&&i)r.update(t),this.emitEvents&&this.emitEvent({type:exports.ListStateFlowEventType.ITEM_UPDATED,item:t,timestamp:Date.now()});else{var s=y("".concat(this.key,"_item_").concat(n),t);this.itemStateFlows.set(n,s),this.stateFlow.update(a(a([],o,!0),[t],!1)),this.itemAdditionCallbacks.forEach((function(e){e.onItemAdded(t)})),this.emitEvents&&this.emitEvent({type:exports.ListStateFlowEventType.ITEM_ADDED,item:t,timestamp:Date.now()})}var u=this.pendingItemSubscriptions.get(n);u&&(u.forEach((function(t,r){var o=e.itemStateFlows.get(n);o&&o.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(),o=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&&o&&this.emitEvent({type:exports.ListStateFlowEventType.ITEM_REMOVED,item:o,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 b.debug("[Kotlineum] ListStateFlow.subscribeToItemsByIds: No IDs provided"),function(){};b.debug("[Kotlineum] ListStateFlow.subscribeToItemsByIds: Subscribing to ".concat(t.length," items"));var o=[];return t.forEach((function(t,i){var s="".concat(e,"_").concat(i),u=r.subscribeToItem(t,s,n);o.push(u)})),function(){o.forEach((function(t){return t()}))}},t.prototype.subscribeToItem=function(t,e,n){var r=this,o=String(t),i=this.itemStateFlows.get(o);return i?i.subscribe(e,n):(this.pendingItemSubscriptions.has(o)||this.pendingItemSubscriptions.set(o,new Map),this.pendingItemSubscriptions.get(o).set(e,n),function(){var t=r.pendingItemSubscriptions.get(o);t&&(t.delete(e),0===t.size&&r.pendingItemSubscriptions.delete(o))})},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){b.debug("[Kotlineum] ListStateFlow.addItems: Adding ".concat(t.length," items"));var n=a([],this.stateFlow.getValue(),!0),r=[],o=[],i=[];t.forEach((function(t){var s=String(t[e.idField]);i.push(s);var u=n.findIndex((function(t){return String(t[e.idField])===s})),c=e.itemStateFlows.get(s);if(u>=0){if(c)c.update(t);else{var a=y("".concat(e.key,"_item_").concat(s),t);e.itemStateFlows.set(s,a)}n[u]=t,o.push(t)}else{a=y("".concat(e.key,"_item_").concat(s),t);e.itemStateFlows.set(s,a),n.push(t),r.push(t)}})),this.stateFlow.update(n),i.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:exports.ListStateFlowEventType.ITEM_ADDED,items:r,timestamp:Date.now()})),o.length>0&&this.emitEvents&&this.emitEvent({type:exports.ListStateFlowEventType.ITEM_UPDATED,items:o,timestamp:Date.now()})}else b.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){b.debug("[Kotlineum] ListStateFlow.batchUpdate: Updating ".concat(t.length," items"));var n=this.stateFlow.getValue(),r=a([],n,!0),o=[],i=[],s=new Map;t.forEach((function(t){var u=t.id,c=t.update,a=String(u);i.push(a);var l=e.itemStateFlows.get(a);if(l){var f=c(l.getValue());l.update(f),r=r.map((function(t){return String(t[e.idField])===a?f:t})),o.push(f)}else{var p=n.find((function(t){return String(t[e.idField])===a}));if(p){var h=c(p),d=y("".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})),o.push(h)}}})),this.stateFlow.update(r),i.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))}})),o.length>0&&this.emitEvents&&this.emitEvent({type:exports.ListStateFlowEventType.ITEM_UPDATED,items:o,timestamp:Date.now()})}else b.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 a([],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 u(this,void 0,void 0,(function(){var e;return c(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 u(this,void 0,void 0,(function(){var n;return c(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){b.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 D(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new _(t,e,n)}var T=function(){function t(t){this.isDisposed=!1,this.stateFlow=E({data:t,loading:!1,error:null}),this.eventsFlow=e()}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(s(s({},this.stateFlow.getValue()),{loading:t}))},t.prototype.setError=function(t){this.stateFlow.update(s(s({},this.stateFlow.getValue()),{error:t}))},t.prototype.updateData=function(t){this.stateFlow.update(s(s({},this.stateFlow.getValue()),{data:t}))},t.prototype.updateState=function(t){this.stateFlow.update(s(s({},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}(),x=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 o=new(e.bind.apply(e,a([void 0],n,!1)));return this.viewModels.set(t,o),o}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 O(e,n){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];var i=x.getInstance(),s=i.get.apply(i,a([e,n],r,!1)),u=t.useState(s.getState()),c=u[0],l=u[1],f=t.useRef("vm-".concat(e,"-").concat(Math.random()));return t.useEffect((function(){var t=s.subscribeToState(f.current,l);return function(){t()}}),[s]),[c,s]}var C=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 u(this,void 0,void 0,(function(){return c(this,(function(t){return[2,this.promise]}))}))},t}(),M=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 u(this,void 0,void 0,(function(){var t=this;return c(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}(),A=function(){function t(t){this._active=!0,this._job=t||new M}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 M;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 C;return this._job.onCancel((function(){t.completed||t.reject(new Error("Cancelled"))})),t},t.prototype.cancel=function(){this._active=!1,this._job.cancel()},t}();var L=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}();var k=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=L.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)},o=0,i=this.providers;o<i.length;o++){r(i[o])}},t}();var V=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 o(r,t),r.prototype.collect=function(t){return u(this,void 0,void 0,(function(){var r=this;return c(this,(function(o){switch(o.label){case 0:return[4,n.collect({emit:function(n){return u(r,void 0,void 0,(function(){var r;return c(this,(function(o){switch(o.label){case 0:return[4,e(n)];case 1:return r=o.sent(),[4,t.emit(r)];case 2:return o.sent(),[2]}}))}))}})];case 1:return o.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 o(r,t),r.prototype.collect=function(t){return u(this,void 0,void 0,(function(){var r=this;return c(this,(function(o){switch(o.label){case 0:return[4,n.collect({emit:function(n){return u(r,void 0,void 0,(function(){return c(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 o.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 o(r,t),r.prototype.collect=function(t){return u(this,void 0,void 0,(function(){var r=this;return c(this,(function(o){switch(o.label){case 0:return[4,n.collect({emit:function(n){return u(r,void 0,void 0,(function(){var r=this;return c(this,(function(o){switch(o.label){case 0:return[4,e(n).collect({emit:function(e){return u(r,void 0,void 0,(function(){return c(this,(function(n){switch(n.label){case 0:return[4,t.emit(e)];case 1:return n.sent(),[2]}}))}))}})];case 1:return o.sent(),[2]}}))}))}})];case 1:return o.sent(),[2]}}))}))},r}(t))},t.prototype.take=function(e){if(e<=0)return j();var n=this;return new(function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return o(r,t),r.prototype.collect=function(t){return u(this,void 0,void 0,(function(){var r,o=this;return c(this,(function(i){switch(i.label){case 0:return r=e,[4,n.collect({emit:function(e){return u(o,void 0,void 0,(function(){return c(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 P;return[2]}}))}))}}).catch((function(t){if(!(t instanceof P))throw t}))];case 1:return i.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 o(r,t),r.prototype.collect=function(t){return u(this,void 0,void 0,(function(){var r=this;return c(this,(function(o){switch(o.label){case 0:return[4,n.collect({emit:function(n){return u(r,void 0,void 0,(function(){return c(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 o.sent(),[2]}}))}))},r}(t))},t.prototype.toArray=function(){return u(this,void 0,void 0,(function(){var t,e=this;return c(this,(function(n){switch(n.label){case 0:return t=[],[4,this.collect({emit:function(n){return u(e,void 0,void 0,(function(){return c(this,(function(e){return t.push(n),[2]}))}))}})];case 1:return n.sent(),[2,t]}}))}))},t.prototype.first=function(){return u(this,void 0,void 0,(function(){var t,e=this;return c(this,(function(n){switch(n.label){case 0:return t=null,[4,this.take(1).collect({emit:function(n){return u(e,void 0,void 0,(function(){return c(this,(function(e){return t=n,[2]}))}))}})];case 1:return n.sent(),[2,t]}}))}))},t.prototype.reduce=function(t,e){return u(this,void 0,void 0,(function(){var n,r=this;return c(this,(function(o){switch(o.label){case 0:return n=t,[4,this.collect({emit:function(t){return u(r,void 0,void 0,(function(){return c(this,(function(r){switch(r.label){case 0:return[4,e(n,t)];case 1:return n=r.sent(),[2]}}))}))}})];case 1:return o.sent(),[2,n]}}))}))},t.prototype.collectValues=function(t){return u(this,void 0,void 0,(function(){var e=this;return c(this,(function(n){switch(n.label){case 0:return[4,this.collect({emit:function(n){return u(e,void 0,void 0,(function(){return c(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}(),P=function(t){function e(){var e=t.call(this,"Flow collection was cancelled")||this;return e.name="FlowCancellationException",e}return o(e,t),e}(Error);function R(t){return new(function(e){function n(){return null!==e&&e.apply(this,arguments)||this}return o(n,e),n.prototype.collect=function(e){return u(this,void 0,void 0,(function(){return c(this,(function(n){switch(n.label){case 0:return[4,t(e)];case 1:return n.sent(),[2]}}))}))},n}(V))}function j(){for(var t=this,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return R((function(n){return u(t,void 0,void 0,(function(){var t,r,o;return c(this,(function(i){switch(i.label){case 0:t=0,r=e,i.label=1;case 1:return t<r.length?(o=r[t],[4,n.emit(o)]):[3,4];case 2:i.sent(),i.label=3;case 3:return t++,[3,1];case 4:return[2]}}))}))}))}exports.AbstractFlow=V,exports.CoroutineScope=A,exports.DIContainer=L,exports.Deferred=C,exports.FlowCancellationException=P,exports.GlobalListStateFlow=D,exports.GlobalSharedFlow=I,exports.GlobalStateFlow=y,exports.Inject=function(t){return function(e,n){var r=t||n;Object.defineProperty(e,n,{get:function(){return L.getInstance().get(r)},enumerable:!0,configurable:!0})}},exports.Injectable=function(t){return void 0===t&&(t={}),function(e){var n=t.token||e.name,r=!1!==t.singleton;return L.getInstance().registerFactory(n,(function(){return new e}),r),e}},exports.Job=M,exports.ListStateFlow=_,exports.Module=k,exports.SharedFlow=e,exports.StateFlow=E,exports.ViewModel=T,exports.ViewModelProvider=x,exports.asFlow=function(t){var e=this;return R((function(n){return u(e,void 0,void 0,(function(){var e=this;return c(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 o=t.subscribe("flow-subscription",(function(t){return u(e,void 0,void 0,(function(){var e;return c(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,n.emit(t)];case 1:return i.sent(),[3,3];case 2:if(!((e=i.sent())instanceof P))throw e;return o(),r(),[3,3];case 3:return[2]}}))}))}))}))];case 2:return r.sent(),[2]}}))}))}))},exports.awaitAll=function(t){return u(this,void 0,void 0,(function(){return c(this,(function(e){return[2,Promise.all(t)]}))}))},exports.coroutineScope=function(t){return u(this,void 0,void 0,(function(){var e;return c(this,(function(n){switch(n.label){case 0:e=new A,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]}}))}))},exports.createCoroutineScope=function(){return new A},exports.delay=function(t){return new Promise((function(e){return setTimeout(e,t)}))},exports.flow=R,exports.flowFromArray=function(t){return j.apply(void 0,t)},exports.flowFromPromise=function(t){var e=this;return R((function(n){return u(e,void 0,void 0,(function(){var e;return c(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]}}))}))}))},exports.flowOf=j,exports.inject=function(t){return L.getInstance().get(t)},exports.launchFlowCollection=function(t,e,n){var r=this;return t.launch((function(){return u(r,void 0,void 0,(function(){var t=this;return c(this,(function(r){switch(r.label){case 0:return[4,e.collect({emit:function(e){return u(t,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,n(e)];case 1:return t.sent(),[2]}}))}))}})];case 1:return r.sent(),[2]}}))}))}))},exports.provide=function(t,e){L.getInstance().register(t,e)},exports.provideFactory=function(t,e,n){void 0===n&&(n=!1),L.getInstance().registerFactory(t,e,n)},exports.ticker=function(t,e){var n=this;return void 0===e&&(e=0),R((function(r){return u(n,void 0,void 0,(function(){var n;return c(this,(function(o){switch(o.label){case 0:return e>0?[4,new Promise((function(t){return setTimeout(t,e)}))]:[3,2];case 1:o.sent(),o.label=2;case 2:n=0,o.label=3;case 3:return[4,r.emit(n++)];case 4:return o.sent(),[4,new Promise((function(e){return setTimeout(e,t)}))];case 5:return o.sent(),[3,3];case 6:return[2]}}))}))}))},exports.useContainer=function(){return L.getInstance()},exports.useDependency=function(e){var n=t.useState((function(){return L.getInstance().get(e)})),r=n[0],o=n[1];return t.useEffect((function(){var t=L.getInstance().get(e);t!==r&&o(t)}),[e,r]),r},exports.useGlobalListStateFlow=function(e,n,r){void 0===n&&(n=[]),void 0===r&&(r={});var o=D(e,n,r),i=t.useState(o.getItems()),s=i[0],u=i[1];return t.useEffect((function(){var t=o.subscribeToList("hook-".concat(e,"-").concat(Math.random()),(function(t){u(t)}));return u(o.getItems()),t}),[e,o]),[s,o]},exports.useGlobalSharedFlow=function(e,n){var r=I(e);return t.useEffect((function(){var t;return n&&(t=r.subscribe("hook-".concat(e,"-").concat(Math.random()),n)),function(){t&&t()}}),[e,n]),[function(t){return r.emit(t)},function(t){return r.subscribe("sub-".concat(e,"-").concat(Math.random()),t)}]},exports.useGlobalSharedFlowWithState=function(e,n){var r=t.useState(n),o=r[0],i=r[1],s=I(e);return t.useEffect((function(){return s.subscribe("state-".concat(e,"-").concat(Math.random()),(function(t){return i(t)}))}),[e]),[o,function(t){return s.emit(t)}]},exports.useGlobalStateFlow=function(e,n,r){var o=t.useState(n),i=o[0],s=o[1],u=y(e,n,r);return t.useEffect((function(){var t=u.subscribe("hook-".concat(e,"-").concat(Math.random()),(function(t){s(t)}));return s(u.getValue()),t}),[e,n]),[i,function(t){return u.update(t)}]},exports.useGlobalViewModel=function(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var o="global-".concat(t);return O.apply(void 0,a([o,e],n,!1))},exports.useListItem=function(e,n){var r=t.useState(e.getItem(n)),o=r[0],i=r[1];return t.useEffect((function(){var t=e.subscribeToItem(n,"item-hook-".concat(n,"-").concat(Math.random()),(function(t){i(t)}));return i(e.getItem(n)),t}),[e,n]),[o,function(t){o&&e.updateItem(n,t)}]},exports.useListStateFlow=function(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new _(t,e,n)},exports.useProvider=function(e,n,r){void 0===r&&(r=[]);var o=t.useState((function(){var t=n();return L.getInstance().register(e,t),t})),i=o[0],s=o[1];return t.useEffect((function(){var t=n();return L.getInstance().register(e,t),s(t),function(){L.getInstance().remove(e)}}),r),i},exports.useSharedFlow=n,exports.useSharedFlowWithState=function(e){var r=t.useState(e),o=r[0],i=r[1],s=n();return t.useRef((function(){s.subscribe("internal-state",(function(t){return i(t)}))})).current(),[o,s.emit,function(t){return s.subscribe("sub-".concat(Math.random()),t)}]},exports.useStateFlow=function(e,n){var r=t.useState(e),o=r[0],i=r[1],s=t.useRef(new Map),u=t.useRef(new Map),c=!1!==(null==n?void 0:n.emitEvents),a={getValue:function(){return o},subscribe:function(t,e){return s.current.set(t,e),e(o),function(){return a.unsubscribe(t)}},unsubscribe:function(t){s.current.delete(t),b.debug("Unsubscribed from StateFlow hook with ID: ".concat(t))},update:function(t){i(t),s.current.forEach((function(e){try{e(t)}catch(t){b.error("Error in StateFlow subscriber:",t)}})),c&&a.emitEvent&&a.emitEvent({type:exports.StateFlowEventType.VALUE_UPDATED,key:"stateflow-hook",value:t,timestamp:Date.now()})},getSubscriberCount:function(){return s.current.size},dispose:function(){s.current.clear(),u.current.clear(),b.debug("StateFlow hook disposed")},subscribeToEvents:function(t,e){return u.current.set(t,e),b.debug("New event subscription to StateFlow hook with ID: ".concat(t)),function(){u.current.delete(t),b.debug("Unsubscribed from StateFlow hook events with ID: ".concat(t))}},emitEvent:function(t){u.current.forEach((function(e){try{e(t)}catch(t){b.error("Error in StateFlow hook event subscriber:",t)}}))}},l=[o,a.update];return Object.assign(l,a),l},exports.useViewModel=O,exports.withTimeout=function(t,e){return u(this,void 0,void 0,(function(){var n,r;return c(this,(function(o){switch(o.label){case 0:r=new Promise((function(e,r){n=setTimeout((function(){r(new Error("Timed out after ".concat(t,"ms")))}),t)})),o.label=1;case 1:return o.trys.push([1,,3,4]),[4,Promise.race([e(),r])];case 2:return[2,o.sent()];case 3:return clearTimeout(n),[7];case 4:return[2]}}))}))}; //# sourceMappingURL=index.js.map