summaryrefslogtreecommitdiff
path: root/js/gjs/application.ts
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-05-19 02:39:53 +0200
committerAylur <[email protected]>2024-05-19 02:39:53 +0200
commit1425b396b08f0e91d45bbd0f92b1309115c7c870 (patch)
tree8af1a899a14d8a01a9ef50e248c077b48aed25bc /js/gjs/application.ts
init 0.1.0
Diffstat (limited to 'js/gjs/application.ts')
-rw-r--r--js/gjs/application.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/js/gjs/application.ts b/js/gjs/application.ts
new file mode 100644
index 0000000..32dcda7
--- /dev/null
+++ b/js/gjs/application.ts
@@ -0,0 +1,51 @@
+import Astal from "gi://Astal"
+import GObject from "gi://GObject"
+import Gio from "gi://Gio"
+import { RequestHandler, Config, runJS } from "../src/application.js"
+
+// @ts-expect-error missing types
+// https://github.com/gjsify/ts-for-gir/issues/164
+import { setConsoleLogDomain } from "console"
+import { exit, programArgs } from "system"
+
+class AstalJS extends Astal.Application {
+ static { GObject.registerClass(this) }
+
+ eval = runJS
+ requestHandler?: RequestHandler
+
+ vfunc_response(msg: string, conn: Gio.SocketConnection): void {
+ if (typeof this.requestHandler === "function") {
+ this.requestHandler(msg, response => {
+ Astal.write_sock(conn, response, (_, res) =>
+ Astal.write_sock_finish(res),
+ )
+ })
+ } else {
+ super.vfunc_response(msg, conn)
+ }
+ }
+
+ start({ requestHandler, css, hold, ...cfg }: Config = {}, callback?: (args: string[]) => void) {
+ Object.assign(this, cfg)
+ setConsoleLogDomain(this.instanceName)
+
+ this.requestHandler = requestHandler
+ this.connect("activate", () => callback?.(programArgs))
+ if (!this.acquire_socket()) {
+ print(`Astal instance "${this.instanceName}" already running`)
+ exit(1)
+ }
+
+ if (css)
+ this.apply_css(css, false)
+
+ hold ??= true
+ if (hold)
+ this.hold()
+
+ this.runAsync([])
+ }
+}
+
+export const App = new AstalJS