-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Tracking changes between start and end #19
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,40 @@ | ||
import 'package:meta/meta.dart'; | ||
import 'package:mobx/src/core/context.dart'; | ||
import 'package:mobx/src/core/derivation.dart'; | ||
import 'package:mobx/src/core/reaction.dart'; | ||
|
||
/// Tracks changes that happen between [Tracking.begin] and [Tracking.end]. | ||
/// | ||
/// This should only be used in situations where it is not possible to | ||
/// track changes inside a callback function. | ||
@experimental | ||
class Tracking { | ||
Tracking.begin(ReactiveContext context, Function() onInvalidate, | ||
{String name}) | ||
: assert(context != null), | ||
assert(onInvalidate != null), | ||
assert(name != ''), | ||
_reaction = TrackingReaction(context, onInvalidate, | ||
name: name ?? context.nameFor('Transaction')) { | ||
_prevDerivation = _reaction.startTracking(); | ||
} | ||
|
||
final TrackingReaction _reaction; | ||
Derivation _prevDerivation; | ||
bool _ended = false; | ||
|
||
void end() { | ||
if (_ended) { | ||
return; | ||
} | ||
|
||
_ended = true; | ||
_reaction.endTracking(_prevDerivation); | ||
_prevDerivation = null; | ||
} | ||
|
||
void dispose() { | ||
end(); | ||
_reaction.dispose(); | ||
} | ||
} |
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,57 @@ | ||
import 'package:mobx/mobx.dart'; | ||
import 'package:mobx/src/api/context.dart'; | ||
import 'package:mobx/src/core/tracking.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('Tracking reacts to changes to reactive values between begin and end', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also have a test with a autorun() to ensure the reactions are kicked off when the observables change outside |
||
() { | ||
final var1 = observable(0); | ||
final var2 = observable(0); | ||
var i = 0; | ||
|
||
final tracking = Tracking.begin(currentContext, () { | ||
i++; | ||
}); | ||
final var3 = observable(0); | ||
var1.value; | ||
tracking.end(); | ||
|
||
// No changes, no calls to onInvalidate | ||
expect(i, equals(0)); | ||
|
||
// Change outside tracking, no onInvalidate call | ||
var2.value += 1; | ||
expect(i, equals(0)); | ||
|
||
// Change outside tracking to an observable created inside tracking | ||
// no onInvalidate call | ||
var3.value += 1; | ||
expect(i, equals(0)); | ||
|
||
// Changing a value that was read when tracking was active | ||
// calls onInvalidate | ||
var1.value += 1; | ||
expect(i, equals(1)); | ||
|
||
// No calls to onInvalidate after first change | ||
var1.value += 1; | ||
expect(i, equals(1)); | ||
}); | ||
|
||
test("disposed Tracking doesn't call onInvalidate", () { | ||
final var1 = observable(0); | ||
|
||
var i = 0; | ||
final tracking = Tracking.begin(currentContext, () { | ||
i++; | ||
}); | ||
var1.value; | ||
tracking | ||
..end() | ||
..dispose(); | ||
|
||
var1.value += 1; | ||
expect(i, equals(0)); | ||
}); | ||
} |
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.
How about a more explicit name like
DerivationTracker
? Going for a track-er rather than track-ingThere 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.
Can we use it in the current
Reaction
,ComputedValue
implementations ? I think this would be an internally useful extraction of the tracking behaviorThere 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.
I'll refactor context a bit, add most of the logic there, but I think this should be the public (and unsafe) API of doing tracking without callbacks. The callback API should be used for 99.9% use cases, even in Reaction & ComputedValue.
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 about using callbacks as they are more natural. Internally we can have a
callback
interface to the start-end blocks of tracking. That way we are dog-fooding thisTracker
class and it becomes a core behavior (which it is really).I would think of this as the antithesis of
untracked()
. So a callback would be wrapped intracked()
, which internally uses the Tracker with the start/end blocks.Here is a sample implementation of
tracked
(not tested).