-
Notifications
You must be signed in to change notification settings - Fork 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
Listen for changes to specific paths #18
Comments
I did some work regarding this on a library that would "decorate" Some caveats I faced was that any element of the I don't have much code to show yet, since it's still in heavy development. Here is how I would fetch or create node paths that reflect the var rootNode = new Structure();
function fetchNode(rootNode, keyPath, create) {
var
current = rootNode,
i = 0;
for(i = 0; i < keyPath.length; i++) {
var children = current.get('children', NOT_SET);
if(children === NOT_SET) {
if(create) {
children = current.set('children', Immutable.Map()).get('children');
} else {
current = null;
break;
}
}
var key = keyPath[i];
var current = children.get(key, NOT_SET);
if(current === NOT_SET) {
if(create) {
current = children.set(key, Immutable.Map()).get(key);
} else {
current = null;
break;
}
}
}
return current;
} |
Since we can't do something like matching |
immstruct already knows the path of function on (ev, key, fn) {
structure.on(ev, function (path, old, neww) {
if (path === key)
fn.apply(null, arguments);
});
} |
I was thinking something similar, but I decided to go with a structure similar to a trie tree to be efficient on space complexity in the long term. |
I've been experimenting with implementing // some promise returning function
var a_listener = co.wrap(function*(results){
// Input:
// results = {
// event: 'change',
// newValue: ...,
// oldValue: ...
// };
// update to execute another listener
struct.cursor('b').update(function(m) {
return m.set('foo', 'bar');
});
// suggestion: wait until listener for ['b'] finishes?
yield immstruct.waitFor(struct, 'b');
// update keypath with no listener
struct.cursor('c').update(function(m) {
return m.set('foo', 'bar');
});
});
var b_listener = co.wrap(function*(results){
// how to handle cycles?
struct.cursor('a').update(function(m) {
return m.set('foo', 'bar');
});
});
var struct = immstruct({ a: {}, b: {}, c: {} });
immstruct.listenTo(struct, ['a'], a_listener);
immstruct.listenTo(struct, ['b'], b_listener);
struct.cursor('a').update(function(m) {
return m.set('foo', 0);
});
// how to handle case when modifying cursor while listener for ['a'] executes?
struct.cursor('a').update(function(m) {
return m.set('foo', 1);
}); |
Reference cursors should cover this part, no? #30 |
Yep. This should be closed. |
We can easily add listeners to when specific pieces of data inside a structure changes.
Of course, you can aldready do
But something along the lines of this would be nice
The text was updated successfully, but these errors were encountered: