Skip to content

fix(lazy): implement instanceof properly #3287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -3666,6 +3666,7 @@ public java.lang.Object produce()
| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
| <code><a href="#cdk8s.Lazy.isLazy">isLazy</a></code> | Checks if an object is a Lazy instance. |

---

Expand All @@ -3683,6 +3684,24 @@ Lazy.any(IAnyProducer producer)

---

##### `isLazy` <a name="isLazy" id="cdk8s.Lazy.isLazy"></a>

```java
import org.cdk8s.Lazy;

Lazy.isLazy(java.lang.Object obj)
```

Checks if an object is a Lazy instance.

###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>

- *Type:* java.lang.Object

The object to check.

---



### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>
Expand Down
21 changes: 21 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -3881,6 +3881,7 @@ def produce() -> typing.Any
| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
| <code><a href="#cdk8s.Lazy.isLazy">is_lazy</a></code> | Checks if an object is a Lazy instance. |

---

Expand All @@ -3900,6 +3901,26 @@ cdk8s.Lazy.any(

---

##### `is_lazy` <a name="is_lazy" id="cdk8s.Lazy.isLazy"></a>

```python
import cdk8s

cdk8s.Lazy.is_lazy(
obj: typing.Any
)
```

Checks if an object is a Lazy instance.

###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>

- *Type:* typing.Any

The object to check.

---



### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>
Expand Down
19 changes: 19 additions & 0 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,7 @@ public produce(): any
| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
| <code><a href="#cdk8s.Lazy.isLazy">isLazy</a></code> | Checks if an object is a Lazy instance. |

---

Expand All @@ -3200,6 +3201,24 @@ Lazy.any(producer: IAnyProducer)

---

##### `isLazy` <a name="isLazy" id="cdk8s.Lazy.isLazy"></a>

```typescript
import { Lazy } from 'cdk8s'

Lazy.isLazy(obj: any)
```

Checks if an object is a Lazy instance.

###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>

- *Type:* any

The object to check.

---



### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>
Expand Down
26 changes: 23 additions & 3 deletions src/lazy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
const LAZY_SYMBOL = Symbol.for("cdk8s.Lazy");

export class Lazy {
public static any(producer: IAnyProducer): any {
return new Lazy(producer) as unknown as any;
}

private constructor(private readonly producer: IAnyProducer) {}
/**
* Checks if an object is a Lazy instance
* @param obj The object to check
*/
static isLazy(obj: any): boolean {
return obj !== null && typeof obj === "object" && LAZY_SYMBOL in obj;
}

/**
* Implements `instanceof Lazy` using the more reliable `Lazy.isLazy` static method
*
* @param obj The object to check
* @internal
*/
public static [Symbol.hasInstance](obj: any): boolean {
return Lazy.isLazy(obj);
}

private constructor(private readonly producer: IAnyProducer) {
Object.defineProperty(this, LAZY_SYMBOL, { value: true });
}

public produce(): any {
return this.producer.produce();
Expand All @@ -13,5 +35,3 @@ export class Lazy {
export interface IAnyProducer {
produce(): any;
}


Loading