-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also move gobject.object overrides to new file allowing to construct objects by extending
- Loading branch information
Showing
5 changed files
with
116 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// deno-lint-ignore-file no-explicit-any | ||
import { GConnectFlags } from "../bindings/enums.js"; | ||
import g from "../bindings/mod.js"; | ||
import { createCallback } from "../types/callback.js"; | ||
|
||
type Handler = (...args: unknown[]) => unknown; | ||
|
||
function addObjectMethods(object: any) { | ||
object.prototype.connect = function ( | ||
action: string, | ||
callback: Handler, | ||
) { | ||
const signalInfo = Reflect.getMetadata( | ||
"gi:signals", | ||
this.constructor, | ||
action.split("::")[0], | ||
); | ||
|
||
const cb = createCallback(signalInfo, callback, this); | ||
const handler = g.signal.connect_data( | ||
Reflect.getOwnMetadata("gi:ref", this), | ||
action, | ||
cb.pointer, | ||
null, | ||
null, | ||
GConnectFlags.SWAPPED, | ||
); | ||
|
||
return handler; | ||
}; | ||
|
||
object.prototype.on = function ( | ||
action: string, | ||
callback: Handler, | ||
) { | ||
return this.connect(action, callback); | ||
}; | ||
|
||
object.prototype.once = function ( | ||
action: string, | ||
callback: Handler, | ||
) { | ||
const handler = this.connect(action, (...args: unknown[]) => { | ||
callback(...args); | ||
this.off(handler); | ||
}); | ||
|
||
return handler; | ||
}; | ||
|
||
object.prototype.off = function (handler: Handler) { | ||
g.signal.handler_disconnect( | ||
Reflect.getOwnMetadata("gi:ref", this), | ||
handler as any, | ||
); | ||
}; | ||
|
||
object.prototype.emit = function (action: string) { | ||
g.signal.emit_by_name( | ||
Reflect.getOwnMetadata("gi:ref", this), | ||
action, | ||
); | ||
}; | ||
} | ||
|
||
export function _init(GObject: any) { | ||
addObjectMethods(GObject.Object); | ||
} |
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,22 @@ | ||
import * as GObject from "./GObject.ts"; | ||
|
||
export interface GIOverride { | ||
name: string; | ||
version: string; | ||
// deno-lint-ignore no-explicit-any | ||
module: any; | ||
} | ||
|
||
export const overrides: GIOverride[] = [ | ||
{ name: "GObject", version: "2.0", module: GObject }, | ||
]; | ||
|
||
export function loadOverride(namespace: string, repo: unknown) { | ||
const override = overrides.find((override) => { | ||
return override.name === namespace; | ||
}); | ||
|
||
if (override) { | ||
override.module._init(repo); | ||
} | ||
} |
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