Skip to content

Commit

Permalink
Pass a different BoskInfo object to DriverFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Feb 18, 2024
1 parent 6dfc16c commit e4dc484
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions bosk-core/src/main/java/io/vena/bosk/Bosk.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,20 @@ public Bosk(String name, Type rootType, DefaultRootFunction<R> 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<R> 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) {
Expand All @@ -133,6 +143,22 @@ public Bosk(String name, Type rootType, R defaultRoot, DriverFactory<R> driverFa
this(name, rootType, b->defaultRoot, driverFactory);
}

record UnderConstruction<RR extends StateTreeNode>(
String name,
Identifier instanceID,
RootReference<RR> rootReference,
RegisterHooksMethod m
) implements BoskInfo<RR> {
@Override
public void registerHooks(Object receiver) throws InvalidTypeException {
m.registerHooks(receiver);
}
}

private interface RegisterHooksMethod {
void registerHooks(Object receiver) throws InvalidTypeException;
}

/**
* You can use <code>Bosk::simpleDriver</code> as the
* <code>driverFactory</code> if you don't want any additional driver functionality.
Expand Down

0 comments on commit e4dc484

Please sign in to comment.