@wuisdk/plugin-wss 0.1.1 → 0.1.2

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
@@ -24,6 +24,12 @@ interface WSPacket<T = unknown> {
24
24
  head: string;
25
25
  data?: T;
26
26
  }
27
+ interface WSBrdPacket<T = unknown> {
28
+ from: string;
29
+ to: string;
30
+ cmd: string;
31
+ msg: T;
32
+ }
27
33
  interface WSParams {
28
34
  url: string;
29
35
  app: string;
@@ -49,4 +55,4 @@ declare const info: () => {
49
55
  name: string;
50
56
  };
51
57
  //#endregion
52
- export { type IWssService, type WSPacket, type WSParams, type WSTopic, type WssPluginOptions, WuiWssPlugin, info };
58
+ export { type IWssService, type WSBrdPacket, type WSPacket, type WSParams, type WSTopic, type WssPluginOptions, WuiWssPlugin, info };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var e=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new t(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},t=class{ctx;options;abortController=new AbortController;worker=null;wsuuid=null;subscribers=new Map;constructor(e,t){this.ctx=e,this.options=t}init(){if(!window.Worker){console.warn(`[WssService] Web Workers not supported.`);return}if(!this.options?.worker){console.warn(`[WssService] Missing Worker configuration.`);return}let{config:e}=this.ctx,{signal:t}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{head:t,data:n}=e.data;switch(t){case`ACCEPT`:this.handleAccept(n);break;case`DISCON`:this.handleDisconnect()}let r=this.subscribers.get(t);r&&r.forEach(e=>e(n))},{signal:t});let n={head:`INIT`,data:{url:this.options.endpoint??`wss://wkrh.info:8855/ws`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(n)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,new Set),this.post({head:`SUBSCRIBE`,data:{topic:e}}));let r=this.subscribers.get(e),i=t;r.add(i);let a=!1,o=()=>{if(a)return;a=!0,n&&n.removeEventListener(`abort`,o);let t=this.subscribers.get(e);t&&(t.delete(i),t.size===0&&(this.subscribers.delete(e),this.post({head:`UNSUBSCRIBE`,data:{topic:e}})))};return n&&(n.aborted?o():n.addEventListener(`abort`,o,{once:!0})),o}handleAccept(e){this.wsuuid=e.uuid??``,console.log(`wsuuid:`,this.wsuuid),this.ctx.events.emit(`wss:connected`),this.ctx.ui.toast.success(`เชื่อมต่อ WssHub สำเร็จ.!`)}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`),this.ctx.ui.toast.error(`ขาดการเชื่อมต่อ WssHub.!`)}};const n=()=>({version:`0.1.1`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
1
+ var e=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new t(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},t=class{ctx;options;abortController=new AbortController;worker=null;wsuuid=null;subscribers=new Map;constructor(e,t){this.ctx=e,this.options=t}init(){if(!window.Worker){console.warn(`[WssService] Web Workers not supported.`);return}if(!this.options?.worker){console.warn(`[WssService] Missing Worker configuration.`);return}let{config:e}=this.ctx,{signal:t}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{head:t,data:n}=e.data;switch(t){case`ACCEPT`:this.handleAccept(n);break;case`DISCON`:this.handleDisconnect()}let r=this.subscribers.get(t);r&&r.forEach(e=>e(n))},{signal:t});let n={head:`INIT`,data:{url:this.options.endpoint??`wss://wkrh.info:8855/ws`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(n)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,new Set),this.post({head:`SUBSCRIBE`,data:{topic:e}}));let r=this.subscribers.get(e),i=t;r.add(i);let a=!1,o=()=>{if(a)return;a=!0,n&&n.removeEventListener(`abort`,o);let t=this.subscribers.get(e);t&&(t.delete(i),t.size===0&&(this.subscribers.delete(e),this.post({head:`UNSUBSCRIBE`,data:{topic:e}})))};return n&&(n.aborted?o():n.addEventListener(`abort`,o,{once:!0})),o}handleAccept(e){this.wsuuid=e.uuid??``,console.log(`wsuuid:`,this.wsuuid),this.ctx.events.emit(`wss:connected`),this.ctx.ui.toast.success(`เชื่อมต่อ WssHub สำเร็จ.!`)}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`),this.ctx.ui.toast.error(`ขาดการเชื่อมต่อ WssHub.!`)}};const n=()=>({version:`0.1.2`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuisdk/plugin-wss",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {