Skip to content

Instantly share code, notes, and snippets.

View possibilities's full-sized avatar

Mike Bannister possibilities

  • AI Life
  • Portland East
View GitHub Profile
(function() {
var firstBlood = true;
Template.foof.moof = function() {
if (firstBlood) {
firstBlood = false;
doShitFirstTimeAround();
} else{
AWSSum = {
load: function(module) {
var awssum = NodeModules.require('awssum');
return awssum.load(module);
},
loadSyncInterface: function(module, className) {
// Get a reference to the original interface
var interface = AWSSum.load(module)[className];
13:25 deberg: [17:21:57] hey, i just saw your question.
13:25 eiki: [17:22:17] great - any ideas?
13:25 deberg: [17:22:19] the thing to use is futures.
13:25 eiki: [17:22:36] what's the difference?
13:25 eiki: [17:22:45] and do you know of any sample code?
13:25 deberg: [17:23:02] yeah, one place to look is the meteor HTTP package.
13:25 deberg: [17:23:11] packages/http/httpcall_server.js:64
13:25 deberg: [17:23:39] this is the basic pattern for wrapping a node callback.
13:25 eiki: [17:24:09] https://github.com/meteor/meteor/blob/master/packages/http/httpcall_server.js#L65
13:25 deberg: [17:24:35] you want to declare a new future, call future.return() inside the callback, and block the outer caller (your method) with future.wait().
@possibilities
possibilities / meteor-async.md
Created August 23, 2012 22:53
Meteor Async Guide

From Meteor's documentation:

In Meteor, your server code runs in a single thread per request, not in the asynchronous callback style typical of Node. We find the linear execution model a better fit for the typical server code in a Meteor application.

This guide serves as a mini-tour of tools, trix and patterns that can be used to run async code in Meteor.

Basic async

Sometimes we need to run async code in Meteor.methods. For this we create a Future to block until the async code has finished. This pattern can be seen all over Meteor's own codebase:

Package.describe({
summary: "Moment.js packaged for Meteor"
});
Package.on_use(function (api, where) {
where = where || ['client', 'server'];
api.add_files('lib/moment/moment.js', where);
});
# Setup dir and repo
mkdir underscore-string
cd underscore-string
git init
# Make some files we'll need
touch README.md package.js smart.json
# Add the submodule and checkout the desired branch
git submodule add git://github.com/epeli/underscore.string.git lib/underscore.string
12:34 tmeasday: possibilities, homunq: autosubscribe does a little bit more than that. Maybe you don't care but it e.g. captures subscriptions and stops them etc. You might want to take a look at https://github.com/tmeasday/meteor-deps-extensions
12:35 possibilities: tmeasday: but seems to work well without doing any of that, is there overhead
12:35 tmeasday: well, homunq isn't here anymore, but hey..
12:35 tmeasday: possibilities: hard to say i guess
12:36 possibilities: tmeasday: so you have your own lighter weight solution eh?
12:36 tmeasday: yeah, a couple of things i reckon they should add to the deps module.. i've thought of a few more actually but haven't gotten around to implementing them
12:36 tmeasday: I got tired of writing the same code all the time
12:37 possibilities: good work. i've been overloading autosubscribe to do all this. ends up with a lot of bookkeeping code.
{{#if widgetsLoading}}
<p>loading widgets...</p>
{{else}}
{{#ifAny widgets}}
{{#each widgets}}
<p>Name: {{name}}</p>
{{/each}}
{{/ifAny}}
{{/if}}
var userCount = 0;
Meteor.publish(null, function() {
userCount++;
this.onStop(function() {
userCount--;
});
});
Meteor.methods({