Skip to content

Commit

Permalink
dropping postmessage trick, update window title by time & book name
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Dec 14, 2024
1 parent 7a34529 commit 7e4f1b1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
8 changes: 6 additions & 2 deletions zss/device/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export function gadgetserver_clearscroll(sender: string, player: string) {
hub.emit('gadgetserver:clearscroll', sender, undefined, player)
}

export function register_flush(sender: string, books: string) {
hub.emit('register:flush', sender, books)
export function register_flush(
sender: string,
historylabel: string,
books: string,
) {
hub.emit('register:flush', sender, [historylabel, books])
}

export function register_share(sender: string, player: string) {
Expand Down
24 changes: 1 addition & 23 deletions zss/device/clock.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
import { createdevice } from 'zss/device'
import { TICK_FPS, TICK_RATE } from 'zss/mapping/tick'

type TIMEOUT_FN = () => void

const timeouts: TIMEOUT_FN[] = []
const messageName = 'zed-clock-proc'

function schedule(fn: TIMEOUT_FN) {
timeouts.push(fn)
window.postMessage(messageName, '*')
}

window.addEventListener(
'message',
(event) => {
if (event.source == window && event.data == messageName) {
event.stopPropagation()
const fn = timeouts.shift()
fn?.()
}
},
true,
)

const clockdevice = createdevice('clock', [], () => {
// no-op
})
Expand Down Expand Up @@ -54,7 +32,7 @@ function wake() {
}

previous = now
schedule(wake)
setTimeout(wake, 1)
}

// server is ready
Expand Down
10 changes: 7 additions & 3 deletions zss/device/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createdevice } from 'zss/device'
import { useGadgetClient } from 'zss/gadget/data/state'
import { doasync } from 'zss/mapping/func'
import { waitfor } from 'zss/mapping/tick'
import { ispresent, isstring } from 'zss/mapping/types'
import { isarray, ispresent, isstring } from 'zss/mapping/types'
import { writecopyit, writeheader, writeoption } from 'zss/words/writeui'

import {
Expand Down Expand Up @@ -187,8 +187,12 @@ const register = createdevice(
}
break
case 'flush':
if (isstring(message.data)) {
writestate(message.data)
if (isarray(message.data)) {
const [maybehistorylabel, maybecontent] = message.data
if (isstring(maybehistorylabel) && isstring(maybecontent)) {
document.title = maybehistorylabel
writestate(maybecontent)
}
}
break
case 'select':
Expand Down
11 changes: 9 additions & 2 deletions zss/device/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
memoryreadflags,
memorymessage,
memorycleanup,
memoryreadbookbysoftware,
MEMORY_LABEL,
} from 'zss/memory'
import { bookreadcodepagebyaddress } from 'zss/memory/book'
import { codepageresetstats } from 'zss/memory/codepage'
Expand Down Expand Up @@ -47,8 +49,13 @@ const observers: Record<string, MAYBE<UNOBSERVE_FUNC>> = {}
// save state
async function savestate() {
const books = memoryreadbooklist()
if (books.length) {
register_flush(vm.name(), await compressbooks(books))
const mainbook = memoryreadbookbysoftware(MEMORY_LABEL.MAIN)
if (books.length && ispresent(mainbook)) {
register_flush(
vm.name(),
`${new Date().toISOString()} ${mainbook.name}`,
await compressbooks(books),
)
}
}

Expand Down

0 comments on commit 7e4f1b1

Please sign in to comment.