Skip to content

Commit 9d36711

Browse files
axmmisakaKagamihara Nadeshiko
authored andcommitted
Add necessary capabilities for reactors to create other reactors and make connections
0. Added _addChild and _addSibling necessary for mutation APIs 1. Solved issues related to hierarchy and ownership.
1 parent 3248225 commit 9d36711

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/core/reactor.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ export abstract class Reactor extends Component {
166166
*/
167167
private readonly _keyChain = new Map<Component, symbol>();
168168

169+
// This is the keychain for creation, i.e. if Reactor R's mutation created reactor B,
170+
// then R is B's creator, even if they are siblings. R should have access to B,
171+
// at least semantically......?
172+
private readonly _creatorKeyChain = new Map<Component, symbol>();
173+
169174
/**
170175
* This graph has in it all the dependencies implied by this container's
171176
* ports, reactions, and connections.
@@ -400,6 +405,9 @@ export abstract class Reactor extends Component {
400405
return owner._getKey(component, this._keyChain.get(owner));
401406
}
402407
}
408+
return component
409+
.getContainer()
410+
._getKey(component, this._creatorKeyChain.get(component.getContainer()));
403411
}
404412

405413
/**
@@ -1608,6 +1616,36 @@ export abstract class Reactor extends Component {
16081616
toString(): string {
16091617
return this._getFullyQualifiedName();
16101618
}
1619+
1620+
protected _addChild<R extends Reactor, G extends unknown[]>(
1621+
constructor: new (container: Reactor, ...args: G) => R,
1622+
...args: G
1623+
): R {
1624+
const newReactor = new constructor(this, ...args);
1625+
return newReactor;
1626+
}
1627+
1628+
protected _addSibling<R extends Reactor, G extends unknown[]>(
1629+
constructor: new (container: Reactor, ...args: G) => R,
1630+
...args: G
1631+
): R {
1632+
if (this._getContainer() == null) {
1633+
throw new Error(
1634+
`Reactor ${this} does not have a parent. Sibling is not well-defined.`
1635+
);
1636+
}
1637+
if (this._getContainer() === this) {
1638+
throw new Error(
1639+
`Reactor ${this} is self-contained. Adding sibling creates logical issue.`
1640+
);
1641+
}
1642+
const newReactor = this._getContainer()._addChild(
1643+
constructor,
1644+
...args
1645+
);
1646+
this._creatorKeyChain.set(newReactor, newReactor._key);
1647+
return newReactor;
1648+
}
16111649
}
16121650

16131651
/*

0 commit comments

Comments
 (0)