tauri-plugin-midi 0.2.0

A WebMIDI-compatible plugin for Tauri
1
"use strict";(()=>{function o(t,s,e,n){if(e==="a"&&!n)throw new TypeError("Private accessor was defined without a getter");if(typeof s=="function"?t!==s||!n:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return e==="m"?n:e==="a"?n.call(t):n?n.value:s.get(t)}function h(t,s,e,n,i){if(n==="m")throw new TypeError("Private method is not writable");if(n==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof s=="function"?t!==s||!i:!s.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return n==="a"?i.call(t,e):i?i.value=e:s.set(t,e),e}var p,u,_,E,U,M="__TAURI_TO_IPC_KEY__";function T(t,s=!1){return window.__TAURI_INTERNALS__.transformCallback(t,s)}var b=class{constructor(s){p.set(this,void 0),u.set(this,0),_.set(this,[]),E.set(this,void 0),h(this,p,s||(()=>{}),"f"),this.id=T(e=>{let n=e.index;if("end"in e){n==o(this,u,"f")?this.cleanupCallback():h(this,E,n,"f");return}let i=e.message;if(n==o(this,u,"f")){for(o(this,p,"f").call(this,i),h(this,u,o(this,u,"f")+1,"f");o(this,u,"f")in o(this,_,"f");){let a=o(this,_,"f")[o(this,u,"f")];o(this,p,"f").call(this,a),delete o(this,_,"f")[o(this,u,"f")],h(this,u,o(this,u,"f")+1,"f")}o(this,u,"f")===o(this,E,"f")&&this.cleanupCallback()}else o(this,_,"f")[n]=i})}cleanupCallback(){window.__TAURI_INTERNALS__.unregisterCallback(this.id)}set onmessage(s){h(this,p,s,"f")}get onmessage(){return o(this,p,"f")}[(p=new WeakMap,u=new WeakMap,_=new WeakMap,E=new WeakMap,M)](){return`__CHANNEL__:${this.id}`}toJSON(){return this[M]()}};async function d(t,s={},e){return window.__TAURI_INTERNALS__.invoke(t,s,e)}U=new WeakMap;var N;(function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WINDOW_SUSPENDED="tauri://suspended",t.WINDOW_RESUMED="tauri://resumed",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"})(N||(N={}));async function O(t,s){window.__TAURI_EVENT_PLUGIN_INTERNALS__.unregisterListener(t,s),await d("plugin:event|unlisten",{event:t,eventId:s})}async function D(t,s,e){var n;let i=typeof e?.target=="string"?{kind:"AnyLabel",label:e.target}:(n=e?.target)!==null&&n!==void 0?n:{kind:"Any"};return d("plugin:event|listen",{event:t,target:i,handler:T(s)}).then(a=>async()=>O(t,a))}async function L(t,s,e){return D(t,n=>{O(t,n.id),s(n)},e)}async function R(t,s){await d("plugin:event|emit",{event:t,payload:s})}var g={openInput:t=>d("plugin:midi|open_input",{id:t}),closeInput:t=>d("plugin:midi|close_input",{id:t}),openOutput:t=>d("plugin:midi|open_output",{id:t}),closeOutput:t=>d("plugin:midi|close_output",{id:t}),outputSend:(t,s,e)=>d("plugin:midi|output_send",{id:t,msg:s,timestamp:e})},A={midiMessage:C("plugin:midi:midi-message"),stateChange:C("plugin:midi:state-change")};function C(t,s,e){let n=r=>l=>r({...l,payload:e?e(l.payload):l.payload}),i=r=>s?s(r):r;return Object.assign(r=>({listen:l=>r.listen(t,n(l)),once:l=>r.once(t,n(l)),emit:(l=>r.emit(t,i(l)))}),{listen:r=>D(t,n(r)),once:r=>L(t,n(r)),emit:(r=>R(t,i(r)))})}var P,V=new Promise((t,s)=>{P=()=>t(void 0),setTimeout(()=>s(new Error("Failed to initialise WebMIDI")),1e4)}),S=[],W=[],k=new Set;A.stateChange.listen(t=>{let{inputs:s,outputs:e}=t.payload;S=s,W=e,P();for(let n of k)n.__tauri_statechange(s,e)});var m=class extends Event{constructor(s,e){super(s,e),this.port=e?.port||null}};globalThis.MIDIConnectionEvent=m;var f=class extends EventTarget{constructor(){super();this.sysexEnabled=!0;this.inputs=new Map;this.outputs=new Map;this.onstatechange=null;this.__tauri_statechange(S,W),k.add(this),this.addEventListener("statechange",e=>{if(this.onstatechange)return this.onstatechange(e)})}addEventListener(e,n,i){super.addEventListener(e,n,i)}removeEventListener(e,n,i){super.removeEventListener(e,n,i)}__tauri_statechange(e,n){let i=[];for(let[a,c]of this.inputs)e.find(([r,l])=>r===a)||(this.inputs.delete(a),c.state="disconnected",i.push(c));for(let[a,c]of this.outputs)n.find(([r,l])=>r===a)||(this.outputs.delete(a),c.state="disconnected",i.push(c));for(let[a,c]of e){if(this.inputs.has(a))continue;let r=new w(this,a,c);r.state="connected",this.inputs.set(a,r),i.push(r)}for(let[a,c]of n){if(this.outputs.has(a))continue;let r=new y(this,a,c);r.state="connected",this.outputs.set(a,r),i.push(r)}setTimeout(()=>i.forEach(a=>a.__tauri_dispatch_statechange()),0)}};globalThis.MIDIAccess=f;var I=class extends EventTarget{constructor(e,n,i,a){super();this.access=e;this.identifier=n;this.name=i;this.type=a;this.connection="closed";this.manufacturer=null;this.onstatechange=null;this.state="disconnected";this.version=null;this.id=n,this.name=i,this.addEventListener("statechange",c=>{if(this.onstatechange)return this.onstatechange(c)})}__tauri_dispatch_statechange(){this.dispatchEvent(new m("statechange",{port:this})),this.access.dispatchEvent(new m("statechange",{port:this}))}async open(){return this.connection==="open"||this.connection==="pending"?this:this.state==="disconnected"?(this.connection="pending",this.__tauri_dispatch_statechange(),this):(this.type==="input"?await g.openInput(this.id):await g.openOutput(this.id),this.connection="open",this.__tauri_dispatch_statechange(),this)}async close(){return this.connection==="closed"?this:(this.type==="input"?await g.closeInput(this.id):await g.closeOutput(this.id),this.connection="closed",this.__tauri_dispatch_statechange(),this)}};globalThis.MIDIPort=I;var v=class extends Event{constructor(s,e){super(s,e),this.data=e?.data??new Uint8Array(new ArrayBuffer(0))}};globalThis.MIDIMessageEvent=v;var w=class extends I{constructor(e,n,i){super(e,n,i,"input");this._onmidimessage=null;this.addEventListener("midimessage",a=>{if(this.onmidimessage)return this.onmidimessage(a)})}open(){return this.stopListening||(this.stopListening=A.midiMessage.listen(e=>{let[n,i,a]=e.payload;if(n!==this.id)return;let c=parseInt(i),r=new v("midimessage",{data:new Uint8Array(a)});Object.defineProperty(r,"timeStamp",{value:c}),Object.defineProperty(r,"receivedTime",{value:c}),this.dispatchEvent(r)})),super.open()}close(){return this.stopListening&&(this.stopListening?.then(e=>e()),this.stopListening=void 0),super.close()}get onmidimessage(){return this._onmidimessage}set onmidimessage(e){this._onmidimessage=e,e&&this.connection!=="open"&&this.open()}addEventListener(e,n,i){super.addEventListener(e,n,i),e==="midimessage"&&n&&this.state==="connected"&&this.connection!=="open"&&this.open()}};globalThis.MIDIInput=w;var y=class extends I{constructor(s,e,n){super(s,e,n,"output")}send(s,e){if(this.state==="disconnected")throw new Error("MIDIOutput is disconnected");let n=this.state==="connected"&&this.connection==="closed"?this.open():Promise.resolve(),i=e?Math.trunc(performance.timeOrigin+e):null;n.then(()=>g.outputSend(this.id,s,i?i.toString():null))}};globalThis.MIDIOutput=y;navigator.requestMIDIAccess=()=>V.then(()=>new f);})();