Skip to content

Commit

Permalink
Fix light bulb errors (also add a dev mode to build script)
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Nov 28, 2024
1 parent fbd05e4 commit d6ba560
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 149 deletions.
9 changes: 6 additions & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import * as fs from 'fs'

const serve = process.env.ESBUILD_SERVE === '1'
const baseurl = process.env.ESBUILD_BASEURL || ''
const nodeEnv = process.env.NODE_ENV
const isProd = nodeEnv !== 'development'

console.log(`Building with baseurl ${baseurl}`)

const ctx = await esbuild.context({
entryPoints: ['src/index.ts'],
minify: true,
minify: isProd,
bundle: true,
metafile: true,
splitting: true,
splitting: isProd,
outdir: 'dist',
format: 'esm',
target: ['es2020'],
Expand All @@ -23,7 +25,8 @@ const ctx = await esbuild.context({
'.svg': 'file'
},
define: {
'process.env.BASEURL': JSON.stringify(baseurl)
'process.env.BASEURL': JSON.stringify(baseurl),
'process.env.NODE_ENV': JSON.stringify(nodeEnv)
},
plugins: [
htmlPlugin({
Expand Down
64 changes: 32 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"name": "erratic-gate",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "ESBUILD_SERVE=1 node ./build.js",
"build": "node ./build.js",
"check": "tsc"
},
"devDependencies": {
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@types/deepmerge": "^2.2.0",
"@types/mainloop.js": "^1.0.5",
"@types/node": "^20.8.9",
"@types/react-dom": "^18.2.14",
"@types/react-router-dom": "^4.3.4",
"esbuild": "^0.19.5",
"esbuild-sass-plugin": "^2.16.0",
"typescript": "^5.0.2"
},
"dependencies": {
"@eix-js/utils": "0.0.6",
"@material-ui/core": "^4.2.1",
"deepmerge": "^4.0.0",
"keycode": "^2.2.0",
"mainloop.js": "^1.0.4",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-toastify": "^5.3.2",
"rxjs": "^6.5.2",
"rxjs-hooks": "^0.5.1"
}
"name": "erratic-gate",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "NODE_ENV=development ESBUILD_SERVE=1 node ./build.js",
"build": "node ./build.js",
"check": "tsc"
},
"devDependencies": {
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@types/deepmerge": "^2.2.0",
"@types/mainloop.js": "^1.0.5",
"@types/node": "^20.8.9",
"@types/react-dom": "^18.2.14",
"@types/react-router-dom": "^4.3.4",
"esbuild": "^0.19.5",
"esbuild-sass-plugin": "^2.16.0",
"typescript": "^5.0.2"
},
"dependencies": {
"@eix-js/utils": "0.0.6",
"@material-ui/core": "^4.2.1",
"deepmerge": "^4.0.0",
"keycode": "^2.2.0",
"mainloop.js": "^1.0.4",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-toastify": "^5.3.2",
"rxjs": "^6.5.2",
"rxjs-hooks": "^0.5.1"
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ async function main() {
}
}

new EventSource('/esbuild').addEventListener('change', () => location.reload())
if (process.env.BASEURL === 'development') {
new EventSource('/esbuild').addEventListener('change', () =>
location.reload()
)
}

// Call entry
main().catch((error) => {
Expand Down
92 changes: 46 additions & 46 deletions src/modules/activation/classes/ExecutionQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,68 @@ import { filter, take } from 'rxjs/operators'
* Keeps track of what a task should do and where the output should be delivered
*/
export interface Task<T> {
output: Subject<T>
execute: () => Promise<T>
output: Subject<T>
execute: () => Promise<T>
}

/**
* Used to execute a number of async tasks
*/
export class ExecutionQueue<T> {
/**
* An array of all the tasks wich need to be executed
*/
private tasks: Task<T>[] = []
/**
* An array of all the tasks wich need to be executed
*/
private tasks: Task<T>[] = []

/**
* Keeps track of the current task
*/
private current: Promise<T> | null
/**
* Keeps track of the current task
*/
private current: Promise<T> | null

/**
* Wheather the tasks should continue beeing executed
*/
public active = true
/**
* Whether the tasks should continue being executed
*/
public active = true

/**
* Adds a new task to the queue
*
* @param task The task to add
*/
public push(task: () => Promise<T>) {
const executionSubject = new Subject<T>()
const executionPromise = executionSubject.pipe(take(1)).toPromise()
/**
* Adds a new task to the queue
*
* @param task The task to add
*/
public push(task: () => Promise<T>) {
const executionSubject = new Subject<T>()
const executionPromise = executionSubject.pipe(take(1)).toPromise()

this.tasks.push({
output: executionSubject,
execute: task
})
this.tasks.push({
output: executionSubject,
execute: task
})

if (!this.current) {
this.next()
}

return executionPromise
if (!this.current) {
this.next()
}

/**
* Executes the next task in the queue
*/
private next() {
const task = this.tasks.shift()
return executionPromise
}

/**
* Executes the next task in the queue
*/
private next() {
const task = this.tasks.shift()

if (task) {
this.current = task.execute()
if (task) {
this.current = task.execute()

this.current.then(result => {
task.output.next(result)
this.current.then((result) => {
task.output.next(result)

if (this.active) {
this.next()
}
})
} else {
this.current = null
if (this.active) {
this.next()
}
})
} else {
this.current = null
}
}
}
2 changes: 1 addition & 1 deletion src/modules/saving/templates/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const lightTemplate: PartialTemplate = {
activation: `
const { main, active } = context.colors
context.color(context.getBinary(2) ? active : main)
context.color(context.getBinary(0) ? active : main)
`
},
integration: {
Expand Down
49 changes: 26 additions & 23 deletions src/modules/simulation/classes/Gate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,53 +295,56 @@ export class Gate {
props: Property[] = this.template.properties.data,
target: GateProps = this.props,
path: string[] = []
) {
): boolean {
// We don't want to update until every prop has been created
let shouldUpdate = false
let lockUpdates = true

if (this.template.properties.enabled) {
for (const prop of props) {
if (isGroup(prop)) {
const { groupName } = prop
target[groupName] = {} as GateProps
this.assignProps(
shouldUpdate ||= this.assignProps(
typeof source[groupName] === 'object'
? (source[groupName] as PropsSave)
: {},
prop.props,
target[groupName] as GateProps,
[...path, groupName]
)
} else {
const { name, base, needsUpdate } = prop
shouldUpdate ||= needsUpdate || false

continue
}

const { name, base, needsUpdate } = prop

const subject = new BehaviorSubject(
source.hasOwnProperty(name) ? source[name] : base
)
const subject = new BehaviorSubject(
source.hasOwnProperty(name) ? source[name] : base
)

target[name] = subject
target[name] = subject

this.subscriptions.push(
subject.subscribe((value) => {
if (!lockUpdates && needsUpdate && path.length === 0) {
return this.update()
}
this.subscriptions.push(
subject.subscribe((value) => {
if (!lockUpdates && needsUpdate && path.length === 0) {
return this.update()
}

if (path.length === 0) {
return
}
if (path.length === 0) {
return
}

this.updateNestedProp([...path, name], value)
})
)
this.updateNestedProp([...path, name], value)
})
)
}
}
}

lockUpdates = false
this.update()

if (path.length === 0 && shouldUpdate) this.update()

return shouldUpdate
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/modules/simulation/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GateTemplate, Property, RawProp } from './types/GateTemplate'
import { GateTemplate, RawProp } from './types/GateTemplate'
import { categories } from '../saving/data/categories'
import { getRendererSafely } from '../logic-gates/helpers/getRendererSafely'

export const DefaultGateTemplate: GateTemplate = {
metadata: {
Expand Down
Loading

0 comments on commit d6ba560

Please sign in to comment.