2020-10-20 10:25:04 +00:00
|
|
|
import {Subscribable} from $to_relative "/js/store/Subscribable.js"
|
|
|
|
import {SubscribeValue} from $to_relative "/js/store/SubscribeValue.js"
|
2020-10-15 03:43:37 +00:00
|
|
|
|
|
|
|
class SubscribeMap extends Subscribable {
|
2020-10-19 11:43:33 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
Object.assign(this.events, {
|
|
|
|
addItem: [],
|
|
|
|
changeItem: [],
|
|
|
|
removeItem: []
|
|
|
|
})
|
|
|
|
this.map = new Map()
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
has(key) {
|
|
|
|
return this.map.has(key) && this.map.get(key).exists()
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
get(key) {
|
|
|
|
if (this.map.has(key)) {
|
|
|
|
return this.map.get(key)
|
|
|
|
} else {
|
|
|
|
this.map.set(key, new SubscribeValue())
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
set(key, value) {
|
|
|
|
let s
|
|
|
|
if (this.map.has(key)) {
|
|
|
|
s = this.map.get(key).set(value)
|
|
|
|
this.broadcast("changeItem", key)
|
|
|
|
} else {
|
|
|
|
s = new SubscribeValue().set(value)
|
|
|
|
this.map.set(key, s)
|
|
|
|
this.broadcast("addItem", key)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2020-10-15 03:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 11:43:33 +00:00
|
|
|
export {SubscribeMap}
|