Carbon/src/js/store/SubscribeMap.js

42 lines
806 B
JavaScript
Raw Normal View History

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 {
constructor() {
super()
Object.assign(this.events, {
addItem: [],
changeItem: [],
removeItem: []
})
this.map = new Map()
}
2020-10-15 03:43:37 +00:00
has(key) {
return this.map.has(key) && this.map.get(key).exists()
}
2020-10-15 03:43:37 +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
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
}
export {SubscribeMap}