Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit f7d5687

Browse files
authored
Merge pull request #1497 from atom/aw/refholder-tweaks
RefHolder.map() should return a RefHolder
2 parents 2202c1b + 023fda0 commit f7d5687

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

lib/models/ref-holder.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ export default class RefHolder {
7878
return Promise.resolve(this.get());
7979
}
8080

81-
map(block) {
82-
if (!this.isEmpty()) {
83-
return block(this.get());
84-
}
85-
return null;
81+
map(present, absent = () => this) {
82+
return RefHolder.on(this.isEmpty() ? absent() : present(this.get()));
8683
}
8784

8885
setter = value => {

lib/views/commit-view.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export default class CommitView extends React.Component {
388388
}
389389

390390
excludeCoAuthor() {
391-
const author = this.refCoAuthorSelect.map(c => c.getFocusedOption());
391+
const author = this.refCoAuthorSelect.map(c => c.getFocusedOption()).getOr(null);
392392
if (!author || author.isNew()) {
393393
return;
394394
}
@@ -548,15 +548,15 @@ export default class CommitView extends React.Component {
548548
return CommitView.focus.EDITOR;
549549
}
550550

551-
if (this.refAbortMergeButton.map(e => e.contains(event.target))) {
551+
if (this.refAbortMergeButton.map(e => e.contains(event.target)).getOr(false)) {
552552
return CommitView.focus.ABORT_MERGE_BUTTON;
553553
}
554554

555-
if (this.refCommitButton.map(e => e.contains(event.target))) {
555+
if (this.refCommitButton.map(e => e.contains(event.target)).getOr(false)) {
556556
return CommitView.focus.COMMIT_BUTTON;
557557
}
558558

559-
if (this.refCoAuthorSelect.map(c => c.wrapper.contains(event.target))) {
559+
if (this.refCoAuthorSelect.map(c => c.wrapper.contains(event.target)).getOr(false)) {
560560
return CommitView.focus.COAUTHOR_INPUT;
561561
}
562562

test/models/ref-holder.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,51 @@ describe('RefHolder', function() {
2626
assert.strictEqual(h.get(), 1234);
2727
});
2828

29+
describe('map', function() {
30+
it('returns an empty RefHolder as-is', function() {
31+
const h = new RefHolder();
32+
assert.strictEqual(h.map(() => 14), h);
33+
});
34+
35+
it('returns a new RefHolder wrapping the value returned from its present block', function() {
36+
const h = new RefHolder();
37+
h.setter(12);
38+
assert.strictEqual(h.map(x => x + 1).get(), 13);
39+
});
40+
41+
it('returns a RefHolder returned from its present block', function() {
42+
const h0 = new RefHolder();
43+
h0.setter(14);
44+
45+
const o = h0.map(() => {
46+
const h1 = new RefHolder();
47+
h1.setter(12);
48+
return h1;
49+
});
50+
51+
assert.notStrictEqual(0, h0);
52+
assert.strictEqual(o.get(), 12);
53+
});
54+
55+
it('returns a new RefHolder wrapping the value returned from its absent block', function() {
56+
const h = new RefHolder();
57+
58+
const o = h.map(x => 1, () => 2);
59+
assert.strictEqual(o.get(), 2);
60+
});
61+
62+
it('returns a RefHolder returned from its absent block', function() {
63+
const h0 = new RefHolder();
64+
65+
const o = h0.map(x => 1, () => {
66+
const h1 = new RefHolder();
67+
h1.setter(1);
68+
return h1;
69+
});
70+
assert.strictEqual(o.get(), 1);
71+
});
72+
});
73+
2974
it('notifies subscribers when it becomes available', function() {
3075
const h = new RefHolder();
3176
const callback = sinon.spy();

0 commit comments

Comments
 (0)