-
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 all commits
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
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,34 @@ | ||
import { Tracker } from 'meteor/tracker'; | ||
import { autorun } from '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(); | ||
} | ||
} | ||
}; | ||
}; |
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,37 @@ | ||
import { Tracker } from 'meteor/tracker'; | ||
import { action } from 'mobx'; | ||
|
||
export default (actionPrefix = '', observableArray, handle, cursor) => { | ||
|
||
if (handle.ready()) { | ||
// initially fetch all documents and store them in the observable array | ||
Tracker.nonreactive(() => { | ||
action(`${actionPrefix}: initial fetch`, (comments) => { | ||
observableArray.replace(comments); | ||
})(cursor.fetch()); | ||
}); | ||
|
||
// observe changes after initial fetch | ||
cursor.observe({ | ||
// _suppress_initial suppresses addedAt callback for documents initially fetched | ||
_suppress_initial: true, | ||
addedAt: action(`${actionPrefix}: document added`, (document, atIndex) => { | ||
observableArray.splice(atIndex, 0, document); | ||
}), | ||
changedAt: action(`${actionPrefix}: document changed`, (newDocument, oldDocument, atIndex) => { | ||
observableArray.splice(atIndex, 1, newDocument); | ||
}), | ||
removedAt: action(`${actionPrefix}: document removed`, (oldDocument, atIndex) => { | ||
observableArray.splice(atIndex, 1); | ||
}), | ||
movedTo: action(`${actionPrefix}: document moved`, (document, fromIndex, toIndex) => { | ||
observableArray.splice(fromIndex, 1); | ||
observableArray.splice(toIndex, 0, document); | ||
}), | ||
}); | ||
} else { | ||
action(`${actionPrefix}: initialized`, () => { | ||
observableArray.clear(); | ||
})(); | ||
} | ||
}; |
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.
👍