$synchronize works with promises too.

This commit is contained in:
Julien Fontanet 2013-12-17 23:25:33 +01:00
parent 6e75a3829a
commit 23311c0235
3 changed files with 28 additions and 18 deletions

View File

@ -6,6 +6,10 @@ $fiber = require 'fibers'
#=====================================================================
$isPromise = (obj) -> obj? and $_.isFunction obj.then
#=====================================================================
# Makes a function running in its own fiber.
$fiberize = (fn) ->
(args...) ->
@ -29,24 +33,22 @@ $synchronize = (fn, ctx) ->
fiber.throwInto error
else
fiber.run result
fn.apply ctx, args
result = fn.apply ctx, args
# A promise can only be detected once the function has been
# called.
if $isPromise result
result.then(
(result) -> fiber.run result
(error) -> fiber.throwInto result
)
$fiber.yield()
# TODO: remove promises ASAP.
$waitForPromise = (promise) ->
fiber = $fiber.current
promise.then(
(value) -> fiber.run value
(error) -> fiber.throwInto error
)
$fiber.yield()
#=====================================================================
module.exports = {
$fiberize
$sleep
$synchronize
$waitForPromise
}

View File

@ -25,7 +25,7 @@ $Session = require './session'
$XO = require './xo'
# Helpers for dealing with fibers.
{$fiberize, $waitForPromise} = require './fibers-utils'
{$fiberize, $synchronize} = require './fibers-utils'
# HTTP/HTTPS server which can listen on multiple ports.
$WebServer = require './web-server'
@ -59,10 +59,11 @@ $handleJsonRpcCall = (api, session, encodedRequest) ->
return formatError $API.err.INVALID_REQUEST
# Executes the requested method on the API.
exec = $synchronize 'exec', api
try
JSON.stringify {
jsonrpc: '2.0'
result: $waitForPromise (api.exec session, request)
result: exec session, request
id: request.id
}
catch error

View File

@ -28,12 +28,18 @@ $Model = require './model'
$XAPI = require './xapi'
# Helpers for dealing with fibers.
{$fiberize, $synchronize, $waitForPromise} = require './fibers-utils'
{$fiberize, $synchronize} = require './fibers-utils'
#=====================================================================
$hash = $synchronize 'hash', $hashy
$needsRehash = $synchronize 'needsRehash', $hashy
$randomBytes = $synchronize 'randomBytes', $crypto
$verifyHash = $synchronize 'verify', $hashy
#=====================================================================
# Models and collections.
@ -67,16 +73,16 @@ class $User extends $Model
validate: -> # TODO
setPassword: (password) ->
@set 'password', $waitForPromise ($hashy.hash password)
@set 'password', $hash password
# Checks the password and updates the hash if necessary.
checkPassword: (password) ->
hash = @get 'hash'
unless $waitForPromise ($hashy.verify password, hash)
unless $verifyHash password, hash
return false
if $waitForPromise ($hashy.needsRehash hash)
if $needsRehash hash
@setPassword password
true
@ -261,7 +267,8 @@ class $XO
throw error unless error[0] is 'SESSION_NOT_REGISTERED'
# Connects to existing servers.
connect server for server in $waitForPromise @servers.get()
getServers = $synchronize 'get', @servers
connect server for server in getServers()
# Automatically connects to new servers.
@servers.on 'add', (servers) ->