Skip to content

Commit

Permalink
fix: race condition, update deps (#22)
Browse files Browse the repository at this point in the history
* chore: upgrade deps

* chore: fix race condition
  • Loading branch information
webbertakken authored Aug 11, 2024
1 parent c740772 commit 13503f9
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 302 deletions.
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,36 @@
"dayjs": "^1.11.12"
},
"devDependencies": {
"@cspell/eslint-plugin": "^8.7.0",
"@cspell/eslint-plugin": "^8.13.2",
"@types/async": "^3.2.24",
"@types/bun": "^1.1.6",
"@types/jest": "^29.5.12",
"@types/node": "^22.2.0",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"@vitest/coverage-v8": "^1.5.0",
"bun": "^1.1.20",
"@vitest/coverage-v8": "^2.0.5",
"bun": "^1.1.22",
"cross-env": "^7.0.3",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsonc": "^2.15.1",
"eslint-plugin-jsonc": "^2.16.0",
"eslint-plugin-no-unsanitized": "^4.0.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-security": "^3.0.0",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-security": "^3.0.1",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-sonarjs": "^0.25.1",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^52.0.0",
"eslint-plugin-vitest": "^0.5.3",
"eslint-plugin-vitest": "^0.5.4",
"husky": "^8.0.3",
"lint-staged": "^15.1.0",
"prettier": "^3.2.5",
"shellcheck": "^2.2.0",
"tsx": "^4.7.2",
"type-fest": "^4.15.0",
"lint-staged": "^15.2.8",
"prettier": "^3.3.3",
"shellcheck": "^3.0.0",
"tsx": "^4.17.0",
"type-fest": "^4.24.0",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
"vitest": "^2.0.5"
},
"volta": {
"node": "22.6.0",
Expand Down
56 changes: 28 additions & 28 deletions src/core/runtime-precedence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import { TServiceParams } from '@digital-alchemy/core'

export function RuntimePrecedence({ logger, config, hass, lifecycle }: TServiceParams) {
// Whether this runtime is in development mode or not
const isDevelop = config.homeAutomation.NODE_ENV === 'development'

// When developing locally, the production runtime will pause and the development runtime will take over
// @ts-expect-error - Entity will be created by setting the state here.
const isDevelopmentActive = hass.refBy.id('binary_sensor.is_development_runtime_active')

// Block outgoing commands and most incoming messages in prod when dev overrides it.
isDevelopmentActive.onUpdate(() => {
if (isDevelopmentActive.state === 'on') {
logger.info('Development runtime takes over')
// dev takes over, prod pauses
hass.socket.pauseMessages = !isDevelop
} else {
logger.info('Resuming production runtime')
// prod resumes, dev pauses
hass.socket.pauseMessages = isDevelop
}
})

// Update the state on startup
lifecycle.onReady(() => {
const isDevelop = config.homeAutomation.NODE_ENV === 'development'

// When developing locally, the production runtime will pause and the development runtime will take over
// @ts-expect-error - Entity will be created by setting the state here.
const isDevelopmentActive = hass.refBy.id('binary_sensor.is_development_runtime_active')

// Block outgoing commands and most incoming messages in prod when dev overrides it.
isDevelopmentActive.onUpdate(() => {
if (isDevelopmentActive.state === 'on') {
logger.info('Development runtime takes over')
// dev takes over, prod pauses
hass.socket.pauseMessages = !isDevelop
} else {
logger.info('Resuming production runtime')
// prod resumes, dev pauses
hass.socket.pauseMessages = isDevelop
}
})

// Update the state on startup
if (isDevelop) isDevelopmentActive.state = 'on'
})

// Give the go ahead for production to take over again when shutting down
lifecycle.onPreShutdown(async () => {
if (!isDevelop) return
// Give the go ahead for production to take over again when shutting down
lifecycle.onPreShutdown(async () => {
if (!isDevelop) return

isDevelopmentActive.state = 'off'
isDevelopmentActive.state = 'off'

const result = await isDevelopmentActive.nextState(5000)
if (!result) return logger.error(`Unable to verify that production runtime has taken over.`)
const result = await isDevelopmentActive.nextState(5000)
if (!result) return logger.error(`Unable to verify that production runtime has taken over.`)

logger.info(`Production runtime has taken over. Development: ${result.state}`)
logger.info(`Production runtime has taken over. Development: ${result.state}`)
})
})
}
Loading

0 comments on commit 13503f9

Please sign in to comment.