Skip to content

Commit

Permalink
Merge pull request sonnetjs#1 from 13770129/main
Browse files Browse the repository at this point in the history
Made minor changes to sonnet-core
  • Loading branch information
htmujahid authored Oct 29, 2024
2 parents af3c617 + d69cd33 commit eb9531f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/sonnet-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonnetjs/core",
"version": "0.0.33",
"version": "0.0.34",
"files": [
"dist"
],
Expand Down
22 changes: 4 additions & 18 deletions packages/sonnet-core/src/abstract/SonnetComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export interface SonnetComponentProps {
}

export default abstract class SonnetComponent {
constructor() {}

protected _id: string = '';
id(id: string) {
this._id = id;
Expand All @@ -27,26 +25,14 @@ export default abstract class SonnetComponent {
return this;
}

private _hashIdCache: string | undefined = undefined;
private _hashIdCache?: string;
get hashId() {
if (this._hashIdCache) {
return this._hashIdCache;
}
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
const hash = array[0].toString().substring(0, 8);
this._hashIdCache = hash;
return hash;
return this._hashIdCache ??= window.crypto.getRandomValues(new Uint32Array(1))[0].toString().substring(0, 8);
}

private _parentCache: HTMLElement | undefined = undefined;
private _parentCache?: HTMLElement;
get parent() {
if (this._parentCache) {
return this._parentCache;
}
const parent = document.getElementById(this.hashId) as HTMLElement;
this._parentCache = parent;
return parent;
return this._parentCache ??= document.getElementById(this.hashId) as HTMLElement;
}

rerender(querySelector: string) {
Expand Down
2 changes: 0 additions & 2 deletions packages/sonnet-core/src/core/SonnetApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export class SonnetApp {
private _isInitialized: boolean = false;
private _mountedId: string = '';

constructor() {}

get component() {
return this._component;
}
Expand Down
11 changes: 7 additions & 4 deletions packages/sonnet-core/src/core/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ interface Component<T> {
script?: () => void;
}

const EMPTY_HEAD = 'head(){return""}';
const EMPTY_SCRIPT = 'script(){}';

export function $component<T>(component: Component<T>) {
return (args?: T) => {
const instance = new component(args);
if (isBrowser()) {
// head tags
if (component.head && component.head.toString() !== 'head(){return""}') {
if (component.head && component.head.toString() !== EMPTY_HEAD) {
event.once<SonnetHead>('head', component.head as () => SonnetHead);
}
if (instance.head && instance.head.toString() !== 'head(){return""}') {
if (instance.head && instance.head.toString() !== EMPTY_HEAD) {
event.on<SonnetHead>('head', instance.head.bind(instance));
}
// scripts
if (component.script && component.script.toString() !== 'script(){}') {
if (component.script && component.script.toString() !== EMPTY_SCRIPT) {
event.once('script', component.script);
}
if (instance.script && instance.script.toString() !== 'script(){}') {
if (instance.script && instance.script.toString() !== EMPTY_SCRIPT) {
event.on('script', instance.script.bind(instance));
}
}
Expand Down

0 comments on commit eb9531f

Please sign in to comment.