@wuisdk/plugin-wss 0.1.4 → 0.1.6

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
@@ -42,13 +42,18 @@ interface WSTimePacket {
42
42
  time: string;
43
43
  unix: number;
44
44
  }
45
+ interface WSNotifyPacket {
46
+ title?: string;
47
+ icon?: string;
48
+ body: string;
49
+ }
45
50
  //#endregion
46
51
  //#region src/service.d.ts
47
52
  export declare class WuiWssPlugin implements WuiPlugin {
48
- private options?;
53
+ private options;
49
54
  readonly name = "wui-wss-plugin";
50
55
  private service;
51
- constructor(options?: WssPluginOptions | undefined);
56
+ constructor(options: WssPluginOptions);
52
57
  install(context: WuiContext): void;
53
58
  init(): void;
54
59
  destroy(): void;
@@ -60,4 +65,4 @@ export declare const info: () => {
60
65
  name: string;
61
66
  };
62
67
  //#endregion
63
- 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}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))}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.4`,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.6`,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.4",
3
+ "version": "0.1.6",
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