Skip to content

Commit d6fbc53

Browse files
committed
add comments
1 parent 6c2d1f5 commit d6fbc53

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

async/main.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#!/usr/local/bin/node
22

3+
// Import other code modules
34
var mongo = require('./mongo_sim');
45
var Fiber = require('fibers');
56
var Future = require('fibers/future');
67

78
/*
9+
* "Callback hell" is when we have to nest callback functions in order to
10+
* control the order of execution. It can get very difficult to read!
11+
*/
12+
13+
/*
14+
Example of callback hell:
15+
816
mongo.getCollection('items', function (err, collection) {
917
collection.findOne({_id: 5}, function (err, result) {
1018
console.log("Few! Finally got a result");
@@ -13,7 +21,11 @@ mongo.getCollection('items', function (err, collection) {
1321
});
1422
*/
1523

16-
24+
/**
25+
* We can use the Fibers library to turn asynchronous code into synchronous
26+
* code. It makes it easier to read. You can "yield" a fiber which only blocks
27+
* the code within it, and not the entire event loop.
28+
*/
1729
var tryUsingFibers = function () {
1830
Fiber(function () {
1931
var fiber = Fiber.current;
@@ -29,6 +41,12 @@ var tryUsingFibers = function () {
2941
}).run();
3042
};
3143

44+
/**
45+
* Futures is a nice api on top of Fibers and is the preferred way to work with
46+
* Fibers. Notice the future can "wait" which will yield the current fiber.
47+
* It can also "return" which will resume execution where we left off with
48+
* the "wait()" call.
49+
*/
3250
var tryUsingFutures = function () {
3351
var myFunc = function () {
3452
var future = new Future;
@@ -44,6 +62,11 @@ var tryUsingFutures = function () {
4462
Fiber(myFunc).run();
4563
};
4664

65+
/**
66+
* Here's where we create a fiber and run it. Notice we pass in a function.
67+
* This is the code that will be executed inside the fiber and can be "paused"
68+
* by "yielding" the fiber.
69+
*/
4770
Fiber(function () {
4871
var collection = mongo.getCollection('items');
4972
var doc = collection.findOne({_id: 5});

async/mongo_sim.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
var Future = require('fibers/future');
22

33
var findOne = function (sel, callback) {
4+
// create a new future
45
var future = new Future;
6+
7+
// put this callback function back on the event loop (queue) in 2 seconds.
8+
// It will execute when it makes its way to the front of the line. This code
9+
// is called "asynchronous" because it executes at a *later time* on the
10+
// event loop.
511
setTimeout(function () {
12+
13+
// return a value at the point where we called "wait"
614
future.return({title: "My great title!"});
715
}, 2000);
816

17+
// pause execution of the fiber here. When we return the future, execution
18+
// will resume at this point, and the value returned from future.wait() will
19+
// be the parameter passed into future.return. In this case the value will
20+
// be the object: {title: "My great title"}
921
return future.wait();
1022
};
1123

@@ -18,6 +30,9 @@ getCollection = function (name, callback) {
1830
return future.wait();
1931
};
2032

33+
/**
34+
* Export an object we can use in other files that import this one.
35+
*/
2136
module.exports = {
2237
getCollection: getCollection
2338
};

pubsub/pubsub.js

+58-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,68 @@ if (Meteor.isClient) {
55
}
66
});
77

8-
Meteor.subscribe('allItems');
8+
Meteor.subscribe('one');
9+
Meteor.subscribe('two');
910
}
1011

1112
if (Meteor.isServer) {
13+
var Future = Npm.require('fibers/future');
14+
15+
Meteor.publish('one', function () {
16+
var future = new Future;
17+
18+
// send an "added" ddp message
19+
this.added('items', Random.id(), {title: 'one'});
20+
21+
// send a "ready" ddp message
22+
this.ready();
23+
24+
setTimeout(function () {
25+
future.return();
26+
}, 5000);
27+
28+
// Careful! This will block. Try not to block publish functions by yielding
29+
// the fiber. For example, don't make external API calls. You can, however,
30+
// put that code in a setTimeout to make it run outside this current fiber.
31+
// That will result in not blocking downstream publish functions from
32+
// running.
33+
return future.wait();
34+
});
35+
36+
Meteor.publish('two', function () {
37+
this.added('items', Random.id(), {title: 'two'});
38+
this.ready();
39+
});
40+
41+
1242
Meteor.publish('allItems', function () {
13-
return Items.find({},{sort: {order: 1}});
43+
var subscription = this;
44+
var cursor = Items.find({parentId: 4},{sort: {order: 1}});
45+
46+
// observeChanges is what hooks DDP up to the Mongo database for real time
47+
// updates. Every time a document that affects this cursor is added, changed
48+
// or removed, the corresponding callbacks here will be called. Then
49+
// the appropriate ddp messages are published to the client. The DDP server
50+
// takes care of making sure a client doesn't get duplicate messages.
51+
var handle = cursor.observeChanges({
52+
added: function (id, doc) {
53+
subscription.added('items', id, doc);
54+
},
55+
56+
changed: function (id, doc) {
57+
subscription.changed('items', id, doc);
58+
},
59+
60+
removed: function (id) {
61+
subscription.removed(id);
62+
}
63+
});
64+
65+
subscription.ready();
66+
67+
subscription.onStop(function () {
68+
handle.stop();
69+
});
1470
});
1571

1672
Meteor.publish('item', function (id) {

0 commit comments

Comments
 (0)