Skip to content

Commit eb9531f

Browse files
authored
Merge pull request #1 from 13770129/main
Made minor changes to sonnet-core
2 parents af3c617 + d69cd33 commit eb9531f

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

packages/sonnet-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sonnetjs/core",
3-
"version": "0.0.33",
3+
"version": "0.0.34",
44
"files": [
55
"dist"
66
],

packages/sonnet-core/src/abstract/SonnetComponent.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export interface SonnetComponentProps {
77
}
88

99
export default abstract class SonnetComponent {
10-
constructor() {}
11-
1210
protected _id: string = '';
1311
id(id: string) {
1412
this._id = id;
@@ -27,26 +25,14 @@ export default abstract class SonnetComponent {
2725
return this;
2826
}
2927

30-
private _hashIdCache: string | undefined = undefined;
28+
private _hashIdCache?: string;
3129
get hashId() {
32-
if (this._hashIdCache) {
33-
return this._hashIdCache;
34-
}
35-
const array = new Uint32Array(1);
36-
window.crypto.getRandomValues(array);
37-
const hash = array[0].toString().substring(0, 8);
38-
this._hashIdCache = hash;
39-
return hash;
30+
return this._hashIdCache ??= window.crypto.getRandomValues(new Uint32Array(1))[0].toString().substring(0, 8);
4031
}
4132

42-
private _parentCache: HTMLElement | undefined = undefined;
33+
private _parentCache?: HTMLElement;
4334
get parent() {
44-
if (this._parentCache) {
45-
return this._parentCache;
46-
}
47-
const parent = document.getElementById(this.hashId) as HTMLElement;
48-
this._parentCache = parent;
49-
return parent;
35+
return this._parentCache ??= document.getElementById(this.hashId) as HTMLElement;
5036
}
5137

5238
rerender(querySelector: string) {

packages/sonnet-core/src/core/SonnetApp.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export class SonnetApp {
1616
private _isInitialized: boolean = false;
1717
private _mountedId: string = '';
1818

19-
constructor() {}
20-
2119
get component() {
2220
return this._component;
2321
}

packages/sonnet-core/src/core/factory.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ interface Component<T> {
1111
script?: () => void;
1212
}
1313

14+
const EMPTY_HEAD = 'head(){return""}';
15+
const EMPTY_SCRIPT = 'script(){}';
16+
1417
export function $component<T>(component: Component<T>) {
1518
return (args?: T) => {
1619
const instance = new component(args);
1720
if (isBrowser()) {
1821
// head tags
19-
if (component.head && component.head.toString() !== 'head(){return""}') {
22+
if (component.head && component.head.toString() !== EMPTY_HEAD) {
2023
event.once<SonnetHead>('head', component.head as () => SonnetHead);
2124
}
22-
if (instance.head && instance.head.toString() !== 'head(){return""}') {
25+
if (instance.head && instance.head.toString() !== EMPTY_HEAD) {
2326
event.on<SonnetHead>('head', instance.head.bind(instance));
2427
}
2528
// scripts
26-
if (component.script && component.script.toString() !== 'script(){}') {
29+
if (component.script && component.script.toString() !== EMPTY_SCRIPT) {
2730
event.once('script', component.script);
2831
}
29-
if (instance.script && instance.script.toString() !== 'script(){}') {
32+
if (instance.script && instance.script.toString() !== EMPTY_SCRIPT) {
3033
event.on('script', instance.script.bind(instance));
3134
}
3235
}

0 commit comments

Comments
 (0)