@veltdev/sdk
Version:
Velt is an SDK to add collaborative features to your product within minutes. Example: Comments like Figma, Frame.io, Google docs or sheets, Recording like Loom, Huddles like Slack and much more.
14 lines (13 loc) • 9.35 kB
JavaScript
import{b as Y}from"./chunk-3J52NZMN.js";import{$ as j,C as A,Cf as D,E as g,Gi as _,Hi as x,Ii as Q,Jd as w,L as S,Ld as y,M as B,Md as U,Nd as O,S as F,T as H,W as E,ca as b,di as W,ei as L,g as $,jh as P,l as C,li as R,mi as M,p as k,r as u,ti as N,v as m,w as z,yi as K}from"./chunk-GR3NESUB.js";var he=(()=>{let p=class p{constructor(e,t,r,a,s,c,n,i,d,o){this.authService=e,this.commonDbService=t,this.databaseService=r,this.docService=a,this.domService=s,this.loggingService=c,this.metadataService=n,this.commonDbUtils=i,this.configService=d,this.realtimeAdapter=o,this.HEARTBEAT_DURATION=5*1e3,this.heartbeatEnabled$=new C(!1),this.subscriptions=[],this.heartbeatCreatedTimestamps=new Map,this.initializedHeartbeatIds=new Set;try{this.loggingService.log("%c[WB] Creating CLASS: HeartbeatService","color: blue;"),this.heartbeatEnabled$.pipe(S()).subscribe(h=>{h?this.setupHeartbeat():this.teardownHeartbeat()}),this.docService.addPreDocumentIdChangeFunction("heartbeat-service",h=>h?.documentId?this.removeDocumentHeartbeat(h):Promise.resolve())}catch(h){this.loggingService.catch("Error in HeartbeatService constructor: ",h)}}setupWebWorker(e){try{let t=`
let intervalId;
self.addEventListener('message', (event) => {
if (event.data === 'start') {
if (intervalId) clearInterval(intervalId);
intervalId = setInterval(() => {
self.postMessage('tick');
}, ${e});
} else if (event.data === 'stop') {
if (intervalId) clearInterval(intervalId);
}
});
`;if(typeof Worker<"u"){let r=new Blob([t],{type:"application/javascript"});return new Worker(URL.createObjectURL(r))}return}catch(t){this.loggingService.catch("Error in HeartbeatService setupWebWorker: ",t);return}}setupHeartbeat(){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: setupHeartbeat","color: green;"),this.worker=this.setupWebWorker(this.HEARTBEAT_DURATION),this.worker){this.worker.onmessage=()=>{this.authService.getUser()&&this.sendHeartbeat()};let r=this.authService.getUser$().subscribe(a=>{a?(this.worker?.postMessage("start"),this.sendHeartbeat()):this.worker?.postMessage("stop")});this.subscriptions.push(r)}else{let r=this.authService.getUser$().pipe(H(a=>a?A(this.HEARTBEAT_DURATION).pipe(F(0)):k)).subscribe(()=>this.sendHeartbeat());this.subscriptions.push(r)}let e=this.docService.getDocumentPaths$().pipe(g(r=>!!r),E(()=>this.sendHeartbeat())).subscribe();this.subscriptions.push(e);let t=z([this.databaseService.getDb().pipe(g(r=>!!r)),this.authService.getUser$(),this.docService.getDocumentPaths$()]).pipe(B(),E(([[r,a,s],[c,n,i]])=>{if(n&&i){let d=this.domService.getTabId();if(d){let o=this.generateHeartbeatId(n.userId,d),h={id:o,organizationId:i.organizationId},I=this.commonDbUtils.getDocumentPath({feature:"heartbeat",properties:h,defaultDocumentPaths:i,apiKey:this.configService.getApiKey(),useFirestore:!1});if(I&&c&&this.realtimeAdapter.rtdbOnDisconnectSet("heartbeat",this.realtimeAdapter.rtdbRef(c,I),null),i.documentId){let l={id:o,documentId:i.documentId,organizationId:i.organizationId},v=this.commonDbUtils.getDocumentPath({feature:"heartbeatByDocument",properties:l,defaultDocumentPaths:i,apiKey:this.configService.getApiKey(),useFirestore:!1});v&&c&&this.realtimeAdapter.rtdbOnDisconnectSet("heartbeat",this.realtimeAdapter.rtdbRef(c,v),null)}if(s&&s.documentId&&s.documentId!==i.documentId){let l={id:o,documentId:s.documentId,organizationId:s.organizationId,skipFirestore:!0};this.commonDbService.deleteData({feature:"heartbeatByDocument",properties:l})}}}})).subscribe();this.subscriptions.push(t)}catch(e){this.loggingService.catch("Error in HeartbeatService setupHeartbeat: ",e)}}teardownHeartbeat(){try{this.loggingService.log("%c[WB] Calling FUNCTION: teardownHeartbeat","color: green;"),this.worker&&(this.worker.postMessage("stop"),this.worker.terminate(),this.worker=void 0),this.subscriptions.forEach(e=>e.unsubscribe()),this.subscriptions=[],this.heartbeatCreatedTimestamps.clear(),this.initializedHeartbeatIds.clear()}catch(e){this.loggingService.catch("Error in HeartbeatService teardownHeartbeat: ",e)}}sendHeartbeat(){try{this.loggingService.log("%c[WB] Calling HeartbeatService FUNCTION: sendHeartbeat","color: green;");let e=this.authService.getUser(),t=this.docService.getDocumentPaths(),r=this.domService.getTabId();if(e&&t&&r){let a=Date.now(),s=this.generateHeartbeatId(e.userId,r),c=this.docService.getBaseMetadata("heartbeat"),n=c.documentId,i=t.organizationId,d=`${s}_${i}`,o=n?`${s}_${i}_${n}`:null,h=this.heartbeatCreatedTimestamps.get(d)??a;this.heartbeatCreatedTimestamps.has(d)||this.heartbeatCreatedTimestamps.set(d,h);let I=!this.initializedHeartbeatIds.has(d),l={id:s,createdTimestamp:h,lastUpdatedTimestamp:a,userId:e.userId,veltUserId:D(e.userId),tabId:r,metadata:c};if(I?(this.commonDbService.setData({feature:"heartbeat",properties:{id:l.id,skipFirestore:!0},data:l}),this.initializedHeartbeatIds.add(d)):this.commonDbService.updateData({feature:"heartbeat",properties:{id:l.id,skipFirestore:!0},data:l}),n&&o){let v=this.heartbeatCreatedTimestamps.get(o)??a;this.heartbeatCreatedTimestamps.has(o)||this.heartbeatCreatedTimestamps.set(o,v);let G=!this.initializedHeartbeatIds.has(o),f={id:s,createdTimestamp:v,lastUpdatedTimestamp:a,userId:e.userId,veltUserId:D(e.userId),tabId:r,metadata:c};G?(this.commonDbService.setData({feature:"heartbeatByDocument",properties:{id:f.id,skipFirestore:!0,documentId:n},data:f}),this.initializedHeartbeatIds.add(o)):this.commonDbService.updateData({feature:"heartbeatByDocument",properties:{id:f.id,skipFirestore:!0,documentId:n},data:f})}}}catch(e){this.loggingService.catch("Error in HeartbeatService sendHeartbeat: ",e)}}generateHeartbeatId(e,t){return D(e+"_"+t)}getHeartbeatData(e){try{let t=this.docService.getDocumentPaths(),r=e?.organizationId||t?.organizationId,a=e?.documentId,s,c={organizationId:r,skipFirestore:!0,useQuery:!0};if(a?(s="allHeartbeatsByDocument",c.documentId=a):s="allHeartbeats",!t)return u(null);if(e?.userId){let n=e?.limit??50;return c.rtdbQuery=[U("userId"),w(e.userId),y(n)],this.commonDbService.dbListener({feature:s,properties:c}).pipe(g(i=>!!i?.data),m(i=>i?.data),m(i=>i?Object.values(i??{}).map(d=>d):null),m(i=>{if(i){let d=e?.lastUpdatedTimestamp?.$gte??Date.now()-15e3;return i.filter(o=>!(d&&o.lastUpdatedTimestamp<d))}return i}))}else{let n=e?.orderBy??"lastUpdatedTimestamp",i=e?.lastUpdatedTimestamp?.$gte??Date.now()-15e3,d=e?.limit??50;return c.rtdbQuery=[U(n),O(i),y(d)],this.commonDbService.dbListener({feature:s,properties:c}).pipe(g(o=>!!o?.data),m(o=>o?.data),m(o=>o?Object.values(o??{}).map(h=>h):null))}}catch(t){return this.loggingService.catch("Error in HeartbeatService getHeartbeatData: ",t),u(null)}}getSpecificHeartbeat(e){try{let t=this.generateHeartbeatId(e.userId,e.tabId),a=this.docService.getDocumentPaths()?.organizationId;if(!a)return u(null);let s,c={id:t,organizationId:a,skipFirestore:!0};return e.documentId?(s="heartbeatByDocument",c.documentId=e.documentId):s="heartbeat",this.commonDbService.dbListener({feature:s,properties:c}).pipe(g(n=>!!n?.data),m(n=>n?.data),S((n,i)=>n?.lastUpdatedTimestamp===i?.lastUpdatedTimestamp))}catch(t){return this.loggingService.catch("Error in HeartbeatService getSpecificHeartbeat: ",t),u(null)}}removeDocumentHeartbeat(e){return $(this,null,function*(){try{let t=this.authService.getUser()?.userId,r=this.domService.getTabId();if(t&&r&&e?.documentId){let a=this.generateHeartbeatId(t,r),s=`${a}_${e.organizationId}_${e.documentId}`;this.heartbeatCreatedTimestamps.delete(s),this.initializedHeartbeatIds.delete(s),yield this.commonDbService.deleteData({feature:"heartbeatByDocument",properties:{id:a,documentId:e.documentId,organizationId:e.organizationId,skipFirestore:!0}})}return Promise.resolve()}catch(t){return this.loggingService.catch("Error in HeartbeatService removeDocumentHeartbeat: ",t),Promise.resolve()}})}getHeartbeat(e){try{return(e?.userId?u(e.userId):this.authService.getUser$().pipe(m(r=>r?.userId))).pipe(H(r=>r?this.docService.getDocumentPaths$().pipe(H(a=>a?this.getHeartbeatData({userId:r,documentId:a.documentId,organizationId:a.organizationId}):u(null))):u(null)),m(r=>({data:r})),S(P))}catch(t){return this.loggingService.catch("Error in HeartbeatService getHeartbeat: ",t),u({data:null})}}enableHeartbeat(){try{if(this.heartbeatEnabled$.value===!0)return;this.heartbeatEnabled$.next(!0)}catch(e){this.loggingService.catch("Error in HeartbeatService enableHeartbeat: ",e)}}disableHeartbeat(){try{if(this.heartbeatEnabled$.value===!1)return;this.heartbeatEnabled$.next(!1)}catch(e){this.loggingService.catch("Error in HeartbeatService disableHeartbeat: ",e)}}getHeartbeatEnabled$(){try{return this.heartbeatEnabled$.asObservable()}catch(e){return this.loggingService.catch("Error in HeartbeatService getHeartbeatEnabled$: ",e),u(!1)}}getHeartbeatEnabled(){try{return this.heartbeatEnabled$.value}catch(e){return this.loggingService.catch("Error in HeartbeatService getHeartbeatEnabled: ",e),!1}}ngOnDestroy(){try{this.teardownHeartbeat()}catch(e){this.loggingService.catch("Error in HeartbeatService ngOnDestroy: ",e)}}};p.\u0275fac=function(t){return new(t||p)(b(M),b(Q),b(N),b(K),b(R),b(W),b(Y),b(_),b(L),b(x))},p.\u0275prov=j({token:p,factory:p.\u0275fac,providedIn:"root"});let T=p;return T})();export{he as a};