-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Initial implementation of new-style refs #1554
Merged
+236
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright 2013-2014 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @providesModule ReactRef | ||
*/ | ||
|
||
"use strict"; | ||
|
||
var ReactUpdates = require('ReactUpdates'); | ||
|
||
var accumulate = require('accumulate'); | ||
var assign = require('Object.assign'); | ||
var forEachAccumulated = require('forEachAccumulated'); | ||
var invariant = require('invariant'); | ||
|
||
function ReactRef() { | ||
this._value = null; | ||
this._successCallbacks = null; | ||
this._failureCallbacks = null; | ||
} | ||
|
||
/** | ||
* Call the enqueued success or failure callbacks for a ref, as appropriate. | ||
*/ | ||
function dispatchCallbacks() { | ||
/*jshint validthis:true */ | ||
var successCallbacks = this._successCallbacks; | ||
var failureCallbacks = this._failureCallbacks; | ||
this._successCallbacks = null; | ||
this._failureCallbacks = null; | ||
|
||
if (this._value) { | ||
forEachAccumulated(successCallbacks, callSuccess, this); | ||
} else { | ||
forEachAccumulated(failureCallbacks, callFailure); | ||
} | ||
} | ||
|
||
/** | ||
* Call a single success callback, passing the ref's value. | ||
*/ | ||
function callSuccess(cb) { | ||
/*jshint validthis:true */ | ||
cb(this._value); | ||
} | ||
|
||
/** | ||
* Call a single failure callback, passing no arguments. | ||
*/ | ||
function callFailure(cb) { | ||
cb(); | ||
} | ||
|
||
assign(ReactRef.prototype, { | ||
/** | ||
* Get the value of a ref asynchronously. Accepts a success callback and an | ||
* optional failure callback. If the ref has been rendered, the success | ||
* callback will be called with the component instance; otherwise, the failure | ||
* callback will be executed. | ||
* | ||
* @param {function} success Callback in case of success | ||
* @param {?function} failure Callback in case of failure | ||
*/ | ||
then: function(success, failure) { | ||
invariant( | ||
typeof success === 'function', | ||
'ReactRef.then(...): Must provide a success callback.' | ||
); | ||
if (this._successCallbacks == null) { | ||
ReactUpdates.asap(dispatchCallbacks, this); | ||
} | ||
this._successCallbacks = accumulate(this._successCallbacks, success); | ||
if (failure) { | ||
this._failureCallbacks = accumulate(this._failureCallbacks, failure); | ||
} | ||
} | ||
}); | ||
|
||
ReactRef.attachRef = function(ref, value) { | ||
ref._value = value; | ||
}; | ||
|
||
ReactRef.detachRef = function(ref, value) { | ||
// Check that `component` is still the current ref because we do not want to | ||
// detach the ref if another component stole it. | ||
if (ref._value === value) { | ||
ref._value = null; | ||
} | ||
}; | ||
|
||
module.exports = ReactRef; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebmarkbage's property descriptor API idea will make declaring refs really elegant. For now, we have this
componentWillMount
method which is as close to a constructor as we'll get until ES6.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, ES6 classes will make this feel a lot nicer.