From 3c66921c175c10b5a917183d3419513c6502577e Mon Sep 17 00:00:00 2001 From: Jeroen Cranendonk Date: Tue, 27 Jan 2015 13:40:10 -0700 Subject: [PATCH 1/2] Fixed value check on makeCursor Certain code paths call makeCursor with four arguments, with 'value' undefined. Since arguments.length is still four in this case, the value is not retrieved from rootData, and an incorrect cursor type can be generated. --- contrib/cursor/__tests__/Cursor.ts | 7 +++++++ contrib/cursor/index.js | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/contrib/cursor/__tests__/Cursor.ts b/contrib/cursor/__tests__/Cursor.ts index 91adc3521c..e05c1e1460 100644 --- a/contrib/cursor/__tests__/Cursor.ts +++ b/contrib/cursor/__tests__/Cursor.ts @@ -49,6 +49,13 @@ describe('Cursor', () => { expect(deepCursor.deref()).toBe(data.getIn(Immutable.fromJS(['a', 'b']))); }); + it('cursor return new cursors of correct type', () => { + var data = Immutable.fromJS({ a: [1, 2, 3] }); + var cursor = Cursor.from(data); + var deepCursor = cursor.cursor('a'); + expect(deepCursor.findIndex).toBeDefined(); + }); + it('can be treated as a value', () => { var data = Immutable.fromJS(json); var cursor = Cursor.from(data, ['a', 'b']); diff --git a/contrib/cursor/index.js b/contrib/cursor/index.js index 61e9574c78..19d16e1f86 100644 --- a/contrib/cursor/index.js +++ b/contrib/cursor/index.js @@ -220,7 +220,7 @@ IndexedCursor.prototype = IndexedCursorPrototype; var NOT_SET = {}; // Sentinel value function makeCursor(rootData, keyPath, onChange, value) { - if (arguments.length < 4) { + if (value === void 0) { value = rootData.getIn(keyPath); } var size = value && value.size; From f7b56a7b04eaadfa276903c5417258d516957f7a Mon Sep 17 00:00:00 2001 From: Jeff Barczewski Date: Mon, 9 Mar 2015 17:34:18 -0500 Subject: [PATCH 2/2] fix cursor.cursor() using argument.length When calling cursor.cursor(), the subCursor fn calls makeCursor and its logic expects arguments.length less than 4 to trigger making subCursor. Unfortunately subCursor calls it with 4 arguments, so we can correct this so that it will call with 3 or 4 depending on how it was called. This is an alternate fix to @jcranendonk's since the other fix checked if value was void 0 (undefined), however it might be valid to pass undefined to these functions, so the argument.length check is probably safer. --- contrib/cursor/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/contrib/cursor/index.js b/contrib/cursor/index.js index 19d16e1f86..37eb012eda 100644 --- a/contrib/cursor/index.js +++ b/contrib/cursor/index.js @@ -220,7 +220,7 @@ IndexedCursor.prototype = IndexedCursorPrototype; var NOT_SET = {}; // Sentinel value function makeCursor(rootData, keyPath, onChange, value) { - if (value === void 0) { + if (arguments.length < 4) { value = rootData.getIn(keyPath); } var size = value && value.size; @@ -233,6 +233,13 @@ function wrappedValue(cursor, keyPath, value) { } function subCursor(cursor, keyPath, value) { + if (arguments.length < 3) { + return makeCursor( // call without value + cursor._rootData, + newKeyPath(cursor._keyPath, keyPath), + cursor._onChange + ); + } return makeCursor( cursor._rootData, newKeyPath(cursor._keyPath, keyPath),