postpostscript avatar
fetchWorker
@postpostscript
Script
fetchWorker: communicate with a worker over a fetch -like interface! Example: import { fetchWorker } from "https://esm.town/v/postpostscript/fetchWorker"; using worker = await fetchWorker({ url: "https://esm.town/v/postpostscript/fetchWorkerExample", handler: "handler", }); const res = await worker.fetch("/", { method: "POST", body: JSON.stringify({ test: 1, }), headers: { "Content-Type": "application/json", }, }); console.log(await res.json()); // { // method: "POST", // url: "https://7ae81ab0-04cf-485a-ae09-054c4d3be6b3.val.town/", // text: { test: 1 }, // headers: { "content-type": "application/json" } // } Full Example Options: url ( string , required): URL that the worker will fetch the handler from handler ( string , defaults to default ): name of export that will execute the request
postpostscript avatar
reactiveStateBlob
@postpostscript
Script
reactiveStateBlob: wrap blob state in a proxy to autosave it on changes Examples (full example at @postpostscript/reactiveStateBlobExample ) import { reactiveStateBlob } from "https://esm.town/v/postpostscript/reactiveStateBlob" using state = await reactiveStateBlob({ viewCount: 0, rows: [] as { x: number; y: number; }[], }); state.viewCount += 1; state.rows.push({ x: Math.random(), y: Math.random(), }); This infers the key from the name of the val that uses it. To specify it, pass the key option: using state = await reactiveStateBlob({ viewCount: 0, rows: [] as { x: number; y: number; }[], }, { key: 'reactiveStateBlobExample.state', }); Updating Schema If you want to update the schema, or always verify the state that is pulled from the job, pass a function as the first argument: using state = await reactiveStateBlob((existingState) => { return { viewCount: (existingState.viewCount ?? 0) as number, rows: (existingState.rows ?? []) as { x: number; y: number; }[], someNewField: (existingState.someNewField ?? "") as string, } }) Options using state = await reactiveStateBlob<{ value: number; }>({ value: 0, }, { log: true, // log when saving key: "blobKey", // blob key to fetch/save to timeout: 100, // ms, defaults to 10 lock: true, // or LockOptions (see https://www.val.town/v/postpostscript/lock#options) }) See Also @postpostscript/counter (example at @postpostscript/counterExample )
1
Next
April 24, 2024