Skip to content

Commit

Permalink
Move callback to before due called
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-lippert committed Jan 10, 2024
1 parent c4d50f6 commit 8ba773a
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ export class Alarm {
async fetch(req) {
const { url, method, headers } = req
const { pathname, searchParams } = new URL(url)
let newCallback = searchParams.has('callback') && decodeURIComponent(searchParams.get('callback'))
if (newCallback && this.callback != newCallback) {
await this.state.storage.put('callback', this.callback = newCallback)
await this.state.storage.delete('body')
await this.state.storage.delete('contentType')
}
if (method === 'POST') {
await this.state.storage.put('body', this.body = await req.text())
if (headers.has('Content-Type'))
await this.state.storage.put('contentType', this.contentType = headers.get('Content-Type'))
}
if (searchParams.has('every')) {
await this.state.storage.put('every', this.every = parseInt(searchParams.get('every')))
}
let currentAlarm = await this.storage.getAlarm()
if (currentAlarm == null || Date.now() <= this.due) {
if (searchParams.has('fromnow')) {
Expand All @@ -58,21 +72,6 @@ export class Alarm {
await this.storage.setAlarm(this.due)
}

let newCallback = searchParams.has('callback') && decodeURIComponent(searchParams.get('callback'))
if (newCallback && this.callback != newCallback) {
await this.state.storage.put('callback', this.callback = newCallback)
await this.state.storage.delete('body')
await this.state.storage.delete('contentType')
}
if (method === 'POST') {
await this.state.storage.put('body', this.body = await req.text())
if (headers.has('Content-Type'))
await this.state.storage.put('contentType', this.contentType = headers.get('Content-Type'))
}
if (searchParams.has('every')) {
await this.state.storage.put('every', this.every = parseInt(searchParams.get('every')))
}

return new Response(JSON.stringify({
key: pathname.split('/')[1],
callback: this.callback,
Expand Down

0 comments on commit 8ba773a

Please sign in to comment.