Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 116 forks source link

"mutable" monkey still loses attached functions #418

Closed tnrich closed 8 years ago

tnrich commented 8 years ago

I'd like to derive an interval tree based on the state within my baobab tree using a monkey. That interval tree comes from a library that includes helpful features like searching the interval tree and others.

Unfortunately, it seems like the returned value from the monkey is stripped of its attached functions.

Here's an example: https://tonicdev.com/tnrich/mutable-monkey-loses-attached-functions

Thanks for all the help with this monkey business :)

Yomguithereal commented 8 years ago

Your example does not show a mutable monkey. You do not seem to have disabled immutability through the monkey's option, no?

I was sure that this naming would bring this kind of puns. Keep them going :).

tnrich commented 8 years ago

Right, sorry about that, I'm not able to get the baobab@2.3.0-rc1 on tonicDev, so it throws an error if I try to define the monkey as immutable:false. Here's the actual code I'm looking at, but it behaves the same as above:


var some = require('lodash').some
var IntervalTree = require('interval-tree2')
var Baobab = require('baobab');
var monkey = Baobab.monkey;
var tree = new Baobab({
    iTree: monkey([
        function function_name (argument) {
            var iTree = new IntervalTree(400)
            iTree.add(22,54,'bar');
            return iTree;
        },{
            immutable: false
        }
        ])    
}, {lazyMonkeys: false});

console.log('tree.get(): ' + JSON.stringify(tree.get(['test']),null,4));
var monkeyITree = tree.get(['iTree'])

var normalITree = new IntervalTree(400)
normalITree.add(22,54,'bar');
debugger;

Where the normalITree has the search method, but the monkeyITree doesn't.

tnrich commented 8 years ago

Here's a repo with the two errors in it if you'd like to inspect closer: https://github.com/tnrich/baobab-nested-monkey-error-1.0.0

418

417

Yomguithereal commented 8 years ago

The problem seems linked to the monkeys' laziness somehow.

Yomguithereal commented 8 years ago

Ok found it.

Yomguithereal commented 8 years ago

In the meantime, let the monkeys be lazy and you should be set.