2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {BaseObservableMap} from "./BaseObservableMap.js";
|
2019-03-16 00:59:17 +05:30
|
|
|
/*
|
2020-08-12 21:08:40 +05:30
|
|
|
so a mapped value can emit updates on it's own with this._emitSpontaneousUpdate that is passed in the mapping function
|
2019-03-16 00:59:17 +05:30
|
|
|
how should the mapped value be notified of an update though? and can it then decide to not propagate the update?
|
|
|
|
*/
|
2020-04-21 00:56:39 +05:30
|
|
|
export class MappedMap extends BaseObservableMap {
|
2019-02-27 01:18:57 +05:30
|
|
|
constructor(source, mapper) {
|
|
|
|
super();
|
|
|
|
this._source = source;
|
2019-02-21 04:18:16 +05:30
|
|
|
this._mapper = mapper;
|
|
|
|
this._mappedValues = new Map();
|
2020-08-12 21:08:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
_emitSpontaneousUpdate(key, params) {
|
|
|
|
const value = this._mappedValues.get(key);
|
|
|
|
if (value) {
|
|
|
|
this.emitUpdate(key, value, params);
|
|
|
|
}
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
onAdd(key, value) {
|
2020-08-12 21:08:40 +05:30
|
|
|
const emitSpontaneousUpdate = this._emitSpontaneousUpdate.bind(this, key);
|
|
|
|
const mappedValue = this._mapper(value, emitSpontaneousUpdate);
|
2019-02-21 04:18:16 +05:30
|
|
|
this._mappedValues.set(key, mappedValue);
|
|
|
|
this.emitAdd(key, mappedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRemove(key, _value) {
|
|
|
|
const mappedValue = this._mappedValues.get(key);
|
|
|
|
if (this._mappedValues.delete(key)) {
|
|
|
|
this.emitRemove(key, mappedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 01:18:57 +05:30
|
|
|
onUpdate(key, value, params) {
|
2019-02-21 04:18:16 +05:30
|
|
|
const mappedValue = this._mappedValues.get(key);
|
|
|
|
if (mappedValue !== undefined) {
|
2020-08-12 21:08:40 +05:30
|
|
|
// TODO: map params somehow if needed?
|
|
|
|
this.emitUpdate(key, mappedValue, params);
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubscribeFirst() {
|
2019-02-27 01:18:57 +05:30
|
|
|
this._subscription = this._source.subscribe(this);
|
2019-02-21 04:18:16 +05:30
|
|
|
for (let [key, value] of this._source) {
|
2020-08-12 21:08:40 +05:30
|
|
|
const emitSpontaneousUpdate = this._emitSpontaneousUpdate.bind(this, key);
|
|
|
|
const mappedValue = this._mapper(value, emitSpontaneousUpdate);
|
2019-02-21 04:18:16 +05:30
|
|
|
this._mappedValues.set(key, mappedValue);
|
|
|
|
}
|
|
|
|
super.onSubscribeFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
onUnsubscribeLast() {
|
2020-10-06 15:50:03 +05:30
|
|
|
super.onUnsubscribeLast();
|
2019-02-27 01:18:57 +05:30
|
|
|
this._subscription = this._subscription();
|
2019-02-21 04:18:16 +05:30
|
|
|
this._mappedValues.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
onReset() {
|
|
|
|
this._mappedValues.clear();
|
|
|
|
this.emitReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.iterator]() {
|
2019-02-28 03:20:08 +05:30
|
|
|
return this._mappedValues.entries();
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
2020-08-21 22:51:53 +05:30
|
|
|
|
|
|
|
get size() {
|
|
|
|
return this._mappedValues.size;
|
|
|
|
}
|
2020-10-12 22:01:55 +05:30
|
|
|
|
|
|
|
get(key) {
|
|
|
|
return this._mappedValues.get(key);
|
|
|
|
}
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|