Skip to content

Commit 4b9a37f

Browse files
authored
Merge pull request #1 from sarmadka/main
Initial implementation.
2 parents b45b728 + c26b52c commit 4b9a37f

11 files changed

Lines changed: 755 additions & 2 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.apm
2+

Examples/example.alusus

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import "Srl/Console";
2+
import "Apm";
3+
Apm.importFile("Alusus/Promises");
4+
5+
use Srl;
6+
use Promises;
7+
8+
class Num {
9+
def val: Int;
10+
handler this~init() this.val = 0;
11+
handler this~init(i: Int) this.val = i;
12+
handler this~init(i: ref[Num]) this.val = i.val;
13+
handler this = ref[Num] this.val = value.val;
14+
}
15+
16+
// Test Promise.
17+
func testPromise {
18+
Console.print("Test Promise:\n");
19+
def promise: SrdRef[Promise[Num]] = Promise[Num].new();
20+
Console.print("promise 1 - status: %d, value: %d\n", promise.status, promise.result.val);
21+
promise.resolve(Num(5));
22+
Console.print("promise 1 - status: %d, value: %d\n", promise.status, promise.result.val);
23+
promise.reject(castSrdRef[SrdRef[GenericError]().{
24+
construct();
25+
code = 1;
26+
message = String("Unknown error 1");
27+
}, Error]);
28+
Console.print("promise 1 - status: %d, error: %ld\n", promise.status, promise.error.obj~ptr);
29+
30+
promise = Promise[Num].new();
31+
Console.print("promise 2 - status: %d, value: %d\n", promise.status, promise.result.val);
32+
promise.reject(castSrdRef[SrdRef[GenericError]().{
33+
construct();
34+
code = 1;
35+
message = String("Unknown error 2");
36+
}, Error]);
37+
Console.print("promise 2 - status: %d, error: %s\n", promise.status, promise.error.getMessage().buf);
38+
promise.resolve(Num(5));
39+
Console.print("promise 2 - status: %d, value: %d\n", promise.status, promise.result.val);
40+
}
41+
testPromise();
42+
43+
// Test Promise.then
44+
func testPromiseThen {
45+
Console.print("\nTest Promise.then:\n");
46+
def promise: SrdRef[Promise[Num]] = Promise[Num].new();
47+
def then: SrdRef[Promise[String]] = promise.then[String](
48+
closure (input: Num, p: ref[Promise[String]]) {
49+
Console.print("ThenPromise triggered\n");
50+
p.resolve(String("ThenPromise - received value: ") + input.val);
51+
}
52+
);
53+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
54+
promise.resolve(Num(6));
55+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
56+
}
57+
testPromiseThen();
58+
59+
// Test Promise.catch
60+
func testPromiseCatch {
61+
Console.print("\nTest Promise.catch:\n");
62+
def promise: SrdRef[Promise[Num]] = Promise[Num].new();
63+
def catch: SrdRef[Promise[Num]] = promise.catch(
64+
closure (err: SrdRef[Error], p: ref[Promise[Num]]) {
65+
Console.print("CatchPromise triggered. error: %s\n", err.getMessage().buf);
66+
p.resolve(Num(7));
67+
}
68+
);
69+
Console.print("status: %d, value: %d\n", catch.status, catch.result.val);
70+
promise.reject(castSrdRef[SrdRef[GenericError]().{
71+
construct();
72+
code = 1;
73+
message = String("Unknown error 3");
74+
}, Error]);
75+
Console.print("status: %d, value: %d\n", catch.status, catch.result.val);
76+
}
77+
testPromiseCatch();
78+
79+
// Test Promise.then.catch
80+
func testPromiseThenCatch {
81+
Console.print("\nTest Promise.then.catch:\n");
82+
def promise: SrdRef[Promise[Num]] = Promise[Num].new();
83+
def catch: SrdRef[Promise[String]] = promise.then[String](
84+
closure (input: Num, p: ref[Promise[String]]) {
85+
Console.print("ThenPromise triggered\n");
86+
p.resolve(String("ThenPromise - received value: ") + input.val);
87+
}
88+
).catch(
89+
closure (err: SrdRef[Error], p: ref[Promise[String]]) {
90+
Console.print("CatchPromise triggered\n");
91+
p.resolve(String("CatchPromise - error was: ") + err.getMessage());
92+
}
93+
);
94+
Console.print("status: %d, value: %s\n", catch.status, catch.result.buf);
95+
promise.reject(castSrdRef[SrdRef[GenericError]().{
96+
construct();
97+
code = 1;
98+
message = String("Unknown error 4");
99+
}, Error]);
100+
Console.print("status: %d, value: %s\n", catch.status, catch.result.buf);
101+
}
102+
testPromiseThenCatch();
103+
104+
// Test Promise.catch.then
105+
func testPromiseCatchThen {
106+
Console.print("\nTest Promise.catch.then:\n");
107+
def promise: SrdRef[Promise[Num]] = Promise[Num].new();
108+
def then: SrdRef[Promise[String]] = promise.catch(
109+
closure (err: SrdRef[Error], p: ref[Promise[Num]]) {
110+
Console.print("CatchPromise triggered\n");
111+
p.resolve(Num(8));
112+
}
113+
).then[String](
114+
closure (input: Num, p: ref[Promise[String]]) {
115+
Console.print("ThenPromise triggered\n");
116+
p.resolve(String("ThenPromise - received value: ") + input.val);
117+
}
118+
);
119+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
120+
promise.resolve(Num(9));
121+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
122+
}
123+
testPromiseCatchThen();
124+
125+
// Test AllPromise resolve.
126+
func testAllPromiseThen {
127+
Console.print("\nTest AllPromise.then:\n");
128+
def promise1: SrdRef[Promise[Int]] = Promise[Int].new();
129+
def promise2: SrdRef[Promise[Int]] = Promise[Int].new();
130+
def then: SrdRef[Promise[String]] = Promise[Int]
131+
.all({ promise1, promise2 })
132+
.then[String](
133+
closure (input: Array[Int], p: ref[Promise[String]]) {
134+
Console.print("ThenPromise triggered\n");
135+
p.resolve(String("ThenPromise - received values: ") + input(0) + ", " + input(1));
136+
}
137+
);
138+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
139+
promise1.resolve(10);
140+
Console.print("promise1 resolved. status: %d, value: %s\n", then.status, then.result.buf);
141+
promise2.resolve(11);
142+
Console.print("promise2 resolved. status: %d, value: %s\n", then.status, then.result.buf);
143+
}
144+
testAllPromiseThen();
145+
146+
// Test AllPromise reject.
147+
func testAllPromiseCatch {
148+
Console.print("\nTest AllPromise.catch:\n");
149+
def promise1: SrdRef[Promise[Int]] = Promise[Int].new();
150+
def promise2: SrdRef[Promise[Int]] = Promise[Int].new();
151+
def catchPromise: SrdRef[Promise[String]] = Promise[Int]
152+
.all({ promise1, promise2 })
153+
.then[String](
154+
closure (input: Array[Int], p: ref[Promise[String]]) {
155+
Console.print("ThenPromise triggered\n");
156+
p.resolve(String("ThenPromise - received values: ") + input(0) + ", " + input(1));
157+
}
158+
).catch(
159+
closure (err: SrdRef[Error], p: ref[Promise[String]]) {
160+
Console.print("CatchPromise triggered\n");
161+
p.resolve(String("CatchPromise - error was: ") + err.getMessage());
162+
}
163+
);
164+
Console.print("status: %d, value: %s\n", catchPromise.status, catchPromise.result.buf);
165+
promise1.resolve(10);
166+
Console.print("promise1 resolved. status: %d, value: %s\n", catchPromise.status, catchPromise.result.buf);
167+
promise2.reject(castSrdRef[SrdRef[GenericError]().{
168+
construct();
169+
code = 1;
170+
message = String("Unknown error 5");
171+
}, Error]);
172+
Console.print("promise2 rejected. status: %d, value: %s\n", catchPromise.status, catchPromise.result.buf);
173+
}
174+
testAllPromiseCatch();
175+
176+
func testIgnoreResult {
177+
Console.print("\nTest this.ignoreResult:\n");
178+
def promise1: SrdRef[Promise[Num]] = Promise[Num].new();
179+
def promise2: SrdRef[Promise[String]] = Promise[String].new();
180+
def then: SrdRef[Promise[String]] = Promise[Int]
181+
.all({ promise1.ignoreResult(), promise2.ignoreResult() })
182+
.then[String](
183+
closure (input: Array[Int], p: ref[Promise[String]]) {
184+
Console.print("ThenPromise triggered\n");
185+
p.resolve(String("ThenPromise - received values: ") + input(0) + ", " + input(1));
186+
}
187+
);
188+
Console.print("status: %d, value: %s\n", then.status, then.result.buf);
189+
promise1.resolve(Num(10));
190+
Console.print("promise1 resolved. status: %d, value: %s\n", then.status, then.result.buf);
191+
promise2.resolve(String("11"));
192+
Console.print("promise2 resolved. status: %d, value: %s\n", then.status, then.result.buf);
193+
}
194+
testIgnoreResult();
195+
196+
// TODO: Localize
197+

