Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
func: stabilize base components
Browse files Browse the repository at this point in the history
  • Loading branch information
Mafrans committed Jan 23, 2021
1 parent d3d9741 commit 9b71171
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 29 deletions.
Binary file added images/Stadia+128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Stadia+16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Stadia+32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Stadia+48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"https://stadia.google.com/*"
],
"js": [
"dist/index.js"
"dist/main/index.js"
]
}
],
Expand Down
63 changes: 54 additions & 9 deletions modules/main/src/components/AbstractComponent.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,74 @@
import Internal from '../decorators/@Internal';
import React from 'react';
import ReactDOM from 'react-dom';
import Logger from '../Logger';
import Util from '../Util';

export default class AbstractComponent {
export interface DefaultProps {

}

export interface DefaultState {
renderer: HTMLElement | null
}

export default class AbstractComponent<A extends DefaultProps, B extends DefaultState> extends React.Component<A, B> {

public __useReact = false;
public name: string;
public renderer: HTMLElement | null = null;

constructor(name: string) {
this.name = name;
constructor(data: { props?: A, name: string }) {
super(data.props !== undefined ? data.props : {} as A);
this.name = data.name;

// TODO: Not safe, find better solution
this.state = {
renderer: null
} as B

this.__start();
setInterval(() => this.__tick(), 1000);
}

@Internal('onStart')
public async __start() {
Logger.component(`Component ${this.name} has been enabled`);
this.onStart();
}

@Internal('onUpdate')
public async __tick() {}
public async __tick() {
console.log("tick");
this.onUpdate();
}

public async updateRenderer() {
await Util.updateRenderer();
this.renderer = Util.renderer;
if (this.__useReact) {
this.setState(state => ({ renderer: Util.renderer }));
}
}

/**
* @deprecated React render function, only override this if you're 100% sure you know what you're doing.
* @see onRender
*/
render() {
if (!this.__useReact) return null;

if (this.state.renderer !== null) {
const renderData = this.onRender();
if(renderData === null) return null;

const target = this.state.renderer.querySelector(renderData.query);
if (target !== null) {
return ReactDOM.createPortal(
renderData.node,
<Element>this.state.renderer.querySelector(renderData.query),
);
}
}
return null;
}

async onStart(): Promise<void> {}
async onUpdate(): Promise<void> {}
onRender(): { query: string, node: React.ReactNode } | null { return null }
}
26 changes: 26 additions & 0 deletions modules/main/src/components/TestComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import AbstractComponent, { DefaultProps, DefaultState } from './AbstractComponent';
import ReactComponent from '../decorators/@ReactComponent';

@ReactComponent
export default class TestComponent extends AbstractComponent<DefaultProps, DefaultState> {
constructor() {
super({ name: "Test Component" });
}

async onStart(): Promise<void> {}

async onUpdate(): Promise<void> {
this.updateRenderer();
console.log(this.__useReact);
}

onRender(): { query: string; node: React.ReactNode } {
return {
query: '.CVVXfc',
node: <button>Test Button</button>
}
}
}


13 changes: 0 additions & 13 deletions modules/main/src/decorators/@Internal.ts

This file was deleted.

5 changes: 5 additions & 0 deletions modules/main/src/decorators/@ReactComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function ReactComponent<T extends { new (...args: any[]): {} }>(constructor: T) {
return class extends constructor {
__useReact = true
};
}
1 change: 0 additions & 1 deletion modules/main/src/index.ts

This file was deleted.

20 changes: 20 additions & 0 deletions modules/main/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import ReactDOM from 'react-dom';
import TestComponent from './components/TestComponent';
import Util from './Util';

console.log("main2");

window.addEventListener('load', async () => {
await Util.updateRenderer();

const root = document.createElement('div');
document.body.appendChild(root)

ReactDOM.render(
<div>
<TestComponent/>
</div>,
root
)
})
75 changes: 73 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "modules/main/src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:dev@main": "parcel build modules/main/src/index.ts -d ./dist/main",
"build:dev@main": "parcel build modules/main/src/index.tsx -d ./dist/main",
"build:dev@popup": "parcel build modules/popup/src/index.ts -d ./dist/popup",
"build:dev": "npm run build:dev@main && npm run build:dev@popup"
},
Expand All @@ -25,7 +25,15 @@
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/preset-react": "^7.12.10",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"typescript": "^4.1.3"
},
"browserslist": [ "last 2 Chrome versions" ]
"browserslist": [
"last 2 Chrome versions"
],
"dependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"jsx": "react",
"allowJs": true,
"moduleResolution": "node",
"strict": true
"strict": true,
"allowSyntheticDefaultImports": true
}
}

0 comments on commit 9b71171

Please sign in to comment.