| 14 | import java.util.stream.Collectors; |
| 15 | |
| 16 | public class Auto extends Box implements IO.Loaded { |
| 17 | |
| 18 | static public final Dict.Prop<Number> auto = new Dict.Prop<>("auto").type() |
| 19 | .toCanon() |
| 20 | .doc("set to non-zero integer to make this box automatically `_.begin()` when it is loaded; the integer gives the order: all boxes marked '1' are run before boxes marked '2'. In addition boxes are executed from top to bottom "); |
| 21 | |
| 22 | static { |
| 23 | IO.persist(auto); |
| 24 | } |
| 25 | |
| 26 | private final Map<Box, Long> done; |
| 27 | |
| 28 | |
| 29 | public Auto(Box root) { |
| 30 | |
| 31 | root.properties.put(auto, 0); // makes property appear in autocomplete for everyone |
| 32 | |
| 33 | done = new HashMap<>(); |
| 34 | |
| 35 | properties.putToMap(Callbacks.onLoad, "__autoload__", (b) -> { |
| 36 | // we only auto things once. |
| 37 | loaded(); |
| 38 | return null; |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void loaded() { |
| 44 | |
| 45 | List<Triple<Box, Float, Number>> run = breadthFirst(both()).filter(x -> x.properties.has(auto)) |
| 46 | .filter(x -> !done.containsKey(x)) |
| 47 | .filter(x -> x.properties.getFloat(auto, 0) > 0) |
| 48 | .map(x -> new Triple<>(x, x.properties.get(Box.frame).y, x.properties.get(auto))) |
| 49 | .collect(Collectors.toList()); |
| 50 | |
| 51 | // mark everything as done right now, in case there are exceptions. |
| 52 | run.forEach(x -> done.put(x.first, System.currentTimeMillis())); |
| 53 | |
| 54 | Collections.sort(run, (a, b) -> { |
| 55 | if (a.third.doubleValue() > b.third.doubleValue()) return 1; |
| 56 | if (a.third.doubleValue() < b.third.doubleValue()) return -1; |
| 57 | |
| 58 | if (a.second.doubleValue() < b.second.doubleValue()) return 1; |
| 59 | if (a.second.doubleValue() > b.second.doubleValue()) return -1; |
| 60 | |
| 61 | return 0; |
| 62 | }); |
| 63 | |
| 64 | |
| 65 | if (run.size() > 0) { |
| 66 | System.out.println("\n\n\n gathered auto list is "); |
| 67 | run.forEach((t) -> { |
| 68 | System.out.println(" " + t); |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | for (Triple<Box, Float, Number> t : run) { |