Skip to content

Commit

Permalink
Update promise.yue
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown-gd authored Aug 5, 2024
1 parent d24c776 commit 42e6c79
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions promise.yue
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,21 @@
--]]
_G = _G
import environment from _G.gpm
import Error, TypeError, RuntimeError, PromiseError, isfunction, istable, isstring, getmetatable, pcall, xpcall, error, ipairs, iserror from environment
import error, getmetatable, pcall, xpcall, ipairs, istable from _G
import Error, TypeError, RuntimeError, PromiseError, isstring, iserror, iscallable from environment
import create, resume, yield, running from coroutine
import format, match from environment.string
import remove from environment.table
import captureStack from Error
import Simple from timer

setTimeout = timer.Simple
-- getThenable = (obj) ->
-- thenFn = obj.Then or obj.next
-- return iscallable(thenFn) and thenFn

iscallable = (obj) ->
if isfunction obj
return true

meta = getmetatable obj
return meta and meta.__call and true or false

getThenable = (obj) ->
thenFn = obj.Then or obj.next
return iscallable(thenFn) and thenFn

getAwaitable = (obj) ->
awaitable = obj.Await or obj.await
return iscallable(awaitable) and awaitable
-- getAwaitable = (obj) ->
-- awaitable = obj.Await or obj.await
-- return iscallable(awaitable) and awaitable

once = ->
wasCalled = false
Expand All @@ -65,7 +58,9 @@ class Promise
STATE_FULFILLED = 2
STATE_REJECTED = 3

IsPromise = (obj) -> istable(obj) and obj.__class == Promise
IsPromise = (any) ->
metatable = getmetatable( any )
return metatable and metatable.__class == Promise

new: (executor) =>
@state = STATE_PENDING
Expand Down Expand Up @@ -115,7 +110,7 @@ class Promise
if @state == STATE_PENDING
return

setTimeout 0, ->
Simple 0, ->
queue = @queue
index = 1
while queue and queue[index]
Expand Down Expand Up @@ -154,14 +149,14 @@ class Promise
value.queue[] = @
else
finalizePromise( value, @ )
elseif istable value
ok, thenable = try getThenable(value)
unless ok then return reject( @, thenable )

if thenable
resolveThenable( @, value, thenable )
else
fulfill( @, value )
-- elseif istable value
-- ok, thenable = try getThenable(value)
-- unless ok then return reject( @, thenable )

-- if thenable
-- resolveThenable( @, value, thenable )
-- else
-- fulfill( @, value )
else
fulfill( @, value )

Expand Down Expand Up @@ -288,21 +283,23 @@ class Promise
Then( p, onResolve, onReject )
return wait!

thenable = istable(p) and getThenable(p)
if iscallable( thenable )
pcall( thenable, p, onResolve, onReject )
return wait!
-- thenable = istable(p) and getThenable(p)
-- if iscallable( thenable )
-- pcall( thenable, p, onResolve, onReject )
-- return wait!

return true, p

Await = (p) ->
awaitable = istable(p) and getAwaitable(p)
if awaitable and not IsPromise(p)
return awaitable(p)
-- awaitable = istable(p) and getAwaitable(p)
-- if awaitable and not IsPromise(p)
-- return awaitable(p)

ok, result = SafeAwait(p)
if ok then return result
else error result
if ok
return result

return error( result )

@All: (promises) ->
p = Promise!
Expand Down Expand Up @@ -374,7 +371,7 @@ class Promise

@Delay: (time, value) ->
p = Promise!
setTimeout time, -> Resolve( p, value )
Simple time, -> Resolve( p, value )
return p

@HTTP: (options) ->
Expand Down

0 comments on commit 42e6c79

Please sign in to comment.