Skip to content

Commit

Permalink
released v3.0.0-next.3 - switching to
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Jun 15, 2024
1 parent 77b19d7 commit c588a76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ npm install itty-durable@next
import { IttyDurable } from 'itty-durable'

export class Counter extends IttyDurable {
// this property will be persisted
value = 20
// anything in this.persisted will be automatically stored
$persisted = {
value = 0
}

increment(by: number = 1) {
this.value += by
this.$persisted.value += by

return this.$props() // optionally return the props
}
Expand All @@ -58,24 +60,23 @@ Under the hood, `IttyDurable` (which directy extends `DurableObject`) returns a

# API

The API of `IttyDurable` is intentionally minimalist. In fact, we only expose a handful of properties/methods:
The API of `IttyDurable` is intentionally minimalist. In fact, we only expose a handful of properties/methods, each prefixed with a `$` for clarity/separation:

### `$` - memory/temp properties
The `$` property on your DO defaults to a blank object, and is designed to house any memory-only properties you want. This property, and all contents within it, is specifically excluded from storage writes.
### `$persisted` -
The `$persisted` property in your DO is automatically synced via the store (defaults to using DO internal storage using a single key). Anything you put in here will be saved. Anything outside will reside in memory only, resetting when the DO resets/sleeps.

```ts
export class Counter extends IttyDurable {
// this property will be persisted
value = 20
// this property will not be persisted
foo = 'bar'

// but this will not
$ = {
history: [1,2,3]
// but this will be
$persisted = {
value = 0
}

increment(by: number = 1) {
this.value += by
this.$.history.push(by)
this.persisted.value += by
}
}
```
Expand Down Expand Up @@ -134,7 +135,7 @@ The `$props()` method is used to simplify the extraction of your own properties,
```ts
export class Counter extends IttyDurable {
value = 20
foo = 'bar'
foo = 'bar'

increment(by: number = 1) {
this.value += by
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "itty-durable",
"description": "Easier to use Durable Objects.",
"version": "3.0.0-next.2",
"version": "3.0.0-next.3",
"main": "./index.js",
"module": "./index.mjs",
"types": "./index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/IttyDurable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class IttyDurable extends DurableObject {
#persistLoaded: undefined | true

// persisted attributes
persisted: any = {}
$persisted: any = {}

// default store
$store: DurableStore = {
Expand Down Expand Up @@ -74,7 +74,7 @@ export class IttyDurable extends DurableObject {
// add a new timer, saving props to store when triggered
this.#persistTimer = setTimeout(
() => {
this.$store.put.call(this, this.persisted)
this.$store.put.call(this, this.$persisted)
},
delay
)
Expand Down

0 comments on commit c588a76

Please sign in to comment.