2024-07-07 13:43:18 +02:00
(() => {
2024-07-07 19:21:08 +02:00
// node_modules/bloatless-react/index.ts
2024-07-07 22:11:18 +02:00
var State = class {
_value ;
_bindings = /* @__PURE__ */ new Set ();
// init
constructor ( initialValue ) {
this . _value = initialValue ;
}
// value
get value () {
return this . _value ;
}
set value ( newValue ) {
if ( this . _value == newValue ) return ;
this . _value = newValue ;
this . callSubscriptions ();
}
// subscriptions
callSubscriptions () {
this . _bindings . forEach (( fn ) => fn ( this . _value ));
}
subscribe ( fn ) {
this . _bindings . add ( fn );
fn ( this . _value );
}
2024-07-11 15:08:25 +02:00
subscribeSilent ( fn ) {
this . _bindings . add ( fn );
}
2024-07-07 22:11:18 +02:00
// stringify
toString () {
return JSON . stringify ( this . _value );
}
};
2024-07-19 18:17:00 +02:00
function persistState ( localStorageKey , state ) {
state . subscribe (() => {
const stringifiedValue = state . toString ();
localStorage . setItem ( localStorageKey , stringifiedValue );
});
}
function restoreState ( localStorageKey , initialStateValue ) {
const storedString = localStorage . getItem ( localStorageKey ) ?? JSON . stringify ( initialStateValue );
const convertedValue = JSON . parse ( storedString );
const state = new State ( convertedValue );
persistState ( localStorageKey , state );
return state ;
}
2024-07-07 19:21:08 +02:00
function createElement ( tagName , attributes = {}, ... children ) {
const element = document . createElement ( tagName );
if ( attributes != null )
Object . entries ( attributes ). forEach (( entry ) => {
const [ attributename , value ] = entry ;
const [ directiveKey , directiveValue ] = attributename . split ( ":" );
switch ( directiveKey ) {
case "on" : {
switch ( directiveValue ) {
case "enter" : {
element . addEventListener ( "keydown" , ( e ) => {
if ( e . key != "Enter" ) return ;
value ();
});
break ;
}
default : {
element . addEventListener ( directiveValue , value );
}
}
break ;
}
case "subscribe" : {
2024-07-09 14:11:20 +02:00
const state = value ;
state . subscribe (
( newValue ) => element [ directiveValue ] = newValue
);
2024-07-07 19:21:08 +02:00
break ;
}
case "bind" : {
const state = value ;
state . subscribe (
( newValue ) => element [ directiveValue ] = newValue
);
element . addEventListener (
"input" ,
() => state . value = element [ directiveValue ]
);
break ;
}
case "toggle" : {
2024-07-08 00:39:22 +02:00
if ( value . subscribe ) {
const state = value ;
state . subscribe (
( newValue ) => element . toggleAttribute ( directiveValue , newValue )
);
} else {
element . toggleAttribute ( directiveValue , value );
}
2024-07-07 19:21:08 +02:00
break ;
}
case "set" : {
const state = value ;
state . subscribe (
( newValue ) => element . setAttribute ( directiveValue , newValue )
);
break ;
}
2024-07-09 14:11:20 +02:00
case "children" : {
switch ( directiveValue ) {
case "set" : {
const state = value ;
state . subscribe (( newValue ) => {
element . innerHTML = "" ;
2024-07-09 18:21:28 +02:00
element . append (...[ newValue ]. flat ());
2024-07-09 14:11:20 +02:00
});
break ;
}
case "append" :
case "appendandscroll" :
case "prepend" : {
element . style . scrollBehavior = "smooth" ;
try {
const [ listState , toElement ] = value ;
listState . handleAddition (( newItem ) => {
const child = toElement ( newItem );
listState . handleRemoval (
newItem ,
() => child . remove ()
);
if ( directiveValue == "append" ) {
element . append ( child );
} else if ( directiveValue == "prepend" ) {
element . prepend ( child );
}
});
} catch {
2024-07-11 00:56:06 +02:00
throw `error: cannot process subscribe:children directive because StateItemConverter is not defined. Usage: "subscribe:children={[list, converter]}"; you can find a more detailed example in the documentation` ;
2024-07-09 14:11:20 +02:00
}
}
}
}
2024-07-07 19:21:08 +02:00
default :
element . setAttribute ( attributename , value );
}
});
children . filter (( x ) => x ). forEach (( child ) => element . append ( child ));
return element ;
}
2024-07-09 15:15:17 +02:00
// node_modules/udn-frontend/index.ts
var UDNFrontend = class {
ws ;
// HANDLERS
connectionHandler = () => {
};
disconnectionHandler = () => {
};
messageHandler = ( data ) => {
};
2024-07-19 18:00:01 +02:00
mailboxHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
2024-07-19 18:00:01 +02:00
mailboxConnectionHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
2024-07-19 18:00:01 +02:00
mailboxDeleteHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
// INIT
set onconnect ( handler ) {
this . connectionHandler = handler ;
}
set ondisconnect ( handler ) {
this . disconnectionHandler = handler ;
}
set onmessage ( handler ) {
this . messageHandler = handler ;
}
set onmailboxcreate ( handler ) {
this . mailboxHandler = handler ;
}
set onmailboxconnect ( handler ) {
this . mailboxConnectionHandler = handler ;
}
set onmailboxdelete ( handler ) {
this . mailboxDeleteHandler = handler ;
}
// UTILITY METHODS
send ( messageObject ) {
if ( this . ws == void 0 ) return false ;
const messageString = JSON . stringify ( messageObject );
this . ws . send ( messageString );
return true ;
}
// PUBLIC METHODS
// connection
2024-07-19 18:17:00 +02:00
connect ( address ) {
2024-07-09 15:15:17 +02:00
try {
this . disconnect ();
2024-07-19 18:17:00 +02:00
this . ws = new WebSocket ( address );
2024-07-09 15:15:17 +02:00
this . ws . addEventListener ( "open" , this . connectionHandler );
this . ws . addEventListener ( "close" , this . disconnectionHandler );
this . ws . addEventListener ( "message" , ( message ) => {
const dataString = message . data . toString ();
const data = JSON . parse ( dataString );
if ( data . assignedMailboxId ) {
return this . mailboxHandler ( data . assignedMailboxId );
} else if ( data . connectedMailboxId ) {
return this . mailboxConnectionHandler ( data . connectedMailboxId );
} else if ( data . deletedMailbox ) {
return this . mailboxDeleteHandler ( data . deletedMailbox );
} else {
this . messageHandler ( data );
}
});
} catch ( error ) {
console . error ( error );
}
}
disconnect () {
this . ws ? . close ();
}
// message
sendMessage ( channel , body ) {
const messageObject = {
messageChannel : channel ,
messageBody : body
};
return this . send ( messageObject );
}
// subscription
subscribe ( channel ) {
const messageObject = {
subscribeChannel : channel
};
return this . send ( messageObject );
}
unsubscribe ( channel ) {
const messageObject = {
unsubscribeChannel : channel
};
return this . send ( messageObject );
}
// mailbox
requestMailbox () {
const messageObject = {
requestingMailboxSetup : true
};
return this . send ( messageObject );
}
2024-07-19 18:00:01 +02:00
connectMailbox ( mailboxId ) {
2024-07-09 15:15:17 +02:00
const messageObject = {
2024-07-19 18:00:01 +02:00
requestedMailbox : mailboxId
2024-07-09 15:15:17 +02:00
};
return this . send ( messageObject );
}
2024-07-19 18:00:01 +02:00
deleteMailbox ( mailboxId ) {
2024-07-09 15:15:17 +02:00
const messageObject = {
2024-07-19 18:00:01 +02:00
deletingMailbox : mailboxId
2024-07-09 15:15:17 +02:00
};
return this . send ( messageObject );
}
};
2024-07-19 18:00:01 +02:00
// src/utility.ts
function stringify ( data ) {
return JSON . stringify ( data );
2024-07-12 14:30:30 +02:00
}
2024-07-19 18:00:01 +02:00
// src/Model/connectionModel.ts
var ConnectionModel = class {
// basic
udn ;
// data
get isConnected () {
return this . udn . ws != void 0 && this . udn . ws . readyState == 1 ;
2024-07-13 17:13:59 +02:00
}
2024-07-19 18:02:23 +02:00
get address () {
2024-07-19 18:00:01 +02:00
return this . udn . ws ? . url ;
2024-07-13 17:13:59 +02:00
}
2024-07-19 18:00:01 +02:00
// connection
2024-07-19 18:17:00 +02:00
connect = ( address ) => {
this . udn . connect ( address );
2024-07-19 18:02:23 +02:00
};
disconnect = () => {
2024-07-19 18:00:01 +02:00
this . udn . disconnect ();
2024-07-19 18:02:23 +02:00
};
handleConnectionChange = () => {
console . log ( "connection status:" , this . isConnected , this . address );
};
2024-07-19 18:00:01 +02:00
// messaging
2024-07-19 18:02:23 +02:00
sendChatMessage = ( chatMessage ) => {
2024-07-19 18:00:01 +02:00
const stringifiedBody = stringify ( chatMessage );
return this . udn . sendMessage ( chatMessage . channel , stringifiedBody );
2024-07-19 18:02:23 +02:00
};
2024-07-19 18:00:01 +02:00
// setup
2024-07-19 18:17:00 +02:00
constructor ( configuration ) {
2024-07-19 18:00:01 +02:00
this . udn = new UDNFrontend ();
2024-07-19 18:17:00 +02:00
this . udn . onmessage = ( data ) => {
configuration . messageHandler ( data );
};
this . udn . onconnect = () => {
this . handleConnectionChange ();
configuration . connectionChangeHandler ();
};
this . udn . ondisconnect = () => {
this . handleConnectionChange ();
configuration . connectionChangeHandler ();
};
2024-07-09 17:23:29 +02:00
}
};
2024-07-09 17:27:15 +02:00
2024-07-19 18:00:01 +02:00
// src/View/maintenance.tsx
2024-07-19 18:17:00 +02:00
var addressInput = restoreState ( "maintenance-address" , "" );
var connectedAddress = new State ( "" );
var isConnected = new State ( false );
var connectionModel = new ConnectionModel ({
connectionChangeHandler () {
isConnected . value = connectionModel . isConnected ;
connectedAddress . value = connectionModel . address ?? "---" ;
},
messageHandler ( data ) {
console . log ( data );
}
});
2024-07-19 18:00:01 +02:00
function connect () {
2024-07-19 18:17:00 +02:00
connectionModel . connect ( addressInput . value );
2024-07-12 21:11:33 +02:00
}
2024-07-19 18:00:01 +02:00
function disconnect () {
connectionModel . disconnect ();
2024-07-09 17:23:29 +02:00
}
2024-07-19 18:00:01 +02:00
document . querySelector ( "main" ). append (
2024-07-19 18:17:00 +02:00
/* @__PURE__ */ createElement ( "div" , { class : "flex-column" }, /* @__PURE__ */ createElement ( "input" , { "bind:value" : addressInput }), /* @__PURE__ */ createElement ( "button" , { "on:click" : connect , "toggle:disabled" : isConnected }, "Connect" ), /* @__PURE__ */ createElement ( "button" , { "on:click" : disconnect }, "Disconnect" ), /* @__PURE__ */ createElement ( "hr" , null ), /* @__PURE__ */ createElement ( "span" , { "subscribe:innerText" : connectedAddress }))
2024-07-19 18:00:01 +02:00
);
2024-07-07 13:43:18 +02:00
})();