[mod] before esm

This commit is contained in:
Andy Bunce 2025-07-17 15:31:04 +01:00
parent 3c60660f16
commit 8df5f011bc
8 changed files with 147 additions and 22 deletions

View file

@ -2,24 +2,24 @@
: Simple WebSocket chat. Utility functions.
: @author BaseX Team, BSD License
:)
module namespace chat-util = 'chat/util';
module namespace lsp-util = 'chat/util';
import module namespace session = 'http://basex.org/modules/session';
import module namespace ws = 'http://basex.org/modules/ws';
(:~ User id (bound to sessions and WebSockets). :)
declare variable $chat-util:id := 'id';
declare variable $lsp-util:id := 'id';
(:~
: Sends a users list (all, active) to all registered clients.
:)
declare function chat-util:users() as empty-sequence() {
declare function lsp-util:users() as empty-sequence() {
ws:emit(map{
'type': 'users',
'users': array { sort(user:list()) },
'active': array { distinct-values(
for $id in ws:ids()
return ws:get($id, $chat-util:id)
return ws:get($id, $lsp-util:id)
)}
})
};
@ -29,15 +29,15 @@ declare function chat-util:users() as empty-sequence() {
: @param $text text to be sent
: @param $to receiver of a private message (optional)
:)
declare function chat-util:message(
declare function lsp-util:message(
$text as xs:string,
$to as xs:string?
) as empty-sequence() {
let $ws-ids := ws:ids()[not($to) or ws:get(., $chat-util:id) = $to]
let $ws-ids := ws:ids()[not($to) or ws:get(., $lsp-util:id) = $to]
return ws:send(map{
'type': 'message',
'text': serialize($text),
'from': ws:get(ws:id(), $chat-util:id),
'from': ws:get(ws:id(), $lsp-util:id),
'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
'private': boolean($to)
}, $ws-ids)
@ -47,10 +47,10 @@ declare function chat-util:message(
: Closes all WebSocket connections from the specified user.
: @param $name username
:)
declare function chat-util:close(
declare function lsp-util:close(
$name as xs:string
) as empty-sequence() {
for $id in ws:ids()
where ws:get($id, $chat-util:id) = $name
where ws:get($id, $lsp-util:id) = $name
return ws:close($id)
};

View file

@ -2,19 +2,24 @@
: Simple WebSocket chat. WebSocket functions.
: @author BaseX Team, BSD License
:)
module namespace chat-ws = 'chat-ws';
module namespace lsp-ws = 'lsp-ws';
import module namespace chat-util = 'chat/util' at 'lsp-util.xqm';
declare
%ws:error('/lsp', '{$error}')
function lsp-ws:error($error) {
trace($error,"ERR ")
};
(:~
: Creates a WebSocket connection. Registers the user and notifies all clients.
:)
declare
%ws:connect('/lsp')
function chat-ws:connect() as empty-sequence() {
ws:set(ws:id()=>trace("CONNECT: "), $chat-util:id, session:get($chat-util:id)),
chat-util:users()
function lsp-ws:connect() as empty-sequence() {
ws:set(ws:id()=>trace("CONNECT: "), $chat-util:id, session:get($chat-util:id))
(: ,chat-util:users() :)
};
(:~
@ -23,10 +28,10 @@ function chat-ws:connect() as empty-sequence() {
:)
declare
%ws:message('/lsp', '{$message}')
function chat-ws:message(
function lsp-ws:message(
$message as xs:string
) as empty-sequence() {
let $json := parse-json($message)
let $json := parse-json($message=>trace("MSG "))
let $type := $json?type
return if($type = 'message') then (
chat-util:message($json?text, $json?to)
@ -40,7 +45,7 @@ function chat-ws:message(
:)
declare
%ws:close('/lsp')
function chat-ws:close() as empty-sequence() {
function lsp-ws:close() as empty-sequence() {
ws:delete(ws:id(), $chat-util:id),
chat-util:users()
};

View file

@ -0,0 +1,28 @@
import * as aceBuilds from 'https://esm.run/ace-builds';
import ace from 'https://cdn.jsdelivr.net/npm/ace/+esm'
/* import 'ace-builds/src-noconflict/mode-javascript';
import 'ace-builds/src-noconflict/theme-chrome'; */
/* import {AceLanguageClient} from "ace-linters/build/ace-language-client";
const serverData = {
module: () => import("ace-linters/build/language-client"),
modes: "json|json5",
type: "socket",
socket: new WebSocket("ws://127.0.0.1:3000/ws/lsp"), // your websocket server address
}
*/
// Initialize the editor
const editor = ace.edit("editor", {
theme: "ace/theme/chrome",
mode: "ace/mode/javascript",
fontSize: "14px",
showPrintMargin: false,
useWorker: false // Disable web worker for this simple demo
});
// Create a language provider for WebSocket
//let languageProvider = AceLanguageClient.for(serverData);
//languageProvider.registerEditor(editor);

View file

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>BaseX LSP</title>
<script src="https://www.unpkg.com/ace-builds@latest/src-noconflict/ace.js"></script>
<script src="https://www.unpkg.com/ace-builds@latest/src-noconflict/ext-language_tools.js"></script>
<script src="https://www.unpkg.com/ace-builds@latest/src-noconflict/ext-modelist.js"></script>
<!-- -->
<script src="https://www.unpkg.com/ace-linters@latest/build/ace-linters.js"></script>
<script src="https://www.unpkg.com/ace-linters@latest/build/ace-language-client.js"></script>
<script type="module" src="acego.js"></script>
</head>
<body>
<div>something<button onclick="foo()">send</button><a href="/dba/logs" target="_blank">dba</a></div>
<div id="editor" style="height: 100px">some text</div>
<script>
var modelist = ace.require('ace/ext/modelist');
if(modelist.modesByName['json'] == undefined) {
console.log("mode doesn't exist");
}
var servers = [
{
module: () => import("XXXXXace-linters/build/language-client"),
modes: "json",
type: "socket",
socket: new WebSocket("ws://127.0.0.1:3000/ws/lsp"),
}
];
let languageProvider = AceLanguageClient.for(servers);
ace.require("ace/ext/language_tools"); //To allow autocompletion
var editor = ace.edit("editor", {
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
mode: "json"
});
languageProvider.registerEditor(editor);
// editor.session.setMode("astro"); // mode now contains "ace/mode/javascript".
function foo(){
servers[0].socket.send("TTTTT")
alert("hi")
}
</script>
</body>
</html>