Skip to content

Commit 27bd4f1

Browse files
authored
fix: target initialization (#47)
Set muliple targets as `[]` and single target as `null` if they cannot be found.
1 parent 31eb90d commit 27bd4f1

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Set muliple targets as `[]` and single target as `null` if they cannot be found
13+
1014
## [0.5.0-beta.2] - 2024-08-26
1115

1216
### Added
@@ -15,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1519

1620
### Fixed
1721

18-
- Optmize event listeners so that only the added/removed actions are processed ([#41](https://github.com/Ambiki/impulse/pull/41))
22+
- Optimize event listeners so that only the added/removed actions are processed ([#41](https://github.com/Ambiki/impulse/pull/41))
1923

2024
### Fixed
2125

packages/core/src/decorators/target.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export type TargetType = {
88
export function target() {
99
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1010
return (ctor: any, key: string) => {
11-
Object.defineProperty(Object.getPrototypeOf(ctor), key, { configurable: true, value: null });
1211
const store = new Store(ctor, 'target');
1312
store.add({ key, multiple: false });
1413
};
@@ -17,7 +16,6 @@ export function target() {
1716
export function targets() {
1817
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1918
return (ctor: any, key: string) => {
20-
Object.defineProperty(Object.getPrototypeOf(ctor), key, { configurable: true, value: [] });
2119
const store = new Store(ctor, 'target');
2220
store.add({ key, multiple: true });
2321
};

packages/core/src/target.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export default class Target implements TokenListObserverDelegate {
2020
}
2121

2222
start() {
23+
// Initialize targets with an empty array if it references multiple targets, or with null if it references a single
24+
// target. Later, if we find matching targets, we set them accordingly. If we don't, we can still iterate over
25+
// targets because it is an array.
26+
for (const key of this.keys) {
27+
this.defineProperty(key, this.isKeyMultiple(key) ? [] : null);
28+
}
29+
2330
if (!this.tokenListObserver) {
2431
this.tokenListObserver = new TokenListObserver(this.instance, 'data-target', this);
2532
this.tokenListObserver.start();

0 commit comments

Comments
 (0)