Examples/example.output

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Test Promise:
2+
promise 1 - status: 0, value: 0
3+
promise 1 - status: 1, value: 5
4+
promise 1 - status: 1, error: 0
5+
promise 2 - status: 0, value: 0
6+
promise 2 - status: 2, error: Unknown error 2
7+
promise 2 - status: 2, value: 0
8+
9+
Test Promise.then:
10+
status: 0, value:
11+
ThenPromise triggered
12+
status: 1, value: ThenPromise - received value: 6
13+
14+
Test Promise.catch:
15+
status: 0, value: 0
16+
CatchPromise triggered. error: Unknown error 3
17+
status: 1, value: 7
18+
19+
Test Promise.then.catch:
20+
status: 0, value:
21+
CatchPromise triggered
22+
status: 1, value: CatchPromise - error was: Unknown error 4
23+
24+
Test Promise.catch.then:
25+
status: 0, value:
26+
ThenPromise triggered
27+
status: 1, value: ThenPromise - received value: 9
28+
29+
Test AllPromise.then:
30+
status: 0, value:
31+
promise1 resolved. status: 0, value:
32+
ThenPromise triggered
33+
promise2 resolved. status: 1, value: ThenPromise - received values: 10, 11
34+
35+
Test AllPromise.catch:
36+
status: 0, value:
37+
promise1 resolved. status: 0, value:
38+
CatchPromise triggered
39+
promise2 rejected. status: 1, value: CatchPromise - error was: Unknown error 5
40+
41+
Test this.ignoreResult:
42+
status: 0, value:
43+
promise1 resolved. status: 0, value:
44+
ThenPromise triggered
45+
promise2 resolved. status: 1, value: ThenPromise - received values: 0, 0

0 commit comments

Comments
 (0)