From 51dffaad98846e76fad81d18a2ed75d0f42d5ef0 Mon Sep 17 00:00:00 2001 From: KoenLav <k.lavrijssen@gmail.com> Date: Wed, 16 Mar 2016 16:16:40 +0100 Subject: [PATCH 1/3] Bugfix for rare 'race condition' --- packages/dalgard_viewmodel/lib/nexus.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/dalgard_viewmodel/lib/nexus.js b/packages/dalgard_viewmodel/lib/nexus.js index 0f0922a..45e5e8b 100644 --- a/packages/dalgard_viewmodel/lib/nexus.js +++ b/packages/dalgard_viewmodel/lib/nexus.js @@ -77,6 +77,7 @@ Nexus = class Nexus extends Base { _isSetPrevented: { value: null, writable: true }, }); + var that = this; // Unbind element on view refreshed this.onRefreshed(this.unbind); @@ -85,11 +86,27 @@ Nexus = class Nexus extends Base { this.onDestroyed(this.unbind); // Unbind element on computation invalidation - this.onInvalidate(() => this.unbind(true)); + this.onInvalidate(function() { + if (that.view.viewModelReady) { + that.unbind(true); + // Set the viewModel to unready + view.viewModelReady = false; + } + else { + that.onReady(function () { + that.unbind(true) + }) + } + }); // Bind element on view ready - this.onReady(this.bind); + this.onReady(function() { + that.bind(); + + // Set the viewModel to ready + view.viewModelReady = true; + }); } @@ -221,7 +238,6 @@ Nexus = class Nexus extends Base { }); } - // Add to view list this.view[ViewModel.nexusesKey].add(this); @@ -259,7 +275,6 @@ Nexus = class Nexus extends Base { binding.dispose.call(this.context, prop); } - // Remove from global list Nexus.remove(this); From 0070d2144974055269bf5fd150980436f8af1dbb Mon Sep 17 00:00:00 2001 From: KoenLav <k.lavrijssen@gmail.com> Date: Wed, 16 Mar 2016 21:06:12 +0100 Subject: [PATCH 2/3] Removed var that = this; --- packages/dalgard_viewmodel/lib/nexus.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/dalgard_viewmodel/lib/nexus.js b/packages/dalgard_viewmodel/lib/nexus.js index 45e5e8b..9af54bc 100644 --- a/packages/dalgard_viewmodel/lib/nexus.js +++ b/packages/dalgard_viewmodel/lib/nexus.js @@ -77,8 +77,6 @@ Nexus = class Nexus extends Base { _isSetPrevented: { value: null, writable: true }, }); - var that = this; - // Unbind element on view refreshed this.onRefreshed(this.unbind); @@ -86,23 +84,23 @@ Nexus = class Nexus extends Base { this.onDestroyed(this.unbind); // Unbind element on computation invalidation - this.onInvalidate(function() { - if (that.view.viewModelReady) { - that.unbind(true); + this.onInvalidate(() => { + if (view.viewModelReady) { + this.unbind(true); // Set the viewModel to unready view.viewModelReady = false; } else { - that.onReady(function () { - that.unbind(true) + this.onReady(function () { + this.unbind(true) }) } }); // Bind element on view ready - this.onReady(function() { - that.bind(); + this.onReady(() => { + this.bind(); // Set the viewModel to ready view.viewModelReady = true; From 0f7e613c77aa65edaeb1ba32d6e365ba8c4f434a Mon Sep 17 00:00:00 2001 From: KoenLav <k.lavrijssen@gmail.com> Date: Wed, 16 Mar 2016 21:24:39 +0100 Subject: [PATCH 3/3] Removed var that = this and moved viewModelReady = false outside of onInvalidate and into the Nexus constructor --- packages/dalgard_viewmodel/lib/nexus.js | 56 +++++++++++++------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/packages/dalgard_viewmodel/lib/nexus.js b/packages/dalgard_viewmodel/lib/nexus.js index 9af54bc..1f59046 100644 --- a/packages/dalgard_viewmodel/lib/nexus.js +++ b/packages/dalgard_viewmodel/lib/nexus.js @@ -15,7 +15,7 @@ Nexus = class Nexus extends Base { let key = null; let vm = null; let prop = null; - + // Possibly get key if (!is_detached && _.isArray(context.args) && _.isString(context.args[0])) key = context.args[0]; @@ -77,6 +77,9 @@ Nexus = class Nexus extends Base { _isSetPrevented: { value: null, writable: true }, }); + // Set the viewModel to unready + view.viewModelReady = false; + // Unbind element on view refreshed this.onRefreshed(this.unbind); @@ -85,26 +88,25 @@ Nexus = class Nexus extends Base { // Unbind element on computation invalidation this.onInvalidate(() => { + // If the onReady bind has fired unbind the element immediately if (view.viewModelReady) { - this.unbind(true); - - // Set the viewModel to unready - view.viewModelReady = false; - } - else { - this.onReady(function () { - this.unbind(true) - }) - } - }); + this.unbind(true); + } + // Else add the unbind function to the onReady handler (to be executed after the onReady bind) + else { + this.onReady(function () { + this.unbind(true) + }) + } + }); // Bind element on view ready this.onReady(() => { this.bind(); - // Set the viewModel to ready - view.viewModelReady = true; - }); + // Set the viewModel to ready + view.viewModelReady = true; + }); } @@ -126,7 +128,7 @@ Nexus = class Nexus extends Base { // Compare with element if (_.isElement(test)) return test === this.elem(); - + return super(test); } @@ -219,21 +221,21 @@ Nexus = class Nexus extends Base { if (binding.set) { // Ensure type of definition property check(binding.set, Function); - + // Wrap set function and add it to list of autoruns this.autorun(comp => { if (comp.firstRun) { - // Save computation for unbind - defineProperties(this, { - comp: { value: comp }, - }); - } + // Save computation for unbind + defineProperties(this, { + comp: { value: comp }, + }); + } - const new_value = prop && prop(); + const new_value = prop && prop(); - if (!this.isSetPrevented()) - binding.set.call(this.context, elem, new_value); - }); + if (!this.isSetPrevented()) + binding.set.call(this.context, elem, new_value); + }); } // Add to view list @@ -264,7 +266,7 @@ Nexus = class Nexus extends Base { if (this.comp) this.comp.stop(); - + // Possibly run dispose function if (binding.dispose) { // Ensure type of definition property