-
-
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 all commits
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
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,170 @@ | ||
import 'package:mobx/mobx.dart'; | ||
import 'package:mobx/src/api/context.dart'; | ||
import 'package:mobx/src/core/reaction.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test( | ||
'DerivationTracker reacts to changes to reactive values between begin and end', | ||
() { | ||
var i = 0; | ||
|
||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
|
||
final var1 = observable(0); | ||
final var2 = observable(0); | ||
|
||
tracker.start(); | ||
final var3 = observable(0); | ||
var1.value; | ||
tracker.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('DerivationTracker can be used multiple times', () { | ||
var i = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker.start(); | ||
var1.value; | ||
tracker.end(); | ||
|
||
expect(i, equals(0)); | ||
|
||
var1.value += 1; | ||
expect(i, equals(1)); | ||
|
||
tracker.start(); | ||
var1.value; | ||
tracker.end(); | ||
|
||
var1.value += 1; | ||
expect(i, equals(2)); | ||
|
||
final var2 = observable(0); | ||
tracker.start(); | ||
var2.value; | ||
tracker.end(); | ||
|
||
var2.value += 1; | ||
expect(i, equals(3)); | ||
}); | ||
|
||
test("disposed DerivationTracker doesn't call onInvalidate", () { | ||
var i = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker.start(); | ||
var1.value; | ||
tracker | ||
..end() | ||
..dispose(); | ||
|
||
var1.value += 1; | ||
expect(i, equals(0)); | ||
}); | ||
|
||
test('calling start multiple times before calling end does nothing', () { | ||
var i = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker..start()..start()..start(); | ||
|
||
var1.value; | ||
|
||
tracker | ||
..start() | ||
..end(); | ||
|
||
expect(i, equals(0)); | ||
|
||
var1.value += 1; | ||
expect(i, equals(1)); | ||
}); | ||
|
||
test('calling end multiple times after start does nothing', () { | ||
var i = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker.start(); | ||
var1.value; | ||
tracker..end()..end()..end(); | ||
|
||
expect(i, equals(0)); | ||
|
||
var1.value += 1; | ||
expect(i, equals(1)); | ||
}); | ||
|
||
test('calling dispose multiple times does nothing', () { | ||
var i = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker.start(); | ||
var1.value; | ||
tracker..dispose()..dispose()..dispose(); | ||
|
||
expect(i, equals(0)); | ||
|
||
var1.value += 1; | ||
expect(i, equals(0)); | ||
}); | ||
|
||
test('autorun works inside tracking', () { | ||
var i = 0; | ||
var autoVar = 0; | ||
final tracker = DerivationTracker(currentContext, () { | ||
i++; | ||
}); | ||
final var1 = observable(0); | ||
|
||
tracker.start(); | ||
var1.value; | ||
autorun((_) { | ||
autoVar += var1.value; | ||
}); | ||
tracker.end(); | ||
|
||
var1.value = 1; | ||
|
||
expect(i, equals(1)); | ||
expect(autoVar, equals(1)); | ||
}); | ||
} |
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.
Not clear why we need a
reaction
here for tracking? Could you please elaborate? Is it mostly to wrap theonInvalidate
in some derivation?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.
Basically yes, I'm not sure how to do it better. DerivationTracker is pretty much Reaction, but with
track
replaced withstart
andend
, so it made sense to delegate to Reaction.