-
Notifications
You must be signed in to change notification settings - Fork 8
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
Live query support #10
Merged
rhyslbw
merged 5 commits into
develop
from
feature/stopping-subscription-and-livequery-support
Aug 18, 2016
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f74585a
Introduce stopping subscriptions and livequery support
darko-mijic 5fb2431
Remove unnecessary subscription stopping
darko-mijic 42fa40d
Clear observable cursor using dedicated mobx method
darko-mijic 7d5f0e1
Improve comments
darko-mijic 87a7f7f
Update readme to new api
darko-mijic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Tracker } from 'meteor/tracker'; | ||
import { autorun } from 'mobx'; | ||
|
||
export default (trackerMobxAutorun) => { | ||
let mobxDisposer = null; | ||
let computation = null; | ||
let hasBeenStarted; | ||
let subscriptionHandle; | ||
return { | ||
start() { | ||
let isFirstRun = true; | ||
computation = Tracker.autorun(() => { | ||
if (mobxDisposer) { | ||
mobxDisposer(); | ||
isFirstRun = true; | ||
} | ||
mobxDisposer = autorun(() => { | ||
if (isFirstRun) { | ||
subscriptionHandle = trackerMobxAutorun(); | ||
} else { | ||
computation.invalidate(); | ||
subscriptionHandle && subscriptionHandle.stop(); | ||
} | ||
isFirstRun = false; | ||
}); | ||
}); | ||
hasBeenStarted = true; | ||
}, | ||
stop() { | ||
if (hasBeenStarted) { | ||
computation.stop(); | ||
mobxDisposer(); | ||
subscriptionHandle && subscriptionHandle.stop(); | ||
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. And when autorun is stopped. |
||
} | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,40 +1,10 @@ | ||
import { Tracker } from 'meteor/tracker'; | ||
import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions'; | ||
import observeCursor from './observe-cursor'; | ||
import autorun from './autorun'; | ||
|
||
checkNpmVersions({ | ||
'mobx': '2.x' | ||
}, 'space:tracker-mobx-autorun'); | ||
|
||
const { autorun } = require('mobx'); | ||
|
||
export default (trackerMobxAutorun) => { | ||
let mobxDisposer = null; | ||
let computation = null; | ||
let hasBeenStarted; | ||
return { | ||
start() { | ||
let isFirstRun = true; | ||
computation = Tracker.autorun(() => { | ||
if (mobxDisposer) { | ||
mobxDisposer(); | ||
isFirstRun = true; | ||
} | ||
mobxDisposer = autorun(() => { | ||
if (isFirstRun) { | ||
trackerMobxAutorun(); | ||
} else { | ||
computation.invalidate(); | ||
} | ||
isFirstRun = false; | ||
}); | ||
}); | ||
hasBeenStarted = true; | ||
}, | ||
stop() { | ||
if (hasBeenStarted) { | ||
computation.stop(); | ||
mobxDisposer(); | ||
} | ||
} | ||
}; | ||
}; | ||
export default autorun; | ||
export const observe = observeCursor; |
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,38 @@ | ||
import { Tracker } from 'meteor/tracker'; | ||
import { action } from 'mobx'; | ||
|
||
export default (actionPrefix = '', storeAttribute, handle, cursor) => { | ||
|
||
if (handle.ready()) { | ||
// initial fetch... | ||
Tracker.nonreactive(() => { | ||
action(`${actionPrefix}: initial fetch`, (comments) => { | ||
storeAttribute.replace(comments); | ||
})(cursor.fetch()); | ||
}); | ||
|
||
// ...and then observe | ||
cursor.observe({ | ||
// we don't want that the addedAt function is triggered x times at the beginning | ||
// just fetch them once (see above) | ||
_suppress_initial: true, | ||
addedAt: action(`${actionPrefix}: document added`, (document, atIndex) => { | ||
storeAttribute.splice(atIndex, 0, document); | ||
}), | ||
changedAt: action(`${actionPrefix}: document changed`, (newDocument, oldDocument, atIndex) => { | ||
storeAttribute.splice(atIndex, 1, newDocument); | ||
}), | ||
removedAt: action(`${actionPrefix}: document removed`, (oldDocument, atIndex) => { | ||
storeAttribute.splice(atIndex, 1); | ||
}), | ||
movedTo: action(`${actionPrefix}: document moved`, (document, fromIndex, toIndex) => { | ||
storeAttribute.splice(fromIndex, 1); | ||
storeAttribute.splice(toIndex, 0, document); | ||
}), | ||
}); | ||
} else { | ||
action(`${actionPrefix}: initialized`, () => { | ||
storeAttribute.replace([]); | ||
})(); | ||
} | ||
}; |
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.
Subscriptions are stopped on each autorun invalidation.
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.
Is that even needed? See meteor docs
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.
You are right, this is not needed and it will cause wasteful re-subscriptions.
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.
This allows also for multiple subscriptions per autorun
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.
We should probably introduce array with subscription handle references to support stopping multiple subscriptions in
stop()
method.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.
@darko-mijic what @RSchwan means (and the docs say) is that all subscriptions (any number) are automatically stopped when the autorun is invalidated 😉 so we do not need todo anything here.
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.
Oh, you are right. We don't even need to stop subscription in scope of autorun.stop(), ok ;)