Browse files

Demo for quiescence.

  • Loading branch information...
1 parent ac4b0d6 commit c947cbf1b306ad1146f3260ef7e49c093e15d9d8 @glasser glasser committed Nov 5, 2012
View
1 examples/other/quiescence/.meteor/.gitignore
@@ -0,0 +1 @@
+local
View
7 examples/other/quiescence/.meteor/packages
@@ -0,0 +1,7 @@
+# Meteor packages used by this project, one per line.
+#
+# 'meteor add' and 'meteor remove' will edit this file for you,
+# but you can also edit it by hand.
+
+insecure
+preserve-inputs
View
35 examples/other/quiescence/quiescence.html
@@ -0,0 +1,35 @@
+<head>
+ <title>quiescence</title>
+</head>
+
+<body>
+ {{> clock}}
+ {{> updated}}
+ {{> stream}}
+</body>
+
+<template name="clock">
+ <h1>Current time</h1>
+ <p>{{time}}</p>
+</template>
+
+<template name="updated">
+ <h1>Update object</h1>
+ <p>The magic number is {{magic}}. Click the button to set it to a random
+ integer in the client stub. The server will add 0.5 the that integer.</p>
+ <button id='update-button'>Update</button>
+</template>
+
+<template name="stream">
+ <h1>Stream in results</h1>
+ <p>Click the button. Note that the results stream in instead of appearing all
+ at once. Note that the clock continues to tick while the streaming method
+ is running. Note that the "update" button above works while the streaming
+ method is running (ie, the server's "add 0.5" is processed).</p>
+ <button id='stream-button'>Stream</button>
+ <ul>
+ {{#each results}}
+ <li>{{text}}</li>
+ {{/each}}
+ </ul>
+</template>
View
95 examples/other/quiescence/quiescence.js
@@ -0,0 +1,95 @@
+Time = new Meteor.Collection("time");
+Results = new Meteor.Collection("results");
+Magic = new Meteor.Collection("magic");
+
+if (Meteor.isServer) {
+ Meteor.publish("time", function () {
+ var self = this;
+ var publishTime = function () {
+ var when = + new Date;
+ self.set("time", "now", {timestamp: when});
+ self.flush();
+ };
+ publishTime();
+ self.complete();
+ self.flush();
+ var interval = Meteor.setInterval(publishTime, 1000);
+ self.onStop(function () {
+ Meteor.clearInterval(interval);
+ });
+ });
+ Meteor.publish("results", function () {
+ return Results.find();
+ });
+ Meteor.publish("magic", function () {
+ return Magic.find();
+ });
+
+ Meteor.startup(function () {
+ if (Magic.find().count() === 0) {
+ Magic.insert({number: 42});
+ }
+ });
+
+ var sleep = function (ms) {
+ var fiber = Fiber.current;
+ setTimeout(function() {
+ fiber.run();
+ }, ms);
+ Fiber.yield();
+ };
+
+ Meteor.methods({
+ getResults: function () {
+ this.unblock();
+ Results.remove({});
+ for (var i = 0; i < 5; ++i) {
+ sleep(1000);
+ Results.insert({i: i, text: 'result ' + i});
+ }
+ }});
+} else {
+ Meteor.subscribe("time");
+ Meteor.subscribe("results");
+ Meteor.subscribe("magic");
+
+ Template.clock.time = function () {
+ var now = Time.findOne('now');
+ if (!now)
+ return "(loading)";
+ return new Date(now.timestamp).toTimeString();
+ };
+
+ Template.updated.magic = function () {
+ var singleton = Magic.findOne();
+ if (!singleton)
+ return "(loading)";
+ return singleton.number;
+ };
+ Template.updated.events({
+ 'click #update-button': function () {
+ var num = Math.round(Math.random()*100);
+ Meteor.call('setMagic', num);
+ }
+ });
+
+ Template.stream.events({
+ 'click #stream-button': function () {
+ Meteor.call('getResults');
+ }
+ });
+
+ Template.stream.results = function () {
+ return Results.find({}, {sort: ['i']});
+ };
+}
+
+Meteor.methods({
+ setMagic: function (num) {
+ if (this.isSimulation) {
+ Magic.update({}, {$set: {number: num}});
+ } else {
+ Magic.update({}, {$set: {number: num + 0.5}});
+ }
+ }
+});

0 comments on commit c947cbf

Please sign in to comment.