signalk-tides 2.2.0 → 2.3.0

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.
@@ -1 +0,0 @@
1
- import{d as e,f as t,l as n,n as r,o as i,t as a,u as o}from"./useUnitPreferences-lYEQOWom.js";var s=`plotterExt/1`,c={PARSE_ERROR:-32700,INVALID_REQUEST:-32600,METHOD_NOT_FOUND:-32601,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603,HOST_ERROR:-32e3,TIMEOUT:-32001,CONNECTION_CLOSED:-32002},l=class e extends Error{code;data;constructor(e,t={}){super(e),this.name=`RpcError`,this.code=t.code??c.HOST_ERROR;let n={...t.data??{}};t.reason!==void 0&&(n.reason=t.reason),this.data=Object.keys(n).length>0?n:void 0}get reason(){return typeof this.data?.reason==`string`?this.data.reason:void 0}toErrorObject(){return{code:this.code,message:this.message,...this.data?{data:this.data}:{}}}static fromErrorObject(t){return new e(t.message,{code:t.code,data:t.data})}static from(t){return t instanceof e?t:t instanceof Error?new e(t.message,{code:c.INTERNAL_ERROR}):new e(String(t),{code:c.INTERNAL_ERROR})}},u=`bus.ready`,d=`bus.handshake`;function f(e,t){return e===t||p(e.split(`.`),0,t.split(`.`),0)}function p(e,t,n,r){for(;t<e.length;){let i=e[t];if(i===`**`){if(t===e.length-1)return!0;for(let i=r;i<=n.length;i++)if(p(e,t+1,n,i))return!0;return!1}if(r>=n.length||i!==`*`&&i!==n[r])return!1;t++,r++}return r===n.length}function m(e,t){for(let n of e)if(f(n,t))return!0;return!1}function h(e){return{bus:s,msg:e}}function g(e){if(typeof e!=`object`||!e)return null;let t=e;return t.bus===`plotterExt/1`&&_(t.msg)?t.msg:null}function _(e){if(typeof e!=`object`||!e)return!1;let t=e;if(t.jsonrpc!==`2.0`)return!1;if(typeof t.method==`string`)return t.id===void 0||typeof t.id==`string`||typeof t.id==`number`;if(typeof t.id!=`string`&&typeof t.id!=`number`&&t.id!==null)return!1;let n=`result`in t,r=t.error,i=typeof r==`object`&&!!r&&typeof r.code==`number`&&typeof r.message==`string`;return n?!(`error`in t):i}function v(e){return`method`in e&&`id`in e&&e.id!==void 0}function y(e){return`method`in e&&(!(`id`in e)||e.id===void 0)}function b(e){return!(`method`in e)}function x(e,t={}){let n=t.listenWindow??globalThis,r=t.origin??n.location?.origin??`*`;return{post(t){e.postMessage(t,r)},listen(t){let i=n=>{n.source===e&&(r===`*`||n.origin===r)&&t(n.data)};return n.addEventListener(`message`,i),()=>n.removeEventListener(`message`,i)}}}var S=1e4,C=class{callTimeoutMs;port;unlisten;onError;pending=new Map;methods=new Map;eventHandlers=new Set;idPrefix=Math.random().toString(36).slice(2,8);seq=0;closed=!1;constructor(e){this.port=e.port,this.callTimeoutMs=e.callTimeoutMs??S,this.onError=e.onError??(e=>console.warn(`[plotterext-bus]`,e)),this.unlisten=this.port.listen(e=>this.onData(e))}registerMethod(e,t){this.methods.set(e,t)}unregisterMethod(e){this.methods.delete(e)}onEvent(e,t){let n={patterns:e,fn:t};return this.eventHandlers.add(n),()=>this.eventHandlers.delete(n)}notify(e,t){this.send({jsonrpc:`2.0`,method:e,...t===void 0?{}:{params:t}})}call(e,t,n={}){if(this.closed)return Promise.reject(new l(`Bus endpoint is closed`,{code:c.CONNECTION_CLOSED,reason:`CLOSED`}));let r=`${this.idPrefix}-${++this.seq}`,i=n.timeoutMs??this.callTimeoutMs;return new Promise((n,a)=>{let o=i>0?setTimeout(()=>{this.pending.delete(r),a(new l(`Call timed out after ${i}ms: ${e}`,{code:c.TIMEOUT,reason:`TIMEOUT`}))},i):null;this.pending.set(r,{resolve:n,reject:a,timer:o}),this.send({jsonrpc:`2.0`,id:r,method:e,...t===void 0?{}:{params:t}})})}close(){if(!this.closed){this.closed=!0,this.unlisten();for(let[,e]of this.pending)e.timer&&clearTimeout(e.timer),e.reject(new l(`Bus endpoint closed`,{code:c.CONNECTION_CLOSED,reason:`CLOSED`}));this.pending.clear(),this.eventHandlers.clear()}}send(e){this.closed||this.port.post(h(e))}onData(e){let t=g(e);t&&(b(t)?this.onResponse(t):v(t)?this.onRequest(t):y(t)&&this.onNotification(t.method,t.params))}onResponse(e){if(e.id===null)return;let t=this.pending.get(e.id);t&&(this.pending.delete(e.id),t.timer&&clearTimeout(t.timer),`error`in e?t.reject(l.fromErrorObject(e.error)):t.resolve(e.result))}async onRequest(e){let t=this.methods.get(e.method);if(!t){this.send({jsonrpc:`2.0`,id:e.id,error:{code:c.METHOD_NOT_FOUND,message:`Method not found: ${e.method}`}});return}try{let n=await t(e.params,{endpoint:this});this.send({jsonrpc:`2.0`,id:e.id,result:n===void 0?null:n})}catch(t){this.send({jsonrpc:`2.0`,id:e.id,error:l.from(t).toErrorObject()})}}onNotification(e,t){for(let n of[...this.eventHandlers])if(m(n.patterns,e))try{n.fn(e,t)}catch(e){this.onError(e)}}},w=class{handshake;endpoint;constructor(e,t){this.endpoint=e,this.handshake=t}get context(){return this.handshake.context}get apiVersion(){return this.handshake.apiVersion}get capabilities(){return this.handshake.capabilities}hasCapability(e){return this.handshake.capabilities.includes(e)}call(e,t,n){return this.endpoint.call(e,t,n)}notify(e,t){this.endpoint.notify(e,t)}async subscribe(e,t){let n=this.endpoint.onEvent(e,t),r;try{r=(await this.call(`events.subscribe`,{patterns:e})).subscriptionId}catch(e){throw n(),e}return async()=>{n(),await this.call(`events.unsubscribe`,{subscriptionId:r}).catch(()=>{})}}state={get:async(e,t)=>(await this.call(`state.get`,{...t?{scope:t}:{},...e?{keys:e}:{}})).values??{},set:async(e,t)=>{await this.call(`state.set`,{...t?{scope:t}:{},values:e})}};signalk={subscribe:async(e,t)=>{let n=e.map(e=>`sk.${e}`),r=await this.subscribe(n,(e,n)=>t(n)),i;try{i=(await this.call(`signalk.subscribe`,{paths:e})).subscriptionId}catch(e){throw await r(),e}return async()=>{await r(),await this.call(`signalk.unsubscribe`,{subscriptionId:i}).catch(()=>{})}},put:(e,t)=>this.call(`signalk.put`,{path:e,value:t})};route={list:async()=>(await this.call(`route.list`)).routes??[],create:async e=>await this.call(`route.create`,e),get:async e=>await this.call(`route.get`,{routeId:e}),replace:async(e,t)=>await this.call(`route.replace`,{routeId:e,points:t}),save:async(e,t)=>await this.call(`route.save`,{routeId:e,...t??{}}),show:async e=>await this.call(`route.show`,{ref:e}),hide:async e=>{await this.call(`route.hide`,{routeId:e})},delete:async e=>{await this.call(`route.delete`,{routeId:e})}};chart={list:async()=>(await this.call(`chart.list`)).charts??[],setVisibility:async(e,t)=>{await this.call(`chart.setVisibility`,{ids:e,visible:t})},setOpacity:async(e,t)=>{await this.call(`chart.setOpacity`,{ids:e,opacity:t})},setOrder:async e=>{await this.call(`chart.setOrder`,{order:e})},setTime:async(e,t)=>{await this.call(`chart.setTime`,{ids:e,time:t})}};nightMode={get:async()=>await this.call(`nightMode.get`),set:async e=>{await this.call(`nightMode.set`,e)}};resourceGroup={apply:async e=>await this.call(`resourceGroup.apply`,{id:e})};map={getView:async()=>await this.call(`map.getView`),center:async(e,t)=>{await this.call(`map.center`,{position:e,...typeof t==`number`?{zoom:t}:{}})},fitBounds:async e=>{await this.call(`map.fitBounds`,{bounds:e})}};close(){this.endpoint.close()}};function T(e={}){let t=new C({port:e.port??x(globalThis.parent,{origin:`*`}),callTimeoutMs:e.callTimeoutMs,onError:e.onError}),n=e.id?{id:e.id}:void 0;return new Promise((r,i)=>{let a=!1,o=t.onEvent([d],(e,n)=>{a||(a=!0,p(),r(new w(t,n)))}),s=setInterval(()=>t.notify(u,n),e.readyIntervalMs??250),f=setTimeout(()=>{a||(a=!0,p(),t.close(),i(new l(`Timed out waiting for host handshake`,{code:c.TIMEOUT,reason:`HANDSHAKE_TIMEOUT`})))},e.timeoutMs??1e4),p=()=>{o(),clearInterval(s),clearTimeout(f)};t.notify(u,n)})}var E=o(),D=t(e(),1),O=n(),{VITE_SIGNALK_URL:k=window.location.toString()}={BASE_URL:`./`,DEV:!1,MODE:`production`,PROD:!0,SSR:!1},A=new URL(`/signalk/v2/api`,k).toString(),j=`tides`,M=5e3,N=36e5;function P(){let[e,t]=(0,D.useState)(0);return(0,D.useEffect)(()=>{let e=setInterval(()=>t(e=>e+1),N);return()=>clearInterval(e)},[]),e}var F=1500;function I(e){let t=(0,D.useRef)(!1);return(0,D.useEffect)(()=>{if(!e?.hasCapability(`widgets`))return;let n,r=()=>{t.current=!1,n=setTimeout(()=>{t.current=!0,e.call(`ui.openConfigPanel`).catch(()=>{})},F)},i=()=>clearTimeout(n),a=[`pointerup`,`pointercancel`,`pointerleave`];return window.addEventListener(`pointerdown`,r),a.forEach(e=>window.addEventListener(e,i)),()=>{i(),window.removeEventListener(`pointerdown`,r),a.forEach(e=>window.removeEventListener(e,i))}},[e]),(0,D.useCallback)(()=>t.current,[])}function L(){let[e,t]=(0,D.useState)(null);return(0,D.useEffect)(()=>{let e=!1;return T({timeoutMs:M}).then(n=>{e||t(n)}).catch(()=>{}),()=>{e=!0}},[]),e}function R(){let{units:e,ready:t}=a(),n=L(),o=I(n),s=P();if(!t)return null;let c=(0,O.jsx)(i,{id:`vessel/default`,fill:!0,className:`h-full`},s);return(0,O.jsx)(r,{baseUrl:A,units:e,children:n?.hasCapability(`ui`)&&n.hasCapability(`panels.iframe`)?(0,O.jsx)(`button`,{type:`button`,"aria-label":`Open Tides`,className:`block h-full w-full cursor-pointer text-left`,onClick:()=>{o()||n.call(`ui.togglePanel`,{panel:j}).catch(()=>{})},children:c}):c})}(0,E.createRoot)(document.getElementById(`widget`)).render((0,O.jsx)(R,{}));