2021-02-22 20:27:58 +00:00
|
|
|
import {
|
|
|
|
store,
|
|
|
|
Sib,
|
|
|
|
StoreMixin
|
2021-05-11 08:50:17 +00:00
|
|
|
} from 'https://cdn.skypack.dev/@startinblox/core@0.17';
|
2020-06-18 18:02:54 +00:00
|
|
|
|
|
|
|
export const HublReactivity = {
|
|
|
|
name: 'hubl-reactivity',
|
|
|
|
use: [StoreMixin],
|
|
|
|
attributes: {
|
|
|
|
targetSrc: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
callback: function () {
|
|
|
|
this.subscribe();
|
|
|
|
}
|
2021-05-20 14:01:13 +00:00
|
|
|
}
|
2020-12-02 09:35:21 +00:00
|
|
|
},
|
|
|
|
async fetchData(value) {
|
|
|
|
this.resourceId = null;
|
|
|
|
if (this.nestedField) {
|
|
|
|
const resource = store.get(value) || await store.getData(value, this.context);
|
|
|
|
const nestedResource = await resource[this.nestedField]
|
|
|
|
this.resourceId = nestedResource ? nestedResource['@id'] : null;
|
|
|
|
} else {
|
|
|
|
this.resourceId = value;
|
|
|
|
}
|
|
|
|
this.subscribe();
|
2020-06-18 18:02:54 +00:00
|
|
|
},
|
2021-05-20 14:01:13 +00:00
|
|
|
unsubscribe(resourceId, targetSrc) {
|
|
|
|
const resourcesSub = store.subscriptionVirtualContainersIndex.get(resourceId);
|
|
|
|
const targetSub = store.subscriptionVirtualContainersIndex.get(targetSrc);
|
|
|
|
const newResourceSub = resourcesSub.filter(r => r !== targetSrc);
|
|
|
|
const newTargetSub = targetSub.filter(r => r !== resourceId);
|
|
|
|
store.subscriptionVirtualContainersIndex.set(resourceId, newResourceSub);
|
|
|
|
store.subscriptionVirtualContainersIndex.set(targetSrc, newTargetSub);
|
|
|
|
},
|
|
|
|
detached() {
|
|
|
|
this.unsubscribe(this.resourceId, this.targetSrc)
|
|
|
|
},
|
2020-06-18 18:02:54 +00:00
|
|
|
subscribe() {
|
2021-05-20 14:01:13 +00:00
|
|
|
if (this.oldResourceId && this.oldTargetSrc && (this.oldResourceId !== this.resourceId || this.oldTargetSrc !== this.targetSrc)) {
|
|
|
|
this.unsubscribe(this.oldResourceId, this.oldTargetSrc);
|
|
|
|
}
|
|
|
|
if (this.resourceId && this.targetSrc) {
|
2020-06-18 18:02:54 +00:00
|
|
|
store.subscribeVirtualContainerTo(this.resourceId, this.targetSrc);
|
2021-05-20 14:01:13 +00:00
|
|
|
store.subscribeVirtualContainerTo(this.targetSrc, this.resourceId);
|
|
|
|
this.oldResourceId = this.resourceId;
|
|
|
|
this.oldTargetSrc = this.targetSrc;
|
2020-06-18 18:02:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sib.register(HublReactivity);
|