$synchronize works with promises too.
This commit is contained in:
parent
6e75a3829a
commit
23311c0235
@ -6,6 +6,10 @@ $fiber = require 'fibers'
|
|||||||
|
|
||||||
#=====================================================================
|
#=====================================================================
|
||||||
|
|
||||||
|
$isPromise = (obj) -> obj? and $_.isFunction obj.then
|
||||||
|
|
||||||
|
#=====================================================================
|
||||||
|
|
||||||
# Makes a function running in its own fiber.
|
# Makes a function running in its own fiber.
|
||||||
$fiberize = (fn) ->
|
$fiberize = (fn) ->
|
||||||
(args...) ->
|
(args...) ->
|
||||||
@ -29,24 +33,22 @@ $synchronize = (fn, ctx) ->
|
|||||||
fiber.throwInto error
|
fiber.throwInto error
|
||||||
else
|
else
|
||||||
fiber.run result
|
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()
|
$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 = {
|
module.exports = {
|
||||||
$fiberize
|
$fiberize
|
||||||
$sleep
|
$sleep
|
||||||
$synchronize
|
$synchronize
|
||||||
$waitForPromise
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ $Session = require './session'
|
|||||||
$XO = require './xo'
|
$XO = require './xo'
|
||||||
|
|
||||||
# Helpers for dealing with fibers.
|
# Helpers for dealing with fibers.
|
||||||
{$fiberize, $waitForPromise} = require './fibers-utils'
|
{$fiberize, $synchronize} = require './fibers-utils'
|
||||||
|
|
||||||
# HTTP/HTTPS server which can listen on multiple ports.
|
# HTTP/HTTPS server which can listen on multiple ports.
|
||||||
$WebServer = require './web-server'
|
$WebServer = require './web-server'
|
||||||
@ -59,10 +59,11 @@ $handleJsonRpcCall = (api, session, encodedRequest) ->
|
|||||||
return formatError $API.err.INVALID_REQUEST
|
return formatError $API.err.INVALID_REQUEST
|
||||||
|
|
||||||
# Executes the requested method on the API.
|
# Executes the requested method on the API.
|
||||||
|
exec = $synchronize 'exec', api
|
||||||
try
|
try
|
||||||
JSON.stringify {
|
JSON.stringify {
|
||||||
jsonrpc: '2.0'
|
jsonrpc: '2.0'
|
||||||
result: $waitForPromise (api.exec session, request)
|
result: exec session, request
|
||||||
id: request.id
|
id: request.id
|
||||||
}
|
}
|
||||||
catch error
|
catch error
|
||||||
|
@ -28,12 +28,18 @@ $Model = require './model'
|
|||||||
$XAPI = require './xapi'
|
$XAPI = require './xapi'
|
||||||
|
|
||||||
# Helpers for dealing with fibers.
|
# 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
|
$randomBytes = $synchronize 'randomBytes', $crypto
|
||||||
|
|
||||||
|
$verifyHash = $synchronize 'verify', $hashy
|
||||||
|
|
||||||
#=====================================================================
|
#=====================================================================
|
||||||
# Models and collections.
|
# Models and collections.
|
||||||
|
|
||||||
@ -67,16 +73,16 @@ class $User extends $Model
|
|||||||
validate: -> # TODO
|
validate: -> # TODO
|
||||||
|
|
||||||
setPassword: (password) ->
|
setPassword: (password) ->
|
||||||
@set 'password', $waitForPromise ($hashy.hash password)
|
@set 'password', $hash password
|
||||||
|
|
||||||
# Checks the password and updates the hash if necessary.
|
# Checks the password and updates the hash if necessary.
|
||||||
checkPassword: (password) ->
|
checkPassword: (password) ->
|
||||||
hash = @get 'hash'
|
hash = @get 'hash'
|
||||||
|
|
||||||
unless $waitForPromise ($hashy.verify password, hash)
|
unless $verifyHash password, hash
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if $waitForPromise ($hashy.needsRehash hash)
|
if $needsRehash hash
|
||||||
@setPassword password
|
@setPassword password
|
||||||
|
|
||||||
true
|
true
|
||||||
@ -261,7 +267,8 @@ class $XO
|
|||||||
throw error unless error[0] is 'SESSION_NOT_REGISTERED'
|
throw error unless error[0] is 'SESSION_NOT_REGISTERED'
|
||||||
|
|
||||||
# Connects to existing servers.
|
# 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.
|
# Automatically connects to new servers.
|
||||||
@servers.on 'add', (servers) ->
|
@servers.on 'add', (servers) ->
|
||||||
|
Loading…
Reference in New Issue
Block a user