Skip to content

Commit b88b159

Browse files
update
1 parent d4e0255 commit b88b159

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/main/java/top/rgb39/ecs/executor/RuntimeChain.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.concurrent.CompletableFuture;
6+
7+
import javax.annotation.Nullable;
8+
59
import top.rgb39.ecs.arch.App;
610

711
public class RuntimeChain {
812
private Map<String, SystemChain> sysChains = new HashMap<>();
13+
14+
@Nullable
915
private String currentSchedule = null;
1016

1117
public void setSystemChain(String runtimeLabel, SystemChain sysChain) {
@@ -28,16 +34,27 @@ public String[] getScheduleSequence() {
2834
return (String[]) this.sysChains.keySet().toArray(new String[0]);
2935
}
3036

31-
public void scheduleOnce(App app) {
32-
String[] scheduleSeq = getScheduleSequence();
33-
for (int i = 0; i < scheduleSeq.length; i++) {
34-
currentSchedule = scheduleSeq[i];
35-
SystemChain sysChain = this.sysChains.get(scheduleSeq[i]);
36-
sysChain.run(app);
37-
}
37+
public CompletableFuture<?> scheduleOnce(App app) {
38+
return CompletableFuture.runAsync(() -> {
39+
String[] scheduleSeq = getScheduleSequence();
40+
var futureList = new CompletableFuture[scheduleSeq.length];
41+
42+
for (int i = 0; i < scheduleSeq.length; i++) {
43+
currentSchedule = scheduleSeq[i];
44+
SystemChain sysChain = this.sysChains.get(scheduleSeq[i]);
45+
futureList[i] = sysChain.run(app);
46+
}
47+
48+
CompletableFuture.allOf(futureList).join();
49+
currentSchedule = null;
50+
});
3851
}
3952

4053
public String current() {
4154
return currentSchedule;
4255
}
56+
57+
public boolean idle() {
58+
return currentSchedule == null;
59+
}
4360
}

src/main/java/top/rgb39/ecs/executor/SystemChain.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ public void runWithOnlyReflects(App app) {
2323
ParameterImplementor implementor = new ParameterImplementor(method.getParameters());
2424
implementor.matchArgumentsOnlyReflect(app);
2525
try {
26-
implementor.invoke(SystenInstanceRecord.getInstance(method.getDeclaringClass()), method);
26+
implementor.invoke(SystemInstanceRecord.getInstance(method.getDeclaringClass()), method);
2727
} catch (Exception e) {
2828
e.printStackTrace();
2929
}
3030
}
3131
}
3232

33-
public void run(App app) {
33+
public CompletableFuture<?> run(App app) {
3434
if (Objects.isNull(app.table)) {
35-
return;
35+
return CompletableFuture.completedFuture(null);
3636
}
3737

38+
List<CompletableFuture<?>> futureList = new ArrayList<>();
39+
3840
for (Method system : this.systems) {
3941
SystemConfig sysConfig = configRecord.get(system);
4042

@@ -48,23 +50,26 @@ public void run(App app) {
4850
implementor.matchArguments(app, row.getRowId());
4951
try {
5052
if (sysConfig.asynchronous()) {
51-
CompletableFuture.supplyAsync(() -> {
53+
var future = CompletableFuture.supplyAsync(() -> {
5254
try {
53-
return implementor.invoke(SystenInstanceRecord.getInstance(system.getDeclaringClass()), system);
55+
return implementor.invoke(SystemInstanceRecord.getInstance(system.getDeclaringClass()), system);
5456
} catch (Exception e) {
5557
e.printStackTrace();
5658
return null;
5759
}
5860
});
61+
62+
futureList.add(future);
5963
} else {
60-
implementor.invoke(SystenInstanceRecord.getInstance(system.getDeclaringClass()), system);
64+
implementor.invoke(SystemInstanceRecord.getInstance(system.getDeclaringClass()), system);
6165
}
62-
Logger.info("tick", FontColors.GREEN, "system: " + system.getName());
6366
} catch (Exception e) {
6467
Logger.info("tick", FontColors.RED, "system failed: " + system.getName());
6568
}
6669
}
6770
}
71+
72+
return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));
6873
}
6974

7075
public void addSystem(Method system) {

src/main/java/top/rgb39/ecs/executor/SystenInstanceRecord.java renamed to src/main/java/top/rgb39/ecs/executor/SystemInstanceRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Map;
55
import java.util.Objects;
66

7-
public class SystenInstanceRecord {
7+
public class SystemInstanceRecord {
88
private static Map<Class<?>, Object> instRecord = new HashMap<>();
99

1010
public static void setInst(Class<?> clazz, Object instance) {

src/main/java/top/rgb39/ecs/plugin/Events.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import top.rgb39.ecs.executor.ParameterMatcher;
1818
import top.rgb39.ecs.executor.RuntimeLabel;
1919
import top.rgb39.ecs.executor.SystemConfig;
20-
import top.rgb39.ecs.executor.SystenInstanceRecord;
20+
import top.rgb39.ecs.executor.SystemInstanceRecord;
2121

2222
public class Events implements Plugin, ParameterMatcher {
2323

@@ -27,7 +27,7 @@ public class Events implements Plugin, ParameterMatcher {
2727
@Override
2828
public void build(App app) {
2929
ParameterImplementor.registerMatcher(this);
30-
SystenInstanceRecord.setInst(Events.class, this);
30+
SystemInstanceRecord.setInst(Events.class, this);
3131
try {
3232
app.addSystem(
3333
Events.class.getDeclaredMethod("fireEvents"),

0 commit comments

Comments
 (0)