@gamention/pulse-core 0.3.3 → 0.3.4

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.
package/dist/index.d.ts CHANGED
@@ -21,12 +21,17 @@ export declare class Connection extends Emitter {
21
21
  private reconnectTimer;
22
22
  private _state;
23
23
  private permanentlyClosed;
24
+ private handleOpen;
25
+ private handleMessage;
26
+ private handleClose;
27
+ private handleError;
24
28
  get state(): ConnectionState;
25
29
  constructor(endpoint?: string);
26
30
  connect(): void;
27
31
  disconnect(): void;
28
32
  send(message: ClientMessage): void;
29
33
  permanentDisconnect(): void;
34
+ private cleanupWs;
30
35
  private scheduleReconnect;
31
36
  }
32
37
 
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var _=(h,a,e)=>a in h?f(h,a,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[a]=e;var r=(h,a,e)=>_(h,typeof a!="symbol"?a+"":a,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@gamention/pulse-shared");class d{constructor(){r(this,"handlers",new Map)}on(a,e){this.handlers.has(a)||this.handlers.set(a,new Set);const t=this.handlers.get(a);return t.add(e),()=>t.delete(e)}off(a,e){var t;(t=this.handlers.get(a))==null||t.delete(e)}emit(a,e){var t;(t=this.handlers.get(a))==null||t.forEach(i=>i(e))}removeAll(){this.handlers.clear()}}class p extends d{constructor(e){super();r(this,"ws",null);r(this,"endpoint");r(this,"reconnectAttempt",0);r(this,"reconnectTimer",null);r(this,"_state","disconnected");r(this,"permanentlyClosed",!1);this.endpoint=e??c.DEFAULT_ENDPOINT}get state(){return this._state}connect(){this.ws||this.permanentlyClosed||(this._state="connecting",this.emit("state",this._state),this.ws=new WebSocket(this.endpoint),this.ws.addEventListener("open",()=>{this._state="connected",this.reconnectAttempt=0,this.emit("state",this._state)}),this.ws.addEventListener("message",e=>{try{const t=JSON.parse(e.data);this.emit("message",t)}catch{}}),this.ws.addEventListener("close",()=>{this.ws=null,this._state="disconnected",this.emit("state",this._state),this.scheduleReconnect()}),this.ws.addEventListener("error",()=>{var e;(e=this.ws)==null||e.close()}))}disconnect(){var e;this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.reconnectAttempt=0,(e=this.ws)==null||e.close(),this.ws=null,this._state="disconnected",this.emit("state",this._state)}send(e){var t;((t=this.ws)==null?void 0:t.readyState)===WebSocket.OPEN&&this.ws.send(JSON.stringify(e))}permanentDisconnect(){this.permanentlyClosed=!0,this.disconnect()}scheduleReconnect(){if(this.permanentlyClosed)return;const e=Math.min(c.RECONNECT_BASE_DELAY_MS*2**this.reconnectAttempt,c.RECONNECT_MAX_DELAY_MS);this.reconnectAttempt++,this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.connect()},e)}}class u extends d{constructor(){super(...arguments);r(this,"baseUrl","");r(this,"_user",null);r(this,"_config",{...c.DEFAULT_ENV_CONFIG});r(this,"_p2pConfig",null);r(this,"_users",new Map);r(this,"_presence",new Map);r(this,"_threads",new Map);r(this,"_reactions",new Map);r(this,"_notifications",[]);r(this,"_activityLogs",[]);r(this,"_typing",new Map);r(this,"_viewports",new Map);r(this,"_selections",new Map)}get user(){return this._user}get p2pConfig(){return this._p2pConfig}get config(){return this._config}removeComment(e){for(const[t,i]of this._threads){const s=i.comments.findIndex(n=>n.id===e);if(s!==-1){i.comments.splice(s,1),i.comments.length===0&&this._threads.delete(t),this.emit("threads",this.threads);return}}}get presence(){return[...this._presence.values()]}get threads(){return[...this._threads.values()]}get notifications(){return this._notifications}get unreadCount(){return this._notifications.filter(e=>!e.read).length}markNotificationRead(e){const t=this._notifications.find(i=>i.id===e);t&&!t.read&&(t.read=!0,this.emit("notifications",this._notifications))}markAllNotificationsRead(){let e=!1;for(const t of this._notifications)t.read||(t.read=!0,e=!0);e&&this.emit("notifications",this._notifications)}get activityLogs(){return this._activityLogs}getUser(e){return this._users.get(e)}get users(){return[...this._users.values()]}getReactions(e){return this._reactions.get(e)??[]}getTypingUsers(e){const t=this._typing.get(e);if(!t)return[];const i=Date.now(),s=[];for(const[n,o]of t)i-o<3e3&&s.push(n);return s}get viewports(){return this._viewports}getViewport(e){return this._viewports.get(e)}get selections(){return this._selections}resolveUrl(e){return!this.baseUrl||!e||e.startsWith("http://")||e.startsWith("https://")?e:`${this.baseUrl}${e}`}resolveAttachments(e){return e.map(t=>({...t,url:this.resolveUrl(t.url),thumbnailUrl:t.thumbnailUrl?this.resolveUrl(t.thumbnailUrl):void 0}))}resolveComment(e){return!e.attachments||e.attachments.length===0?e:{...e,attachments:this.resolveAttachments(e.attachments)}}resolveThread(e){return{...e,comments:e.comments.map(t=>this.resolveComment(t))}}handleMessage(e){switch(e.type){case"auth:ok":this._config=e.config??{...c.DEFAULT_ENV_CONFIG},this._p2pConfig=e.p2pConfig??null,this._user=e.user,this._users.clear();for(const t of e.users)this._users.set(t.id,t);this._presence.clear();for(const t of e.presence)this._presence.set(t.user.id,t),this._users.set(t.user.id,t.user);this._users.set(e.user.id,e.user),this._threads.clear();for(const t of e.threads)this._threads.set(t.id,this.resolveThread(t));this._notifications=e.notifications,this._reactions.clear();for(const t of e.reactions){const i=this._reactions.get(t.targetId)??[];i.push(t),this._reactions.set(t.targetId,i)}this._activityLogs=[...e.activityLogs],this.emit("auth",e.user),this.emit("presence",this.presence),this.emit("threads",this.threads),this.emit("notifications",this._notifications),this.emit("reactions",null),this.emit("activity-logs",this._activityLogs);break;case"presence:join":this._presence.set(e.user.user.id,e.user),this._users.set(e.user.user.id,e.user.user),this.emit("presence",this.presence);break;case"presence:leave":this._presence.delete(e.userId),this._viewports.delete(e.userId),this._selections.delete(e.userId);for(const t of this._typing.values())t.delete(e.userId);this.emit("presence",this.presence);break;case"presence:update":{const t=this._presence.get(e.userId);t&&(t.status=e.status,this.emit("presence",this.presence));break}case"cursor:move":this.emit("cursor",{userId:e.userId,position:e.position});break;case"click:perform":this.emit("click",{userId:e.userId,position:e.position});break;case"thread:created":this._threads.set(e.thread.id,this.resolveThread(e.thread)),this.emit("threads",this.threads);break;case"comment:created":{const t=this._threads.get(e.threadId);t&&(t.comments.push(this.resolveComment(e.comment)),t.updatedAt=e.comment.createdAt,this.emit("threads",this.threads));break}case"comment:edited":{const t=this._threads.get(e.threadId);if(t){const i=t.comments.findIndex(s=>s.id===e.comment.id);i!==-1&&(t.comments[i]=this.resolveComment(e.comment)),this.emit("threads",this.threads)}break}case"comment:deleted":{const t=this._threads.get(e.threadId);t&&(t.comments=t.comments.filter(i=>i.id!==e.commentId),t.comments.length===0&&this._threads.delete(e.threadId),this.emit("threads",this.threads));break}case"thread:resolved":{const t=this._threads.get(e.threadId);t&&(t.resolved=e.resolved,this.emit("threads",this.threads));break}case"thread:deleted":this._threads.delete(e.threadId),this.emit("threads",this.threads);break;case"reaction:added":{const t=this._reactions.get(e.reaction.targetId)??[];t.push(e.reaction),this._reactions.set(e.reaction.targetId,t),this.emit("reactions",{targetId:e.reaction.targetId,reactions:t});break}case"reaction:removed":{const t=this._reactions.get(e.targetId);if(t){const i=t.filter(s=>s.id!==e.reactionId);this._reactions.set(e.targetId,i),this.emit("reactions",{targetId:e.targetId,reactions:i})}break}case"notification":this._notifications.unshift(e.notification),this.emit("notifications",this._notifications);break;case"typing:indicator":{this._typing.has(e.threadId)||this._typing.set(e.threadId,new Map),this._typing.get(e.threadId).set(e.userId,Date.now()),this.emit("typing",{threadId:e.threadId,userId:e.userId});break}case"viewport:update":{this._viewports.set(e.userId,{scrollX:e.scrollX,scrollY:e.scrollY,viewportWidth:e.viewportWidth,viewportHeight:e.viewportHeight,pageWidth:e.pageWidth,pageHeight:e.pageHeight}),this.emit("viewport",{userId:e.userId});break}case"selection:update":this._selections.set(e.userId,e.selection),this.emit("selection",{userId:e.userId,selection:e.selection});break;case"emoji:drop":this.emit("emoji-drop",{userId:e.userId,emoji:e.emoji,position:e.position});break;case"draw:stroke":this.emit("draw-stroke",{userId:e.userId,points:e.points,color:e.color,width:e.width});break;case"draw:clear":this.emit("draw-clear",{userId:e.userId});break;case"activity:logged":this._activityLogs.unshift(e.activityLog),this._activityLogs.length>100&&(this._activityLogs=this._activityLogs.slice(0,100)),this.emit("activity-logs",this._activityLogs);break;case"auth:error":this.emit("auth:error",e);break;case"error":this.emit("error",e);break}}reset(){this._user=null,this._config={...c.DEFAULT_ENV_CONFIG},this._p2pConfig=null,this._users.clear(),this._presence.clear(),this._threads.clear(),this._reactions.clear(),this._notifications=[],this._activityLogs=[],this._typing.clear(),this._viewports.clear(),this._selections.clear()}}class m extends d{constructor(e){var i;super();r(this,"state");r(this,"connection");r(this,"config");r(this,"heartbeatTimer",null);r(this,"lastCursorSend",0);r(this,"pendingCursor",null);r(this,"cursorTimer",null);r(this,"_p2p",null);r(this,"_p2pInstance",null);r(this,"p2pEnabled");r(this,"iceServers");this.config=e,this.state=new u,this.state.baseUrl=(e.endpoint??"").replace(/^ws(s?):/,"http$1:").replace(/\/$/,"");const t=((i=e.endpoint)==null?void 0:i.replace(/^http/,"ws"))??void 0;this.connection=new p(t),this.p2pEnabled=e.p2p??!1,this.iceServers=e.iceServers??[{urls:"stun:stun.l.google.com:19302"}],this.connection.on("message",s=>{this.state.handleMessage(s),this.emit(s.type,s),s.type==="auth:error"&&this.connection.permanentDisconnect(),this._p2pInstance&&(s.type==="signal:offer"?this._p2pInstance.handleSignalOffer(s.fromUserId,s.sdp):s.type==="signal:answer"?this._p2pInstance.handleSignalAnswer(s.fromUserId,s.sdp):s.type==="signal:ice"?this._p2pInstance.handleIceCandidate(s.fromUserId,s.candidate):s.type==="p2p:sync"?this._p2pInstance.handleP2PSync(s.fromUserId,s.update):s.type==="presence:join"?this._p2pInstance.onPeerJoined(s.user.user.id):s.type==="presence:leave"&&this._p2pInstance.onPeerLeft(s.userId))}),this.connection.on("state",s=>{this.emit("connection",s),s==="connected"?(this.authenticate(),this.startHeartbeat()):s==="disconnected"&&this.stopHeartbeat()})}get connectionState(){return this.connection.state}connect(){this.connection.connect()}disconnect(){this.stopHeartbeat(),this.cursorTimer&&(clearTimeout(this.cursorTimer),this.cursorTimer=null),this.connection.disconnect(),this.state.reset()}destroy(){var e;(e=this._p2pInstance)==null||e.destroy(),this._p2pInstance=null,this._p2p=null,this.disconnect(),this.removeAll(),this.state.removeAll(),this.connection.removeAll()}authenticate(){this.send({type:"auth",apiKey:this.config.apiKey,token:this.config.token,room:this.config.room})}send(e){this.connection.send(e)}moveCursor(e){const t=Date.now();this.pendingCursor=e,t-this.lastCursorSend>=c.CURSOR_THROTTLE_MS?this.flushCursor():this.cursorTimer||(this.cursorTimer=setTimeout(()=>{this.cursorTimer=null,this.flushCursor()},c.CURSOR_THROTTLE_MS))}flushCursor(){this.pendingCursor&&(this.send({type:"cursor:move",position:this.pendingCursor}),this.lastCursorSend=Date.now(),this.pendingCursor=null)}updatePresence(e){this.send({type:"presence:update",status:e})}startHeartbeat(){this.heartbeatTimer||(this.heartbeatTimer=setInterval(()=>{this.send({type:"presence:update",status:"online"})},c.PRESENCE_HEARTBEAT_MS))}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}createThread(e,t={}){const i=crypto.randomUUID();return this.send({type:"thread:create",id:i,body:e,mentions:t.mentions??[],position:t.position??null,attachmentIds:t.attachmentIds}),i}reply(e,t,i=[],s){const n=crypto.randomUUID();return this.send({type:"comment:create",threadId:e,id:n,body:t,mentions:i,attachmentIds:s}),n}editComment(e,t,i=[]){this.send({type:"comment:edit",commentId:e,body:t,mentions:i})}deleteComment(e){this.state.removeComment(e),this.send({type:"comment:delete",commentId:e})}resolveThread(e,t=!0){this.send({type:"thread:resolve",threadId:e,resolved:t})}addReaction(e,t,i){this.send({type:"reaction:add",targetId:e,targetType:t,emoji:i})}removeReaction(e){this.send({type:"reaction:remove",reactionId:e})}markRead(e){this.state.markNotificationRead(e),this.send({type:"notification:read",notificationId:e})}markAllRead(){this.state.markAllNotificationsRead(),this.send({type:"notification:read-all"})}performClick(e){this.send({type:"click:perform",position:e})}sendTyping(e){this.send({type:"typing:start",threadId:e})}updateViewport(e){this.send({type:"viewport:update",...e})}updateSelection(e){this.send({type:"selection:update",selection:e})}dropEmoji(e,t){this.send({type:"emoji:drop",emoji:e,position:t})}drawStroke(e,t,i){this.send({type:"draw:stroke",points:e,color:t,width:i})}clearDrawing(){this.send({type:"draw:clear"})}async uploadFile(e){const t=typeof window<"u"?window.location.origin:"",i=(this.config.endpoint??t).replace(/^ws(s?):/,"http$1:"),s=new FormData;s.append("file",e);const n=await fetch(`${i}/api/v1/upload`,{method:"POST",headers:{"X-Pulse-Key":this.config.apiKey,"X-Pulse-Token":this.config.token},body:s});if(!n.ok){const l=await n.json().catch(()=>({error:"Upload failed"}));throw new Error(l.error??"Upload failed")}const o=await n.json();return o.url&&!o.url.startsWith("http")&&(o.url=`${i}${o.url}`),o.thumbnailUrl&&!o.thumbnailUrl.startsWith("http")&&(o.thumbnailUrl=`${i}${o.thumbnailUrl}`),o}setAppearOffline(e){e?(this.stopHeartbeat(),this.send({type:"presence:update",status:"idle"})):(this.startHeartbeat(),this.send({type:"presence:update",status:"online"}))}get p2p(){return this.p2pEnabled?(this._p2p||(this._p2p=this.initP2P()),this._p2p):Promise.reject(new Error("P2P is not enabled. Pass { p2p: true } in PulseConfig."))}async initP2P(){const e=await new Promise((n,o)=>{if(this.state.user&&this.state.p2pConfig){n({userId:this.state.user.id,p2pConfig:this.state.p2pConfig});return}const l=this.state.on("auth",()=>{if(l(),!this.state.p2pConfig){o(new Error("P2P is not enabled for this environment."));return}n({userId:this.state.user.id,p2pConfig:this.state.p2pConfig})})});let t=this.iceServers;try{const n=await fetch(`${this.state.baseUrl}/api/v1/turn-credentials`,{headers:{Authorization:`Bearer ${this.config.token}`}});n.ok&&(t=(await n.json()).iceServers)}catch{}const{P2PManager:i}=await Promise.resolve().then(()=>require("./P2PManager-e4ShL60A.cjs")),s=new i({roomId:this.config.room,userId:e.userId,baseUrl:this.state.baseUrl,authToken:this.config.token,p2pConfig:e.p2pConfig,iceServers:t,sendWS:n=>this.send(n)});this._p2pInstance=s,await s.initialize();for(const n of this.state.presence)n.user.id!==e.userId&&s.onPeerJoined(n.user.id);return s}}exports.Connection=p;exports.Emitter=d;exports.PulseClient=m;exports.StateManager=u;
1
+ "use strict";var f=Object.defineProperty;var _=(h,a,e)=>a in h?f(h,a,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[a]=e;var r=(h,a,e)=>_(h,typeof a!="symbol"?a+"":a,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@gamention/pulse-shared");class d{constructor(){r(this,"handlers",new Map)}on(a,e){this.handlers.has(a)||this.handlers.set(a,new Set);const t=this.handlers.get(a);return t.add(e),()=>t.delete(e)}off(a,e){var t;(t=this.handlers.get(a))==null||t.delete(e)}emit(a,e){var t;(t=this.handlers.get(a))==null||t.forEach(i=>i(e))}removeAll(){this.handlers.clear()}}class p extends d{constructor(e){super();r(this,"ws",null);r(this,"endpoint");r(this,"reconnectAttempt",0);r(this,"reconnectTimer",null);r(this,"_state","disconnected");r(this,"permanentlyClosed",!1);r(this,"handleOpen",()=>{this._state="connected",this.reconnectAttempt=0,this.emit("state",this._state)});r(this,"handleMessage",e=>{try{const t=JSON.parse(e.data);this.emit("message",t)}catch{}});r(this,"handleClose",()=>{this.cleanupWs(),this._state="disconnected",this.emit("state",this._state),this.scheduleReconnect()});r(this,"handleError",()=>{var e;(e=this.ws)==null||e.close()});this.endpoint=e??c.DEFAULT_ENDPOINT}get state(){return this._state}connect(){this.ws||this.permanentlyClosed||(this._state="connecting",this.emit("state",this._state),this.ws=new WebSocket(this.endpoint),this.ws.addEventListener("open",this.handleOpen),this.ws.addEventListener("message",this.handleMessage),this.ws.addEventListener("close",this.handleClose),this.ws.addEventListener("error",this.handleError))}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.reconnectAttempt=0,this.cleanupWs(),this._state="disconnected",this.emit("state",this._state)}send(e){var t;((t=this.ws)==null?void 0:t.readyState)===WebSocket.OPEN&&this.ws.send(JSON.stringify(e))}permanentDisconnect(){this.permanentlyClosed=!0,this.disconnect()}cleanupWs(){this.ws&&(this.ws.removeEventListener("open",this.handleOpen),this.ws.removeEventListener("message",this.handleMessage),this.ws.removeEventListener("close",this.handleClose),this.ws.removeEventListener("error",this.handleError),this.ws.close(),this.ws=null)}scheduleReconnect(){if(this.permanentlyClosed)return;const e=Math.min(c.RECONNECT_BASE_DELAY_MS*2**this.reconnectAttempt,c.RECONNECT_MAX_DELAY_MS);this.reconnectAttempt++,this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.connect()},e)}}class u extends d{constructor(){super(...arguments);r(this,"baseUrl","");r(this,"_user",null);r(this,"_config",{...c.DEFAULT_ENV_CONFIG});r(this,"_p2pConfig",null);r(this,"_users",new Map);r(this,"_presence",new Map);r(this,"_threads",new Map);r(this,"_reactions",new Map);r(this,"_notifications",[]);r(this,"_activityLogs",[]);r(this,"_typing",new Map);r(this,"_viewports",new Map);r(this,"_selections",new Map)}get user(){return this._user}get p2pConfig(){return this._p2pConfig}get config(){return this._config}removeComment(e){for(const[t,i]of this._threads){const s=i.comments.findIndex(n=>n.id===e);if(s!==-1){i.comments.splice(s,1),i.comments.length===0&&this._threads.delete(t),this.emit("threads",this.threads);return}}}get presence(){return[...this._presence.values()]}get threads(){return[...this._threads.values()]}get notifications(){return this._notifications}get unreadCount(){return this._notifications.filter(e=>!e.read).length}markNotificationRead(e){const t=this._notifications.find(i=>i.id===e);t&&!t.read&&(t.read=!0,this.emit("notifications",this._notifications))}markAllNotificationsRead(){let e=!1;for(const t of this._notifications)t.read||(t.read=!0,e=!0);e&&this.emit("notifications",this._notifications)}get activityLogs(){return this._activityLogs}getUser(e){return this._users.get(e)}get users(){return[...this._users.values()]}getReactions(e){return this._reactions.get(e)??[]}getTypingUsers(e){const t=this._typing.get(e);if(!t)return[];const i=Date.now(),s=[];for(const[n,o]of t)i-o<3e3&&s.push(n);return s}get viewports(){return this._viewports}getViewport(e){return this._viewports.get(e)}get selections(){return this._selections}resolveUrl(e){return!this.baseUrl||!e||e.startsWith("http://")||e.startsWith("https://")?e:`${this.baseUrl}${e}`}resolveAttachments(e){return e.map(t=>({...t,url:this.resolveUrl(t.url),thumbnailUrl:t.thumbnailUrl?this.resolveUrl(t.thumbnailUrl):void 0}))}resolveComment(e){return!e.attachments||e.attachments.length===0?e:{...e,attachments:this.resolveAttachments(e.attachments)}}resolveThread(e){return{...e,comments:e.comments.map(t=>this.resolveComment(t))}}handleMessage(e){switch(e.type){case"auth:ok":this._config=e.config??{...c.DEFAULT_ENV_CONFIG},this._p2pConfig=e.p2pConfig??null,this._user=e.user,this._users.clear();for(const t of e.users)this._users.set(t.id,t);this._presence.clear();for(const t of e.presence)this._presence.set(t.user.id,t),this._users.set(t.user.id,t.user);this._users.set(e.user.id,e.user),this._threads.clear();for(const t of e.threads)this._threads.set(t.id,this.resolveThread(t));this._notifications=e.notifications,this._reactions.clear();for(const t of e.reactions){const i=this._reactions.get(t.targetId)??[];i.push(t),this._reactions.set(t.targetId,i)}this._activityLogs=[...e.activityLogs],this.emit("auth",e.user),this.emit("presence",this.presence),this.emit("threads",this.threads),this.emit("notifications",this._notifications),this.emit("reactions",null),this.emit("activity-logs",this._activityLogs);break;case"presence:join":this._presence.set(e.user.user.id,e.user),this._users.set(e.user.user.id,e.user.user),this.emit("presence",this.presence);break;case"presence:leave":this._presence.delete(e.userId),this._viewports.delete(e.userId),this._selections.delete(e.userId);for(const t of this._typing.values())t.delete(e.userId);this.emit("presence",this.presence);break;case"presence:update":{const t=this._presence.get(e.userId);t&&(t.status=e.status,this.emit("presence",this.presence));break}case"cursor:move":this.emit("cursor",{userId:e.userId,position:e.position});break;case"click:perform":this.emit("click",{userId:e.userId,position:e.position});break;case"thread:created":this._threads.set(e.thread.id,this.resolveThread(e.thread)),this.emit("threads",this.threads);break;case"comment:created":{const t=this._threads.get(e.threadId);t&&(t.comments.push(this.resolveComment(e.comment)),t.updatedAt=e.comment.createdAt,this.emit("threads",this.threads));break}case"comment:edited":{const t=this._threads.get(e.threadId);if(t){const i=t.comments.findIndex(s=>s.id===e.comment.id);i!==-1&&(t.comments[i]=this.resolveComment(e.comment)),this.emit("threads",this.threads)}break}case"comment:deleted":{const t=this._threads.get(e.threadId);t&&(t.comments=t.comments.filter(i=>i.id!==e.commentId),t.comments.length===0&&this._threads.delete(e.threadId),this.emit("threads",this.threads));break}case"thread:resolved":{const t=this._threads.get(e.threadId);t&&(t.resolved=e.resolved,this.emit("threads",this.threads));break}case"thread:deleted":this._threads.delete(e.threadId),this.emit("threads",this.threads);break;case"reaction:added":{const t=this._reactions.get(e.reaction.targetId)??[];t.push(e.reaction),this._reactions.set(e.reaction.targetId,t),this.emit("reactions",{targetId:e.reaction.targetId,reactions:t});break}case"reaction:removed":{const t=this._reactions.get(e.targetId);if(t){const i=t.filter(s=>s.id!==e.reactionId);this._reactions.set(e.targetId,i),this.emit("reactions",{targetId:e.targetId,reactions:i})}break}case"notification":this._notifications.unshift(e.notification),this.emit("notifications",this._notifications);break;case"typing:indicator":{this._typing.has(e.threadId)||this._typing.set(e.threadId,new Map),this._typing.get(e.threadId).set(e.userId,Date.now()),this.emit("typing",{threadId:e.threadId,userId:e.userId});break}case"viewport:update":{this._viewports.set(e.userId,{scrollX:e.scrollX,scrollY:e.scrollY,viewportWidth:e.viewportWidth,viewportHeight:e.viewportHeight,pageWidth:e.pageWidth,pageHeight:e.pageHeight}),this.emit("viewport",{userId:e.userId});break}case"selection:update":this._selections.set(e.userId,e.selection),this.emit("selection",{userId:e.userId,selection:e.selection});break;case"emoji:drop":this.emit("emoji-drop",{userId:e.userId,emoji:e.emoji,position:e.position});break;case"draw:stroke":this.emit("draw-stroke",{userId:e.userId,points:e.points,color:e.color,width:e.width});break;case"draw:clear":this.emit("draw-clear",{userId:e.userId});break;case"activity:logged":this._activityLogs.unshift(e.activityLog),this._activityLogs.length>100&&(this._activityLogs=this._activityLogs.slice(0,100)),this.emit("activity-logs",this._activityLogs);break;case"auth:error":this.emit("auth:error",e);break;case"error":this.emit("error",e);break}}reset(){this._user=null,this._config={...c.DEFAULT_ENV_CONFIG},this._p2pConfig=null,this._users.clear(),this._presence.clear(),this._threads.clear(),this._reactions.clear(),this._notifications=[],this._activityLogs=[],this._typing.clear(),this._viewports.clear(),this._selections.clear()}}class m extends d{constructor(e){var i;super();r(this,"state");r(this,"connection");r(this,"config");r(this,"heartbeatTimer",null);r(this,"lastCursorSend",0);r(this,"pendingCursor",null);r(this,"cursorTimer",null);r(this,"_p2p",null);r(this,"_p2pInstance",null);r(this,"p2pEnabled");r(this,"iceServers");this.config=e,this.state=new u,this.state.baseUrl=(e.endpoint??"").replace(/^ws(s?):/,"http$1:").replace(/\/$/,"");const t=((i=e.endpoint)==null?void 0:i.replace(/^http/,"ws"))??void 0;this.connection=new p(t),this.p2pEnabled=e.p2p??!1,this.iceServers=e.iceServers??[{urls:"stun:stun.l.google.com:19302"}],this.connection.on("message",s=>{this.state.handleMessage(s),this.emit(s.type,s),s.type==="auth:error"&&this.connection.permanentDisconnect(),this._p2pInstance&&(s.type==="signal:offer"?this._p2pInstance.handleSignalOffer(s.fromUserId,s.sdp):s.type==="signal:answer"?this._p2pInstance.handleSignalAnswer(s.fromUserId,s.sdp):s.type==="signal:ice"?this._p2pInstance.handleIceCandidate(s.fromUserId,s.candidate):s.type==="p2p:sync"?this._p2pInstance.handleP2PSync(s.fromUserId,s.update):s.type==="presence:join"?this._p2pInstance.onPeerJoined(s.user.user.id):s.type==="presence:leave"&&this._p2pInstance.onPeerLeft(s.userId))}),this.connection.on("state",s=>{this.emit("connection",s),s==="connected"?(this.authenticate(),this.startHeartbeat()):s==="disconnected"&&this.stopHeartbeat()})}get connectionState(){return this.connection.state}connect(){this.connection.connect()}disconnect(){this.stopHeartbeat(),this.cursorTimer&&(clearTimeout(this.cursorTimer),this.cursorTimer=null),this.connection.disconnect(),this.state.reset()}destroy(){var e;(e=this._p2pInstance)==null||e.destroy(),this._p2pInstance=null,this._p2p=null,this.disconnect(),this.removeAll(),this.state.removeAll(),this.connection.removeAll()}authenticate(){this.send({type:"auth",apiKey:this.config.apiKey,token:this.config.token,room:this.config.room})}send(e){this.connection.send(e)}moveCursor(e){const t=Date.now();this.pendingCursor=e,t-this.lastCursorSend>=c.CURSOR_THROTTLE_MS?this.flushCursor():this.cursorTimer||(this.cursorTimer=setTimeout(()=>{this.cursorTimer=null,this.flushCursor()},c.CURSOR_THROTTLE_MS))}flushCursor(){this.pendingCursor&&(this.send({type:"cursor:move",position:this.pendingCursor}),this.lastCursorSend=Date.now(),this.pendingCursor=null)}updatePresence(e){this.send({type:"presence:update",status:e})}startHeartbeat(){this.heartbeatTimer||(this.heartbeatTimer=setInterval(()=>{this.send({type:"presence:update",status:"online"})},c.PRESENCE_HEARTBEAT_MS))}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}createThread(e,t={}){const i=crypto.randomUUID();return this.send({type:"thread:create",id:i,body:e,mentions:t.mentions??[],position:t.position??null,attachmentIds:t.attachmentIds}),i}reply(e,t,i=[],s){const n=crypto.randomUUID();return this.send({type:"comment:create",threadId:e,id:n,body:t,mentions:i,attachmentIds:s}),n}editComment(e,t,i=[]){this.send({type:"comment:edit",commentId:e,body:t,mentions:i})}deleteComment(e){this.state.removeComment(e),this.send({type:"comment:delete",commentId:e})}resolveThread(e,t=!0){this.send({type:"thread:resolve",threadId:e,resolved:t})}addReaction(e,t,i){this.send({type:"reaction:add",targetId:e,targetType:t,emoji:i})}removeReaction(e){this.send({type:"reaction:remove",reactionId:e})}markRead(e){this.state.markNotificationRead(e),this.send({type:"notification:read",notificationId:e})}markAllRead(){this.state.markAllNotificationsRead(),this.send({type:"notification:read-all"})}performClick(e){this.send({type:"click:perform",position:e})}sendTyping(e){this.send({type:"typing:start",threadId:e})}updateViewport(e){this.send({type:"viewport:update",...e})}updateSelection(e){this.send({type:"selection:update",selection:e})}dropEmoji(e,t){this.send({type:"emoji:drop",emoji:e,position:t})}drawStroke(e,t,i){this.send({type:"draw:stroke",points:e,color:t,width:i})}clearDrawing(){this.send({type:"draw:clear"})}async uploadFile(e){const t=typeof window<"u"?window.location.origin:"",i=(this.config.endpoint??t).replace(/^ws(s?):/,"http$1:"),s=new FormData;s.append("file",e);const n=await fetch(`${i}/api/v1/upload`,{method:"POST",headers:{"X-Pulse-Key":this.config.apiKey,"X-Pulse-Token":this.config.token},body:s});if(!n.ok){const l=await n.json().catch(()=>({error:"Upload failed"}));throw new Error(l.error??"Upload failed")}const o=await n.json();return o.url&&!o.url.startsWith("http")&&(o.url=`${i}${o.url}`),o.thumbnailUrl&&!o.thumbnailUrl.startsWith("http")&&(o.thumbnailUrl=`${i}${o.thumbnailUrl}`),o}setAppearOffline(e){e?(this.stopHeartbeat(),this.send({type:"presence:update",status:"idle"})):(this.startHeartbeat(),this.send({type:"presence:update",status:"online"}))}get p2p(){return this.p2pEnabled?(this._p2p||(this._p2p=this.initP2P()),this._p2p):Promise.reject(new Error("P2P is not enabled. Pass { p2p: true } in PulseConfig."))}async initP2P(){const e=await new Promise((n,o)=>{if(this.state.user&&this.state.p2pConfig){n({userId:this.state.user.id,p2pConfig:this.state.p2pConfig});return}const l=this.state.on("auth",()=>{if(l(),!this.state.p2pConfig){o(new Error("P2P is not enabled for this environment."));return}n({userId:this.state.user.id,p2pConfig:this.state.p2pConfig})})});let t=this.iceServers;try{const n=await fetch(`${this.state.baseUrl}/api/v1/turn-credentials`,{headers:{Authorization:`Bearer ${this.config.token}`}});n.ok&&(t=(await n.json()).iceServers)}catch{}const{P2PManager:i}=await Promise.resolve().then(()=>require("./P2PManager-e4ShL60A.cjs")),s=new i({roomId:this.config.room,userId:e.userId,baseUrl:this.state.baseUrl,authToken:this.config.token,p2pConfig:e.p2pConfig,iceServers:t,sendWS:n=>this.send(n)});this._p2pInstance=s,await s.initialize();for(const n of this.state.presence)n.user.id!==e.userId&&s.onPeerJoined(n.user.id);return s}}exports.Connection=p;exports.Emitter=d;exports.PulseClient=m;exports.StateManager=u;
@@ -1,8 +1,8 @@
1
1
  var u = Object.defineProperty;
2
2
  var f = (h, a, e) => a in h ? u(h, a, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[a] = e;
3
3
  var r = (h, a, e) => f(h, typeof a != "symbol" ? a + "" : a, e);
4
- import { DEFAULT_ENDPOINT as _, RECONNECT_BASE_DELAY_MS as m, RECONNECT_MAX_DELAY_MS as w, DEFAULT_ENV_CONFIG as d, CURSOR_THROTTLE_MS as l, PRESENCE_HEARTBEAT_MS as y } from "@gamention/pulse-shared";
5
- class p {
4
+ import { DEFAULT_ENDPOINT as _, RECONNECT_BASE_DELAY_MS as m, RECONNECT_MAX_DELAY_MS as v, DEFAULT_ENV_CONFIG as d, CURSOR_THROTTLE_MS as p, PRESENCE_HEARTBEAT_MS as w } from "@gamention/pulse-shared";
5
+ class l {
6
6
  constructor() {
7
7
  r(this, "handlers", /* @__PURE__ */ new Map());
8
8
  }
@@ -23,7 +23,7 @@ class p {
23
23
  this.handlers.clear();
24
24
  }
25
25
  }
26
- class v extends p {
26
+ class y extends l {
27
27
  constructor(e) {
28
28
  super();
29
29
  r(this, "ws", null);
@@ -32,30 +32,34 @@ class v extends p {
32
32
  r(this, "reconnectTimer", null);
33
33
  r(this, "_state", "disconnected");
34
34
  r(this, "permanentlyClosed", !1);
35
- this.endpoint = e ?? _;
36
- }
37
- get state() {
38
- return this._state;
39
- }
40
- connect() {
41
- this.ws || this.permanentlyClosed || (this._state = "connecting", this.emit("state", this._state), this.ws = new WebSocket(this.endpoint), this.ws.addEventListener("open", () => {
35
+ // Stored handlers for proper cleanup
36
+ r(this, "handleOpen", () => {
42
37
  this._state = "connected", this.reconnectAttempt = 0, this.emit("state", this._state);
43
- }), this.ws.addEventListener("message", (e) => {
38
+ });
39
+ r(this, "handleMessage", (e) => {
44
40
  try {
45
41
  const t = JSON.parse(e.data);
46
42
  this.emit("message", t);
47
43
  } catch {
48
44
  }
49
- }), this.ws.addEventListener("close", () => {
50
- this.ws = null, this._state = "disconnected", this.emit("state", this._state), this.scheduleReconnect();
51
- }), this.ws.addEventListener("error", () => {
45
+ });
46
+ r(this, "handleClose", () => {
47
+ this.cleanupWs(), this._state = "disconnected", this.emit("state", this._state), this.scheduleReconnect();
48
+ });
49
+ r(this, "handleError", () => {
52
50
  var e;
53
51
  (e = this.ws) == null || e.close();
54
- }));
52
+ });
53
+ this.endpoint = e ?? _;
54
+ }
55
+ get state() {
56
+ return this._state;
57
+ }
58
+ connect() {
59
+ this.ws || this.permanentlyClosed || (this._state = "connecting", this.emit("state", this._state), this.ws = new WebSocket(this.endpoint), this.ws.addEventListener("open", this.handleOpen), this.ws.addEventListener("message", this.handleMessage), this.ws.addEventListener("close", this.handleClose), this.ws.addEventListener("error", this.handleError));
55
60
  }
56
61
  disconnect() {
57
- var e;
58
- this.reconnectTimer && (clearTimeout(this.reconnectTimer), this.reconnectTimer = null), this.reconnectAttempt = 0, (e = this.ws) == null || e.close(), this.ws = null, this._state = "disconnected", this.emit("state", this._state);
62
+ this.reconnectTimer && (clearTimeout(this.reconnectTimer), this.reconnectTimer = null), this.reconnectAttempt = 0, this.cleanupWs(), this._state = "disconnected", this.emit("state", this._state);
59
63
  }
60
64
  send(e) {
61
65
  var t;
@@ -64,18 +68,21 @@ class v extends p {
64
68
  permanentDisconnect() {
65
69
  this.permanentlyClosed = !0, this.disconnect();
66
70
  }
71
+ cleanupWs() {
72
+ this.ws && (this.ws.removeEventListener("open", this.handleOpen), this.ws.removeEventListener("message", this.handleMessage), this.ws.removeEventListener("close", this.handleClose), this.ws.removeEventListener("error", this.handleError), this.ws.close(), this.ws = null);
73
+ }
67
74
  scheduleReconnect() {
68
75
  if (this.permanentlyClosed) return;
69
76
  const e = Math.min(
70
77
  m * 2 ** this.reconnectAttempt,
71
- w
78
+ v
72
79
  );
73
80
  this.reconnectAttempt++, this.reconnectTimer = setTimeout(() => {
74
81
  this.reconnectTimer = null, this.connect();
75
82
  }, e);
76
83
  }
77
84
  }
78
- class I extends p {
85
+ class I extends l {
79
86
  constructor() {
80
87
  super(...arguments);
81
88
  /** HTTP base URL for resolving relative attachment paths (e.g. "http://localhost:4000"). */
@@ -325,7 +332,7 @@ class I extends p {
325
332
  this._user = null, this._config = { ...d }, this._p2pConfig = null, this._users.clear(), this._presence.clear(), this._threads.clear(), this._reactions.clear(), this._notifications = [], this._activityLogs = [], this._typing.clear(), this._viewports.clear(), this._selections.clear();
326
333
  }
327
334
  }
328
- class k extends p {
335
+ class g extends l {
329
336
  constructor(e) {
330
337
  var i;
331
338
  super();
@@ -342,7 +349,7 @@ class k extends p {
342
349
  r(this, "iceServers");
343
350
  this.config = e, this.state = new I(), this.state.baseUrl = (e.endpoint ?? "").replace(/^ws(s?):/, "http$1:").replace(/\/$/, "");
344
351
  const t = ((i = e.endpoint) == null ? void 0 : i.replace(/^http/, "ws")) ?? void 0;
345
- this.connection = new v(t), this.p2pEnabled = e.p2p ?? !1, this.iceServers = e.iceServers ?? [{ urls: "stun:stun.l.google.com:19302" }], this.connection.on("message", (s) => {
352
+ this.connection = new y(t), this.p2pEnabled = e.p2p ?? !1, this.iceServers = e.iceServers ?? [{ urls: "stun:stun.l.google.com:19302" }], this.connection.on("message", (s) => {
346
353
  this.state.handleMessage(s), this.emit(s.type, s), s.type === "auth:error" && this.connection.permanentDisconnect(), this._p2pInstance && (s.type === "signal:offer" ? this._p2pInstance.handleSignalOffer(s.fromUserId, s.sdp) : s.type === "signal:answer" ? this._p2pInstance.handleSignalAnswer(s.fromUserId, s.sdp) : s.type === "signal:ice" ? this._p2pInstance.handleIceCandidate(s.fromUserId, s.candidate) : s.type === "p2p:sync" ? this._p2pInstance.handleP2PSync(s.fromUserId, s.update) : s.type === "presence:join" ? this._p2pInstance.onPeerJoined(s.user.user.id) : s.type === "presence:leave" && this._p2pInstance.onPeerLeft(s.userId));
347
354
  }), this.connection.on("state", (s) => {
348
355
  this.emit("connection", s), s === "connected" ? (this.authenticate(), this.startHeartbeat()) : s === "disconnected" && this.stopHeartbeat();
@@ -376,9 +383,9 @@ class k extends p {
376
383
  // ── Cursors ──
377
384
  moveCursor(e) {
378
385
  const t = Date.now();
379
- this.pendingCursor = e, t - this.lastCursorSend >= l ? this.flushCursor() : this.cursorTimer || (this.cursorTimer = setTimeout(() => {
386
+ this.pendingCursor = e, t - this.lastCursorSend >= p ? this.flushCursor() : this.cursorTimer || (this.cursorTimer = setTimeout(() => {
380
387
  this.cursorTimer = null, this.flushCursor();
381
- }, l));
388
+ }, p));
382
389
  }
383
390
  flushCursor() {
384
391
  this.pendingCursor && (this.send({ type: "cursor:move", position: this.pendingCursor }), this.lastCursorSend = Date.now(), this.pendingCursor = null);
@@ -390,7 +397,7 @@ class k extends p {
390
397
  startHeartbeat() {
391
398
  this.heartbeatTimer || (this.heartbeatTimer = setInterval(() => {
392
399
  this.send({ type: "presence:update", status: "online" });
393
- }, y));
400
+ }, w));
394
401
  }
395
402
  stopHeartbeat() {
396
403
  this.heartbeatTimer && (clearInterval(this.heartbeatTimer), this.heartbeatTimer = null);
@@ -535,8 +542,8 @@ class k extends p {
535
542
  }
536
543
  }
537
544
  export {
538
- v as Connection,
539
- p as Emitter,
540
- k as PulseClient,
545
+ y as Connection,
546
+ l as Emitter,
547
+ g as PulseClient,
541
548
  I as StateManager
542
549
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gamention/pulse-core",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Core client SDK for Pulse — WebSocket connection, state management, and API for real-time collaboration",
5
5
  "type": "module",
6
6
  "main": "./dist/pulse-core.cjs",