Skip to content

Commit

Permalink
Add Some Typescript (#5)
Browse files Browse the repository at this point in the history
* Made GolemProvider (previously GolemNode) and GolemNodeApi typescript

* Convert GolemNodeSync to TypeScript
  • Loading branch information
vciancio authored Apr 16, 2021
1 parent f866cf9 commit 0ba0853
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 43 deletions.
5 changes: 5 additions & 0 deletions app/package-lock.json

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

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"react-gauge-chart": "^0.3.0",
"react-scripts": "4.0.3",
"rxjs": "^6.6.7",
"typescript": "^4.2.3",
"web-vitals": "^1.1.1"
},
"scripts": {
Expand Down
10 changes: 0 additions & 10 deletions app/src/models/GolemNode.js

This file was deleted.

27 changes: 27 additions & 0 deletions app/src/models/GolemProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Object representing the current state of the Golem Provider
*/
type GolemProvider = {
"info": {
"name": String
"network": String
"processedLastHour": number
"processedTotal": number
"subnet": String
"version": String
"wallet": String
}
"hardware": {
"cpu": {
"percentUsage": number
};
"isProcessingTask": Boolean
"memory": {
"available": number
"percent": number
"used": number
};
}
}

export default GolemProvider
1 change: 1 addition & 0 deletions app/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import GolemNode from '../models/GolemNode';
import GolemProvider from '../models/GolemProvider';

const headers = {
'Content-Type': 'application/json',
}

async function getNodeInfo(address) {
async function getNodeInfo(address: String): Promise<GolemProvider|null> {
if(!address){
console.log('getNodeInfo: Address was null');
return null;
Expand All @@ -16,8 +16,8 @@ async function getNodeInfo(address) {
headers: headers,
});
if(req.ok){
let json = await req.json();
return new GolemNode(json);
let provider: GolemProvider = await req.json();
return provider
}
return null;
}
Expand Down
60 changes: 31 additions & 29 deletions app/src/utils/GolemNodeSync.js → app/src/utils/GolemNodeSync.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { Subject } from 'rxjs'
import { ReplaySubject } from 'rxjs'
import GolemProvider from '../models/GolemProvider';
import EnvConfig from './EnvConfig'
import GolemNodeApi from './GolemNodeApi'

const POLL_INTERVAL_DEFAULT = 5000
const POLLING_RATE = EnvConfig.pollingRate ? EnvConfig.pollingRate : POLL_INTERVAL_DEFAULT;
const POLL_INTERVAL_DEFAULT: number = 5000
const POLLING_RATE: number = EnvConfig.pollingRate ? +EnvConfig.pollingRate : POLL_INTERVAL_DEFAULT;

class GolemNodeSync {

/*
* _nodes: {
* "192.168.1.100" : ReplaySubject (Golem Node)
* }
*/
_nodes: Map<String, ReplaySubject<GolemProvider>>
_timer: NodeJS.Timeout | null
_isRunning: Boolean

constructor() {
this._nodes = {}
this._nodes = new Map()
this._timer = null
this._isRunning = false

Expand All @@ -23,17 +22,16 @@ class GolemNodeSync {
Object.seal(this)
}

/**
* Recieve updates for a node
* @param {String} address
* @param {Callback} subscription
* @returns {Subscriber} subscriber
*/
subscribeToNode(address, onUpdate, onError) {
if (!(address in this._nodes)) {
/** Recieve updates for a node */
subscribeToNode(
address: String,
onUpdate: (g: GolemProvider) => void,
onError: (e: Error) => void) {

if (!this._nodes.has(address)) {
this._createNodeSubject(address)
}
const subscriber = this._nodes[address].subscribe(onUpdate, onError)
const subscriber = this._nodes.get(address)!!.subscribe(onUpdate, onError)

if (!this._isRunning) {
console.log('Starting Polling Loop')
Expand All @@ -43,8 +41,9 @@ class GolemNodeSync {
return subscriber
}

_createNodeSubject(address) {
this._nodes[address] = new Subject()
_createNodeSubject(address: String) {
this._nodes.set(address, new ReplaySubject<GolemProvider>())
console.log('Keys: ', this._nodes.keys())
}

async _loop() {
Expand All @@ -59,15 +58,15 @@ class GolemNodeSync {
}
this._timer = null

const addresses = Object.keys(this._nodes)
const addresses = Array.from(this._nodes.keys())
// Stop running if we don't have any addresses to process
if (addresses.length === 0) {
this._isRunning = false
console.warn('No address to poll, stopping loop')
return
}

// Process our Nodes
// Process all of the Nodes
const promises = addresses.map((v) => this._processAddress(v))
await Promise.all(promises)
console.log('Finished processing')
Expand All @@ -76,30 +75,33 @@ class GolemNodeSync {
this._timer = setTimeout(() => this._loop(), POLLING_RATE);
}

/**
* Fetch a node and send it to subscribers
* @param {String} address
*/
async _processAddress(address) {
/** Fetch a node and send it to subscribers */
async _processAddress(address: String) {
if (!address) {
console.error('Recieved null address')
return
}

const subject = this._nodes[address]
const subject = this._nodes.get(address)
if (!subject) {
console.error('No Subject for ' + address)
return
}

try {
const node = await GolemNodeApi.getNodeInfo(address)
if (!node) {
this._nodes.delete(address)
subject.error(new Error('No node for address'))
console.warn('Removing ', address, ' from polling list')
return
}
subject.next(node)
} catch (e) {
const msg = 'Failed to load data for ' + address
console.error(msg, e)
subject.error(e)
delete this._nodes[address]
this._nodes.delete(address)
console.warn('Removing ', address, ' from polling list')
}
}
Expand Down
26 changes: 26 additions & 0 deletions app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}

0 comments on commit 0ba0853

Please sign in to comment.