summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-09-17 23:32:46 +0000
committerAylur <[email protected]>2024-09-17 23:32:46 +0000
commit95c2658ce7b6539d0dff3cb88a3d4ca14c7d9b23 (patch)
tree75f78c44c7df4a8d1d7af1fd981dde54f833f670
parent1cceb88514e0b6fc12d84772073b1b592a0e5317 (diff)
style(gjs): binding runtime private properties
so people don't try to access the emitter
-rw-r--r--core/gjs/src/binding.ts41
1 files changed, 21 insertions, 20 deletions
diff --git a/core/gjs/src/binding.ts b/core/gjs/src/binding.ts
index feec6fc..95d905f 100644
--- a/core/gjs/src/binding.ts
+++ b/core/gjs/src/binding.ts
@@ -21,10 +21,11 @@ export interface Connectable {
}
export default class Binding<Value> {
- private emitter: Subscribable<Value> | Connectable
- private prop?: string
private transformFn = (v: any) => v
+ #emitter: Subscribable<Value> | Connectable
+ #prop?: string
+
static bind<
T extends Connectable,
P extends keyof T,
@@ -37,51 +38,51 @@ export default class Binding<Value> {
}
private constructor(emitter: Connectable | Subscribable<Value>, prop?: string) {
- this.emitter = emitter
- this.prop = prop && kebabify(prop)
+ this.#emitter = emitter
+ this.#prop = prop && kebabify(prop)
}
toString() {
- return `Binding<${this.emitter}${this.prop ? `, "${this.prop}"` : ""}>`
+ return `Binding<${this.#emitter}${this.#prop ? `, "${this.#prop}"` : ""}>`
}
as<T>(fn: (v: Value) => T): Binding<T> {
- const bind = new Binding(this.emitter, this.prop)
+ const bind = new Binding(this.#emitter, this.#prop)
bind.transformFn = (v: Value) => fn(this.transformFn(v))
return bind as unknown as Binding<T>
}
get(): Value {
- if (typeof this.emitter.get === "function")
- return this.transformFn(this.emitter.get())
+ if (typeof this.#emitter.get === "function")
+ return this.transformFn(this.#emitter.get())
- if (typeof this.prop === "string") {
- const getter = `get_${snakeify(this.prop)}`
- if (typeof this.emitter[getter] === "function")
- return this.transformFn(this.emitter[getter]())
+ if (typeof this.#prop === "string") {
+ const getter = `get_${snakeify(this.#prop)}`
+ if (typeof this.#emitter[getter] === "function")
+ return this.transformFn(this.#emitter[getter]())
- return this.transformFn(this.emitter[this.prop])
+ return this.transformFn(this.#emitter[this.#prop])
}
throw Error("can not get value of binding")
}
subscribe(callback: (value: Value) => void): () => void {
- if (typeof this.emitter.subscribe === "function") {
- return this.emitter.subscribe(() => {
+ if (typeof this.#emitter.subscribe === "function") {
+ return this.#emitter.subscribe(() => {
callback(this.get())
})
}
- else if (typeof this.emitter.connect === "function") {
- const signal = `notify::${this.prop}`
- const id = this.emitter.connect(signal, () => {
+ else if (typeof this.#emitter.connect === "function") {
+ const signal = `notify::${this.#prop}`
+ const id = this.#emitter.connect(signal, () => {
callback(this.get())
})
return () => {
- (this.emitter.disconnect as Connectable["disconnect"])(id)
+ (this.#emitter.disconnect as Connectable["disconnect"])(id)
}
}
- throw Error(`${this.emitter} is not bindable`)
+ throw Error(`${this.#emitter} is not bindable`)
}
}