Add some generic methods for dealing with selectors (#888)

This commit is contained in:
Jordan Eldredge 2019-08-24 23:49:21 -07:00
parent 2790a2995b
commit db8bbf0102
2 changed files with 32 additions and 13 deletions

View file

@ -1,14 +1,18 @@
import * as Utils from "./utils";
function findNodeByUid(state, uid) {
return Utils.findInTree(state.xmlTree, node => {
return node.uid === uid;
});
function findNodeByUid(uid) {
// TODO: Do some clever caching here.
return state => {
return Utils.findInTree(state.xmlTree, node => {
return node.uid === uid;
});
};
}
export function getTop(uid) {
const findNodeInState = findNodeByUid(uid);
return state => {
const node = findNodeByUid(state, uid);
const node = findNodeInState(state);
return Number(node.attributes.y) || 0;
};
}

View file

@ -11,6 +11,25 @@ class GuiObject extends MakiObject {
super(node, parent, annotations, store);
this.visible = true;
this._selectorCache = new Map();
}
_useUidSelector(selector) {
// TODO: use memoize for this
if (!this._selectorCache.has(selector)) {
this._selectorCache.set(selector, selector(this._uid));
}
return this._selectorCache.get(selector)(this._store.getState());
}
_compareToUidSelector(value, selector) {
const selectorValue = this._useUidSelector(selector);
if (selectorValue !== value) {
console.error(
`Maki state ${value} is out of sync with tree state ${selectorValue}`
);
}
return value;
}
/**
@ -62,14 +81,10 @@ class GuiObject extends MakiObject {
}
gettop() {
const stateTop = MakiSelectors.getTop(this._uid)(this._store.getState());
const makiTop = Number(this.attributes.y) || 0;
if (stateTop !== makiTop) {
console.error(
`Maki state ${makiTop} is out of sync with tree state ${stateTop}`
);
}
return makiTop;
return this._compareToUidSelector(
Number(this.attributes.y) || 0,
MakiSelectors.getTop
);
}
getleft() {