forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileScriptBlock.cs
More file actions
45 lines (38 loc) · 1.55 KB
/
WhileScriptBlock.cs
File metadata and controls
45 lines (38 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack.Script
{
/// <summary>
/// while block
/// Usages: {{#while times > 0}} {{times}}. {{times - 1 | to => times}} {{/while}}
/// {{#while b}} {{ false | to => b }} {{else}} {{b}} was false {{/while}}
///
/// Max Iterations = Context.Args[ScriptConstants.MaxQuota]
/// </summary>
public class WhileScriptBlock : ScriptBlock
{
public override string Name => "while";
public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken ct)
{
var result = await block.Argument.GetJsExpressionAndEvaluateToBoolAsync(scope,
ifNone: () => throw new NotSupportedException("'while' block does not have a valid expression")).ConfigAwait();
var iterations = 0;
if (result)
{
do
{
await WriteBodyAsync(scope, block, ct);
result = await block.Argument.GetJsExpressionAndEvaluateToBoolAsync(scope,
ifNone: () => throw new NotSupportedException("'while' block does not have a valid expression"));
Context.DefaultMethods.AssertWithinMaxQuota(iterations++);
} while (result);
}
else
{
await WriteElseAsync(scope, block.ElseBlocks, ct);
}
}
}
}