-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bound: added decorator to automatically bound methods to the instance
- Loading branch information
Andrea Zanenghi
committed
Sep 30, 2024
1 parent
2d5bb6a
commit 4551f81
Showing
12 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ecopages/radiant": patch | ||
--- | ||
|
||
Added @bound decorator to simplify the binding of methods that runs in untracked events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { CodeBlock } from '@/components/code-block/code-block.kita'; | ||
import { DocsLayout } from '@/layouts/docs-layout'; | ||
|
||
export const layout = DocsLayout; | ||
|
||
export const getMetadata = () => ({ | ||
title: 'Docs | @bound', | ||
description: 'The place to learn about @ecopages/radiant', | ||
}) | ||
|
||
# @bound | ||
--- | ||
|
||
The bound decorator binds a method to the instance of the class it is defined in. This is useful when passing a method as a callback to another function, such as in response to an event listener. | ||
|
||
<CodeBlock > | ||
```typescript | ||
@bound doSomething() {...} | ||
``` | ||
</CodeBlock> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { RadiantElement } from '@/core/radiant-element'; | ||
|
||
/** | ||
* A decorator to bind a method to the instance. | ||
* @param target {@link RadiantElement} | ||
* @param propertyKey string | ||
* @param descriptor {@link PropertyDescriptor} | ||
* @returns | ||
*/ | ||
export function bound(target: RadiantElement, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor { | ||
const originalMethod = descriptor.value; | ||
|
||
return { | ||
configurable: true, | ||
get() { | ||
/** | ||
* Check if the method is already bound to the instance. | ||
*/ | ||
if (this === (target as any).prototype || Object.hasOwn(this, propertyKey)) { | ||
return originalMethod; | ||
} | ||
|
||
/** | ||
* Bind the method to the instance. | ||
*/ | ||
const boundMethod = originalMethod.bind(this); | ||
Object.defineProperty(this, propertyKey, { | ||
value: boundMethod, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
return boundMethod; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type WithChildren<T = unknown> = T & { children?: JSX.Element | JSX.Element[] }; | ||
|
||
export type WithChildrenAndClassName<T = unknown> = WithChildren<T> & { className?: string }; | ||
|
||
export type FocusableElement = | ||
| HTMLAnchorElement | ||
| HTMLButtonElement | ||
| HTMLInputElement | ||
| HTMLTextAreaElement | ||
| HTMLSelectElement | ||
| (HTMLElement & { tabindex: number }); |