2020-10-23 14:15:14 +00:00
|
|
|
const {Subscribable} = require("./subscribable.js")
|
2020-10-15 03:43:37 +00:00
|
|
|
|
|
|
|
class SubscribeValue extends Subscribable {
|
2020-10-19 11:43:33 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.hasData = false
|
|
|
|
this.data = null
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
exists() {
|
|
|
|
return this.hasData
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
value() {
|
|
|
|
if (this.hasData) return this.data
|
|
|
|
else return null
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
set(data) {
|
|
|
|
const exists = this.exists()
|
|
|
|
this.data = data
|
|
|
|
this.hasData = true
|
|
|
|
if (exists) {
|
|
|
|
this.broadcast("editSelf", this.data)
|
|
|
|
} else {
|
|
|
|
this.broadcast("addSelf", this.data)
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
edit(f) {
|
|
|
|
if (this.exists()) {
|
2020-11-08 12:11:28 +00:00
|
|
|
this.data = f(this.data)
|
2020-10-19 11:43:33 +00:00
|
|
|
this.set(this.data)
|
|
|
|
} else {
|
|
|
|
throw new Error("Tried to edit a SubscribeValue that had no value")
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
delete() {
|
|
|
|
this.hasData = false
|
|
|
|
this.broadcast("removeSelf")
|
|
|
|
return this
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 14:15:14 +00:00
|
|
|
module.exports = {SubscribeValue}
|