Skip to content

Commit b28cb78

Browse files
authored
Remove unnecessary branches (redux-orm#316)
* only check if parent is a model selector as otherwise it currently has to be a field selector * set minimum branch coverage to 98%
1 parent 67705b3 commit b28cb78

9 files changed

+48
-61
lines changed

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ module.exports = {
1717
coveragePathIgnorePatterns: ["test/*"],
1818
coverageThreshold: {
1919
global: {
20-
branches: 95,
20+
branches: 98,
2121
functions: 100,
22-
lines: 99,
22+
lines: 100,
2323
statements: 99,
2424
},
2525
},

src/Session.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,21 @@ const Session = class Session {
176176
if (!clauseFiltersByAttribute(clause, idAttribute)) {
177177
return false;
178178
}
179-
const id = clause.payload[idAttribute];
180-
if (id === null) return false;
181179
/**
182180
* We previously knew which row we wanted to access,
183181
* so there was no need to scan the entire table.
184182
*/
185-
accessedIds.add(id);
183+
accessedIds.add(clause.payload[idAttribute]);
186184
return true;
187185
});
188186

189187
const accessedIndexes = [];
190188
const { indexes } = this.state[table];
191189
clauses.forEach(clause => {
192190
Object.keys(indexes).forEach(attr => {
193-
if (!clauseFiltersByAttribute(clause, attr)) return;
191+
if (!clauseFiltersByAttribute(clause, attr)) {
192+
return;
193+
}
194194
const value = clause.payload[attr];
195195
accessedIndexes.push([table, attr, value]);
196196
});

src/db/Table.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ export class Table {
484484
indexIdsToDelete.forEach(([attr, value, id]) => {
485485
const arr = nextIndexes[attr][value];
486486
const idx = arr.indexOf(id);
487-
if (idx !== -1) {
488-
ops.mutable.splice(idx, 1, [], arr);
489-
}
487+
ops.mutable.splice(idx, 1, [], arr);
490488
});
491489
indexIdsToAdd.forEach(([attr, value, id]) => {
492490
ops.mutable.push(id, nextIndexes[attr][value]);
@@ -526,7 +524,7 @@ export class Table {
526524
[value]: ops.batch.filter(
527525
batchToken,
528526
rowId => rowId !== id,
529-
indexMap[attr][value] || []
527+
indexMap[attr][value]
530528
),
531529
},
532530
indexMap[attr]
@@ -567,10 +565,7 @@ export class Table {
567565
if (withMutations) {
568566
idsToDelete.forEach(id => {
569567
const idx = arr.indexOf(id);
570-
if (idx !== -1) {
571-
ops.mutable.splice(idx, 1, [], arr);
572-
}
573-
568+
ops.mutable.splice(idx, 1, [], arr);
574569
ops.mutable.omit(id, branch[mapName]);
575570
});
576571
// delete ids from all indexes

src/fields/Field.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import DefaultFieldInstaller from "./DefaultFieldInstaller";
55
* @memberof module:fields
66
*/
77
export class Field {
8-
constructor() {
9-
this.index = false;
10-
}
11-
128
get installerClass() {
139
return DefaultFieldInstaller;
1410
}
@@ -25,10 +21,6 @@ export class Field {
2521
return null;
2622
}
2723

28-
get installsForwardsDescriptor() {
29-
return true;
30-
}
31-
3224
get installsForwardsVirtualField() {
3325
return false;
3426
}
@@ -40,6 +32,10 @@ export class Field {
4032
get installsBackwardsVirtualField() {
4133
return false;
4234
}
35+
36+
get index() {
37+
return false;
38+
}
4339
}
4440

4541
export default Field;

src/fields/FieldInstallerTemplate.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export class FieldInstallerTemplate {
5555
}
5656

5757
run() {
58-
if (this.field.installsForwardsDescriptor) {
59-
this.installForwardsDescriptor();
60-
}
58+
this.installForwardsDescriptor();
6159
if (this.field.installsForwardsVirtualField) {
6260
this.installForwardsVirtualField();
6361
}

src/fields/ForeignKey.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import {
99
* @memberof module:fields
1010
*/
1111
export class ForeignKey extends RelationalField {
12-
constructor(...args) {
13-
super(...args);
14-
this.index = true;
15-
}
16-
1712
createForwardsDescriptor(fieldName, model, toModel, throughModel) {
1813
return forwardsManyToOneDescriptor(fieldName, toModel.modelName);
1914
}
2015

2116
createBackwardsDescriptor(fieldName, model, toModel, throughModel) {
2217
return backwardsManyToOneDescriptor(fieldName, model.modelName);
2318
}
19+
20+
get index() {
21+
return true;
22+
}
2423
}
2524

2625
export default ForeignKey;

src/redux.js

+23-28
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,36 @@ function toSelector(arg) {
9797
}
9898
if (arg instanceof SelectorSpec) {
9999
const { _orm: orm, cachePath } = arg;
100-
let ormSelectors;
101100
let level;
102-
if (cachePath && cachePath.length) {
103-
// the selector cache for the spec's ORM
104-
if (!selectorCache.has(orm)) {
105-
selectorCache.set(orm, new Map());
106-
}
107-
ormSelectors = selectorCache.get(orm);
108101

109-
/**
110-
* Drill down into selector map by cachePath.
111-
*
112-
* The selector itself is stored under a special SELECTOR_KEY
113-
* so that we can store selectors below it as well.
114-
* @private
115-
*/
116-
level = ormSelectors;
117-
for (let i = 0; i < cachePath.length; ++i) {
118-
if (!level.has(cachePath[i])) {
119-
level.set(cachePath[i], new Map());
120-
}
121-
level = level.get(cachePath[i]);
122-
}
123-
if (level && level.has(SELECTOR_KEY)) {
124-
// Cache hit: the selector has been created before
125-
return level.get(SELECTOR_KEY);
102+
// the selector cache for the spec's ORM
103+
if (!selectorCache.has(orm)) {
104+
selectorCache.set(orm, new Map());
105+
}
106+
const ormSelectors = selectorCache.get(orm);
107+
108+
/**
109+
* Drill down into selector map by cachePath.
110+
*
111+
* The selector itself is stored under a special SELECTOR_KEY
112+
* so that we can store selectors below it as well.
113+
*/
114+
level = ormSelectors;
115+
for (let i = 0; i < cachePath.length; ++i) {
116+
if (!level.has(cachePath[i])) {
117+
level.set(cachePath[i], new Map());
126118
}
119+
level = level.get(cachePath[i]);
120+
}
121+
if (level && level.has(SELECTOR_KEY)) {
122+
// Cache hit: the selector has been created before
123+
return level.get(SELECTOR_KEY);
127124
}
128125

129126
const selector = createSelectorFromSpec(arg);
130127

131-
if (cachePath && cachePath.length) {
132-
// Save the selector at the cachePath position
133-
level.set(SELECTOR_KEY, selector);
134-
}
128+
// Save the selector at the cachePath position
129+
level.set(SELECTOR_KEY, selector);
135130

136131
return selector;
137132
}

src/test/functional/immutableSessions.js

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ describe("Immutable session", () => {
138138
);
139139
});
140140

141+
it("withId(null) returns null", () => {
142+
const { Book } = session;
143+
expect(Book.withId(null)).toBe(null);
144+
});
145+
141146
it("withId returns null if model instance not found", () => {
142147
const { Book } = session;
143148
expect(Book.withId(9043290)).toBe(null);

src/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ function attachQuerySetMethods(modelClass, querySetClass) {
111111
if (typeof descriptor.get !== "undefined") {
112112
descriptor.get = querySetGetterDelegatorFactory(methodName);
113113
Object.defineProperty(modelClass, methodName, descriptor);
114-
defined = true;
115-
} else if (typeof descriptor.value === "function") {
114+
} else {
116115
modelClass[methodName] = querySetDelegatorFactory(
117116
methodName
118117
);
119-
defined = true;
120118
}
119+
defined = true;
121120
}
122121
if (defined) {
123122
leftToDefine.splice(i--, 1);

0 commit comments

Comments
 (0)