@wuisdk/plugin-wss 0.1.3 → 0.1.5

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
@@ -16,6 +16,7 @@ interface WssPluginOptions {
16
16
  endpoint?: string;
17
17
  }
18
18
  interface IWssService {
19
+ readonly wsuuid: string;
19
20
  post(packet: WSPacket): void;
20
21
  subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
21
22
  }
@@ -41,6 +42,11 @@ interface WSTimePacket {
41
42
  time: string;
42
43
  unix: number;
43
44
  }
45
+ interface WSNotifyPacket {
46
+ title?: string;
47
+ icon?: string;
48
+ body: string;
49
+ }
44
50
  //#endregion
45
51
  //#region src/service.d.ts
46
52
  export declare class WuiWssPlugin implements WuiPlugin {
@@ -59,4 +65,4 @@ export declare const info: () => {
59
65
  name: string;
60
66
  };
61
67
  //#endregion
62
- export type { IWssService, WSBrdPacket, WSPacket, WSParams, WSTimePacket, WSTopic, WssPluginOptions };
68
+ export type { IWssService, WSBrdPacket, WSNotifyPacket, WSPacket, WSParams, WSTimePacket, WSTopic, WssPluginOptions };
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();break;case`BRDMSG`:this.handleBroadcast(n);break;case`VERIFY`:console.log(`🐸`,t,n)}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.verifyToken()}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`)}handleBroadcast(e){let t=e.to===this.wsuuid||e.to==`@all`;e.cmd===`LOGS`&&console.log(e.msg),e.cmd===`CONRST`&&t&&(sessionStorage.clear(),window.location.reload()),e.cmd===`LOGOUT`&&t&&(localStorage.clear(),sessionStorage.clear(),window.location.reload()),e.cmd===`RESULT`&&t&&(console.log(e.cmd,`From:`,e.from),console.log(e.msg))}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTH_TOKEN);t&&this.post({head:`VERIFY`,data:t})}};const n=()=>({version:`0.1.3`,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();break;case`BRDMSG`:this.handleBroadcast(n);break;case`VERIFY`:console.log(`🐸`,t,n)}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}get wsuuid(){return this._wsuuid??``}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.verifyToken()}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`)}handleBroadcast(e){let t=e.to===this._wsuuid||e.to==`@all`;e.cmd===`LOGS`&&console.log(e.msg),e.cmd===`CONRST`&&t&&(sessionStorage.clear(),window.location.reload()),e.cmd===`LOGOUT`&&t&&(localStorage.clear(),sessionStorage.clear(),window.location.reload()),e.cmd===`RESULT`&&t&&(console.log(e.cmd,`From:`,e.from),console.log(e.msg)),e.cmd===`NOTIFY`&&t&&(this.handleNotify(e.msg),console.log(e.cmd,e.msg))}handleNotify(e){let t=e.title??`Hub Notify`,n=e.icon??`https://cdn.jsdelivr.net/gh/wkrdev/assets/img/icon/icon-06.png`,r=e.body??``;Notification.requestPermission().then(e=>{e==`granted`&&new Notification(t,{body:r,icon:n})}).catch(e=>console.error(e))}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTH_TOKEN);t&&this.post({head:`VERIFY`,data:t})}};const n=()=>({version:`0.1.5`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@wuisdk/plugin-wss",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
7
7
  "import": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts"
9
9
  },
10
- "./hub.worker": {
11
- "import": "./dist/hub.worker.js",
12
- "types": "./dist/hub.worker.d.ts"
10
+ "./wss.worker": {
11
+ "import": "./dist/wss.worker.js",
12
+ "types": "./dist/wss.worker.d.ts"
13
13
  }
14
14
  },
15
15
  "publishConfig": {
File without changes
File without changes