small improvement on code for lazy updates, fixed dissappearing label bug
This commit is contained in:
parent
b36b909073
commit
4929d12d33
2 changed files with 58 additions and 27 deletions
|
|
@ -36,24 +36,51 @@ const show = function (vector) {
|
||||||
return {description: `vector@${vector.id} is visible`};
|
return {description: `vector@${vector.id} is visible`};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const update_lazy_objects = function () {
|
export const update_visible_objects = function () {
|
||||||
let b = Object.values(bindings); // a lazy expression must be bound
|
Object.entries(bindings).forEach(entry => { // a lazy expression must be bound
|
||||||
for (let i = 0; i < b.length; i++) { // unbound ones can exist, but are meaningless, because you cannot refer to them
|
const [name, binding] = entry;
|
||||||
let binding = b[i];
|
let value = state[binding.name];
|
||||||
if (state[binding.name].lazy_expression) { // if lazy,
|
if (value.lazy_expression) {
|
||||||
let value = visit(state[binding.name].lazy_expression); // reevaluate,
|
let new_value = visit(value.lazy_expression); // reevaluate,
|
||||||
let existing_value = bindings[binding.name].evaluated; // update view
|
let existing_value = binding.evaluated; // update view
|
||||||
|
if (new_value.is_vector || existing_value.is_vector) {
|
||||||
|
bindings[name].evaluated = new_value;
|
||||||
|
new_value.label_text = existing_value.label_text; // SAD
|
||||||
|
update_vector(existing_value, new_value, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Object.entries(references).forEach(entry => {
|
||||||
|
const [id, value] = entry;
|
||||||
|
if (value.lazy_expression) {
|
||||||
|
let new_value = visit(value.lazy_expression);
|
||||||
|
let existing_value = value;
|
||||||
|
if (new_value.is_vector || existing_value.is_vector) {
|
||||||
|
new_value.lazy_expression = value.lazy_expression;
|
||||||
|
references[id] = new_value;
|
||||||
if (existing_value && existing_value.id) {
|
if (existing_value && existing_value.id) {
|
||||||
// update view after reevaluation of lazy vectors
|
// update view after reevaluation of lazy vectors
|
||||||
update_vector_arrow(existing_value.id, value);
|
update_vector_arrow(existing_value.id, new_value);
|
||||||
bindings[binding.name].evaluated = value;
|
} else if (new_value.is_new && new_value.is_vector) {
|
||||||
} else if (value.is_new && value.is_vector){
|
|
||||||
// hidden lazy vector reappears
|
// hidden lazy vector reappears
|
||||||
value.label_text=binding.name;
|
add_vector_arrow_to_svg(new_value);
|
||||||
add_vector_arrow_to_svg(value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_vector(existing_value, new_value, binding_name) {
|
||||||
|
if (existing_value && existing_value.id) {
|
||||||
|
// update view after reevaluation of lazy vectors
|
||||||
|
update_vector_arrow(existing_value.id, new_value);
|
||||||
|
} else if (new_value.is_new && new_value.is_vector) {
|
||||||
|
// hidden lazy vector reappears
|
||||||
|
new_value.label_text = binding_name;
|
||||||
|
add_vector_arrow_to_svg(new_value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const adjust_input_element_height = function () {
|
export const adjust_input_element_height = function () {
|
||||||
|
|
@ -115,7 +142,9 @@ const handle_enter = function () {
|
||||||
let binding;
|
let binding;
|
||||||
if (value.is_binding) { // if it's declaration work with the initializer
|
if (value.is_binding) { // if it's declaration work with the initializer
|
||||||
binding = value.name; // but we also need the name of the bound variable
|
binding = value.name; // but we also need the name of the bound variable
|
||||||
value = state[binding];
|
value = state[binding]; // lookup the value for the binding
|
||||||
|
} else if (value.id) {
|
||||||
|
references['@' + value.id] = value;
|
||||||
}
|
}
|
||||||
while (value.lazy_expression) {
|
while (value.lazy_expression) {
|
||||||
value = value.get();
|
value = value.get();
|
||||||
|
|
@ -139,7 +168,7 @@ const handle_enter = function () {
|
||||||
label(bindings[binding].previous, '@' + bindings[binding].previous.id);
|
label(bindings[binding].previous, '@' + bindings[binding].previous.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
update_lazy_objects();
|
update_visible_objects();
|
||||||
if (value.description) {
|
if (value.description) {
|
||||||
value = value.description;
|
value = value.description;
|
||||||
}
|
}
|
||||||
|
|
@ -170,8 +199,9 @@ const visit = function (expr) {
|
||||||
}
|
}
|
||||||
state[binding_name] = value; // assign new value to binding
|
state[binding_name] = value; // assign new value to binding
|
||||||
|
|
||||||
return bindings[binding_name];
|
return bindings[binding_name]; // don't return the value itself, but the binding_object
|
||||||
}
|
} // with which you can lookup the value
|
||||||
|
|
||||||
case 'group': // expression within parentheses
|
case 'group': // expression within parentheses
|
||||||
return visit(expr.expression);
|
return visit(expr.expression);
|
||||||
case 'unary': {
|
case 'unary': {
|
||||||
|
|
@ -376,7 +406,6 @@ export const create_vector = function (vector) { //rename to create_vector
|
||||||
vector.toString = function () {
|
vector.toString = function () {
|
||||||
return `vector@${this.id}{x0:${vector.x0},y0:${vector.y0} x:${vector.x},y:${vector.y}}`;
|
return `vector@${this.id}{x0:${vector.x0},y0:${vector.y0} x:${vector.x},y:${vector.y}}`;
|
||||||
};
|
};
|
||||||
references["@" + vector.id] = vector;
|
|
||||||
vector.hide = function () {
|
vector.hide = function () {
|
||||||
return hide(this);
|
return hide(this);
|
||||||
};
|
};
|
||||||
|
|
@ -392,7 +421,11 @@ export const create_vector = function (vector) { //rename to create_vector
|
||||||
const resolve_arguments = function (argument_exprs) {
|
const resolve_arguments = function (argument_exprs) {
|
||||||
let arguments_list = [];
|
let arguments_list = [];
|
||||||
for (let i = 0; i < argument_exprs.length; i++) {
|
for (let i = 0; i < argument_exprs.length; i++) {
|
||||||
arguments_list.push(visit(argument_exprs[i]));
|
let value = visit(argument_exprs[i]);
|
||||||
|
if (value.lazy_expression) {
|
||||||
|
value = value.get(); // not convinced this must be here, but where else?
|
||||||
|
}
|
||||||
|
arguments_list.push(value);
|
||||||
}
|
}
|
||||||
return arguments_list;
|
return arguments_list;
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import {update_lazy_objects} from "./index";
|
import {update_visible_objects} from "./index";
|
||||||
|
|
||||||
let vectors_by_id = {};
|
let vectors_by_id = {};
|
||||||
const SVG_NS = 'http://www.w3.org/2000/svg'; // program needs these to create svg elements
|
const SVG_NS = 'http://www.w3.org/2000/svg'; // program needs these to create svg elements
|
||||||
|
|
@ -170,11 +170,9 @@ export const add_vector_arrow_to_svg = function (vector) {
|
||||||
vector_arrow.onmousedown = function start_moving_vector(event) {
|
vector_arrow.onmousedown = function start_moving_vector(event) {
|
||||||
moving_vector = event.target;
|
moving_vector = event.target;
|
||||||
};
|
};
|
||||||
|
|
||||||
vector_group.appendChild(vector_arrow);
|
vector_group.appendChild(vector_arrow);
|
||||||
|
|
||||||
let label = create_label(vector);
|
let label = create_label(vector);
|
||||||
|
|
||||||
vector_group.appendChild(label);
|
vector_group.appendChild(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,7 +295,7 @@ const move_vector = function (event) {
|
||||||
vector.y = (origin_y - current_y) / grid_size;
|
vector.y = (origin_y - current_y) / grid_size;
|
||||||
moving_vector.setAttribute('d', create_d(origin_x, origin_y, current_x, current_y));
|
moving_vector.setAttribute('d', create_d(origin_x, origin_y, current_x, current_y));
|
||||||
update_label(moving_vector.id, moving_vector.id, current_x + 5, current_y + 5);
|
update_label(moving_vector.id, moving_vector.id, current_x + 5, current_y + 5);
|
||||||
update_lazy_objects();
|
update_visible_objects();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue