Skip to content

Commit cf5b860

Browse files
committed
patch(feat): octo overlays can now add or remove anchors beyond constructor.
1 parent d7b40fc commit cf5b860

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

packages/octo/src/overlays/overlay.abstract.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ export abstract class AOverlay<T> extends AModel<IOverlay, T> {
2323
}
2424

2525
for (const anchor of anchors) {
26-
const dependencies = this.addRelationship(anchor.getParent());
27-
dependencies[0].addBehavior('overlayId', DiffAction.ADD, 'MODEL_NAME', DiffAction.ADD);
28-
dependencies[0].addBehavior('overlayId', DiffAction.ADD, 'MODEL_NAME', DiffAction.UPDATE);
29-
dependencies[1].addBehavior('MODEL_NAME', DiffAction.DELETE, 'overlayId', DiffAction.DELETE);
30-
31-
this.anchors.push(anchor);
26+
this.addAnchor(anchor);
3227
}
3328
}
3429

30+
addAnchor(anchor: AAnchor): void {
31+
const dependencies = this.addRelationship(anchor.getParent());
32+
dependencies[0].addBehavior('overlayId', DiffAction.ADD, 'MODEL_NAME', DiffAction.ADD);
33+
dependencies[0].addBehavior('overlayId', DiffAction.ADD, 'MODEL_NAME', DiffAction.UPDATE);
34+
dependencies[1].addBehavior('MODEL_NAME', DiffAction.DELETE, 'overlayId', DiffAction.DELETE);
35+
36+
this.anchors.push(anchor);
37+
}
38+
3539
async diff(previous?: T): Promise<Diff[]> {
3640
const diffs: Diff[] = [];
3741

@@ -53,6 +57,24 @@ export abstract class AOverlay<T> extends AModel<IOverlay, T> {
5357
return `${this.MODEL_NAME}=${this.overlayId}`;
5458
}
5559

60+
removeAnchor(anchor: AAnchor): void {
61+
const overlayDependencyIndex = this.dependencies.findIndex((d) => d.from === this && d.to === anchor.getParent());
62+
if (overlayDependencyIndex > -1) {
63+
this.dependencies.splice(overlayDependencyIndex, 1);
64+
}
65+
const parentDependencyIndex = anchor
66+
.getParent()
67+
['dependencies'].findIndex((d) => d.from === anchor.getParent() && d.to === this);
68+
if (parentDependencyIndex > -1) {
69+
anchor.getParent()['dependencies'].splice(parentDependencyIndex, 1);
70+
}
71+
72+
const anchorIndex = this.anchors.findIndex((a) => a.anchorId === anchor.anchorId);
73+
if (anchorIndex > -1) {
74+
this.anchors.splice(anchorIndex, 1);
75+
}
76+
}
77+
5678
synth(): IOverlay {
5779
return {
5880
anchors: this.anchors.map((a) => a.synth()),

packages/octo/src/overlays/overlay.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,64 @@ describe('Overlay UT', () => {
4747
});
4848
});
4949

50+
describe('removeAnchor()', () => {
51+
it('should remove one anchor parent when multiple anchors have same parent', () => {
52+
const app = new App('test');
53+
const anchor1 = new TestAnchor('anchor-1', app);
54+
const anchor2 = new TestAnchor('anchor-2', app);
55+
56+
const overlay1 = new TestOverlay('overlay-1', {}, [anchor1, anchor2]);
57+
expect(app['dependencies']).toMatchInlineSnapshot(`
58+
[
59+
{
60+
"from": "app=test",
61+
"relationship": undefined,
62+
"to": "test-overlay=overlay-1",
63+
},
64+
{
65+
"from": "app=test",
66+
"relationship": undefined,
67+
"to": "test-overlay=overlay-1",
68+
},
69+
]
70+
`);
71+
expect(overlay1['dependencies']).toMatchInlineSnapshot(`
72+
[
73+
{
74+
"from": "test-overlay=overlay-1",
75+
"relationship": undefined,
76+
"to": "app=test",
77+
},
78+
{
79+
"from": "test-overlay=overlay-1",
80+
"relationship": undefined,
81+
"to": "app=test",
82+
},
83+
]
84+
`);
85+
86+
overlay1.removeAnchor(anchor1);
87+
expect(app['dependencies']).toMatchInlineSnapshot(`
88+
[
89+
{
90+
"from": "app=test",
91+
"relationship": undefined,
92+
"to": "test-overlay=overlay-1",
93+
},
94+
]
95+
`);
96+
expect(overlay1['dependencies']).toMatchInlineSnapshot(`
97+
[
98+
{
99+
"from": "test-overlay=overlay-1",
100+
"relationship": undefined,
101+
"to": "app=test",
102+
},
103+
]
104+
`);
105+
});
106+
});
107+
50108
describe('synth()', () => {
51109
it('should be able to synth an empty overlay', () => {
52110
const overlay = new TestOverlay('overlay-1', {}, []);

0 commit comments

Comments
 (0)