reactiveStateBlob
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
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',
});
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,
  }
})
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)
})
@postpostscript/counter (example at @postpostscript/counterExample)
Migrated from folder: State/reactiveStateBlob