updates
This commit is contained in:
parent
0f39a95efb
commit
be415be9d6
3 changed files with 42 additions and 14 deletions
|
@ -2,11 +2,12 @@ import type { DropdownMenuTriggerProps } from '@kobalte/core/dropdown-menu'
|
||||||
import type { CustomTypeScriptWorker } from '../editor/utils/custom-worker.ts'
|
import type { CustomTypeScriptWorker } from '../editor/utils/custom-worker.ts'
|
||||||
import { timers } from '@fuman/utils'
|
import { timers } from '@fuman/utils'
|
||||||
import { persistentAtom } from '@nanostores/persistent'
|
import { persistentAtom } from '@nanostores/persistent'
|
||||||
import { LucideCheck, LucidePlay, LucidePlug, LucideRefreshCw, LucideSkull, LucideUnplug } from 'lucide-solid'
|
import { LucideCheck, LucideCheckSquare, LucidePlay, LucidePlug, LucideRefreshCw, LucideSkull, LucideSquare, LucideUnplug } from 'lucide-solid'
|
||||||
import { languages, Uri } from 'monaco-editor/esm/vs/editor/editor.api.js'
|
import { languages, Uri } from 'monaco-editor/esm/vs/editor/editor.api.js'
|
||||||
import { type mtcute, workerInvoke } from 'mtcute-repl-worker/client'
|
import { type mtcute, workerInvoke } from 'mtcute-repl-worker/client'
|
||||||
import { nanoid } from 'nanoid'
|
import { nanoid } from 'nanoid'
|
||||||
import { createEffect, createSignal, on, onCleanup, onMount } from 'solid-js'
|
import { createEffect, createSignal, on, onCleanup, onMount } from 'solid-js'
|
||||||
|
import { Dynamic } from 'solid-js/web'
|
||||||
import { Button } from '../../lib/components/ui/button.tsx'
|
import { Button } from '../../lib/components/ui/button.tsx'
|
||||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuGroupLabel, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '../../lib/components/ui/dropdown-menu.tsx'
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuGroupLabel, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '../../lib/components/ui/dropdown-menu.tsx'
|
||||||
import { cn } from '../../lib/utils.ts'
|
import { cn } from '../../lib/utils.ts'
|
||||||
|
@ -19,6 +20,10 @@ const $disconnectAfterSecs = persistentAtom('repl:disconnectAfterSecs', 60, {
|
||||||
encode: String,
|
encode: String,
|
||||||
decode: Number,
|
decode: Number,
|
||||||
})
|
})
|
||||||
|
const $enableUpdates = persistentAtom('repl:enableUpdates', true, {
|
||||||
|
encode: String,
|
||||||
|
decode: value => value === 'true',
|
||||||
|
})
|
||||||
|
|
||||||
export function Runner() {
|
export function Runner() {
|
||||||
const [devtoolsIframe, setDevtoolsIframe] = createSignal<HTMLIFrameElement | undefined>()
|
const [devtoolsIframe, setDevtoolsIframe] = createSignal<HTMLIFrameElement | undefined>()
|
||||||
|
@ -29,6 +34,7 @@ export function Runner() {
|
||||||
const [connectionState, setConnectionState] = createSignal<mtcute.ConnectionState>('offline')
|
const [connectionState, setConnectionState] = createSignal<mtcute.ConnectionState>('offline')
|
||||||
const currentAccountId = useStore($activeAccountId)
|
const currentAccountId = useStore($activeAccountId)
|
||||||
const disconnectAfterSecs = useStore($disconnectAfterSecs)
|
const disconnectAfterSecs = useStore($disconnectAfterSecs)
|
||||||
|
const enableUpdates = useStore($enableUpdates)
|
||||||
|
|
||||||
let currentScriptId: string | undefined
|
let currentScriptId: string | undefined
|
||||||
let deadTimer: timers.Timer | undefined
|
let deadTimer: timers.Timer | undefined
|
||||||
|
@ -67,6 +73,7 @@ export function Runner() {
|
||||||
iframe.contentWindow!.postMessage({
|
iframe.contentWindow!.postMessage({
|
||||||
event: 'INIT',
|
event: 'INIT',
|
||||||
accountId: currentAccountId(),
|
accountId: currentAccountId(),
|
||||||
|
logUpdates: enableUpdates(),
|
||||||
}, '*')
|
}, '*')
|
||||||
setRunnerLoaded(true)
|
setRunnerLoaded(true)
|
||||||
deadTimer = timers.setTimeout(() => {
|
deadTimer = timers.setTimeout(() => {
|
||||||
|
@ -180,11 +187,20 @@ export function Runner() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleDisconnect() {
|
function handleDisconnect() {
|
||||||
runnerIframe()?.contentWindow!.postMessage({ event: 'DISCONNECT' }, '*')
|
runnerIframe()?.contentWindow?.postMessage({ event: 'DISCONNECT' }, '*')
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleConnect() {
|
function handleConnect() {
|
||||||
runnerIframe()?.contentWindow!.postMessage({ event: 'RECONNECT' }, '*')
|
runnerIframe()?.contentWindow?.postMessage({ event: 'RECONNECT' }, '*')
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleToggleUpdates() {
|
||||||
|
const newValue = !enableUpdates()
|
||||||
|
$enableUpdates.set(newValue)
|
||||||
|
runnerIframe()?.contentWindow?.postMessage({
|
||||||
|
event: 'TOGGLE_UPDATES',
|
||||||
|
value: newValue,
|
||||||
|
}, '*')
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -249,15 +265,10 @@ export function Runner() {
|
||||||
onClick={connectionState() === 'offline' ? handleConnect : handleDisconnect}
|
onClick={connectionState() === 'offline' ? handleConnect : handleDisconnect}
|
||||||
class="text-xs"
|
class="text-xs"
|
||||||
>
|
>
|
||||||
{connectionState() === 'offline' ? (
|
<Dynamic
|
||||||
<LucidePlug
|
component={connectionState() === 'offline' ? LucidePlug : LucideUnplug}
|
||||||
class="mr-2 size-3"
|
class="mr-2 size-3"
|
||||||
/>
|
/>
|
||||||
) : (
|
|
||||||
<LucideUnplug
|
|
||||||
class="mr-2 size-3"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{connectionState() === 'offline' ? 'Connect' : 'Disconnect'}
|
{connectionState() === 'offline' ? 'Connect' : 'Disconnect'}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
|
@ -269,6 +280,13 @@ export function Runner() {
|
||||||
/>
|
/>
|
||||||
Restart runner
|
Restart runner
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem class="text-xs" onClick={handleToggleUpdates}>
|
||||||
|
<Dynamic
|
||||||
|
component={enableUpdates() ? LucideCheckSquare : LucideSquare}
|
||||||
|
class="mr-2 size-3"
|
||||||
|
/>
|
||||||
|
Log updates
|
||||||
|
</DropdownMenuItem>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuGroup>
|
<DropdownMenuGroup>
|
||||||
<DropdownMenuGroupLabel class="text-xs">
|
<DropdownMenuGroupLabel class="text-xs">
|
||||||
|
|
|
@ -40,7 +40,6 @@ function AddAccountDialog(props: {
|
||||||
show: boolean
|
show: boolean
|
||||||
testMode: boolean
|
testMode: boolean
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
// onAccountCreated: (accountId: string, user: User, dcId: number) => void
|
|
||||||
}) {
|
}) {
|
||||||
const [accountId, setAccountId] = createSignal<string | undefined>(undefined)
|
const [accountId, setAccountId] = createSignal<string | undefined>(undefined)
|
||||||
let closeTimeout: timers.Timer | undefined
|
let closeTimeout: timers.Timer | undefined
|
||||||
|
@ -51,7 +50,6 @@ function AddAccountDialog(props: {
|
||||||
finished = false
|
finished = false
|
||||||
} else {
|
} else {
|
||||||
props.onClose()
|
props.onClose()
|
||||||
// client()?.close()
|
|
||||||
timers.clearTimeout(closeTimeout)
|
timers.clearTimeout(closeTimeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ chobitsu.setOnMessage((message: string) => {
|
||||||
|
|
||||||
let lastAccountId: string | undefined
|
let lastAccountId: string | undefined
|
||||||
let lastConnectionState: ConnectionState | undefined
|
let lastConnectionState: ConnectionState | undefined
|
||||||
|
let logUpdates = false
|
||||||
|
|
||||||
function initClient(accountId: string) {
|
function initClient(accountId: string) {
|
||||||
lastAccountId = accountId
|
lastAccountId = accountId
|
||||||
|
@ -58,6 +59,11 @@ function initClient(accountId: string) {
|
||||||
lastConnectionState = state
|
lastConnectionState = state
|
||||||
window.parent.postMessage({ event: 'CONNECTION_STATE', value: state }, HOST_ORIGIN)
|
window.parent.postMessage({ event: 'CONNECTION_STATE', value: state }, HOST_ORIGIN)
|
||||||
})
|
})
|
||||||
|
window.tg.onUpdate.add((update) => {
|
||||||
|
if (!logUpdates) return
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log('%c[UPDATE]%c %s: %o', 'color: #8d7041', 'color: unset', update.name, update.data)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('message', ({ data }) => {
|
window.addEventListener('message', ({ data }) => {
|
||||||
|
@ -81,7 +87,9 @@ window.addEventListener('message', ({ data }) => {
|
||||||
sendToDevtools({ method: 'DOM.documentUpdated' })
|
sendToDevtools({ method: 'DOM.documentUpdated' })
|
||||||
|
|
||||||
initClient(data.accountId)
|
initClient(data.accountId)
|
||||||
|
logUpdates = data.logUpdates
|
||||||
window.tg?.connect()
|
window.tg?.connect()
|
||||||
|
window.tg!.startUpdatesLoop()
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
window.parent.postMessage({ event: 'PING' }, HOST_ORIGIN)
|
window.parent.postMessage({ event: 'PING' }, HOST_ORIGIN)
|
||||||
|
@ -112,6 +120,7 @@ window.addEventListener('message', ({ data }) => {
|
||||||
if (lastConnectionState !== 'offline') {
|
if (lastConnectionState !== 'offline') {
|
||||||
window.parent.postMessage({ event: 'CONNECTION_STATE', value: 'offline' }, HOST_ORIGIN)
|
window.parent.postMessage({ event: 'CONNECTION_STATE', value: 'offline' }, HOST_ORIGIN)
|
||||||
window.tg.connect()
|
window.tg.connect()
|
||||||
|
window.tg.startUpdatesLoop()
|
||||||
}
|
}
|
||||||
} else if (data.event === 'DISCONNECT') {
|
} else if (data.event === 'DISCONNECT') {
|
||||||
// todo: we dont have a clean way to disconnect i think
|
// todo: we dont have a clean way to disconnect i think
|
||||||
|
@ -122,6 +131,9 @@ window.addEventListener('message', ({ data }) => {
|
||||||
window.parent.postMessage({ event: 'CONNECTION_STATE', value: 'offline' }, HOST_ORIGIN)
|
window.parent.postMessage({ event: 'CONNECTION_STATE', value: 'offline' }, HOST_ORIGIN)
|
||||||
} else if (data.event === 'RECONNECT') {
|
} else if (data.event === 'RECONNECT') {
|
||||||
window.tg.connect()
|
window.tg.connect()
|
||||||
|
} else if (data.event === 'TOGGLE_UPDATES') {
|
||||||
|
if (data.value === logUpdates) return
|
||||||
|
logUpdates = data.value
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue