multyx-client 0.1.5 → 0.1.7

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/message.d.ts CHANGED
@@ -9,8 +9,6 @@ export declare function UncompressUpdate(str: string): {
9
9
  name?: undefined;
10
10
  response?: undefined;
11
11
  uuid?: undefined;
12
- publicData?: undefined;
13
- clientUUID?: undefined;
14
12
  client?: undefined;
15
13
  tps?: undefined;
16
14
  constraintTable?: undefined;
@@ -27,8 +25,6 @@ export declare function UncompressUpdate(str: string): {
27
25
  name?: undefined;
28
26
  response?: undefined;
29
27
  uuid?: undefined;
30
- publicData?: undefined;
31
- clientUUID?: undefined;
32
28
  client?: undefined;
33
29
  tps?: undefined;
34
30
  constraintTable?: undefined;
@@ -45,8 +41,6 @@ export declare function UncompressUpdate(str: string): {
45
41
  property?: undefined;
46
42
  data?: undefined;
47
43
  uuid?: undefined;
48
- publicData?: undefined;
49
- clientUUID?: undefined;
50
44
  client?: undefined;
51
45
  tps?: undefined;
52
46
  constraintTable?: undefined;
@@ -56,15 +50,13 @@ export declare function UncompressUpdate(str: string): {
56
50
  } | {
57
51
  instruction: string;
58
52
  uuid: string;
59
- publicData: any;
53
+ data: any;
60
54
  team?: undefined;
61
55
  path?: undefined;
62
56
  value?: undefined;
63
57
  property?: undefined;
64
- data?: undefined;
65
58
  name?: undefined;
66
59
  response?: undefined;
67
- clientUUID?: undefined;
68
60
  client?: undefined;
69
61
  tps?: undefined;
70
62
  constraintTable?: undefined;
@@ -73,7 +65,7 @@ export declare function UncompressUpdate(str: string): {
73
65
  space?: undefined;
74
66
  } | {
75
67
  instruction: string;
76
- clientUUID: string;
68
+ client: string;
77
69
  team?: undefined;
78
70
  path?: undefined;
79
71
  value?: undefined;
@@ -82,8 +74,6 @@ export declare function UncompressUpdate(str: string): {
82
74
  name?: undefined;
83
75
  response?: undefined;
84
76
  uuid?: undefined;
85
- publicData?: undefined;
86
- client?: undefined;
87
77
  tps?: undefined;
88
78
  constraintTable?: undefined;
89
79
  clients?: undefined;
@@ -105,9 +95,7 @@ export declare function UncompressUpdate(str: string): {
105
95
  name?: undefined;
106
96
  response?: undefined;
107
97
  uuid?: undefined;
108
- publicData?: undefined;
109
- clientUUID?: undefined;
110
- };
98
+ } | undefined;
111
99
  export declare function CompressUpdate(update: Update): string;
112
100
  export declare class Message {
113
101
  name: string;
package/dist/message.js CHANGED
@@ -23,9 +23,9 @@ function UncompressUpdate(str) {
23
23
  if (instruction == '5')
24
24
  return { instruction: 'resp', name: specifier, response: data[0] };
25
25
  if (instruction == '6')
26
- return { instruction: 'conn', uuid: specifier, publicData: data[0] };
26
+ return { instruction: 'conn', uuid: specifier, data: data[0] };
27
27
  if (instruction == '7')
28
- return { instruction: 'dcon', clientUUID: specifier };
28
+ return { instruction: 'dcon', client: specifier };
29
29
  if (instruction == '8')
30
30
  return {
31
31
  instruction: 'init',
@@ -54,9 +54,9 @@ function CompressUpdate(update) {
54
54
  code = 2;
55
55
  pieces = [update.name, JSON.stringify(update.response)];
56
56
  }
57
- if (!pieces)
57
+ if (!pieces || code === undefined)
58
58
  return '';
59
- let compressed = code;
59
+ let compressed = code.toString();
60
60
  for (let i = 0; i < pieces.length; i++) {
61
61
  compressed += pieces[i].replace(/;/g, ';_');
62
62
  if (i < pieces.length - 1)
package/multyx.js CHANGED
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Multyx=e():t.Multyx=e()}(self,(()=>(()=>{"use strict";var t={376:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Controller=void 0;const i=s(210);e.Controller=class{constructor(t){this.listening=new Set,this.ws=t,this.preventDefault=!1,this.keys={},this.mouse={x:NaN,y:NaN,down:!1,centerX:0,centerY:0,scaleX:1,scaleY:1},document.addEventListener("keydown",(t=>{this.preventDefault&&t.preventDefault;const e=t.key.toLowerCase();this.keys[e]&&this.listening.has("keyhold")&&this.relayInput("keyhold",{code:e}),this.keys[t.code]&&this.listening.has("keyhold")&&this.relayInput("keyhold",{code:t.code}),this.listening.has(e)&&!this.keys[e]&&this.relayInput("keydown",{code:t.key}),this.listening.has(t.code)&&!this.keys[t.code]&&this.relayInput("keydown",{code:t.code}),this.keys[e]=!0,this.keys[t.code]=!0})),document.addEventListener("keyup",(t=>{this.preventDefault&&t.preventDefault;const e=t.key.toLowerCase();delete this.keys[e],delete this.keys[t.code],this.listening.has(e)&&this.relayInput("keyup",{code:e}),this.listening.has(t.code)&&this.relayInput("keyup",{code:t.code})})),document.addEventListener("mousedown",(t=>{if(this.preventDefault&&t.preventDefault,this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.mouse.down=!0,this.listening.has("mousedown")&&this.relayInput("mousedown",{x:this.mouse.x,y:this.mouse.y})})),document.addEventListener("mouseup",(t=>{if(this.preventDefault&&t.preventDefault,this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.mouse.down=!1,this.listening.has("mouseup")&&this.relayInput("mouseup",{x:this.mouse.x,y:this.mouse.y})})),document.addEventListener("mousemove",(t=>{if(this.preventDefault&&t.preventDefault,this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.listening.has("mousemove")&&this.relayInput("mousemove",{x:this.mouse.x,y:this.mouse.y})}))}mapCanvasPosition(t,e){var s,i,o,n,r,l,h,a,u,c,p,d,f,m,g,y;const b="top"in e,v="bottom"in e,w="left"in e,x="right"in e,M=e.anchor,S=t.getBoundingClientRect(),j=(t,...e)=>{const s=t?"Cannot include value for ":"Must include value for ",i=1==e.length?e[0]:e.slice(0,-1).join(", ")+(t?" and ":" or ")+e.slice(-1)[0],o=M?" if anchoring at "+M:" if not anchoring";console.error(s+i+o)},k=S.width/S.height,E=S.height/S.width;if((Number.isNaN(k)||Number.isNaN(E))&&console.error("Canvas element bounding box is flat, canvas must be present on the screen"),M){if("center"==M){if(b&&v&&e.top!==-e.bottom||w&&x&&e.left!==-e.right)return j(!0,"top","bottom","left","right");b?(e.left=w?e.left:x?-e.right:-Math.abs(k*e.top),e.right=w?-e.left:x?e.right:Math.abs(k*e.top),e.bottom=-e.top):v?(e.left=w?e.left:x?-e.right:-Math.abs(k*e.bottom),e.right=w?-e.left:x?e.right:Math.abs(k*e.bottom),e.top=-e.bottom):w?(e.top=b?e.top:v?-e.bottom:-Math.abs(E*e.left),e.bottom=b?-e.top:v?e.bottom:Math.abs(E*e.left),e.right=-e.left):x&&(e.top=b?e.top:v?-e.bottom:-Math.abs(E*e.right),e.bottom=b?-e.top:v?e.bottom:Math.abs(E*e.right),e.left=-e.right)}else if("bottom"==M){if(!w&&!x&&!b)return j(!1,"left","right","top");if(e.bottom)return j(!0,"bottom");e.bottom=0,w?(null!==(s=e.top)&&void 0!==s||(e.top=Math.abs(E*e.left*2)),null!==(i=e.right)&&void 0!==i||(e.right=-e.left)):x?(null!==(o=e.top)&&void 0!==o||(e.top=Math.abs(E*e.right*2)),null!==(n=e.left)&&void 0!==n||(e.left=-e.right)):(e.left=-Math.abs(k*e.top/2),e.right=-e.left)}else if("top"==M){if(!w&&!x&&!v)return j(!1,"left","right","bottom");if(e.top)return j(!0,"top");e.top=0,w?(null!==(r=e.bottom)&&void 0!==r||(e.bottom=Math.abs(E*e.left*2)),null!==(l=e.right)&&void 0!==l||(e.right=-e.left)):x?(null!==(h=e.bottom)&&void 0!==h||(e.bottom=Math.abs(E*e.right*2)),null!==(a=e.left)&&void 0!==a||(e.left=-e.right)):(e.left=-Math.abs(k*e.bottom/2),e.right=-e.left)}else if("left"==M){if(!b&&!v&&!x)return j(!1,"top","bottom","right");if(w)return j(!0,"left");e.left=0,b?(null!==(u=e.right)&&void 0!==u||(e.right=-Math.abs(k*e.top*2)),null!==(c=e.bottom)&&void 0!==c||(e.bottom=-e.top)):v?(null!==(p=e.right)&&void 0!==p||(e.right=Math.abs(k*e.bottom*2)),null!==(d=e.top)&&void 0!==d||(e.top=-e.bottom)):(e.top=-Math.abs(E*e.right/2),e.bottom=-e.top)}else if("right"==M){if(!b&&!v&&!w)return j(!1,"top","bottom","left");if(x)return j(!0,"right");e.right=0,b?(null!==(f=e.left)&&void 0!==f||(e.left=-Math.abs(k*e.top*2)),null!==(m=e.bottom)&&void 0!==m||(e.bottom=-e.top)):v?(null!==(g=e.left)&&void 0!==g||(e.left=Math.abs(k*e.bottom*2)),null!==(y=e.top)&&void 0!==y||(e.top=-e.bottom)):(e.top=-Math.abs(E*e.right/2),e.bottom=-e.top)}else if("topleft"==M){if(!x&&!v)return j(!1,"right","bottom");if(w||b)return j(!0,"left","top");e.left=e.top=0,x?e.bottom=Math.abs(E*e.right):e.right=Math.abs(k*e.bottom)}else if("topright"==M){if(!w&&!v)return j(!1,"left","bottom");if(x||b)return j(!0,"right","top");e.right=e.top=0,w?e.bottom=Math.abs(E*e.left):e.left=Math.abs(k*e.bottom)}else if("bottomleft"==M){if(!x&&!b)return j(!1,"right","top");if(v||w)return j(!0,"bottom","left");e.left=e.bottom=0,x?e.top=Math.abs(E*e.right):e.right=Math.abs(k*e.top)}else if("bottomright"==M){if(!b&&!w)return j(!1,"top","left");if(x||v)return j(!0,"bottom","right");e.right=e.bottom=0,w?e.top=Math.abs(E*e.left):e.left=Math.abs(k*e.top)}}else{if(!b&&!v)return j(!1,"top","bottom");if(v?b||(e.top=e.bottom-t.height):e.bottom=e.top+t.height,!w&&!x)return j(!1,"left","right");x?w||(e.left=e.right-t.width):e.right=e.left+t.width}const C=t.getContext("2d");C.setTransform(1,0,0,1,0,0),t.width=Math.floor(Math.abs(e.right-e.left)),t.height=Math.floor(Math.abs(e.bottom-e.top)),e.right<e.left&&C.scale(-1,1),e.top>e.bottom&&C.scale(1,-1),C.translate(-e.left,-e.top)}mapMousePosition(t,e,s=document.body,i=1,o=i){const n=window.innerWidth/(s instanceof HTMLCanvasElement?s.width:s.clientWidth),r=window.innerHeight/(s instanceof HTMLCanvasElement?s.height:s.clientHeight),l=s.getBoundingClientRect();this.mouse.centerX=l.left+t*n,this.mouse.centerY=l.top+e*r,this.mouse.scaleX=i*n,this.mouse.scaleY=o*r}mapMouseToCanvas(t){const e=t.getContext("2d").getTransform(),s=t.getBoundingClientRect(),i=s.width/t.width,o=s.height/t.height;this.mouse.centerX=s.left+e.e*i,this.mouse.centerY=s.top+e.f*o,this.mouse.scaleX=i*e.a,this.mouse.scaleY=o*e.d}setMouseAs(t){this.mouseGetter=t}relayInput(t,e){if(1!==this.ws.readyState)throw new Error("Websocket connection is "+(2==this.ws.readyState?"closing":"closed"));this.ws.send(i.Message.Native(Object.assign({instruction:"input",input:t},e?{data:e}:{})))}}},34:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.MultyxClientValue=e.MultyxClientObject=e.MultyxClientList=void 0,e.IsMultyxClientItem=function(t){return t instanceof i.default||t instanceof o.default||t instanceof n.default};const i=s(70);e.MultyxClientList=i.default;const o=s(614);e.MultyxClientObject=o.default;const n=s(501);e.MultyxClientValue=n.default},70:(t,e,s)=>{var i;Object.defineProperty(e,"__esModule",{value:!0});const o=s(34),n=s(787),r=s(735),l=s(210);class h{addEditCallback(t){this.editCallbacks.push(t)}get value(){var t;const e=[];for(let s=0;s<this.length;s++)e[s]=null===(t=this.get(s))||void 0===t?void 0:t.value;return e}get length(){return this.list.length}set length(t){this.list.length=t}handleShiftOperation(t,e){const s=t>=0?e>=0?"right":"left":0==e?"reverse":e<0?"length":"unknown";switch(s){case"reverse":for(let t=0;t<Math.floor(this.length/2);t++){const e=this.list[t];this.list[t]=this.list[this.length-1-t],this.list[this.length-1-t]=e}break;case"left":for(let s=t;s<this.length;s++)s+e<0||(this.list[s+e]=this.list[s]);break;case"right":for(let s=this.length-1;s>=t;s--)this.list[s+e]=this.list[s];break;case"length":this.length+=e;break;default:this.multyx.options.verbose&&console.error("Unknown shift operation: "+s)}}constructor(t,e,s=[],o){this.editCallbacks=[],this.toString=()=>this.value.toString(),this.valueOf=()=>this.value,this[i]=()=>this.value,this.list=[],this.propertyPath=s,this.multyx=t,this.editable=o;const r=e instanceof n.EditWrapper;e instanceof h&&(e=e.value),e instanceof n.EditWrapper&&(e=e.value);for(let t=0;t<e.length;t++)this.set(t,r?new n.EditWrapper(e[t]):e[t]);return new Proxy(this,{has:(t,e)=>"number"==typeof e?t.has(e):e in t,get:(t,e)=>e in t?t[e]:(isNaN(parseInt(e))||(e=parseInt(e)),t.get(e)),set:(t,e,s)=>e in t?(t[e]=s,!0):!!t.set(e,s),deleteProperty:(t,e)=>"number"==typeof e&&t.delete(e)})}has(t){return t>=0&&t<this.length}get(t){if("number"==typeof t)return this.list[t];if(0==t.length)return this;if(1==t.length)return this.list[parseInt(t[0])];const e=this.list[parseInt(t[0])];return!e||e instanceof o.MultyxClientValue?void 0:e.get(t.slice(1))}recursiveSet(t,e){if(0==t.length)return this.multyx.options.verbose&&console.error(`Attempting to edit MultyxClientList with no path. Setting '${this.propertyPath.join(".")}' to ${e}`),!1;if("shift"==t[0]&&e instanceof n.EditWrapper)return this.handleShiftOperation(parseInt(t[1]),e.value),!0;if(1==t.length)return this.set(parseInt(t[0]),e);let s=this.get(parseInt(t[0]));return(s instanceof o.MultyxClientValue||null==s)&&(this.set(parseInt(t[0]),new n.EditWrapper({})),s=this.get(parseInt(t[0]))),s.set(t.slice(1),e)}set(t,e){if(Array.isArray(t))return this.recursiveSet(t,e);const s=this.get(t),i=e instanceof n.EditWrapper,l=i||this.editable;if((i||(0,o.IsMultyxClientItem)(e))&&(e=e.value),void 0===e)return this.delete(t,i);if(this.list[t]instanceof o.MultyxClientValue&&"object"!=typeof e)return this.list[t].set(i?new n.EditWrapper(e):e);if(!l)return this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")+"."+t}' to ${e}`),!1;this.list[t]=new((0,r.default)(e))(this.multyx,i?new n.EditWrapper(e):e,[...this.propertyPath,t.toString()],this.editable);const h=Symbol.for("_"+this.propertyPath.join(".")+"."+t);this.multyx.events.has(h)&&this.multyx[n.Done].push(...this.multyx.events.get(h).map((e=>()=>e(this.list[t]))));for(const e of this.editCallbacks)this.multyx[n.Add]((()=>e(t,this.get(t),s)));return!0}delete(t,e=!1){const s=this.get(t);if("string"==typeof t&&(t=parseInt(t)),!this.editable&&!e)return this.multyx.options.verbose&&console.error(`Attempting to delete property that is not editable. Deleting '${this.propertyPath.join(".")+"."+t}'`),!1;delete this.list[t];for(const e of this.editCallbacks)this.multyx[n.Add]((()=>e(t,void 0,s)));return e||this.multyx.ws.send(l.Message.Native({instruction:"edit",path:[...this.propertyPath,t.toString()],value:void 0})),!0}await(t){if(this.has(t))return Promise.resolve(this.get(t));const e=Symbol.for("_"+this.propertyPath.join(".")+"."+t);return new Promise((t=>this.multyx.on(e,t)))}push(...t){for(const e of t)this.set(this.length,e);return this.length}pop(){if(0===this.length)return null;const t=this.get(this.length);return this.delete(this.length),t}unshift(...t){for(let e=this.length-1;e>=0;e--)e>=t.length?this.set(e,this.get(e-t.length)):this.set(e,t[e]);return this.length}shift(){if(0==this.length)return;this.length--;const t=this.get(0);for(let t=0;t<this.length;t++)this.set(t,this.get(t+1));return t}slice(t,e){return this.list.slice(t,e)}splice(t,e,...s){return this.list.splice(t,e,...s)}setSplice(t,e,...s){void 0===e&&(e=this.length-t);let i=s.length-e;if(i>0)for(let s=this.length-1;s>=t+e;s--)this.set(s+i,this.get(s));else if(i<0){for(let s=t+e;s<this.length;s++)this.set(s+i,this.get(s));const s=this.length;for(let t=s+i;t<s;t++)this.set(t,void 0)}for(let e=t;e<s.length;e++)this.set(e,s[e])}filter(t){return this.list.filter(((e,s)=>t(e,s,this)))}setFilter(t){const e=[];for(let s=0;s<this.length;s++)e.push(t(this.get(s),s,this));let s=0;for(let t=0;t<e.length;t++)e[t]&&s&&this.set(t-s,this.get(t)),e[t]||s--}map(t){const e=[];for(let s=0;s<this.length;s++)e.push(t(this.get(s),s,this));return e}flat(){return this.list.flat()}setFlat(){for(let t=0;t<this.length;t++){const e=this.get(t);if(e instanceof h)for(let s=0;s<e.length;s++)t++,this.set(t,e[s])}}reduce(t,e){for(let s=0;s<this.length;s++)e=t(e,this.get(s),s,this);return e}reduceRight(t,e){for(let s=this.length-1;s>=0;s--)e=t(e,this.get(s),s,this);return e}reverse(){let t=this.length-1;for(let e=0;e<t;e++){const s=this.get(e),i=this.get(t);this.set(e,i),this.set(t,s)}return this}forEach(t){for(let e=0;e<this.length;e++)t(this.get(e),e,this)}every(t){for(let e=0;e<this.length;e++)if(!t(this.get(e),e,this))return!1;return!0}some(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return!0;return!1}find(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return this.get(e)}findIndex(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return e;return-1}entries(){const t=[];for(let e=0;e<this.length;e++)t.push([this.get(e),e]);return t}keys(){return Array(this.length).fill(0).map(((t,e)=>e))}[n.Unpack](t){var e;for(let s=0;s<this.length;s++)null===(e=this.get(s))||void 0===e||e[n.Unpack](t[s])}[Symbol.iterator](){const t=[];for(let e=0;e<this.length;e++)t[e]=this.get(e);return t[Symbol.iterator]()}}i=Symbol.toPrimitive,e.default=h},614:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0});const i=s(210),o=s(787),n=s(34),r=s(735),l=s(501);class h{get value(){const t={};for(const e in this.object)t[e]=this.object[e];return t}addEditCallback(t){this.editCallbacks.push(t)}[o.Edit](t,e){1!=t.length?(0==t.length&&this.multyx.options.verbose&&console.error("Update path is empty. Attempting to edit MultyxClientObject with no path."),this.has(t[0])||this.set(t[0],new o.EditWrapper({})),this.get(t[0])[o.Edit](t.slice(1),e)):this.set(t[0],new o.EditWrapper(e))}constructor(t,e,s=[],i){this.editCallbacks=[],this.object={},this.propertyPath=s,this.multyx=t,this.editable=i;const n=e instanceof o.EditWrapper;e instanceof h&&(e=e.value),e instanceof o.EditWrapper&&(e=e.value);for(const t in e)this.set(t,n?new o.EditWrapper(e[t]):e[t]);if(this.constructor===h)return new Proxy(this,{has:(t,e)=>t.has(e),get:(t,e)=>e in t?t[e]:t.get(e),set:(t,e,s)=>e in t?(t[e]=s,!0):t.set(e,s),deleteProperty:(t,e)=>t.delete(e,!1)})}has(t){return t in this.object}get(t){if("string"==typeof t)return this.object[t];if(0==t.length)return this;if(1==t.length)return this.object[t[0]];const e=this.object[t[0]];return!e||e instanceof l.default?void 0:e.get(t.slice(1))}recursiveSet(t,e){if(0==t.length)return this.multyx.options.verbose&&console.error(`Attempting to edit MultyxClientObject with no path. Setting '${this.propertyPath.join(".")}' to ${e}`),!1;if(1==t.length)return this.set(t[0],e);let s=this.get(t[0]);return(s instanceof l.default||null==s)&&(isNaN(parseInt(t[1]))?(this.set(t[0],new o.EditWrapper({})),s=this.get(t[0])):(this.set(t[0],new o.EditWrapper([])),s=this.get(t[0]))),s.set(t.slice(1),e)}set(t,e){if(Array.isArray(t))return this.recursiveSet(t,e);const s=e instanceof o.EditWrapper,i=s||this.editable;if((s||(0,n.IsMultyxClientItem)(e))&&(e=e.value),void 0===e)return this.delete(t,s);if(this.object[t]instanceof l.default&&"object"!=typeof e)return this.object[t].set(s?new o.EditWrapper(e):e);if(!i)return this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")+"."+t}' to ${e}`),!1;this.object[t]=new((0,r.default)(e))(this.multyx,s?new o.EditWrapper(e):e,[...this.propertyPath,t],this.editable);const h=Symbol.for("_"+this.propertyPath.join(".")+"."+t);return this.multyx.events.has(h)&&this.multyx[o.Done].push(...this.multyx.events.get(h).map((e=>()=>e(this.object[t])))),!0}delete(t,e=!1){return this.editable||e?(delete this.object[t],e||this.multyx.ws.send(i.Message.Native({instruction:"edit",path:[...this.propertyPath,t],value:void 0})),!0):(this.multyx.options.verbose&&console.error(`Attempting to delete property that is not editable. Deleting '${this.propertyPath.join(".")+"."+t}'`),!1)}keys(){return Object.keys(this.object)}values(){return Object.values(this.object)}entries(){const t=[];for(let e in this.object)t.push([e,this.get(e)]);return t}await(t){if(this.has(t))return Promise.resolve(this.get(t));const e=Symbol.for("_"+this.propertyPath.join(".")+"."+t);return new Promise((t=>this.multyx.on(e,t)))}[o.Unpack](t){var e;for(const s in t)null===(e=this.object[s])||void 0===e||e[o.Unpack](t[s])}}e.default=h},735:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(t){return Array.isArray(t)?s(70).default:"object"==typeof t?s(614).default:s(501).default}},501:(t,e,s)=>{var i;Object.defineProperty(e,"__esModule",{value:!0});const o=s(210),n=s(787);class r{get value(){return this.readModifiers.reduce(((t,e)=>e(t)),this._value)}set value(t){this._value=t}addReadModifier(t){this.readModifiers.push(t)}addEditCallback(t){this.editCallbacks.push(t)}[n.Edit](t,e){0==t.length&&this.set(new n.EditWrapper(e))}constructor(t,e,s=[],o){this.readModifiers=[],this.editCallbacks=[],this.toString=()=>this.value.toString(),this.valueOf=()=>this.value,this[i]=()=>this.value,this.propertyPath=s,this.editable=o,this.multyx=t,this.constraints={},this.set(e);const r=Symbol.for("_"+this.propertyPath.join("."));this.multyx.events.has(r)&&this.multyx[n.Done].push(...this.multyx.events.get(r).map((t=>()=>t(this.value))))}set(t){if(t instanceof n.EditWrapper){const e=this.value;return this.value=t.value,this.editCallbacks.forEach((s=>s(t.value,e))),!0}if(!this.editable)return this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")}' to ${t}`),!1;let e=t;for(const s in this.constraints)if(e=(0,this.constraints[s])(e),null===e)return this.multyx.options.verbose&&console.error(`Attempting to set property that failed on constraint. Setting '${this.propertyPath.join(".")}' to ${t}, stopped by constraint '${s}'`),!1;return this.value===e?(this.value=e,!0):(this.value=e,this.multyx.ws.send(o.Message.Native({instruction:"edit",path:this.propertyPath,value:e})),!0)}bindElement(t){this.addEditCallback(((e,s)=>{e!==s&&(t.innerText=e.toString())}))}[n.Unpack](t){for(const[e,s]of Object.entries(t)){const t=(0,n.BuildConstraint)(e,s);t&&(this.constraints[e]=t)}}}i=Symbol.toPrimitive,e.default=r},210:(t,e)=>{function s(t){let e,s;if("edit"==t.instruction?(e=0,s=[t.path.join("."),JSON.stringify(t.value)]):"input"==t.instruction?(e=1,s=[t.input,JSON.stringify(t.data)]):"resp"==t.instruction&&(e=2,s=[t.name,JSON.stringify(t.response)]),!s)return"";let i=e;for(let t=0;t<s.length;t++)i+=s[t].replace(/;/g,";_"),t<s.length-1&&(i+=";,");return JSON.stringify([i])}Object.defineProperty(e,"__esModule",{value:!0}),e.Message=void 0,e.UncompressUpdate=function(t){const[e,...s]=t.split(/;,/),i=e[0],o=e.slice(1).replace(/;_/g,";"),n=s.map((t=>t.replace(/;_/g,";"))).map((t=>"undefined"==t?void 0:JSON.parse(t)));return"0"==i?{instruction:"edit",team:!1,path:o.split("."),value:n[0]}:"1"==i?{instruction:"edit",team:!0,path:o.split("."),value:n[0]}:"2"==i?{instruction:"self",property:"controller",data:JSON.parse(o)}:"3"==i?{instruction:"self",property:"uuid",data:JSON.parse(o)}:"4"==i?{instruction:"self",property:"constraint",data:JSON.parse(o)}:"9"==i?{instruction:"self",property:"space",data:JSON.parse(o)}:"5"==i?{instruction:"resp",name:o,response:n[0]}:"6"==i?{instruction:"conn",uuid:o,publicData:n[0]}:"7"==i?{instruction:"dcon",clientUUID:o}:"8"==i?{instruction:"init",client:JSON.parse(o),tps:n[0],constraintTable:n[1],clients:n[2],teams:n[3],space:n[4]}:void 0},e.CompressUpdate=s;class i{constructor(t,e,s=!1){this.name=t,this.data=e,this.time=Date.now(),this.native=s}static BundleOperations(t,e){return Array.isArray(e)||(e=[e]),JSON.stringify(new i("_",{operations:e,deltaTime:t}))}static Native(t){return s(t)}static Parse(t){var e,s;const o=JSON.parse(t);return Array.isArray(o)?new i("_",o,!0):new i(null!==(e=o.name)&&void 0!==e?e:"",null!==(s=o.data)&&void 0!==s?s:"",!1)}static Create(t,e){if(0==t.length)throw new Error("Multyx message cannot have empty name");if("_"==t[0]&&(t="_"+t),"function"==typeof e)throw new Error("Multyx data must be JSON storable");return JSON.stringify(new i(t,e))}}e.Message=i},944:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.DefaultOptions=void 0,e.DefaultOptions={port:8443,secure:!1,uri:"localhost",verbose:!1,logUpdateFrame:!1}},787:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.EditWrapper=e.Edit=e.Add=e.Done=e.Unpack=void 0,e.Interpolate=function(t,e,s){let i={value:t[e],time:Date.now()},o={value:t[e],time:Date.now()};Object.defineProperty(t,e,{get:()=>{const t=o.time-i.time;let e=s[0],n=s[0];for(const i of s)t>i.time&&i.time>e.time&&(e=i),t<i.time&&i.time<n.time&&(n=i);const r=(t-e.time)/(n.time-e.time),l=e.progress+r*(n.progress-e.progress);return Number.isNaN(l)?i.value:o.value*l+i.value*(1-l)},set:t=>Date.now()-o.time<10?(o.value=t,!0):(i=Object.assign({},o),o={value:t,time:Date.now()},!0)})},e.BuildConstraint=function(t,e){return"min"==t?t=>t>=e[0]?t:e[0]:"max"==t?t=>t<=e[0]?t:e[0]:"int"==t?t=>Math.floor(t):"ban"==t?t=>e.includes(t)?null:t:"disabled"==t?t=>e[0]?null:t:t=>t},e.Unpack=Symbol("unpack"),e.Done=Symbol("done"),e.Add=Symbol("add"),e.Edit=Symbol("edit"),e.EditWrapper=class{constructor(t){this.value=t}}}},e={};function s(i){var o=e[i];if(void 0!==o)return o.exports;var n=e[i]={exports:{}};return t[i](n,n.exports,s),n.exports}var i={};return(()=>{var t,e=i;const o=s(210),n=s(787),r=s(376),l=s(34),h=s(944);class a{constructor(e={},s){var i;this[t]=[],this.options=Object.assign(Object.assign({},h.DefaultOptions),e);const n=`ws${this.options.secure?"s":""}://${this.options.uri.split("/")[0]}:${this.options.port}/${null!==(i=this.options.uri.split("/")[1])&&void 0!==i?i:""}`;this.ws=new WebSocket(n),this.ping=0,this.space="default",this.events=new Map,this.self={},this.tps=0,this.all={},this.teams=new l.MultyxClientObject(this,{},[],!0),this.clients={},this.controller=new r.Controller(this.ws),null==s||s(),this.ws.onmessage=t=>{var e,s,i,n;const r=o.Message.Parse(t.data);this.ping=2*(Date.now()-r.time),r.native?(this.parseNativeEvent(r),null===(e=this.events.get(a.Native))||void 0===e||e.forEach((t=>t(r)))):(null===(s=this.events.get(r.name))||void 0===s||s.forEach((t=>{const e=t(r.data);void 0!==e&&this.send(r.name,e)})),null===(i=this.events.get(a.Custom))||void 0===i||i.forEach((t=>t(r)))),null===(n=this.events.get(a.Any))||void 0===n||n.forEach((t=>t(r)))}}on(t,e){var s;const i=null!==(s=this.events.get(t))&&void 0!==s?s:[];i.push(e),this.events.set(t,i)}send(t,e){"_"===t[0]&&(t="_"+t);const s={instruction:"resp",name:t,response:e};this.ws.send(o.Message.Native(s))}await(t,e){return this.send(t,e),new Promise((e=>this.events.set(Symbol.for("_"+t),[e])))}loop(t,e){if(e)this.on(a.Start,(()=>setInterval(t,Math.round(1e3/e))));else{const e=()=>{t(),requestAnimationFrame(e)};this.on(a.Start,(()=>requestAnimationFrame(e)))}}[(t=n.Done,n.Add)](t){this[n.Done].push(t)}parseNativeEvent(t){var e,s,i,r;t.data=t.data.map(o.UncompressUpdate),this.options.logUpdateFrame&&console.log(t.data);for(const o of t.data)switch(o.instruction){case"init":this.initialize(o);for(const t of null!==(e=this.events.get(a.Start))&&void 0!==e?e:[])this[n.Done].push((()=>t(o)));this.events.has(a.Start)&&(this.events.get(a.Start).length=0);break;case"edit":if(1==o.path.length)o.team?this.teams.set(o.path[0],new n.EditWrapper(o.value)):this.clients[o.path[0]]=new l.MultyxClientObject(this,new n.EditWrapper(o.value),[o.path[0]],!1);else{const t=o.team?this.teams.get(o.path[0]):this.clients[o.path[0]];if(!t)return;t.set(o.path.slice(1),new n.EditWrapper(o.value))}for(const t of null!==(s=this.events.get(a.Edit))&&void 0!==s?s:[])this[n.Done].push((()=>t(o)));break;case"self":this.parseSelf(o);break;case"conn":this.clients[o.uuid]=new l.MultyxClientObject(this,o.data,[o.uuid],!1);for(const t of null!==(i=this.events.get(a.Connection))&&void 0!==i?i:[])this[n.Done].push((()=>t(this.clients[o.uuid])));break;case"dcon":for(const t of null!==(r=this.events.get(a.Disconnect))&&void 0!==r?r:[]){const e=this.clients[o.client].value;this[n.Done].push((()=>t(e)))}delete this.clients[o.client];break;case"resp":{const t=this.events.get(Symbol.for("_"+o.name))[0];this.events.delete(Symbol.for("_"+o.name)),this[n.Done].push((()=>t(o.response)));break}default:this.options.verbose&&console.error("Server error: Unknown native Multyx instruction")}this[n.Done].forEach((t=>t())),this[n.Done].length=0}initialize(t){this.tps=t.tps,this.uuid=t.client.uuid,this.joinTime=t.client.joinTime,this.controller.listening=new Set(t.client.controller);for(const e of Object.keys(t.teams))this.teams[e]=new n.EditWrapper(t.teams[e]);this.all=this.teams.all,this.clients={};for(const[e,s]of Object.entries(t.clients))e!=this.uuid&&(this.clients[e]=new l.MultyxClientObject(this,new n.EditWrapper(s),[e],!1));const e=new l.MultyxClientObject(this,new n.EditWrapper(t.client.self),[this.uuid],!0);this.self=e,this.clients[this.uuid]=e;for(const[e,s]of Object.entries(t.constraintTable))(this.uuid==e?this.self:this.teams[e])[n.Unpack](s)}parseSelf(t){if("controller"==t.property)this.controller.listening=new Set(t.data);else if("uuid"==t.property)this.uuid=t.data;else if("constraint"==t.property){let e=this.uuid==t.data.path[0]?this.self:this.teams[t.data.path[0]];for(const s of t.data.path.slice(1))e=null==e?void 0:e[s];if(void 0===e)return;e[n.Unpack]({[t.data.name]:t.data.args})}else"space"==t.property&&(this.space=t.data,this.updateSpace())}updateSpace(){"default"!=this.space?document.querySelectorAll("[data-multyx-space]").forEach((t=>{t.style.display=t.dataset.multyxSpace==this.space?"block":"none",t.style.pointerEvents=t.dataset.multyxSpace==this.space?"auto":"none"})):document.querySelectorAll("[data-multyx-space]").forEach((t=>{t.style.display="block",t.style.pointerEvents="auto"}))}}a.Start=Symbol("start"),a.Connection=Symbol("connection"),a.Disconnect=Symbol("disconnect"),a.Edit=Symbol("edit"),a.Native=Symbol("native"),a.Custom=Symbol("custom"),a.Any=Symbol("any"),e.default=a})(),i.default})()));
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Multyx=e():t.Multyx=e()}(self,()=>(()=>{"use strict";var t={249:function(t,e,s){var i,n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const o=s(625),r=s(703),l=n(s(416)),a=s(449);class h{addEditCallback(t){this.editCallbacks.push(t)}get value(){var t;const e=[];for(let s=0;s<this.length;s++)e[s]=null===(t=this.get(s))||void 0===t?void 0:t.value;return e}get length(){return this.list.length}set length(t){this.list.length=t}handleShiftOperation(t,e){const s=t>=0?e>=0?"right":"left":0==e?"reverse":e<0?"length":"unknown";switch(s){case"reverse":for(let t=0;t<Math.floor(this.length/2);t++){const e=this.list[t];this.list[t]=this.list[this.length-1-t],this.list[this.length-1-t]=e}break;case"left":for(let s=t;s<this.length;s++)s+e<0||(this.list[s+e]=this.list[s]);break;case"right":for(let s=this.length-1;s>=t;s--)this.list[s+e]=this.list[s];break;case"length":this.length+=e;break;default:this.multyx.options.verbose&&console.error("Unknown shift operation: "+s)}}constructor(t,e,s=[],n){this.type="list",this.editCallbacks=[],this.toString=()=>this.value.toString(),this.valueOf=()=>this.value,this[i]=()=>this.value,this.list=[],this.propertyPath=s,this.multyx=t,this.editable=n;const o=e instanceof r.EditWrapper;e instanceof h&&(e=e.value),e instanceof r.EditWrapper&&(e=e.value);for(let t=0;t<e.length;t++)this.set(t,o?new r.EditWrapper(e[t]):e[t]);return new Proxy(this,{has:(t,e)=>"number"==typeof e?t.has(e):e in t,get:(t,e)=>e in t?t[e]:(isNaN(parseInt(e))||(e=parseInt(e)),t.get(e)),set:(t,e,s)=>e in t?(t[e]=s,!0):!!t.set(e,s),deleteProperty:(t,e)=>"number"==typeof e&&t.delete(e)})}has(t){return t>=0&&t<this.length}get(t){if("number"==typeof t)return this.list[t];if(0==t.length)return this;if(1==t.length)return this.list[parseInt(t[0])];const e=this.list[parseInt(t[0])];return!e||e instanceof o.MultyxClientValue?void 0:e.get(t.slice(1))}recursiveSet(t,e){if(0==t.length)return this.multyx.options.verbose&&console.error(`Attempting to edit MultyxClientList with no path. Setting '${this.propertyPath.join(".")}' to ${e}`),!1;if("shift"==t[0]&&e instanceof r.EditWrapper)return this.handleShiftOperation(parseInt(t[1]),e.value),!0;if(1==t.length)return this.set(parseInt(t[0]),e);let s=this.get(parseInt(t[0]));return(s instanceof o.MultyxClientValue||null==s)&&(this.set(parseInt(t[0]),new r.EditWrapper({})),s=this.get(parseInt(t[0]))),!(!s||s instanceof o.MultyxClientValue)&&s.set(t.slice(1),e)}set(t,e){if(Array.isArray(t))return this.recursiveSet(t,e);const s=this.get(t),i=e instanceof r.EditWrapper,n=i||this.editable,a=i||(0,o.IsMultyxClientItem)(e)?e.value:e;if(void 0===a)return this.delete(t,i);if(i&&this.tryApplyServerValue(t,a,s))return!0;if(this.list[t]instanceof o.MultyxClientValue&&("object"!=typeof a||null===a)){const e=this.list[t].set(i?new r.EditWrapper(a):a);return this.enqueueEditCallbacks(t,s),e}return n?(this.list[t]=new((0,l.default)(a))(this.multyx,i?new r.EditWrapper(a):a,[...this.propertyPath,t.toString()],this.editable),this.notifyIndexWaiters(t),this.enqueueEditCallbacks(t,s),!0):(this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")+"."+t}' to ${a}`),!1)}delete(t,e=!1){const s=this.get(t);if("string"==typeof t&&(t=parseInt(t)),!this.editable&&!e)return this.multyx.options.verbose&&console.error(`Attempting to delete property that is not editable. Deleting '${this.propertyPath.join(".")+"."+t}'`),!1;delete this.list[t];for(const e of this.editCallbacks)this.multyx[r.Add](()=>e(t,void 0,s));return e||this.multyx.ws.send(a.Message.Native({instruction:"edit",path:[...this.propertyPath,t.toString()],value:void 0})),!0}await(t){if(this.has(t))return Promise.resolve(this.get(t));const e=Symbol.for("_"+this.propertyPath.join(".")+"."+t);return new Promise(t=>this.multyx.on(e,t))}push(...t){for(const e of t)this.set(this.length,e);return this.length}pop(){if(0===this.length)return;const t=this.get(this.length);return this.delete(this.length),t}unshift(...t){for(let e=this.length-1;e>=0;e--)e>=t.length?this.set(e,this.get(e-t.length)):this.set(e,t[e]);return this.length}shift(){if(0==this.length)return;this.length--;const t=this.get(0);for(let t=0;t<this.length;t++)this.set(t,this.get(t+1));return t}slice(t,e){return this.list.slice(t,e)}splice(t,e,...s){return this.list.splice(t,null!=e?e:0,...s)}setSplice(t,e,...s){void 0===e&&(e=this.length-t);let i=s.length-e;if(i>0)for(let s=this.length-1;s>=t+e;s--)this.set(s+i,this.get(s));else if(i<0){for(let s=t+e;s<this.length;s++)this.set(s+i,this.get(s));const s=this.length;for(let t=s+i;t<s;t++)this.set(t,void 0)}for(let e=t;e<s.length;e++)this.set(e,s[e])}filter(t){return this.list.filter((e,s)=>t(e,s,this))}setFilter(t){const e=[];for(let s=0;s<this.length;s++)e.push(t(this.get(s),s,this));let s=0;for(let t=0;t<e.length;t++)e[t]&&s&&this.set(t-s,this.get(t)),e[t]||s--}map(t){const e=[];for(let s=0;s<this.length;s++)e.push(t(this.get(s),s,this));return e}flat(){return this.list.flat()}setFlat(){for(let t=0;t<this.length;t++){const e=this.get(t);if(e instanceof h)for(let s=0;s<e.length;s++)t++,this.set(t,e[s])}}reduce(t,e){for(let s=0;s<this.length;s++)e=t(e,this.get(s),s,this);return e}reduceRight(t,e){for(let s=this.length-1;s>=0;s--)e=t(e,this.get(s),s,this);return e}reverse(){let t=this.length-1;for(let e=0;e<t;e++){const s=this.get(e),i=this.get(t);this.set(e,i),this.set(t,s)}return this}forEach(t){for(let e=0;e<this.length;e++)t(this.get(e),e,this)}every(t){for(let e=0;e<this.length;e++)if(!t(this.get(e),e,this))return!1;return!0}some(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return!0;return!1}find(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return this.get(e)}findIndex(t){for(let e=0;e<this.length;e++)if(t(this.get(e),e,this))return e;return-1}entries(){const t=[];for(let e=0;e<this.length;e++)t.push([this.get(e),e]);return t}keys(){return Array(this.length).fill(0).map((t,e)=>e)}[r.Edit](){}[r.Unpack](t){var e;for(let s=0;s<this.length;s++)null===(e=this.get(s))||void 0===e||e[r.Unpack](t[s])}[Symbol.iterator](){const t=[];for(let e=0;e<this.length;e++)t[e]=this.get(e);return t[Symbol.iterator]()}hydrateFromServer(t){if(Array.isArray(t)){for(let e=0;e<t.length;e++)this.set(e,new r.EditWrapper(t[e]));for(let e=t.length;e<this.length;e++)this.delete(e,!0);this.length=t.length}}tryApplyServerValue(t,e,s){const i=this.list[t];if(!i)return!1;if(i instanceof o.MultyxClientValue&&("object"!=typeof e||null===e))return i.set(new r.EditWrapper(e)),this.enqueueEditCallbacks(t,s),!0;const n="function"==typeof(null==i?void 0:i.hydrateFromServer);return Array.isArray(e)&&n&&"list"===i.type?(i.hydrateFromServer(e),this.enqueueEditCallbacks(t,s),!0):!(null===(l=e)||"object"!=typeof l||Array.isArray(l)||!n||"object"!==i.type||(i.hydrateFromServer(e),this.enqueueEditCallbacks(t,s),0));var l}notifyIndexWaiters(t){var e,s;const i=Symbol.for("_"+this.propertyPath.join(".")+"."+t);this.multyx.events.has(i)&&this.multyx[r.Done].push(...null!==(s=null===(e=this.multyx.events.get(i))||void 0===e?void 0:e.map(e=>()=>e(this.list[t])))&&void 0!==s?s:[])}enqueueEditCallbacks(t,e){for(const s of this.editCallbacks)this.multyx[r.Add](()=>s(t,this.get(t),e))}}i=Symbol.toPrimitive,e.default=h},280:(t,e,s)=>{var i;Object.defineProperty(e,"__esModule",{value:!0});const n=s(449),o=s(703);class r{get value(){return this.readModifiers.reduce((t,e)=>e(t),this._value)}set value(t){this._value=t}addReadModifier(t){this.readModifiers.push(t)}addEditCallback(t){this.editCallbacks.push(t)}[o.Edit](t,e){0==t.length&&this.set(new o.EditWrapper(e))}constructor(t,e,s=[],n){var r,l;this.readModifiers=[],this.editCallbacks=[],this.toString=()=>this.value.toString(),this.valueOf=()=>this.value,this[i]=()=>this.value,this.propertyPath=s,this.editable=n,this.multyx=t,this.constraints={},this.set(e);const a=Symbol.for("_"+this.propertyPath.join("."));this.multyx.events.has(a)&&this.multyx[o.Done].push(...null!==(l=null===(r=this.multyx.events.get(a))||void 0===r?void 0:r.map(t=>()=>t(this.value)))&&void 0!==l?l:[])}set(t){if(t instanceof o.EditWrapper){const e=this.value;return this.value=t.value,this.editCallbacks.forEach(s=>s(t.value,e)),!0}if(!this.editable)return this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")}' to ${t}`),!1;let e=t;for(const s in this.constraints)if(e=(0,this.constraints[s])(e),null===e)return this.multyx.options.verbose&&console.error(`Attempting to set property that failed on constraint. Setting '${this.propertyPath.join(".")}' to ${t}, stopped by constraint '${s}'`),!1;return this.value===e?(this.value=e,!0):(this.value=e,this.multyx.ws.send(n.Message.Native({instruction:"edit",path:this.propertyPath,value:e})),!0)}bindElement(t){this.addEditCallback((e,s)=>{e!==s&&(t.innerText=e.toString())})}[o.Unpack](t){for(const[e,s]of Object.entries(t)){const t=(0,o.BuildConstraint)(e,s);t&&(this.constraints[e]=t)}}}i=Symbol.toPrimitive,e.default=r},416:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(t){return Array.isArray(t)?s(249).default:"object"==typeof t?s(922).default:s(280).default}},449:(t,e)=>{function s(t){let e,s;if("edit"==t.instruction?(e=0,s=[t.path.join("."),JSON.stringify(t.value)]):"input"==t.instruction?(e=1,s=[t.input,JSON.stringify(t.data)]):"resp"==t.instruction&&(e=2,s=[t.name,JSON.stringify(t.response)]),!s||void 0===e)return"";let i=e.toString();for(let t=0;t<s.length;t++)i+=s[t].replace(/;/g,";_"),t<s.length-1&&(i+=";,");return JSON.stringify([i])}Object.defineProperty(e,"__esModule",{value:!0}),e.Message=void 0,e.UncompressUpdate=function(t){const[e,...s]=t.split(/;,/),i=e[0],n=e.slice(1).replace(/;_/g,";"),o=s.map(t=>t.replace(/;_/g,";")).map(t=>"undefined"==t?void 0:JSON.parse(t));return"0"==i?{instruction:"edit",team:!1,path:n.split("."),value:o[0]}:"1"==i?{instruction:"edit",team:!0,path:n.split("."),value:o[0]}:"2"==i?{instruction:"self",property:"controller",data:JSON.parse(n)}:"3"==i?{instruction:"self",property:"uuid",data:JSON.parse(n)}:"4"==i?{instruction:"self",property:"constraint",data:JSON.parse(n)}:"9"==i?{instruction:"self",property:"space",data:JSON.parse(n)}:"5"==i?{instruction:"resp",name:n,response:o[0]}:"6"==i?{instruction:"conn",uuid:n,data:o[0]}:"7"==i?{instruction:"dcon",client:n}:"8"==i?{instruction:"init",client:JSON.parse(n),tps:o[0],constraintTable:o[1],clients:o[2],teams:o[3],space:o[4]}:void 0},e.CompressUpdate=s;class i{constructor(t,e,s=!1){this.name=t,this.data=e,this.time=Date.now(),this.native=s}static BundleOperations(t,e){return Array.isArray(e)||(e=[e]),JSON.stringify(new i("_",{operations:e,deltaTime:t}))}static Native(t){return s(t)}static Parse(t){var e,s;const n=JSON.parse(t);return Array.isArray(n)?new i("_",n,!0):new i(null!==(e=n.name)&&void 0!==e?e:"",null!==(s=n.data)&&void 0!==s?s:"",!1)}static Create(t,e){if(0==t.length)throw new Error("Multyx message cannot have empty name");if("_"==t[0]&&(t="_"+t),"function"==typeof e)throw new Error("Multyx data must be JSON storable");return JSON.stringify(new i(t,e))}}e.Message=i},625:function(t,e,s){var i=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.MultyxClientValue=e.MultyxClientObject=e.MultyxClientList=void 0,e.IsMultyxClientItem=function(t){return t instanceof n.default||t instanceof o.default||t instanceof r.default};const n=i(s(249));e.MultyxClientList=n.default;const o=i(s(922));e.MultyxClientObject=o.default;const r=i(s(280));e.MultyxClientValue=r.default},703:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.EditWrapper=e.Edit=e.Add=e.Done=e.Unpack=void 0,e.Interpolate=function(t,e,s){let i={value:t[e],time:Date.now()},n={value:t[e],time:Date.now()};Object.defineProperty(t,e,{get:()=>{const t=n.time-i.time;let e=s[0],o=s[0];for(const i of s)t>i.time&&i.time>e.time&&(e=i),t<i.time&&i.time<o.time&&(o=i);const r=(t-e.time)/(o.time-e.time),l=e.progress+r*(o.progress-e.progress);return Number.isNaN(l)?i.value:n.value*l+i.value*(1-l)},set:t=>Date.now()-n.time<10?(n.value=t,!0):(i=Object.assign({},n),n={value:t,time:Date.now()},!0)})},e.BuildConstraint=function(t,e){return"min"==t?t=>t>=e[0]?t:e[0]:"max"==t?t=>t<=e[0]?t:e[0]:"int"==t?t=>Math.floor(t):"ban"==t?t=>e.includes(t)?null:t:"disabled"==t?t=>e[0]?null:t:t=>t},e.Unpack=Symbol("unpack"),e.Done=Symbol("done"),e.Add=Symbol("add"),e.Edit=Symbol("edit"),e.EditWrapper=class{constructor(t){this.value=t}}},832:(t,e,s)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Controller=void 0;const i=s(449);e.Controller=class{constructor(t){this.listening=new Set,this.ws=t,this.preventDefault=!1,this.keys={},this.mouse={x:NaN,y:NaN,down:!1,centerX:0,centerY:0,scaleX:1,scaleY:1},document.addEventListener("keydown",t=>{this.preventDefault&&t.preventDefault();const e=t.key.toLowerCase();this.keys[e]&&this.listening.has("keyhold")&&this.relayInput("keyhold",{code:e}),this.keys[t.code]&&this.listening.has("keyhold")&&this.relayInput("keyhold",{code:t.code}),this.listening.has(e)&&!this.keys[e]&&this.relayInput("keydown",{code:t.key}),this.listening.has(t.code)&&!this.keys[t.code]&&this.relayInput("keydown",{code:t.code}),this.keys[e]=!0,this.keys[t.code]=!0}),document.addEventListener("keyup",t=>{this.preventDefault&&t.preventDefault();const e=t.key.toLowerCase();delete this.keys[e],delete this.keys[t.code],this.listening.has(e)&&this.relayInput("keyup",{code:e}),this.listening.has(t.code)&&this.relayInput("keyup",{code:t.code})}),document.addEventListener("mousedown",t=>{if(this.preventDefault&&t.preventDefault(),this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.mouse.down=!0,this.listening.has("mousedown")&&this.relayInput("mousedown",{x:this.mouse.x,y:this.mouse.y})}),document.addEventListener("mouseup",t=>{if(this.preventDefault&&t.preventDefault(),this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.mouse.down=!1,this.listening.has("mouseup")&&this.relayInput("mouseup",{x:this.mouse.x,y:this.mouse.y})}),document.addEventListener("mousemove",t=>{if(this.preventDefault&&t.preventDefault(),this.mouseGetter){const t=this.mouseGetter();this.mouse.x=t.x,this.mouse.y=t.y}else this.mouse.x=(t.clientX-this.mouse.centerX)/this.mouse.scaleX,this.mouse.y=(t.clientY-this.mouse.centerY)/this.mouse.scaleY;this.listening.has("mousemove")&&this.relayInput("mousemove",{x:this.mouse.x,y:this.mouse.y})})}mapCanvasPosition(t,e){const s="top"in e,i="bottom"in e,n="left"in e,o="right"in e,r=e.anchor,l=t.getBoundingClientRect(),a=(t,...e)=>{const s=t?"Cannot include value for ":"Must include value for ",i=1==e.length?e[0]:e.slice(0,-1).join(", ")+(t?" and ":" or ")+e.slice(-1)[0],n=r?" if anchoring at "+r:" if not anchoring";console.error(s+i+n)},h=l.width/l.height,u=l.height/l.width;if((Number.isNaN(h)||Number.isNaN(u))&&console.error("Canvas element bounding box is flat, canvas must be present on the screen"),r){if("center"==r){if(s&&i&&e.top!==-e.bottom||n&&o&&e.left!==-e.right)return a(!0,"top","bottom","left","right");s?(e.left=n?e.left:o?-e.right:-Math.abs(h*e.top),e.right=n?-e.left:o?e.right:Math.abs(h*e.top),e.bottom=-e.top):i?(e.left=n?e.left:o?-e.right:-Math.abs(h*e.bottom),e.right=n?-e.left:o?e.right:Math.abs(h*e.bottom),e.top=-e.bottom):n?(e.top=s?e.top:i?-e.bottom:-Math.abs(u*e.left),e.bottom=s?-e.top:i?e.bottom:Math.abs(u*e.left),e.right=-e.left):o&&(e.top=s?e.top:i?-e.bottom:-Math.abs(u*e.right),e.bottom=s?-e.top:i?e.bottom:Math.abs(u*e.right),e.left=-e.right)}else if("bottom"==r){if(!n&&!o&&!s)return a(!1,"left","right","top");if(e.bottom)return a(!0,"bottom");e.bottom=0,n?(e.top=Math.abs(u*e.left*2),e.right=-e.left):o?(e.top=Math.abs(u*e.right*2),e.left=-e.right):(e.left=-Math.abs(h*e.top/2),e.right=-e.left)}else if("top"==r){if(!n&&!o&&!i)return a(!1,"left","right","bottom");if(e.top)return a(!0,"top");e.top=0,n?(e.bottom=Math.abs(u*e.left*2),e.right=-e.left):o?(e.bottom=Math.abs(u*e.right*2),e.left=-e.right):(e.left=-Math.abs(h*e.bottom/2),e.right=-e.left)}else if("left"==r){if(!s&&!i&&!o)return a(!1,"top","bottom","right");if(n)return a(!0,"left");e.left=0,s?(e.right=-Math.abs(h*e.top*2),e.bottom=-e.top):i?(e.right=Math.abs(h*e.bottom*2),e.top=-e.bottom):(e.top=-Math.abs(u*e.right/2),e.bottom=-e.top)}else if("right"==r){if(!s&&!i&&!n)return a(!1,"top","bottom","left");if(o)return a(!0,"right");e.right=0,s?(e.left=-Math.abs(h*e.top*2),e.bottom=-e.top):i?(e.left=Math.abs(h*e.bottom*2),e.top=-e.bottom):(e.top=-Math.abs(u*e.right/2),e.bottom=-e.top)}else if("topleft"==r){if(!o&&!i)return a(!1,"right","bottom");if(n||s)return a(!0,"left","top");e.left=e.top=0,o?e.bottom=Math.abs(u*e.right):e.right=Math.abs(h*e.bottom)}else if("topright"==r){if(!n&&!i)return a(!1,"left","bottom");if(o||s)return a(!0,"right","top");e.right=e.top=0,n?e.bottom=Math.abs(u*e.left):e.left=Math.abs(h*e.bottom)}else if("bottomleft"==r){if(!o&&!s)return a(!1,"right","top");if(i||n)return a(!0,"bottom","left");e.left=e.bottom=0,o?e.top=Math.abs(u*e.right):e.right=Math.abs(h*e.top)}else if("bottomright"==r){if(!s&&!n)return a(!1,"top","left");if(o||i)return a(!0,"bottom","right");e.right=e.bottom=0,n?e.top=Math.abs(u*e.left):e.left=Math.abs(h*e.top)}}else{if(!s&&!i)return a(!1,"top","bottom");if(i?s||(e.top=e.bottom-t.height):e.bottom=e.top+t.height,!n&&!o)return a(!1,"left","right");o?n||(e.left=e.right-t.width):e.right=e.left+t.width}const c=t.getContext("2d");null==c||c.setTransform(1,0,0,1,0,0),t.width=Math.floor(Math.abs(e.right-e.left)),t.height=Math.floor(Math.abs(e.bottom-e.top)),e.right<e.left&&(null==c||c.scale(-1,1)),e.top>e.bottom&&(null==c||c.scale(1,-1)),null==c||c.translate(-e.left,-e.top)}mapMousePosition(t,e,s=document.body,i=1,n=i){const o=window.innerWidth/(s instanceof HTMLCanvasElement?s.width:s.clientWidth),r=window.innerHeight/(s instanceof HTMLCanvasElement?s.height:s.clientHeight),l=s.getBoundingClientRect();this.mouse.centerX=l.left+t*o,this.mouse.centerY=l.top+e*r,this.mouse.scaleX=i*o,this.mouse.scaleY=n*r}mapMouseToCanvas(t){const e=t.getContext("2d"),s=null==e?void 0:e.getTransform(),i=t.getBoundingClientRect(),n=i.width/t.width,o=i.height/t.height;this.mouse.centerX=i.left+(null==s?void 0:s.e)*n,this.mouse.centerY=i.top+(null==s?void 0:s.f)*o,this.mouse.scaleX=n*(null==s?void 0:s.a),this.mouse.scaleY=o*(null==s?void 0:s.d)}setMouseAs(t){this.mouseGetter=t}relayInput(t,e){if(1!==this.ws.readyState)throw new Error("Websocket connection is "+(2==this.ws.readyState?"closing":"closed"));this.ws.send(i.Message.Native(Object.assign({instruction:"input",input:t},e?{data:e}:{})))}}},922:function(t,e,s){var i=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const n=s(449),o=s(703),r=s(625),l=i(s(416)),a=i(s(280)),h=t=>null!==t&&"object"==typeof t&&!Array.isArray(t);class u{get value(){const t={};for(const e in this.object)t[e]=this.object[e];return t}addEditCallback(t){this.editCallbacks.push(t)}[o.Edit](t,e){var s;1!=t.length?(0==t.length&&this.multyx.options.verbose&&console.error("Update path is empty. Attempting to edit MultyxClientObject with no path."),this.has(t[0])||this.set(t[0],new o.EditWrapper({})),null===(s=this.get(t[0]))||void 0===s||s[o.Edit](t.slice(1),e)):this.set(t[0],new o.EditWrapper(e))}constructor(t,e,s=[],i){this.type="object",this.editCallbacks=[],this.object={},this.propertyPath=s,this.multyx=t,this.editable=i;const n=e instanceof o.EditWrapper;e instanceof u&&(e=e.value),e instanceof o.EditWrapper&&(e=e.value);for(const t in e)this.set(t,n?new o.EditWrapper(e[t]):e[t]);if(this.constructor===u)return new Proxy(this,{has:(t,e)=>t.has(e),get:(t,e)=>e in t?t[e]:t.get(e),set:(t,e,s)=>e in t?(t[e]=s,!0):t.set(e,s),deleteProperty:(t,e)=>t.delete(e,!1)})}has(t){return t in this.object}get(t){if("string"==typeof t)return this.object[t];if(0==t.length)return this;if(1==t.length)return this.object[t[0]];const e=this.object[t[0]];return!e||e instanceof a.default?void 0:e.get(t.slice(1))}recursiveSet(t,e){if(0==t.length)return this.multyx.options.verbose&&console.error(`Attempting to edit MultyxClientObject with no path. Setting '${this.propertyPath.join(".")}' to ${e}`),!1;if(1==t.length)return this.set(t[0],e);let s=this.get(t[0]);return(s instanceof a.default||null==s)&&(isNaN(parseInt(t[1]))?(this.set(t[0],new o.EditWrapper({})),s=this.get(t[0])):(this.set(t[0],new o.EditWrapper([])),s=this.get(t[0]))),s.set(t.slice(1),e)}set(t,e){if(Array.isArray(t))return this.recursiveSet(t,e);const s=e instanceof o.EditWrapper,i=s||this.editable,n=s||(0,r.IsMultyxClientItem)(e)?e.value:e;return void 0===n?this.delete(t,s):!(!s||!this.applyServerValue(t,n))||(this.object[t]instanceof a.default&&("object"!=typeof n||null===n)?this.object[t].set(s?new o.EditWrapper(n):n):i?(this.object[t]=new((0,l.default)(n))(this.multyx,s?new o.EditWrapper(n):n,[...this.propertyPath,t],this.editable),this.notifyPropertyWaiters(t),!0):(this.multyx.options.verbose&&console.error(`Attempting to set property that is not editable. Setting '${this.propertyPath.join(".")+"."+t}' to ${n}`),!1))}delete(t,e=!1){return this.editable||e?(delete this.object[t],e||this.multyx.ws.send(n.Message.Native({instruction:"edit",path:[...this.propertyPath,t],value:void 0})),!0):(this.multyx.options.verbose&&console.error(`Attempting to delete property that is not editable. Deleting '${this.propertyPath.join(".")+"."+t}'`),!1)}keys(){return Object.keys(this.object)}values(){return Object.values(this.object)}entries(){const t=[];for(let e in this.object)t.push([e,this.get(e)]);return t}await(t){if(this.has(t))return Promise.resolve(this.get(t));const e=Symbol.for("_"+this.propertyPath.join(".")+"."+t);return new Promise(t=>this.multyx.on(e,t))}[o.Unpack](t){var e;for(const s in t)null===(e=this.object[s])||void 0===e||e[o.Unpack](t[s])}hydrateFromServer(t){if(!h(t))return;const e=new Set(Object.keys(this.object));for(const[s,i]of Object.entries(t))e.delete(s),this.set(s,new o.EditWrapper(i));for(const t of e)this.delete(t,!0)}applyServerValue(t,e){const s=this.object[t];if(!s)return!1;if(s instanceof a.default&&("object"!=typeof e||null===e))return s.set(new o.EditWrapper(e)),!0;const i="function"==typeof(null==s?void 0:s.hydrateFromServer);return(Array.isArray(e)&&i&&"list"===s.type||!(!h(e)||!i||"object"!==s.type))&&(s.hydrateFromServer(e),!0)}notifyPropertyWaiters(t){var e,s;const i=Symbol.for("_"+this.propertyPath.join(".")+"."+t);this.multyx.events.has(i)&&this.multyx[o.Done].push(...null!==(s=null===(e=this.multyx.events.get(i))||void 0===e?void 0:e.map(e=>()=>e(this.object[t])))&&void 0!==s?s:[])}}e.default=u},960:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.DefaultOptions=void 0,e.DefaultOptions={port:8443,secure:!1,uri:"localhost",verbose:!1,logUpdateFrame:!1}}},e={};function s(i){var n=e[i];if(void 0!==n)return n.exports;var o=e[i]={exports:{}};return t[i].call(o.exports,o,o.exports,s),o.exports}var i={};return(()=>{var t,e=i;const n=s(449),o=s(703),r=s(832),l=s(625),a=s(960);class h{constructor(e={},s){var i;if(this[t]=[],this.options=Object.assign(Object.assign({},a.DefaultOptions),e),!this.options.uri)throw new Error("URI is required");const o=`ws${this.options.secure?"s":""}://${this.options.uri.split("/")[0]}:${this.options.port}/${null!==(i=this.options.uri.split("/")[1])&&void 0!==i?i:""}`;this.ws=new WebSocket(o),this.ping=0,this.space="default",this.events=new Map,this.self={},this.tps=0,this.all={},this.teams=new l.MultyxClientObject(this,{},[],!0),this.clients={},this.controller=new r.Controller(this.ws),null==s||s(),this.ws.onmessage=t=>{var e,s,i,o;const r=n.Message.Parse(t.data);this.ping=2*(Date.now()-r.time),r.native?(this.parseNativeEvent(r),null===(e=this.events.get(h.Native))||void 0===e||e.forEach(t=>t(r))):(null===(s=this.events.get(r.name))||void 0===s||s.forEach(t=>{const e=t(r.data);void 0!==e&&this.send(r.name,e)}),null===(i=this.events.get(h.Custom))||void 0===i||i.forEach(t=>t(r))),null===(o=this.events.get(h.Any))||void 0===o||o.forEach(t=>t(r))}}on(t,e){var s;const i=null!==(s=this.events.get(t))&&void 0!==s?s:[];i.push(e),this.events.set(t,i)}send(t,e){"_"===t[0]&&(t="_"+t);const s={instruction:"resp",name:t,response:e};this.ws.send(n.Message.Native(s))}await(t,e){return this.send(t,e),new Promise(e=>this.events.set(Symbol.for("_"+t),[e]))}loop(t,e){if(e)this.on(h.Start,()=>setInterval(t,Math.round(1e3/e)));else{const e=()=>{t(),requestAnimationFrame(e)};this.on(h.Start,()=>requestAnimationFrame(e))}}[(t=o.Done,o.Add)](t){this[o.Done].push(t)}parseNativeEvent(t){var e,s,i,r,a;t.data=t.data.map(n.UncompressUpdate),this.options.logUpdateFrame&&console.log(t.data);for(const n of t.data)switch(n.instruction){case"init":this.initialize(n);for(const t of null!==(e=this.events.get(h.Start))&&void 0!==e?e:[])this[o.Done].push(()=>t(n));this.events.has(h.Start)&&(this.events.get(h.Start).length=0);break;case"edit":if(1==n.path.length)n.team?this.teams.set(n.path[0],new o.EditWrapper(n.value)):this.clients[n.path[0]]=new l.MultyxClientObject(this,new o.EditWrapper(n.value),[n.path[0]],!1);else{const t=n.team?this.teams.get(n.path[0]):this.clients[n.path[0]];if(!t)return;t.set(n.path.slice(1),new o.EditWrapper(n.value))}for(const t of null!==(s=this.events.get(h.Edit))&&void 0!==s?s:[])this[o.Done].push(()=>t(n));break;case"self":this.parseSelf(n);break;case"conn":this.clients[n.uuid]=new l.MultyxClientObject(this,n.data,[n.uuid],!1);for(const t of null!==(i=this.events.get(h.Connection))&&void 0!==i?i:[])this[o.Done].push(()=>t(this.clients[n.uuid]));break;case"dcon":for(const t of null!==(r=this.events.get(h.Disconnect))&&void 0!==r?r:[]){const e=this.clients[n.client].value;this[o.Done].push(()=>t(e))}delete this.clients[n.client];break;case"resp":{const t=null===(a=this.events.get(Symbol.for("_"+n.name)))||void 0===a?void 0:a[0];this.events.delete(Symbol.for("_"+n.name)),t&&this[o.Done].push(()=>t(n.response));break}default:this.options.verbose&&console.error("Server error: Unknown native Multyx instruction")}this[o.Done].forEach(t=>t()),this[o.Done].length=0}initialize(t){this.tps=t.tps,this.uuid=t.client.uuid,this.joinTime=t.client.joinTime,this.controller.listening=new Set(t.client.controller);for(const e of Object.keys(t.teams))this.teams[e]=new o.EditWrapper(t.teams[e]);this.all=this.teams.all,this.clients={};for(const[e,s]of Object.entries(t.clients))e!=this.uuid&&(this.clients[e]=new l.MultyxClientObject(this,new o.EditWrapper(s),[e],!1));const e=new l.MultyxClientObject(this,new o.EditWrapper(t.client.self),[this.uuid],!0);this.self=e,this.clients[this.uuid]=e;for(const[e,s]of Object.entries(t.constraintTable))(this.uuid==e?this.self:this.teams[e])[o.Unpack](s)}parseSelf(t){if("controller"==t.property)this.controller.listening=new Set(t.data);else if("uuid"==t.property)this.uuid=t.data;else if("constraint"==t.property){let e=this.uuid==t.data.path[0]?this.self:this.teams[t.data.path[0]];for(const s of t.data.path.slice(1))e=null==e?void 0:e[s];if(void 0===e)return;e[o.Unpack]({[t.data.name]:t.data.args})}else"space"==t.property&&(this.space=t.data,this.updateSpace())}updateSpace(){"default"!=this.space?document.querySelectorAll("[data-multyx-space]").forEach(t=>{t.style.display=t.dataset.multyxSpace==this.space?"block":"none",t.style.pointerEvents=t.dataset.multyxSpace==this.space?"auto":"none"}):document.querySelectorAll("[data-multyx-space]").forEach(t=>{t.style.display="block",t.style.pointerEvents="auto"})}}h.Start=Symbol("start"),h.Connection=Symbol("connection"),h.Disconnect=Symbol("disconnect"),h.Edit=Symbol("edit"),h.Native=Symbol("native"),h.Custom=Symbol("custom"),h.Any=Symbol("any"),h.Interpolate=o.Interpolate,e.default=h})(),i.default})());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multyx-client",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Framework designed to simplify the creation of multiplayer browser games by addressing the complexities of managing server-client communication, shared state, and input handling",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/controller.ts CHANGED
@@ -32,7 +32,7 @@ export class Controller {
32
32
  };
33
33
 
34
34
  document.addEventListener('keydown', e => {
35
- if(this.preventDefault) e.preventDefault;
35
+ if(this.preventDefault) e.preventDefault();
36
36
 
37
37
  const key = e.key.toLowerCase();
38
38
 
@@ -57,7 +57,7 @@ export class Controller {
57
57
 
58
58
  });
59
59
  document.addEventListener('keyup', e => {
60
- if(this.preventDefault) e.preventDefault;
60
+ if(this.preventDefault) e.preventDefault();
61
61
 
62
62
  const key = e.key.toLowerCase();
63
63
 
@@ -69,7 +69,7 @@ export class Controller {
69
69
 
70
70
  // Mouse input events
71
71
  document.addEventListener('mousedown', e => {
72
- if(this.preventDefault) e.preventDefault;
72
+ if(this.preventDefault) e.preventDefault();
73
73
 
74
74
  if(this.mouseGetter) {
75
75
  const mouse = this.mouseGetter();
@@ -86,7 +86,7 @@ export class Controller {
86
86
  });
87
87
  });
88
88
  document.addEventListener('mouseup', e => {
89
- if(this.preventDefault) e.preventDefault;
89
+ if(this.preventDefault) e.preventDefault();
90
90
 
91
91
  if(this.mouseGetter) {
92
92
  const mouse = this.mouseGetter();
@@ -103,7 +103,7 @@ export class Controller {
103
103
  });
104
104
  });
105
105
  document.addEventListener('mousemove', e => {
106
- if(this.preventDefault) e.preventDefault;
106
+ if(this.preventDefault) e.preventDefault();
107
107
 
108
108
  if(this.mouseGetter) {
109
109
  const mouse = this.mouseGetter();
@@ -159,32 +159,32 @@ export class Controller {
159
159
  // mb bruh jus trust it works
160
160
  if(!a) {
161
161
  if(!t && !b) return error(false, 'top', 'bottom');
162
- else if(!b) position.bottom = position.top + canvas.height;
163
- else if(!t) position.top = position.bottom - canvas.height;
162
+ else if(!b) position.bottom = position.top! + canvas.height;
163
+ else if(!t) position.top = position.bottom! - canvas.height;
164
164
 
165
165
  if(!l && !r) return error(false, 'left', 'right');
166
- else if(!r) position.right = position.left + canvas.width;
167
- else if(!l) position.left = position.right - canvas.width;
166
+ else if(!r) position.right = position.left! + canvas.width;
167
+ else if(!l) position.left = position.right! - canvas.width;
168
168
  } else if(a == 'center') {
169
- if(t && b && position.top !== -position.bottom
170
- || l && r && position.left !== -position.right) return error(true, 'top', 'bottom', 'left', 'right');
169
+ if(t && b && position.top !== -position.bottom!
170
+ || l && r && position.left !== -position.right!) return error(true, 'top', 'bottom', 'left', 'right');
171
171
 
172
172
  if(t) {
173
- position.left = l ? position.left : r ? -position.right : -Math.abs(wToH * position.top);
174
- position.right = l ? -position.left : r ? position.right : Math.abs(wToH * position.top);
175
- position.bottom = -position.top;
173
+ position.left = l ? position.left! : r ? -position.right! : -Math.abs(wToH * position.top!);
174
+ position.right = l ? -position.left! : r ? position.right! : Math.abs(wToH * position.top!);
175
+ position.bottom = -position.top!;
176
176
  } else if(b) {
177
- position.left = l ? position.left : r ? -position.right : -Math.abs(wToH * position.bottom);
178
- position.right = l ? -position.left : r ? position.right : Math.abs(wToH * position.bottom);
179
- position.top = -position.bottom;
177
+ position.left = l ? position.left! : r ? -position.right! : -Math.abs(wToH * position.bottom!);
178
+ position.right = l ? -position.left! : r ? position.right! : Math.abs(wToH * position.bottom!);
179
+ position.top = -position.bottom!;
180
180
  } else if(l) {
181
- position.top = t ? position.top : b ? -position.bottom : -Math.abs(hToW * position.left);
182
- position.bottom = t ? -position.top : b ? position.bottom : Math.abs(hToW * position.left);
183
- position.right = -position.left;
181
+ position.top = t ? position.top! : b ? -position.bottom! : -Math.abs(hToW * position.left!);
182
+ position.bottom = t ? -position.top! : b ? position.bottom! : Math.abs(hToW * position.left!);
183
+ position.right = -position.left!;
184
184
  } else if(r) {
185
- position.top = t ? position.top : b ? -position.bottom : -Math.abs(hToW * position.right);
186
- position.bottom = t ? -position.top : b ? position.bottom : Math.abs(hToW * position.right);
187
- position.left = -position.right;
185
+ position.top = t ? position.top! : b ? -position.bottom! : -Math.abs(hToW * position.right!);
186
+ position.bottom = t ? -position.top! : b ? position.bottom! : Math.abs(hToW * position.right!);
187
+ position.left = -position.right!;
188
188
  }
189
189
  } else if(a == 'bottom') {
190
190
  if(!l && !r && !t) return error(false, 'left', 'right', 'top');
@@ -192,14 +192,14 @@ export class Controller {
192
192
  position.bottom = 0;
193
193
 
194
194
  if(l) {
195
- position.top ??= Math.abs(hToW * position.left * 2);
196
- position.right ??= -position.left;
195
+ position.top = Math.abs(hToW * position.left! * 2);
196
+ position.right = -position.left!;
197
197
  } else if(r) {
198
- position.top ??= Math.abs(hToW * position.right * 2);
199
- position.left ??= -position.right;
198
+ position.top = Math.abs(hToW * position.right! * 2);
199
+ position.left = -position.right!;
200
200
  } else {
201
- position.left = -Math.abs(wToH * position.top / 2);
202
- position.right = -position.left;
201
+ position.left = -Math.abs(wToH * position.top! / 2);
202
+ position.right = -position.left!;
203
203
  }
204
204
  } else if(a == 'top') {
205
205
  if(!l && !r && !b) return error(false, 'left', 'right', 'bottom');
@@ -207,14 +207,14 @@ export class Controller {
207
207
  position.top = 0;
208
208
 
209
209
  if(l) {
210
- position.bottom ??= Math.abs(hToW * position.left * 2);
211
- position.right ??= -position.left;
210
+ position.bottom = Math.abs(hToW * position.left! * 2);
211
+ position.right = -position.left!;
212
212
  } else if(r) {
213
- position.bottom ??= Math.abs(hToW * position.right * 2);
214
- position.left ??= -position.right;
213
+ position.bottom = Math.abs(hToW * position.right! * 2);
214
+ position.left = -position.right!;
215
215
  } else {
216
- position.left = -Math.abs(wToH * position.bottom / 2);
217
- position.right = -position.left;
216
+ position.left = -Math.abs(wToH * position.bottom! / 2);
217
+ position.right = -position.left!;
218
218
  }
219
219
  } else if(a == 'left') {
220
220
  if(!t && !b && !r) return error(false, 'top', 'bottom', 'right');
@@ -222,14 +222,14 @@ export class Controller {
222
222
  position.left = 0;
223
223
 
224
224
  if(t) {
225
- position.right ??= -Math.abs(wToH * position.top * 2);
226
- position.bottom ??= -position.top;
225
+ position.right = -Math.abs(wToH * position.top! * 2);
226
+ position.bottom = -position.top!;
227
227
  } else if(b) {
228
- position.right ??= Math.abs(wToH * position.bottom * 2);
229
- position.top ??= -position.bottom;
228
+ position.right = Math.abs(wToH * position.bottom! * 2);
229
+ position.top = -position.bottom!;
230
230
  } else {
231
- position.top = -Math.abs(hToW * position.right / 2);
232
- position.bottom = -position.top;
231
+ position.top = -Math.abs(hToW * position.right! / 2);
232
+ position.bottom = -position.top!;
233
233
  }
234
234
  } else if(a == 'right') {
235
235
  if(!t && !b && !l) return error(false, 'top', 'bottom', 'left');
@@ -237,54 +237,54 @@ export class Controller {
237
237
  position.right = 0;
238
238
 
239
239
  if(t) {
240
- position.left ??= -Math.abs(wToH * position.top * 2);
241
- position.bottom ??= -position.top;
240
+ position.left = -Math.abs(wToH * position.top! * 2);
241
+ position.bottom = -position.top!;
242
242
  } else if(b) {
243
- position.left ??= Math.abs(wToH * position.bottom * 2);
244
- position.top ??= -position.bottom;
243
+ position.left = Math.abs(wToH * position.bottom! * 2);
244
+ position.top = -position.bottom!;
245
245
  } else {
246
- position.top = -Math.abs(hToW * position.right / 2);
247
- position.bottom = -position.top;
246
+ position.top = -Math.abs(hToW * position.right! / 2);
247
+ position.bottom = -position.top!;
248
248
  }
249
249
  } else if(a == 'topleft') {
250
250
  if(!r && !b) return error(false, 'right', 'bottom');
251
251
  if(l || t) return error(true, 'left', 'top');
252
252
  position.left = position.top = 0;
253
253
 
254
- if(r) position.bottom = Math.abs(hToW * position.right);
255
- else position.right = Math.abs(wToH * position.bottom);
254
+ if(r) position.bottom = Math.abs(hToW * position.right!);
255
+ else position.right = Math.abs(wToH * position.bottom!);
256
256
  } else if(a == 'topright') {
257
257
  if(!l && !b) return error(false, 'left', 'bottom');
258
258
  if(r || t) return error(true, 'right', 'top');
259
259
  position.right = position.top = 0;
260
260
 
261
- if(l) position.bottom = Math.abs(hToW * position.left);
262
- else position.left = Math.abs(wToH * position.bottom);
261
+ if(l) position.bottom = Math.abs(hToW * position.left!);
262
+ else position.left = Math.abs(wToH * position.bottom!);
263
263
  } else if(a == 'bottomleft') {
264
264
  if(!r && !t) return error(false, 'right', 'top');
265
265
  if(b || l) return error(true, 'bottom', 'left');
266
266
  position.left = position.bottom = 0;
267
267
 
268
- if(r) position.top = Math.abs(hToW * position.right);
269
- else position.right = Math.abs(wToH * position.top);
268
+ if(r) position.top = Math.abs(hToW * position.right!);
269
+ else position.right = Math.abs(wToH * position.top!);
270
270
  } else if(a == 'bottomright') {
271
271
  if(!t && !l) return error(false, 'top', 'left');
272
272
  if(r || b) return error(true, 'bottom', 'right');
273
- position.right = position.bottom = 0;
273
+ position.right = position.bottom = 0 as any;
274
274
 
275
- if(l) position.top = Math.abs(hToW * position.left);
276
- else position.left = Math.abs(wToH * position.top);
275
+ if(l) position.top = Math.abs(hToW * position.left!);
276
+ else position.left = Math.abs(wToH * position.top!);
277
277
  }
278
278
 
279
279
  const ctx = canvas.getContext("2d");
280
- ctx.setTransform(1, 0, 0, 1, 0, 0);
280
+ ctx?.setTransform(1, 0, 0, 1, 0, 0);
281
281
 
282
- canvas.width = Math.floor(Math.abs(position.right-position.left));
283
- canvas.height = Math.floor(Math.abs(position.bottom-position.top));
284
- if(position.right < position.left) ctx.scale(-1, 1);
285
- if(position.top > position.bottom) ctx.scale(1, -1);
282
+ canvas.width = Math.floor(Math.abs(position.right!-position.left!));
283
+ canvas.height = Math.floor(Math.abs(position.bottom!-position.top!));
284
+ if(position.right! < position.left!) ctx?.scale(-1, 1);
285
+ if(position.top! > position.bottom!) ctx?.scale(1, -1);
286
286
 
287
- ctx.translate(-position.left, -position.top);
287
+ ctx?.translate(-position.left!, -position.top!);
288
288
  }
289
289
 
290
290
  /**
@@ -317,18 +317,18 @@ export class Controller {
317
317
  */
318
318
  mapMouseToCanvas(canvas: HTMLCanvasElement) {
319
319
  const ctx = canvas.getContext("2d");
320
- const transform = ctx.getTransform();
320
+ const transform = ctx?.getTransform();
321
321
  const bounding = canvas.getBoundingClientRect();
322
322
 
323
323
  // Ratio between canvas scale to unit pixels
324
324
  const canvasRatioX = bounding.width / canvas.width;
325
325
  const canvasRatioY = bounding.height / canvas.height;
326
326
 
327
- this.mouse.centerX = bounding.left + transform.e * canvasRatioX;
328
- this.mouse.centerY = bounding.top + transform.f * canvasRatioY;
327
+ this.mouse.centerX = bounding.left + transform?.e! * canvasRatioX;
328
+ this.mouse.centerY = bounding.top + transform?.f! * canvasRatioY;
329
329
 
330
- this.mouse.scaleX = canvasRatioX * transform.a;
331
- this.mouse.scaleY = canvasRatioY * transform.d;
330
+ this.mouse.scaleX = canvasRatioX * transform?.a!;
331
+ this.mouse.scaleY = canvasRatioY * transform?.d!;
332
332
  }
333
333
 
334
334
  /**
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Message, UncompressUpdate } from "./message";
2
- import { Unpack, EditWrapper, Add, Edit, Done } from './utils';
2
+ import { Unpack, EditWrapper, Add, Done, Interpolate } from './utils';
3
3
  import { RawObject, ResponseUpdate } from "./types";
4
4
  import { Controller } from "./controller";
5
- import { MultyxClientObject, MultyxClientValue } from "./items";
5
+ import { MultyxClientObject } from "./items";
6
6
  import { DefaultOptions, Options } from "./options";
7
7
  export default class Multyx {
8
8
  ws: WebSocket;
@@ -31,11 +31,14 @@ export default class Multyx {
31
31
  static Custom = Symbol('custom');
32
32
  static Any = Symbol('any');
33
33
 
34
+ static Interpolate = Interpolate;
35
+
34
36
  constructor(options: Options = {}, callback?: () => void) {
35
37
  this.options = { ...DefaultOptions, ...options };
36
38
 
37
- const url = `ws${this.options.secure ? 's' : ''}://${this.options.uri.split('/')[0]}:${this.options.port}/${this.options.uri.split('/')[1] ?? ''}`;
38
- this.ws = new WebSocket(url);
39
+ if(!this.options.uri) throw new Error('URI is required');
40
+ const uri = `ws${this.options.secure ? 's' : ''}://${this.options.uri.split('/')[0]}:${this.options.port}/${this.options.uri.split('/')[1] ?? ''}`;
41
+ this.ws = new WebSocket(uri);
39
42
  this.ping = 0;
40
43
  this.space = 'default';
41
44
  this.events = new Map();
@@ -147,7 +150,7 @@ export default class Multyx {
147
150
  }
148
151
 
149
152
  // Clear start event as it will never be called again
150
- if(this.events.has(Multyx.Start)) this.events.get(Multyx.Start).length = 0;
153
+ if(this.events.has(Multyx.Start)) this.events.get(Multyx.Start)!.length = 0;
151
154
  break;
152
155
  }
153
156
 
@@ -211,9 +214,9 @@ export default class Multyx {
211
214
 
212
215
  // Response to client
213
216
  case 'resp': {
214
- const promiseResolve = this.events.get(Symbol.for("_" + update.name))[0];
217
+ const promiseResolve = this.events.get(Symbol.for("_" + update.name))?.[0];
215
218
  this.events.delete(Symbol.for("_" + update.name));
216
- this[Done].push(() => promiseResolve(update.response));
219
+ if(promiseResolve) this[Done].push(() => promiseResolve(update.response));
217
220
  break;
218
221
  }
219
222