From 62d1e66758c06eeb8d87bfd87157c2fc8b6c5dc5 Mon Sep 17 00:00:00 2001 From: Samuel Lippert Date: Tue, 19 Dec 2023 18:40:33 -0600 Subject: [PATCH] Callback-level repeats --- worker.js | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/worker.js b/worker.js index fb07d90..037c8a4 100644 --- a/worker.js +++ b/worker.js @@ -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() }) @@ -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' } }) }