Skip to content

Commit 07f03de

Browse files
committed
feat: Add asFuture to all SignalX objects
Should replace `futureSignal`, although that method still exists. refs: #30
1 parent 17765d7 commit 07f03de

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

example/2d_tutorial/src/lib/hud.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class Hud extends CanvasLayer {
3535
showMessage('Game Over');
3636

3737
var messageTimer = getNodeT<Timer>('MessageTimer');
38-
await futureSignal(messageTimer!, 'timeout');
38+
await messageTimer!.timeout.asFuture(this);
3939

4040
var message = getNodeT<Label>('Message');
4141
message?.setText('Dodge the \nCreeps!');
4242
message?.show();
4343

4444
final timer = getTree()!.createTimer(1.0);
45-
await futureSignal(timer!, 'timeout');
45+
await timer!.timeout.asFuture(this);
4646

4747
getNodeT<Button>('StartButton')?.show();
4848
}

src/dart/godot_dart/lib/src/core/signals.dart

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:math';
23

34
import 'package:meta/meta.dart';
@@ -126,6 +127,19 @@ class Signal0 extends SignalCallable {
126127
_subscriptions.unsubscribe(subscriptionKey);
127128
}
128129

130+
Future<void> asFuture(GodotObject target) {
131+
final completer = Completer<void>();
132+
var subscriptionKey = 0;
133+
subscriptionKey = connect(target, () {
134+
completer.complete();
135+
});
136+
137+
completer.future.then((_) {
138+
disconnect(subscriptionKey);
139+
});
140+
return completer.future;
141+
}
142+
129143
@override
130144
void unsubscribeAll(GodotObject target) {
131145
_subscriptions.unsubscribeAll(target);
@@ -157,6 +171,20 @@ class Signal1<P1> extends SignalCallable {
157171
_subscriptions.unsubscribe(subscriptionKey);
158172
}
159173

174+
Future<P1> asFuture(GodotObject target) {
175+
final completer = Completer<P1>();
176+
var subscriptionKey = 0;
177+
subscriptionKey = connect(target, (p1) {
178+
completer.complete(p1);
179+
});
180+
181+
completer.future.then((_) {
182+
disconnect(subscriptionKey);
183+
});
184+
185+
return completer.future;
186+
}
187+
160188
@override
161189
void clear() => _subscriptions.clear();
162190

@@ -191,6 +219,20 @@ class Signal2<P1, P2> extends SignalCallable {
191219
_subscriptions.unsubscribe(subscriptionKey);
192220
}
193221

222+
Future<(P1, P2)> asFuture(GodotObject target) {
223+
final completer = Completer<(P1, P2)>();
224+
var subscriptionKey = 0;
225+
subscriptionKey = connect(target, (p1, p2) {
226+
completer.complete((p1, p2));
227+
});
228+
229+
completer.future.then((_) {
230+
disconnect(subscriptionKey);
231+
});
232+
233+
return completer.future;
234+
}
235+
194236
@override
195237
void clear() => _subscriptions.clear();
196238

@@ -226,6 +268,20 @@ class Signal3<P1, P2, P3> extends SignalCallable {
226268
_subscriptions.unsubscribe(subscriptionKey);
227269
}
228270

271+
Future<(P1, P2, P3)> asFuture(GodotObject target) {
272+
final completer = Completer<(P1, P2, P3)>();
273+
var subscriptionKey = 0;
274+
subscriptionKey = connect(target, (p1, p2, p3) {
275+
completer.complete((p1, p2, p3));
276+
});
277+
278+
completer.future.then((_) {
279+
disconnect(subscriptionKey);
280+
});
281+
282+
return completer.future;
283+
}
284+
229285
@override
230286
void clear() => _subscriptions.clear();
231287

@@ -262,6 +318,20 @@ class Signal4<P1, P2, P3, P4> extends SignalCallable {
262318
_subscriptions.unsubscribe(subscriptionKey);
263319
}
264320

321+
Future<(P1, P2, P3, P4)> asFuture(GodotObject target) {
322+
final completer = Completer<(P1, P2, P3, P4)>();
323+
var subscriptionKey = 0;
324+
subscriptionKey = connect(target, (p1, p2, p3, p4) {
325+
completer.complete((p1, p2, p3, p4));
326+
});
327+
328+
completer.future.then((_) {
329+
disconnect(subscriptionKey);
330+
});
331+
332+
return completer.future;
333+
}
334+
265335
@override
266336
void clear() => _subscriptions.clear();
267337

@@ -286,7 +356,7 @@ class Signal5<P1, P2, P3, P4, P5> extends SignalCallable {
286356
args[1].cast<P2>() as P2,
287357
args[2].cast<P3>() as P3,
288358
args[3].cast<P4>() as P4,
289-
args[4].cast<P4>() as P5,
359+
args[4].cast<P5>() as P5,
290360
);
291361
}
292362
}
@@ -299,6 +369,20 @@ class Signal5<P1, P2, P3, P4, P5> extends SignalCallable {
299369
_subscriptions.unsubscribe(subscriptionKey);
300370
}
301371

372+
Future<(P1, P2, P3, P4, P5)> asFuture(GodotObject target) {
373+
final completer = Completer<(P1, P2, P3, P4, P5)>();
374+
var subscriptionKey = 0;
375+
subscriptionKey = connect(target, (p1, p2, p3, p4, p5) {
376+
completer.complete((p1, p2, p3, p4, p5));
377+
});
378+
379+
completer.future.then((_) {
380+
disconnect(subscriptionKey);
381+
});
382+
383+
return completer.future;
384+
}
385+
302386
@override
303387
void clear() => _subscriptions.clear();
304388

0 commit comments

Comments
 (0)