Skip to content
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

Monkey in an array #483

Open
basicdays opened this issue Mar 23, 2017 · 3 comments
Open

Monkey in an array #483

basicdays opened this issue Mar 23, 2017 · 3 comments

Comments

@basicdays
Copy link

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.

@Yomguithereal
Copy link
Owner

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

@basicdays
Copy link
Author

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. :)

@Yomguithereal
Copy link
Owner

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 :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants