-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
283 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
export class TimeoutError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = "TimeoutError"; | ||
} | ||
} | ||
|
||
export function promiseTask(object, method, finish, ...args) { | ||
return new Promise((resolve, reject) => { | ||
object[method](...args, (self, asyncResult) => { | ||
try { | ||
resolve(object[finish](asyncResult)); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function noop() {} | ||
|
||
export class Deferred extends Promise { | ||
constructor(def = noop) { | ||
let res, rej; | ||
super((resolve, reject) => { | ||
def(resolve, reject); | ||
res = resolve; | ||
rej = reject; | ||
}); | ||
this.resolve = res; | ||
this.reject = rej; | ||
} | ||
} | ||
|
||
function delay(ms) { | ||
let timeout; | ||
const promise = new Promise((resolve) => { | ||
timeout = setTimeout(() => { | ||
resolve(); | ||
}, ms); | ||
}); | ||
promise.timeout = timeout; | ||
return promise; | ||
} | ||
|
||
export function once( | ||
object, | ||
signal, | ||
options = { | ||
error: "", | ||
timeout: -1, | ||
}, | ||
) { | ||
let promise; | ||
if (object.connect && object.disconnect) { | ||
promise = promiseSignal(object, signal, options.error); | ||
} else { | ||
promise = promiseEvent(object, signal, options.error); | ||
} | ||
|
||
if (options.timeout < 0) { | ||
return promise; | ||
} | ||
|
||
const promise_timeout = timeout(options.timeout); | ||
return Promise.race([promise, promise_timeout]).finally(() => { | ||
clearTimeout(promise_timeout.timeout_id); | ||
}); | ||
} | ||
|
||
function promiseEvent(object, signal, error_signal) { | ||
const { addListener, removeListener } = normalizeEmitter(object); | ||
|
||
return new Promise((resolve, reject) => { | ||
addListener(signal, listener); | ||
|
||
function cleanup() { | ||
removeListener(signal, listener); | ||
if (error_signal) removeListener(error_signal, error_listener); | ||
} | ||
|
||
if (error_signal) { | ||
addListener(error_signal, error_listener); | ||
} | ||
|
||
function error_listener(err) { | ||
cleanup(); | ||
reject(err); | ||
} | ||
|
||
function listener(...params) { | ||
cleanup(); | ||
resolve(params); | ||
} | ||
}); | ||
} | ||
|
||
function promiseSignal(object, signal, error_signal) { | ||
return new Promise((resolve, reject) => { | ||
const handler_id = object.connect(signal, handler); | ||
let error_handler_id; | ||
|
||
function cleanup() { | ||
object.disconnect(handler_id); | ||
if (error_handler_id) object.disconnect(error_handler_id); | ||
} | ||
|
||
if (error_signal) { | ||
error_handler_id = object.connect(error_signal, (self, error) => { | ||
cleanup(); | ||
reject(error); | ||
}); | ||
} | ||
|
||
function handler(self, ...params) { | ||
cleanup(); | ||
resolve(params); | ||
} | ||
}); | ||
} | ||
|
||
function normalizeEmitter(emitter) { | ||
const addListener = | ||
emitter.on || emitter.addListener || emitter.addEventListener; | ||
const removeListener = | ||
emitter.off || emitter.removeListener || emitter.removeEventListener; | ||
|
||
if (!addListener || !removeListener) { | ||
throw new TypeError("Emitter is not compatible"); | ||
} | ||
|
||
return { | ||
addListener: addListener.bind(emitter), | ||
removeListener: removeListener.bind(emitter), | ||
}; | ||
} | ||
|
||
function timeout(ms) { | ||
return delay(ms).then(() => { | ||
throw new TimeoutError(`Promise timed out after ${ms} milliseconds`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import GObject from "gi://GObject"; | ||
import Gtk from "gi://Gtk"; | ||
import GLib from "gi://GLib"; | ||
|
||
function noop() {} | ||
|
||
const BuilderScope = GObject.registerClass( | ||
{ | ||
Implements: [Gtk.BuilderScope], | ||
}, | ||
class BuilderScope extends GObject.Object { | ||
constructor() { | ||
super(); | ||
this.signal_handlers = Object.create(null); | ||
} | ||
|
||
add_signal_handler(name, fn) { | ||
this.signal_handlers[name] = fn; | ||
} | ||
|
||
// https://docs.gtk.org/gtk4/vfunc.BuilderScope.create_closure.html | ||
vfunc_create_closure(_builder, function_name, flags, _object) { | ||
if (flags & Gtk.BuilderClosureFlags.SWAPPED) { | ||
throw new Error('Signal flag "swapped" is unsupported in JavaScript.'); | ||
} | ||
|
||
const fn = this.signal_handlers[function_name]; | ||
// if (!fn) { | ||
// throw new Error(`No function named \`${function_name}\`.`); | ||
// } | ||
return fn || noop; | ||
} | ||
}, | ||
); | ||
|
||
export function build(uri, params = {}) { | ||
const builder = new Gtk.Builder(); | ||
|
||
const scope = new BuilderScope(); | ||
builder.set_scope(scope); | ||
|
||
for (const [k, v] of Object.entries(params)) { | ||
// signals | ||
if (typeof v === "function") { | ||
scope.add_signal_handler(k, v); | ||
// objects | ||
} else if (v instanceof GObject.Object) { | ||
builder.expose_object(k, v); | ||
} else { | ||
throw new Error("Only function and GObject properties are supported."); | ||
} | ||
} | ||
|
||
const handler = { | ||
get(target, prop, receiver) { | ||
return builder.get_object(prop); | ||
}, | ||
}; | ||
|
||
const proxy = new Proxy({}, handler); | ||
|
||
const g_uri = GLib.Uri.parse(uri, GLib.UriFlags.NONE); | ||
const scheme = g_uri.get_scheme(); | ||
const path = g_uri.get_path(); | ||
if (!scheme || !path) { | ||
throw new Error(`Invalud uri \`${uri}\`.`); | ||
} | ||
|
||
if (scheme === "resource") { | ||
builder.add_from_resource(path); | ||
} else if (scheme === "file") { | ||
builder.add_from_file(path); | ||
} else { | ||
throw new Error(`Unsuported scheme \`${uri}\`.`); | ||
} | ||
|
||
return proxy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import gsx from "./gsx.js"; | ||
import { | ||
resolve, | ||
parseResolve, | ||
getPid, | ||
getGjsVersion, | ||
getGLibVersion, | ||
getGIRepositoryVersion, | ||
} from "./util.js"; | ||
import { build } from "./builder.js"; | ||
import { promiseTask } from "./async.js"; | ||
|
||
export { | ||
gsx, | ||
build, | ||
resolve, | ||
parseResolve, | ||
getPid, | ||
getGjsVersion, | ||
getGLibVersion, | ||
getGIRepositoryVersion, | ||
promiseTask, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.