From e4dc484ede96cb2ff0ff23c5956cd5b459243fc4 Mon Sep 17 00:00:00 2001 From: Patrick Doyle Date: Sun, 18 Feb 2024 13:45:15 -0500 Subject: [PATCH] Pass a different BoskInfo object to DriverFactory --- .../src/main/java/io/vena/bosk/Bosk.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/bosk-core/src/main/java/io/vena/bosk/Bosk.java b/bosk-core/src/main/java/io/vena/bosk/Bosk.java index 582c8d24..1415c9fe 100644 --- a/bosk-core/src/main/java/io/vena/bosk/Bosk.java +++ b/bosk-core/src/main/java/io/vena/bosk/Bosk.java @@ -111,10 +111,20 @@ public Bosk(String name, Type rootType, DefaultRootFunction defaultRootFuncti throw new IllegalArgumentException("Invalid root type " + rootType + ": " + e.getMessage(), e); } - // We do this last because the driver factory is allowed to do such things - // as create References, so it needs the rest of the initialization to - // have completed already. - this.driver = driverFactory.build(this, this.localDriver); + // Rather than pass `this` while it's still under construction, + // pass another object that contains the things that are available + // at this point. + // + UnderConstruction boskInfo = new UnderConstruction<>( + name, instanceID, rootRef, this::registerHooks + ); + + // We do this as late as possible because the driver factory is allowed + // to do such things as create References, so it needs the rest of the + // initialization to have completed already. + // + this.driver = driverFactory.build(boskInfo, this.localDriver); + try { this.currentRoot = requireNonNull(driver.initialRoot(rootType)); } catch (InvalidTypeException | IOException | InterruptedException e) { @@ -133,6 +143,22 @@ public Bosk(String name, Type rootType, R defaultRoot, DriverFactory driverFa this(name, rootType, b->defaultRoot, driverFactory); } + record UnderConstruction( + String name, + Identifier instanceID, + RootReference rootReference, + RegisterHooksMethod m + ) implements BoskInfo { + @Override + public void registerHooks(Object receiver) throws InvalidTypeException { + m.registerHooks(receiver); + } + } + + private interface RegisterHooksMethod { + void registerHooks(Object receiver) throws InvalidTypeException; + } + /** * You can use Bosk::simpleDriver as the * driverFactory if you don't want any additional driver functionality.