Skip to content
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
merged 5 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions autorun.js
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();
Copy link
Member Author

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.

Copy link

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

If you call Meteor.subscribe within a reactive computation, for example using Tracker.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped; it is not necessary to call stop on subscriptions made from inside autorun. However, if the next iteration of your run function subscribes to the same record set (same name and parameters), Meteor is smart enough to skip a wasteful unsubscribe/resubscribe.

Copy link
Member Author

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.

Copy link

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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 ;)

}
isFirstRun = false;
});
});
hasBeenStarted = true;
},
stop() {
if (hasBeenStarted) {
computation.stop();
mobxDisposer();
subscriptionHandle && subscriptionHandle.stop();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And when autorun is stopped.

}
}
};
};
38 changes: 4 additions & 34 deletions index.js
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;
38 changes: 38 additions & 0 deletions observe-cursor.js
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([]);
})();
}
};