Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

feat: refactor polycons to not extend Polycon class #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 47 additions & 128 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,133 +1,5 @@
# API Reference <a name="API Reference" id="api-reference"></a>

## Constructs <a name="Constructs" id="Constructs"></a>

### Polycon <a name="Polycon" id="@monadahq/polycons.Polycon"></a>

A polymorphic construct that can be resolved at construction time into a more specific construct.

#### Initializers <a name="Initializers" id="@monadahq/polycons.Polycon.Initializer"></a>

```typescript
import { Polycon } from '@monadahq/polycons'

new Polycon(qualifier: string, scope: Construct, id: string, props?: any)
```

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#@monadahq/polycons.Polycon.Initializer.parameter.qualifier">qualifier</a></code> | <code>string</code> | *No description.* |
| <code><a href="#@monadahq/polycons.Polycon.Initializer.parameter.scope">scope</a></code> | <code>constructs.Construct</code> | *No description.* |
| <code><a href="#@monadahq/polycons.Polycon.Initializer.parameter.id">id</a></code> | <code>string</code> | *No description.* |
| <code><a href="#@monadahq/polycons.Polycon.Initializer.parameter.props">props</a></code> | <code>any</code> | *No description.* |

---

##### `qualifier`<sup>Required</sup> <a name="qualifier" id="@monadahq/polycons.Polycon.Initializer.parameter.qualifier"></a>

- *Type:* string

---

##### `scope`<sup>Required</sup> <a name="scope" id="@monadahq/polycons.Polycon.Initializer.parameter.scope"></a>

- *Type:* constructs.Construct

---

##### `id`<sup>Required</sup> <a name="id" id="@monadahq/polycons.Polycon.Initializer.parameter.id"></a>

- *Type:* string

---

##### `props`<sup>Optional</sup> <a name="props" id="@monadahq/polycons.Polycon.Initializer.parameter.props"></a>

- *Type:* any

---

#### Methods <a name="Methods" id="Methods"></a>

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@monadahq/polycons.Polycon.toString">toString</a></code> | Returns a string representation of this construct. |

---

##### `toString` <a name="toString" id="@monadahq/polycons.Polycon.toString"></a>

```typescript
public toString(): string
```

Returns a string representation of this construct.

#### Static Functions <a name="Static Functions" id="Static Functions"></a>

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@monadahq/polycons.Polycon.isConstruct">isConstruct</a></code> | Checks if `x` is a construct. |
| <code><a href="#@monadahq/polycons.Polycon.isPolycon">isPolycon</a></code> | Checks if `x` is a polycon. |

---

##### ~~`isConstruct`~~ <a name="isConstruct" id="@monadahq/polycons.Polycon.isConstruct"></a>

```typescript
import { Polycon } from '@monadahq/polycons'

Polycon.isConstruct(x: any)
```

Checks if `x` is a construct.

###### `x`<sup>Required</sup> <a name="x" id="@monadahq/polycons.Polycon.isConstruct.parameter.x"></a>

- *Type:* any

Any object.

---

##### `isPolycon` <a name="isPolycon" id="@monadahq/polycons.Polycon.isPolycon"></a>

```typescript
import { Polycon } from '@monadahq/polycons'

Polycon.isPolycon(x: any)
```

Checks if `x` is a polycon.

###### `x`<sup>Required</sup> <a name="x" id="@monadahq/polycons.Polycon.isPolycon.parameter.x"></a>

- *Type:* any

Any object.

---

#### Properties <a name="Properties" id="Properties"></a>

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#@monadahq/polycons.Polycon.property.node">node</a></code> | <code>constructs.Node</code> | The tree node. |

---

##### `node`<sup>Required</sup> <a name="node" id="@monadahq/polycons.Polycon.property.node"></a>

```typescript
public readonly node: Node;
```

- *Type:* constructs.Node

The tree node.

---


## Structs <a name="Structs" id="Structs"></a>

Expand Down Expand Up @@ -499,6 +371,53 @@ This factory will be used for resolving all polycons into constructs.



### Polycons <a name="Polycons" id="@monadahq/polycons.Polycons"></a>

A class used to create polycons.


#### Static Functions <a name="Static Functions" id="Static Functions"></a>

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@monadahq/polycons.Polycons.create">create</a></code> | *No description.* |

---

##### `create` <a name="create" id="@monadahq/polycons.Polycons.create"></a>

```typescript
import { Polycons } from '@monadahq/polycons'

Polycons.create(qualifier: string, scope: Construct, id: string, props?: any)
```

###### `qualifier`<sup>Required</sup> <a name="qualifier" id="@monadahq/polycons.Polycons.create.parameter.qualifier"></a>

- *Type:* string

---

###### `scope`<sup>Required</sup> <a name="scope" id="@monadahq/polycons.Polycons.create.parameter.scope"></a>

- *Type:* constructs.Construct

---

###### `id`<sup>Required</sup> <a name="id" id="@monadahq/polycons.Polycons.create.parameter.id"></a>

- *Type:* string

---

###### `props`<sup>Optional</sup> <a name="props" id="@monadahq/polycons.Polycons.create.parameter.props"></a>

- *Type:* any

---



### Process <a name="Process" id="@monadahq/polycons.Process"></a>

Runtime code with a named entrypoint.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./process";
export * from "./polycon";
export * from "./polycons";
export * from "./polycon-factory";
67 changes: 0 additions & 67 deletions src/polycon.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/polycons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Construct } from "constructs";
import { PolyconFactory } from "./polycon-factory";

/**
* A class used to create polycons
*/
export class Polycons {
public static create(
qualifier: string,
scope: Construct,
id: string,
props?: any
) {
const factory = PolyconFactory.of(scope);
if (!factory) {
throw new Error(`No factory defined within scope of "${id}"`);
}

const marker = Symbol.for(`polycons.init[${qualifier}]#${id}`);

// check if we are being called from a polycon resolution code path
// this is done by checking if a marker for this polycon is present in the
// scope. if so, we return the construct that was created by the polycon's
// call to super() and delete the marker
if (marker in scope) {
const child = scope.node.tryFindChild(id);
delete (scope as any)[marker]; // delete the marker
return child;
}

// remove the child since we will be replacing it with the construct
// produced by the polycon factory
scope.node.tryRemoveChild(id);

// add the initialization marker to avoid re-entering this path
// when the resolved polycon is initialized.
Object.defineProperty(scope, marker, {
value: true,
enumerable: false,
writable: false,
configurable: true,
});

const resolved = factory.resolveConstruct(qualifier, scope, id, props);

return resolved as Polycons;
}

private constructor() {}
}
Loading