-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from meteor-space/feature/stopping-subscriptio…
…n-and-livequery-support Live query support
- Loading branch information
Showing
4 changed files
with
95 additions
and
90 deletions.
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(); | ||
})(); | ||
} | ||
}; |