Skip to content

Commit

Permalink
Callback-level repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-lippert committed Dec 20, 2023
1 parent 468e354 commit 62d1e66
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,44 @@ export class Counter {
this.state = state
this.state.blockConcurrencyWhile(async () => {
this.value = (await this.state.storage.get('value')) || 0
this.setCount = (await this.state.storage.get('setCount')) || 1
this.repeat = (await this.state.storage.get('repeat')) || true
this.callback = (await this.state.storage.get('callback')) || null
this.callback = (await this.state.storage.get('callback')) || []
})
}

async fetch(req) {
const { origin, pathname, searchParams } = new URL(req.url)
if (req.method === 'POST') {
const { setCount, repeat, callback } = await req.json()
if (setCount !== undefined) this.setCount = Math.max(setCount, 1)
if (repeat !== undefined) this.repeat = repeat ? true : false
const callback = await req.json()
if (callback !== undefined) this.callback = callback
await this.state.storage.put('setCount', this.setCount)
await this.state.storage.put('repeat', this.repeat)
if (!Array.isArray(this.callback) && this.callback !== null) this.callback = [this.callback]
await this.state.storage.put('callback', this.callback)
}
if (searchParams.has('setCount')) {
this.setCount = Math.max(parseInt(searchParams.get('setCount')), 1)
await this.state.storage.put('setCount', this.setCount)
}
if (searchParams.has('callback')) {
this.callback = searchParams.get('callback')
if (searchParams.has('callback') || searchParams.has('setCount') || searchParams.has('repeat')) {
if (typeof this.callback[0] === 'string' || this.callback[0] instanceof String) this.callback[0] = { url: this.callback[0] }
if (!this.callback.length) this.callback[0] = {}
if (searchParams.has('callback')) {
this.callback[0].url = searchParams.get('callback')
if (!this.callback[0].setCount) this.callback[0].setCount = 1
if (this.callback[0].repeat === undefined) this.callback[0].repeat = true
}
if (searchParams.has('setCount')) this.callback[0].setCount = Math.max(parseInt(searchParams.get('setCount')), 1)
if (searchParams.has('repeat')) this.callback[0].repeat = searchParams.get('repeat').toLowerCase()[0] !== 'f'
await this.state.storage.put('callback', this.callback)
}
if (searchParams.has('repeat')) {
this.repeat = searchParams.get('repeat').toLowerCase()[0] !== 'f'
await this.state.storage.put('repeat', this.repeat)
}

const oldValue = this.value
this.value = searchParams.has('reset') ? 0 : searchParams.has('read') ? this.value : this.value + 1

if (this.value != oldValue) {
if (this.callback && this.value % this.setCount === 0 && (this.repeat || this.value === this.setCount)) {
const callbacks = Array.isArray(this.callback) ? this.callback : [callback]
for (let i = 0; i < callbacks.length; i++) {
const url = typeof callbacks[i] === 'string' || callbacks[i] instanceof String ? callbacks[i] : callbacks[i].url
const init = callbacks[i].init || {}
init.headers = { 'content-type': 'application/json', ...(callbacks[i].headers || init.headers) }
init.method = callbacks[i].method || init.method || 'POST'
if (['POST', 'PUT', 'PATCH'].includes(init.method)) init.body = JSON.stringify(callbacks[i].body)
if (this.value > oldValue) {
const callbacks = this.callback ? (Array.isArray(this.callback) ? this.callback : [callback]) : []
for (let i = 0; i < callbacks.length; i++) {
const callback = callbacks[i]
if (this.value % callback.setCount === 0 && (callback.repeat || this.value === callback.setCount)) {
const url = typeof callback === 'string' || callback instanceof String ? callback : callback.url
const init = callback.init || {}
init.headers = { 'content-type': init.body ? 'application/json' : undefined, ...(callback.headers || init.headers) }
if (['POST', 'PUT', 'PATCH'].includes(init.method)) init.body = JSON.stringify(callback.body)
init.method = callback.method || init.method || init.body ? 'POST' : 'GET'
console.log({ url, init })
const data = await fetch(url, init)
console.log({ data: await data.text() })
Expand All @@ -69,6 +65,7 @@ export class Counter {
count: origin + pathname,
read: origin + pathname + '?read',
reset: origin + pathname + '?reset',
callbacks: this.callback?.length ? this.callback : undefined,
}
return new Response(JSON.stringify(retval, null, 2), { headers: { 'content-type': 'application/json' } })
}
Expand Down

0 comments on commit 62d1e66

Please sign in to comment.