Skip to content

Bugfix for rare 'race condition' #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions packages/dalgard_viewmodel/lib/nexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -77,6 +77,8 @@ 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);
Expand All @@ -85,11 +87,26 @@ Nexus = class Nexus extends Base {
this.onDestroyed(this.unbind);

// Unbind element on computation invalidation
this.onInvalidate(() => this.unbind(true));

this.onInvalidate(() => {
// If the onReady bind has fired unbind the element immediately
if (view.viewModelReady) {
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);
this.onReady(() => {
this.bind();

// Set the viewModel to ready
view.viewModelReady = true;
});
}


Expand All @@ -111,7 +128,7 @@ Nexus = class Nexus extends Base {
// Compare with element
if (_.isElement(test))
return test === this.elem();

return super(test);
}

Expand Down Expand Up @@ -204,24 +221,23 @@ 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
this.view[ViewModel.nexusesKey].add(this);

Expand Down Expand Up @@ -250,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
Expand All @@ -259,7 +275,6 @@ Nexus = class Nexus extends Base {
binding.dispose.call(this.context, prop);
}


// Remove from global list
Nexus.remove(this);

Expand Down