Last active
August 29, 2015 14:21
-
-
Save Nullpo/5a32571d4c1f9a907761 to your computer and use it in GitHub Desktop.
scout.promises
This file contains hidden or 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
| //Client code | |
| var socket = io.connect('http://localhost:3000'); | |
| var sock = new Scout(socket, "jquery", $); | |
| var params = { | |
| time:1000, | |
| message:"Timeouteaste" | |
| }; | |
| sock.emit("tiempoEspera", params).done(function(response){ | |
| console.log("response recibida! %o", response); | |
| }); |
This file contains hidden or 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
| doctype html | |
| html | |
| script(src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js') | |
| script(src='/socket.io/socket.io.js') | |
| script(src='scout.js') | |
| script(src='app.js') | |
| head | |
| title= title | |
| link(rel='stylesheet', href='/stylesheets/style.css') | |
| body | |
| block content |
This file contains hidden or 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
| 'use strict' | |
| // Scout.js es isomorfica (se puede usar tanto desde el cliente como desde el server). | |
| var Promises = {}; | |
| Promises.q = function(deferred){ | |
| var promise = deferred && deferred.promise; | |
| return { | |
| create: function (q) { | |
| return q.defer(); | |
| }, | |
| resolve: function (args) { | |
| return deferred.resolve(args); | |
| }, | |
| reject: function (callback) { | |
| return deferred.reject(callback); | |
| }, | |
| then: function (callback) { | |
| return promise.then(callback); | |
| } | |
| }; | |
| }; | |
| Promises.jquery = function(promise){ | |
| return { | |
| create: function ($) { | |
| return $.Deferred(); | |
| }, | |
| resolve: function (args) { | |
| return promise.resolve(args); | |
| }, | |
| reject: function (callback) { | |
| return promise.fail(callback); | |
| }, | |
| then: function (callback) { | |
| return promise.done(callback); | |
| } | |
| }; | |
| }; | |
| var Scout = function(socket, promiseName, promiseObject){ | |
| var WithPromise = Promises[promiseName]; | |
| var WhenPromise = WithPromise; | |
| var PromisesLib = WithPromise(); | |
| return { | |
| emit: function(evtName, params) { | |
| var deferred = PromisesLib.create(promiseObject); | |
| console.log("Emitiendo"); | |
| socket.emit("scout_" + evtName, params); | |
| socket.on("scout_" + evtName, function resolvedPromise(resp){ | |
| socket.removeListener("scout_" + evtName, resolvedPromise); | |
| WithPromise(deferred).resolve(resp); | |
| }); | |
| console.log("Socket returns: %o", socket); | |
| return deferred; | |
| }, | |
| on: function(evtName, callback){ | |
| socket.on("scout_" + evtName, function(data){ | |
| var deferred = PromisesLib.create(promiseObject); | |
| WhenPromise(deferred).then(function(param){ | |
| socket.emit("scout_" + evtName, param); | |
| }); | |
| callback.call(this,data,deferred); | |
| }); | |
| } | |
| } | |
| }; | |
| if(typeof module !== 'undefined'){ | |
| module.exports = Scout; | |
| } |
This file contains hidden or 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
| //after all the express code: | |
| var io = require('socket.io')(server); | |
| var Q = require('q'); | |
| var Scout = require('../public/javascripts/scout.js'); | |
| io.on('connection', function (socket) { | |
| var sock = Scout(socket,"q",Q); | |
| sock.on('tiempoEspera', function(data, defer){ | |
| console.log("Recibido el evento tiempoEspera:", data); | |
| setInterval(function(){ | |
| data.status = "Resolved"; | |
| defer.resolve(data); | |
| },data.time); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment