@pulse-editor/shared-utils 0.1.1-alpha.10 → 0.1.1-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,8 +20,8 @@ export declare class PolyIMC {
20
20
  sendMessage(targetWindowId: string, handlingType: IMCMessageTypeEnum, payload?: any, abortSignal?: AbortSignal): Promise<any>;
21
21
  updateBaseReceiverHandlerMap(handlerMap: ReceiverHandlerMap): void;
22
22
  updateChannelReceiverHandlerMap(targetWindowId: string, handlerMap: ReceiverHandlerMap): void;
23
- createChannel(targetWindow: Window, targetWindowId: string, receiverHandlerMap?: ReceiverHandlerMap): Promise<void>;
24
- removeChannel(targetWindowId: string): Promise<void>;
23
+ createChannel(targetWindow: Window, targetWindowId: string, receiverHandlerMap?: ReceiverHandlerMap): void;
24
+ removeChannel(targetWindowId: string): void;
25
25
  hasChannel(targetWindowId: string): boolean;
26
26
  private getCombinedHandlerMap;
27
27
  private getConnectionListenerHandlerMap;
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- var e,n,t,i;function s(e){return Array.isArray(e)&&1===e.length}function r(e){return"object"==typeof e&&!Array.isArray(e)}!function(e){e.WriteViewFile="write-view-file",e.RequestViewFile="request-view-file",e.Fetch="fetch",e.Notification="notification",e.ThemeChange="theme-change",e.RunAgentMethod="run-agent-method",e.UseVAD="use-vad",e.UseSTT="use-stt",e.UseLLM="use-llm",e.UseTTS="use-tts",e.UseDiffusion="use-diffusion",e.UseOCR="use-ocr",e.UseSpeech2Speech="use-speech2speech",e.RunExtCommand="run-ext-command",e.RequestTerminal="request-terminal",e.ExtReady="ext-ready",e.ExtClose="ext-close",e.Loaded="loaded",e.Acknowledge="acknowledge",e.Abort="abort",e.Error="error"}(e||(e={})),function(e){e.Success="success",e.Error="error",e.Info="info",e.Warning="warning"}(n||(n={})),function(e){e.Generic="generic",e.FileView="file-view",e.ConsoleView="console-view"}(t||(t={})),function(e){e.public="public",e.private="private"}(i||(i={}));const o=3e5;class a{constructor(e,n){this.handlerMap=e,this.pendingTasks=new Map,this.moduleId=n}receiveMessage(n,t){if(this.moduleId===t.from)return;if("development"===process.env.NODE_ENV&&console.log(`Module ${this.moduleId} received message from module ${t.from}:\n ${JSON.stringify(t)}`),t.type===e.Abort){const e=t.id,n=this.pendingTasks.get(e);return void(n&&(console.log("Aborting task",e),n.controller.abort(),this.pendingTasks.delete(e)))}const i=this.handlerMap.get(t.type);if(i){const s=new AbortController,r=s.signal;this.pendingTasks.set(t.id,{controller:s});i(n,t,r).then((i=>{r.aborted||t.type!==e.Acknowledge&&this.acknowledgeSender(n,t.id,i)})).catch((i=>{const s={id:t.id,type:e.Error,payload:i.message,from:this.moduleId};n.postMessage(s,"*")})).finally((()=>{this.pendingTasks.delete(t.id)}))}}acknowledgeSender(n,t,i){const s={id:t,type:e.Acknowledge,payload:i,from:this.moduleId};n.postMessage(s,"*")}}class d{constructor(e,n,t){this.targetWindow=e,this.timeout=n,this.pendingMessages=new Map,this.moduleId=t}async sendMessage(n,t,i){const s=(new Date).getTime().toString(),r={id:s,type:n,payload:t,from:this.moduleId};return new Promise(((n,t)=>{if(i?.aborted)return t(new Error("Request aborted"));const o=()=>{this.pendingMessages.delete(s),this.targetWindow.postMessage({id:s,type:e.Abort,payload:JSON.stringify({status:"Task aborted",data:null})},"*"),t(new Error("Request aborted"))};i?.addEventListener("abort",o),this.pendingMessages.set(s,{resolve:n,reject:t}),this.targetWindow.postMessage(r,"*");const a=setTimeout((()=>{this.pendingMessages.delete(s),i?.removeEventListener("abort",o),t(new Error("Communication with Pulse Editor timeout."))}),this.timeout),d=this.pendingMessages.get(s);d&&(d.resolve=e=>{clearTimeout(a),i?.removeEventListener("abort",o),n(e)},d.reject=()=>{clearTimeout(a),i?.removeEventListener("abort",o),t()})}))}getPendingMessage(e){return this.pendingMessages.get(e)}removePendingMessage(e){this.pendingMessages.delete(e)}}class h{constructor(){}initThisWindow(e,n){this.thisWindow=e;const t=e.viewId;if(!t)throw new Error("Current window's ID is not defined.");this.thisWindowId=t,this.receiverHandlerMap=new Map;const i=new a(this.receiverHandlerMap,this.thisWindowId);this.receiver=i,this.listener=e=>{if(!i)throw new Error("Receiver not initialized at module "+this.thisWindowId);const t=e.data;if(n&&t.from!==n)return;const s=e.source;i.receiveMessage(s,t)},e.addEventListener("message",this.listener),console.log("Adding IMC listener in "+this.thisWindowId)}initOtherWindow(n){if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");this.otherWindow=n;const t=n.viewId;if(!t)throw new Error("Other window's ID is not defined.");this.otherWindowId=t;const i=new d(n,o,this.thisWindowId);if(this.sender=i,!this.receiverHandlerMap)throw new Error("You must initialize the current window first.");this.receiverHandlerMap.set(e.Acknowledge,(async(e,n)=>{const t=i.getPendingMessage(n.id);t&&(t.resolve(n.payload),i.removePendingMessage(n.id))}))}close(){this.listener&&window.removeEventListener("message",this.listener)}async sendMessage(e,n,t){const i=this.sender;if(!i)throw new Error("Sender not initialized");return await i.sendMessage(e,n,t)}updateReceiverHandlerMap(n){if(!this.receiver)throw new Error("Receiver not initialized");this.receiverHandlerMap?.clear(),this.receiverHandlerMap?.set(e.Acknowledge,(async(e,n)=>{const t=this.sender?.getPendingMessage(n.id);t&&(t.resolve(n.payload),this.sender?.removePendingMessage(n.id))})),n.forEach(((e,n)=>{this.receiverHandlerMap?.set(n,e)}))}getThisWindowId(){if(!this.thisWindowId)throw new Error("This window ID is not defined.");return this.thisWindowId}getOtherWindowId(){if(!this.otherWindowId)throw new Error("Other window ID is not defined.");return this.otherWindowId}}class c{constructor(e,n=!1){this.channels=new Map,this.baseReceiverHandlerMap=e,this.channelReceiverHandlerMapMap=new Map,n&&(this.connectionListener=new h,this.connectionListener.initThisWindow(window),this.connectionListener.updateReceiverHandlerMap(this.getConnectionListenerHandlerMap()))}async sendMessage(e,n,t,i){const s=this.channels.get(e);if(!s)throw new Error("Channel not found for window ID "+e);return await s.sendMessage(n,t,i)}updateBaseReceiverHandlerMap(e){this.baseReceiverHandlerMap=e,this.channels.forEach(((e,n)=>{const t=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(n));e.updateReceiverHandlerMap(t)}))}updateChannelReceiverHandlerMap(e,n){const t=this.channels.get(e);if(!t)throw new Error("Channel not found for window ID "+e);const i=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,n);t.updateReceiverHandlerMap(i)}async createChannel(e,n,t){const i=new h;i.initThisWindow(window,n),i.initOtherWindow(e),t?(i.updateReceiverHandlerMap(this.getCombinedHandlerMap(this.baseReceiverHandlerMap,t)),this.channelReceiverHandlerMapMap.set(n,t)):i.updateReceiverHandlerMap(this.baseReceiverHandlerMap),this.channels.set(n,i)}async removeChannel(e){const n=this.channels.get(e);if(!n)throw new Error("Channel not found for window ID "+e);n.close(),this.channels.delete(e),this.channelReceiverHandlerMapMap.delete(e)}hasChannel(e){return this.channels.has(e)}getCombinedHandlerMap(e,n){if(e&&n){return new Map([...e,...n])}return e||(n||new Map)}getConnectionListenerHandlerMap(){return new Map([[e.ExtReady,async(e,n)=>{const t=n.from;this.channels.has(t)?console.log("Channel already exists for window ID "+t+". Re-using the existing channel."):await this.createChannel(e,t,this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(t)))}]])}}class l{constructor(n,t,i){const s=new h;this.listener=s,s.initThisWindow(window),s.updateReceiverHandlerMap(new Map([[e.ExtReady,async(e,s,r)=>{const o=s.from;n.hasChannel(o)?console.log("Channel already exists for window ID "+o+". Re-using the existing channel."):(await n.createChannel(e,o,t),i&&i(e,s))}]]))}close(){this.listener?.close()}}export{i as AccessEnum,l as ConnectionListener,t as ExtensionTypeEnum,e as IMCMessageTypeEnum,h as InterModuleCommunication,a as MessageReceiver,d as MessageSender,n as NotificationTypeEnum,c as PolyIMC,s as isArrayType,r as isObjectType,o as messageTimeout};
1
+ var e,n,t,i;function s(e){return Array.isArray(e)&&1===e.length}function r(e){return"object"==typeof e&&!Array.isArray(e)}!function(e){e.WriteViewFile="write-view-file",e.RequestViewFile="request-view-file",e.Fetch="fetch",e.Notification="notification",e.ThemeChange="theme-change",e.RunAgentMethod="run-agent-method",e.UseVAD="use-vad",e.UseSTT="use-stt",e.UseLLM="use-llm",e.UseTTS="use-tts",e.UseDiffusion="use-diffusion",e.UseOCR="use-ocr",e.UseSpeech2Speech="use-speech2speech",e.RunExtCommand="run-ext-command",e.RequestTerminal="request-terminal",e.ExtReady="ext-ready",e.ExtClose="ext-close",e.Loaded="loaded",e.Acknowledge="acknowledge",e.Abort="abort",e.Error="error"}(e||(e={})),function(e){e.Success="success",e.Error="error",e.Info="info",e.Warning="warning"}(n||(n={})),function(e){e.Generic="generic",e.FileView="file-view",e.ConsoleView="console-view"}(t||(t={})),function(e){e.public="public",e.private="private"}(i||(i={}));const o=3e5;class a{constructor(e,n){this.handlerMap=e,this.pendingTasks=new Map,this.moduleId=n}receiveMessage(n,t){if(this.moduleId===t.from)return;if("development"===process.env.NODE_ENV&&console.log(`Module ${this.moduleId} received message from module ${t.from}:\n ${JSON.stringify(t)}`),t.type===e.Abort){const e=t.id,n=this.pendingTasks.get(e);return void(n&&(console.log("Aborting task",e),n.controller.abort(),this.pendingTasks.delete(e)))}const i=this.handlerMap.get(t.type);if(i){const s=new AbortController,r=s.signal;this.pendingTasks.set(t.id,{controller:s});i(n,t,r).then((i=>{r.aborted||t.type!==e.Acknowledge&&this.acknowledgeSender(n,t.id,i)})).catch((i=>{const s={id:t.id,type:e.Error,payload:i.message,from:this.moduleId};n.postMessage(s,"*")})).finally((()=>{this.pendingTasks.delete(t.id)}))}}acknowledgeSender(n,t,i){const s={id:t,type:e.Acknowledge,payload:i,from:this.moduleId};n.postMessage(s,"*")}}class d{constructor(e,n,t){this.targetWindow=e,this.timeout=n,this.pendingMessages=new Map,this.moduleId=t}async sendMessage(n,t,i){const s=(new Date).getTime().toString(),r={id:s,type:n,payload:t,from:this.moduleId};return new Promise(((n,t)=>{if(i?.aborted)return t(new Error("Request aborted"));const o=()=>{this.pendingMessages.delete(s),this.targetWindow.postMessage({id:s,type:e.Abort,payload:JSON.stringify({status:"Task aborted",data:null})},"*"),t(new Error("Request aborted"))};i?.addEventListener("abort",o),this.pendingMessages.set(s,{resolve:n,reject:t}),this.targetWindow.postMessage(r,"*");const a=setTimeout((()=>{this.pendingMessages.delete(s),i?.removeEventListener("abort",o),t(new Error("Communication with Pulse Editor timeout."))}),this.timeout),d=this.pendingMessages.get(s);d&&(d.resolve=e=>{clearTimeout(a),i?.removeEventListener("abort",o),n(e)},d.reject=()=>{clearTimeout(a),i?.removeEventListener("abort",o),t()})}))}getPendingMessage(e){return this.pendingMessages.get(e)}removePendingMessage(e){this.pendingMessages.delete(e)}}class h{constructor(){}initThisWindow(e,n){this.thisWindow=e;const t=e.viewId;if(!t)throw new Error("Current window's ID is not defined.");this.thisWindowId=t,this.receiverHandlerMap=new Map;const i=new a(this.receiverHandlerMap,this.thisWindowId);this.receiver=i,this.listener=e=>{if(!i)throw new Error("Receiver not initialized at module "+this.thisWindowId);const t=e.data;if(n&&t.from!==n)return;const s=e.source;i.receiveMessage(s,t)},e.addEventListener("message",this.listener),console.log("Adding IMC listener in "+this.thisWindowId)}initOtherWindow(n){if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");this.otherWindow=n;const t=n.viewId;if(!t)throw new Error("Other window's ID is not defined.");this.otherWindowId=t;const i=new d(n,o,this.thisWindowId);if(this.sender=i,!this.receiverHandlerMap)throw new Error("You must initialize the current window first.");this.receiverHandlerMap.set(e.Acknowledge,(async(e,n)=>{const t=i.getPendingMessage(n.id);t&&(t.resolve(n.payload),i.removePendingMessage(n.id))}))}close(){this.listener&&window.removeEventListener("message",this.listener)}async sendMessage(e,n,t){const i=this.sender;if(!i)throw new Error("Sender not initialized");return await i.sendMessage(e,n,t)}updateReceiverHandlerMap(n){if(!this.receiver)throw new Error("Receiver not initialized");this.receiverHandlerMap?.clear(),this.receiverHandlerMap?.set(e.Acknowledge,(async(e,n)=>{const t=this.sender?.getPendingMessage(n.id);t&&(t.resolve(n.payload),this.sender?.removePendingMessage(n.id))})),n.forEach(((e,n)=>{this.receiverHandlerMap?.set(n,e)}))}getThisWindowId(){if(!this.thisWindowId)throw new Error("This window ID is not defined.");return this.thisWindowId}getOtherWindowId(){if(!this.otherWindowId)throw new Error("Other window ID is not defined.");return this.otherWindowId}}class c{constructor(e,n=!1){this.channels=new Map,this.baseReceiverHandlerMap=e,this.channelReceiverHandlerMapMap=new Map,n&&(this.connectionListener=new h,this.connectionListener.initThisWindow(window),this.connectionListener.updateReceiverHandlerMap(this.getConnectionListenerHandlerMap()))}async sendMessage(e,n,t,i){const s=this.channels.get(e);if(!s)throw new Error("Channel not found for window ID "+e);return await s.sendMessage(n,t,i)}updateBaseReceiverHandlerMap(e){this.baseReceiverHandlerMap=e,this.channels.forEach(((e,n)=>{const t=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(n));e.updateReceiverHandlerMap(t)}))}updateChannelReceiverHandlerMap(e,n){const t=this.channels.get(e);if(!t)throw new Error("Channel not found for window ID "+e);const i=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,n);t.updateReceiverHandlerMap(i),this.channelReceiverHandlerMapMap.set(e,n)}createChannel(e,n,t){const i=new h;i.initThisWindow(window,n),i.initOtherWindow(e),t?(i.updateReceiverHandlerMap(this.getCombinedHandlerMap(this.baseReceiverHandlerMap,t)),this.channelReceiverHandlerMapMap.set(n,t)):i.updateReceiverHandlerMap(this.baseReceiverHandlerMap),this.channels.set(n,i)}removeChannel(e){const n=this.channels.get(e);if(!n)throw new Error("Channel not found for window ID "+e);n.close(),this.channels.delete(e),this.channelReceiverHandlerMapMap.delete(e)}hasChannel(e){return this.channels.has(e)}getCombinedHandlerMap(e,n){if(e&&n){return new Map([...e,...n])}return e||(n||new Map)}getConnectionListenerHandlerMap(){return new Map([[e.ExtReady,async(e,n)=>{const t=n.from;this.channels.has(t)?console.log("Channel already exists for window ID "+t+". Re-using the existing channel."):this.createChannel(e,t,this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(t)))}]])}}class l{constructor(n,t,i){const s=new h;this.listener=s,s.initThisWindow(window),s.updateReceiverHandlerMap(new Map([[e.ExtReady,async(e,s,r)=>{const o=s.from;n.hasChannel(o)?console.log("Channel already exists for window ID "+o+". Re-using the existing channel."):(await n.createChannel(e,o,t),i&&i(e,s))}]]))}close(){this.listener?.close()}}export{i as AccessEnum,l as ConnectionListener,t as ExtensionTypeEnum,e as IMCMessageTypeEnum,h as InterModuleCommunication,a as MessageReceiver,d as MessageSender,n as NotificationTypeEnum,c as PolyIMC,s as isArrayType,r as isObjectType,o as messageTimeout};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pulse-editor/shared-utils",
3
- "version": "0.1.1-alpha.10",
3
+ "version": "0.1.1-alpha.12",
4
4
  "main": "dist/main.js",
5
5
  "files": [
6
6
  "dist"