Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Fixed bug when constraint values are set to other constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
soney committed Jun 5, 2014
1 parent 5ab483e commit e178873
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 120 deletions.
176 changes: 88 additions & 88 deletions api/index.html

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions build/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ var Constraint, // Declare here, will be defined later
} else {
return new Constraint(arg0, arg1);
}
},
get_constraint_val = function(x) {
return is_constraint(x) ? x.get() : x;
};

// Constraint Solver
Expand Down Expand Up @@ -811,7 +814,7 @@ var constraint_solver = {
// set this to the node's cached value, which will be returned
node._cached_value = node._options.literal ? node._value :
(isFunction(node._value) ? node._value.call(node._options.context || node, node) :
cjs.get(node._value));
get_constraint_val(node._value));

// The node paused as if this was going to be an asyncronous value but it ended up being syncronous.
// Use that to set the value
Expand Down Expand Up @@ -1191,7 +1194,7 @@ Constraint = function (value, options) {
this._value = value;

// If it's a value
if (this._options.literal || !isFunction(value)) {
if (this._options.literal || (!isFunction(value) && !is_constraint(value))) {
// Then use the specified equality check
var equality_check = this._options.equal || eqeqeq;
if(!equality_check(old_value, value)) {
Expand Down Expand Up @@ -3004,7 +3007,7 @@ MapConstraint = function (options) {
/** @lends cjs.MapConstraint.prototype */

// Utility function to return information about a key
var _find_key = function (key, fetch_unsubstantiated, create_unsubstantiated) {
var _find_key = function (key, fetch_unsubstantiated, create_unsubstantiated, literal) {
// Get the hash
var hash = this._hash(key),
rv = {
Expand Down Expand Up @@ -3032,8 +3035,9 @@ MapConstraint = function (options) {
// Haven't returned yet, so we didn't find the entry. Look for an unsubstantiated
// value instead.
if (fetch_unsubstantiated !== false) { //Not found
var unsubstantiated_values = this._unsubstantiated_values[hash];
var unsubstantiated_index = -1;
var unsubstantiated_values = this._unsubstantiated_values[hash],
unsubstantiated_index = -1;

if (unsubstantiated_values) {
rv.uhv = unsubstantiated_values;
unsubstantiated_index = indexWhere(unsubstantiated_values, index_where_fn);
Expand All @@ -3046,12 +3050,12 @@ MapConstraint = function (options) {
// We haven't returned yet, so we didn't find an unsubstantiated value either
// Check to see if we should create one.
if(create_unsubstantiated === true) {
var is_literal = this._default_literal_values;
var unsubstantiated_info = {
key: new Constraint(key, {literal: true}),
value: new Constraint(undefined, {literal: is_literal}), // will be undefined
index: new Constraint(-1, {literal: true}) // with a negative index
};
var is_literal = this._default_literal_values,
unsubstantiated_info = {
key: new Constraint(key, {literal: true}),
value: new Constraint(undefined, {literal: literal === undefined ? this._default_literal_values : !!literal}), // will be undefined
index: new Constraint(-1, {literal: true}) // with a negative index
};

if(unsubstantiated_values) { // The hash was found but not the particular value
// Add it onto the end
Expand Down Expand Up @@ -3302,7 +3306,7 @@ MapConstraint = function (options) {
proto.put = function (key, value, index, literal) {
cjs.wait();
// Find out if there's a key or unsubstantiated info but don't create it
var ki = _find_key.call(this, key, true, false);
var ki = _find_key.call(this, key, true, false, literal);
// And do the work of putting
_do_set_item_ki.call(this, ki, key, value, index, literal);
cjs.signal();
Expand Down Expand Up @@ -3715,7 +3719,7 @@ MapConstraint = function (options) {
* // 3
*/
proto.getOrPut = function (key, create_fn, create_fn_context, index, literal) {
var ki = _find_key.call(this, key, true, false),
var ki = _find_key.call(this, key, true, false, literal),
key_index = ki.i, // index within hash array
hash_values = ki.hv, // hash array
hash = ki.h, // hash value
Expand Down
4 changes: 2 additions & 2 deletions build/cjs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/cjs.min.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ var Constraint, // Declare here, will be defined later
} else {
return new Constraint(arg0, arg1);
}
},
get_constraint_val = function(x) {
return is_constraint(x) ? x.get() : x;
};

// Constraint Solver
Expand Down Expand Up @@ -166,7 +169,7 @@ var constraint_solver = {
// set this to the node's cached value, which will be returned
node._cached_value = node._options.literal ? node._value :
(isFunction(node._value) ? node._value.call(node._options.context || node, node) :
cjs.get(node._value));
get_constraint_val(node._value));

// The node paused as if this was going to be an asyncronous value but it ended up being syncronous.
// Use that to set the value
Expand Down Expand Up @@ -546,7 +549,7 @@ Constraint = function (value, options) {
this._value = value;

// If it's a value
if (this._options.literal || !isFunction(value)) {
if (this._options.literal || (!isFunction(value) && !is_constraint(value))) {
// Then use the specified equality check
var equality_check = this._options.equal || eqeqeq;
if(!equality_check(old_value, value)) {
Expand Down
23 changes: 12 additions & 11 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ MapConstraint = function (options) {
/** @lends cjs.MapConstraint.prototype */

// Utility function to return information about a key
var _find_key = function (key, fetch_unsubstantiated, create_unsubstantiated) {
var _find_key = function (key, fetch_unsubstantiated, create_unsubstantiated, literal) {
// Get the hash
var hash = this._hash(key),
rv = {
Expand Down Expand Up @@ -187,8 +187,9 @@ MapConstraint = function (options) {
// Haven't returned yet, so we didn't find the entry. Look for an unsubstantiated
// value instead.
if (fetch_unsubstantiated !== false) { //Not found
var unsubstantiated_values = this._unsubstantiated_values[hash];
var unsubstantiated_index = -1;
var unsubstantiated_values = this._unsubstantiated_values[hash],
unsubstantiated_index = -1;

if (unsubstantiated_values) {
rv.uhv = unsubstantiated_values;
unsubstantiated_index = indexWhere(unsubstantiated_values, index_where_fn);
Expand All @@ -201,12 +202,12 @@ MapConstraint = function (options) {
// We haven't returned yet, so we didn't find an unsubstantiated value either
// Check to see if we should create one.
if(create_unsubstantiated === true) {
var is_literal = this._default_literal_values;
var unsubstantiated_info = {
key: new Constraint(key, {literal: true}),
value: new Constraint(undefined, {literal: is_literal}), // will be undefined
index: new Constraint(-1, {literal: true}) // with a negative index
};
var is_literal = this._default_literal_values,
unsubstantiated_info = {
key: new Constraint(key, {literal: true}),
value: new Constraint(undefined, {literal: literal === undefined ? this._default_literal_values : !!literal}), // will be undefined
index: new Constraint(-1, {literal: true}) // with a negative index
};

if(unsubstantiated_values) { // The hash was found but not the particular value
// Add it onto the end
Expand Down Expand Up @@ -457,7 +458,7 @@ MapConstraint = function (options) {
proto.put = function (key, value, index, literal) {
cjs.wait();
// Find out if there's a key or unsubstantiated info but don't create it
var ki = _find_key.call(this, key, true, false);
var ki = _find_key.call(this, key, true, false, literal);
// And do the work of putting
_do_set_item_ki.call(this, ki, key, value, index, literal);
cjs.signal();
Expand Down Expand Up @@ -870,7 +871,7 @@ MapConstraint = function (options) {
* // 3
*/
proto.getOrPut = function (key, create_fn, create_fn_context, index, literal) {
var ki = _find_key.call(this, key, true, false),
var ki = _find_key.call(this, key, true, false, literal),
key_index = ki.i, // index within hash array
hash_values = ki.hv, // hash array
hash = ki.h, // hash value
Expand Down
27 changes: 24 additions & 3 deletions test/unit_tests/map_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,39 @@ dt("Item values are constraints", 5, function() {
m = ma = null;
});

dt("Maps and maps and maps", 2, function() {
dt("Maps and maps and maps", 4, function() {
var m = cjs({})
sub_m = cjs({}),
sub_m_2 = {};
sub_m_2 = cjs({}),
sub_m_3 = {};

ok(!m.has("sub"));
m.put("sub", sub_m);
m.put("sub2", sub_m_2);
m.put("sub3", sub_m_3);
equal(m.get("sub"), sub_m);
equal(m.get("sub2"), sub_m_2);
equal(m.get("sub3"), sub_m_3);

sub_m.put("x", 1);

m.destroy();
sub_m.destroy();
m = sub_m = null;
sub_m_2.destroy();
m = sub_m = sub_m_2 = sub_m_3 = null;
});

dt("Constraints whose value is a map", 1, function() {
var m = cjs({}),
//x = new cjs.Constraint(m),
y = new cjs.Constraint();

//equal(x.get(), m);
y.set(m);
equal(y.get(), m);

m.destroy();
//x.destroy();
y.destroy();
m = /*x =*/ y = null;
});

0 comments on commit e178873

Please sign in to comment.