Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes Promise overridable #262

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions NiL.JS/BaseLibrary/Promise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@

namespace NiL.JS.BaseLibrary
{
public enum PromiseState
{
Pending,
Fulfilled,
Rejected
}

public sealed class Promise
public sealed class Promise : IPromiseLike
{
private Task _innerTask;
private Function _callback;
Expand Down Expand Up @@ -177,20 +170,20 @@ private void callbackInvoke()
_outerTask.SetResult(JSValue.undefined);
}

public static Promise resolve(JSValue data)
public static IPromiseLike resolve(JSValue data)
{
return new Promise(fromResult(data));
}

public static Promise race(IIterable promises)
public static IPromiseLike race(IIterable promises)
{
if (promises == null)
return new Promise(fromException(new JSException(new TypeError("Invalid argruments for Promise.race(...)"))));

return new Promise(whenAny(promises.AsEnumerable().Select(convertToTask).ToArray()));
}

public static Promise all(IIterable promises)
public static IPromiseLike all(IIterable promises)
{
if (promises == null)
return new Promise(fromException(new JSException(new TypeError("Invalid argruments for Promise.all(...)"))));
Expand All @@ -203,20 +196,20 @@ private static Task<JSValue> convertToTask(JSValue arg)
return (arg.Value as Promise)?.Task ?? fromResult(arg);
}

public Promise @catch(Function onRejection)
public IPromiseLike @catch(Function onRejection)
{
return then(null, onRejection);
}

public Promise then(Function onFulfilment, Function onRejection)
public IPromiseLike then(Function onFulfilment, Function onRejection)
{
return then(
onFulfilment == null ? null as Func<JSValue, JSValue> : value => onFulfilment.Call(JSValue.undefined, new Arguments { value }),
onRejection == null ? null as Func<JSValue, JSValue> : value => onRejection.Call(JSValue.undefined, new Arguments { value }));
}

[Hidden]
public Promise then(Func<JSValue, JSValue> onFulfilment, Func<JSValue, JSValue> onRejection)
public IPromiseLike then(Func<JSValue, JSValue> onFulfilment, Func<JSValue, JSValue> onRejection)
{
if (onFulfilment == null && onRejection == null)
return resolve(JSValue.undefined);
Expand Down
2 changes: 1 addition & 1 deletion NiL.JS/Core/Functions/AsyncFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Build(JSValue promise)

private JSValue subscribeOrReturnValue(JSValue promiseOrValue)
{
var p = promiseOrValue?.Value as Promise;
var p = promiseOrValue?.Value as IPromiseLike;
if (p == null)
return promiseOrValue;

Expand Down
30 changes: 30 additions & 0 deletions NiL.JS/Core/IPromiseLike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using NiL.JS.BaseLibrary;
using NiL.JS.Core.Interop;

namespace NiL.JS.Core
{
public enum PromiseState
{
Pending,
Fulfilled,
Rejected
}

public interface IPromiseLike
{
[Hidden]
PromiseState State { get; }

#pragma warning disable IDE1006

IPromiseLike @catch(Function onRejection);

IPromiseLike then(Function onFulfilment, Function onRejection);

[Hidden]
IPromiseLike then(Func<JSValue, JSValue> onFulfilment, Func<JSValue, JSValue> onRejection);

#pragma warning restore IDE1006
}
}
2 changes: 1 addition & 1 deletion NiL.JS/Expressions/AwaitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override JSValue Evaluate(Context context)
return null;
}

if (result != null && (result._valueType < JSValueType.Object || !(result.Value is Promise)))
if (result != null && (result._valueType < JSValueType.Object || !(result.Value is IPromiseLike)))
return result;

context._executionMode = ExecutionMode.Suspend;
Expand Down