baobab icon indicating copy to clipboard operation
baobab copied to clipboard

Monkey in an array

Open basicdays opened this issue 7 years ago • 3 comments

Hey all,

Just ran into this bug. If I create a monkey that contains an array within its own path on the tree, it doesn't seem to be able to act as a getter. I'm currently on version 2.4.1. Here's some code to reproduce.

#!/usr/bin/env node
'use strict';
const Baobab = require('baobab');
const monkey = Baobab.monkey;


function alias(item) {
	return item;
}

let tree = new Baobab({
	stuff: 'things',
	alias: monkey({
		cursors: {data: ['stuff']},
		get: alias,
	}),
});
console.dir(tree.get());
console.dir(tree.get(['alias']));
console.log();

tree = new Baobab({
	stuff: 'things',
	aliases: [
		monkey({
			cursors: {data: ['stuff']},
			get: alias,
		}),
	],
});
console.dir(tree.get());
console.dir(tree.get(['aliases', 0]));
console.log();

tree = new Baobab({
	stuff: 'things',
	aliases: [
		{
			item: monkey({
				cursors: {data: ['stuff']},
				get: alias,
			}),
		},
	],
});
console.dir(tree.get(), {depth: 5});

And its output:

{ stuff: 'things', alias: [Getter] }
{ data: 'things' }

{ stuff: 'things',
  aliases:
   [ MonkeyDefinition {
       type: 'object',
       getter: [Function: alias],
       projection: [Object],
       paths: [Object],
       options: {},
       hasDynamicPaths: false } ] }
MonkeyDefinition {
  type: 'object',
  getter: [Function: alias],
  projection: { data: [ 'stuff' ] },
  paths: [ [ 'stuff' ] ],
  options: {},
  hasDynamicPaths: false }

{ stuff: 'things',
  aliases:
   [ { item:
        MonkeyDefinition {
          type: 'object',
          getter: [Function: alias],
          projection: { data: [ 'stuff' ] },
          paths: [ [ 'stuff' ] ],
          options: {},
          hasDynamicPaths: false } } ] }

Will probably rearchitect how I'm approaching the problem to work around this as I don't know the performance impact anyways.

basicdays avatar Mar 23 '17 14:03 basicdays

I'll try to check that soon but yes, having monkeys in an array is probably a bad idea performance-wise.

Yomguithereal avatar Mar 23 '17 15:03 Yomguithereal

Just started poking around in the source about it and found this in baobab.js line 166:

  /**
   * Internal method used to refresh the tree's monkey register on every
   * update.
   * Note 1) For the time being, placing monkeys beneath array nodes is not
   * allowed for performance reasons.
   *
   * @param  {mixed}   node      - The starting node.
   * @param  {array}   path      - The starting node's path.
   * @param  {string}  operation - The operation that lead to a refreshment.
   * @return {Baobab}            - The tree instance for chaining purposes.
   */
  _refreshMonkeys(node, path, operation) {

If you'd like, I just forked the repo. I could just add a notice in the readme that monkeys that are on a path with an array are not supported at this time for a PR. Wouldn't mind getting a bit more involved in this project. :)

basicdays avatar Mar 23 '17 15:03 basicdays

I recall writing something about this in the README concerning monkeys and arrays but I guess I forgot. My bad. Don't hesitate to open a PR :).

Yomguithereal avatar Mar 23 '17 15:03 Yomguithereal