From d113148053ddec062141333a443ef84704fb6883 Mon Sep 17 00:00:00 2001 From: Matthieu Fesselier Date: Thu, 20 May 2021 16:01:13 +0200 Subject: [PATCH] update: unsubscribe subscriptions --- src/components/hubl-reactivity.js | 26 ++++++++++++++----- .../admin/page-admin-circles-join.pug | 4 +-- .../admin/page-admin-circles-leave.pug | 8 +++--- .../partials/admin/page-admin-projects.pug | 12 ++++----- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/components/hubl-reactivity.js b/src/components/hubl-reactivity.js index 001983c..0d8a883 100644 --- a/src/components/hubl-reactivity.js +++ b/src/components/hubl-reactivity.js @@ -14,10 +14,7 @@ export const HublReactivity = { callback: function () { this.subscribe(); } - }, - refreshData: { - type: String, - }, + } }, async fetchData(value) { this.resourceId = null; @@ -30,11 +27,26 @@ export const HublReactivity = { } this.subscribe(); }, + 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) + }, subscribe() { - if (this.refreshData === '') { // clears cache and re-fetch data + if (this.oldResourceId && this.oldTargetSrc && (this.oldResourceId !== this.resourceId || this.oldTargetSrc !== this.targetSrc)) { + this.unsubscribe(this.oldResourceId, this.oldTargetSrc); + } + if (this.resourceId && this.targetSrc) { store.subscribeVirtualContainerTo(this.resourceId, this.targetSrc); - } else { // notifies components - store.subscribeResourceTo(this.resourceId, this.targetSrc); + store.subscribeVirtualContainerTo(this.targetSrc, this.resourceId); + this.oldResourceId = this.resourceId; + this.oldTargetSrc = this.targetSrc; } } } diff --git a/src/views/partials/admin/page-admin-circles-join.pug b/src/views/partials/admin/page-admin-circles-join.pug index f0dd4e4..ced26ac 100644 --- a/src/views/partials/admin/page-admin-circles-join.pug +++ b/src/views/partials/admin/page-admin-circles-join.pug @@ -16,8 +16,8 @@ solid-widget(name=`hubl-admin-circle-join-button`) hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}` target-src='${value}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}joinable/` target-src='${value}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}` target-src='${value}') - hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${value}' refresh-data) - hubl-reactivity(bind-user nested-field="circles" target-src='${value}' refresh-data) + hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${value}') + hubl-reactivity(bind-user nested-field="circles" target-src='${value}') include page-admin-circles.pug diff --git a/src/views/partials/admin/page-admin-circles-leave.pug b/src/views/partials/admin/page-admin-circles-leave.pug index 749dd30..43a75b2 100644 --- a/src/views/partials/admin/page-admin-circles-leave.pug +++ b/src/views/partials/admin/page-admin-circles-leave.pug @@ -3,8 +3,8 @@ solid-widget(name=`leave-circle-reactivity`) hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}` target-src='${value}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}joinable/` target-src='${value}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}` target-src='${value}') - hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${value}' refresh-data) - hubl-reactivity(bind-user nested-field="circles" target-src='${value}' refresh-data) + hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${value}') + hubl-reactivity(bind-user nested-field="circles" target-src='${value}') solid-widget(name=`hubl-admin-circle-leave-button`) template @@ -17,8 +17,8 @@ solid-widget(name=`hubl-admin-circle-leave-button`) hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}` target-src='${src}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.get}joinable/` target-src='${src}') hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}` target-src='${src}') - hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${src}' refresh-data) - hubl-reactivity(bind-user nested-field="circles" target-src='${src}' refresh-data) + hubl-reactivity(data-src=`${getComponent('circles').endpoints.post}joinable/` target-src='${src}') + hubl-reactivity(bind-user nested-field="circles" target-src='${src}') solid-display( data-src="${src}" fields="circle" diff --git a/src/views/partials/admin/page-admin-projects.pug b/src/views/partials/admin/page-admin-projects.pug index a3db9d8..def6e0c 100644 --- a/src/views/partials/admin/page-admin-projects.pug +++ b/src/views/partials/admin/page-admin-projects.pug @@ -43,8 +43,8 @@ div.segment.full.padding-large.sm-padding-xsmall.sm-padding-top-medium.whitespac hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}` target-src='${value}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}joinable/` target-src='${value}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}` target-src='${value}') - hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${value}' refresh-data) - hubl-reactivity(bind-user nested-field="projects" target-src='${value}' refresh-data) + hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${value}') + hubl-reactivity(bind-user nested-field="projects" target-src='${value}') solid-widget(name=`hubl-admin-project-leave-button`) template @@ -57,8 +57,8 @@ div.segment.full.padding-large.sm-padding-xsmall.sm-padding-top-medium.whitespac hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}` target-src='${src}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}joinable/` target-src='${src}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}` target-src='${src}') - hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${src}' refresh-data) - hubl-reactivity(bind-user nested-field="projects" target-src='${src}' refresh-data) + hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${src}') + hubl-reactivity(bind-user nested-field="projects" target-src='${src}') solid-display( data-src="${src}" fields="project" @@ -122,8 +122,8 @@ div.segment.full.padding-large.sm-padding-xsmall.sm-padding-top-medium.whitespac hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}` target-src='${value}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.get}joinable/` target-src='${value}') hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}` target-src='${value}') - hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${value}' refresh-data) - hubl-reactivity(bind-user nested-field="projects" target-src='${value}' refresh-data) + hubl-reactivity(data-src=`${getComponent('projects').endpoints.post}joinable/` target-src='${value}') + hubl-reactivity(bind-user nested-field="projects" target-src='${value}') solid-display( class='table-body'