|
8 | 8 | <a href="https://www.npmjs.com/package/diffable-objects">
|
9 | 9 | <img src="https://img.shields.io/npm/v/diffable-objects?style=for-the-badge" alt="downloads" height="24">
|
10 | 10 | </a>
|
11 |
| - <a href="https://www.npmjs.com/package/diffable-objects"> |
| 11 | + <a href="https://github.com/zebp/diffable-objects/actions"> |
12 | 12 | <img src="https://img.shields.io/github/actions/workflow/status/zebp/diffable-objects/ci.yaml?branch=main&style=for-the-badge" alt="npm version" height="24">
|
13 | 13 | </a>
|
14 | 14 | <a href="https://github.com/zebp/diffable-objects">
|
15 | 15 | <img src="https://img.shields.io/badge/license-MIT-green?style=for-the-badge" alt="MIT license" height="24">
|
16 | 16 | </a>
|
17 | 17 | </p>
|
18 | 18 |
|
| 19 | +## Installation |
| 20 | + |
| 21 | +``` |
| 22 | +# NPM |
| 23 | +$ npm install --save diffable-objects |
| 24 | +# Yarn |
| 25 | +$ yarn add diffable-objects |
| 26 | +# PNPM |
| 27 | +$ pnpm add diffable-objects |
| 28 | +# Bun |
| 29 | +$ bun add diffable-objects |
| 30 | +``` |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { DurableObject } from "cloudflare:workers"; |
| 36 | +export { diffable, state } from "diffable-objects"; |
| 37 | + |
| 38 | +export class SampleObject extends DurableObject { |
| 39 | + // Within a durale object we can register a property to |
| 40 | + // have its values automatically tracked and persisted. |
| 41 | + #state = state(this.ctx, "state", { count: 0 }); |
| 42 | + |
| 43 | + increment() { |
| 44 | + this.#state.count++; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Currently requires wrangler@next |
| 49 | +export class DecoratorObject extends DurableObject { |
| 50 | + // You can also use decorators if you'd prefer a simpler |
| 51 | + // (but more magic) syntax. |
| 52 | + @diffable |
| 53 | + #state = { count: 0 }; |
| 54 | + |
| 55 | + // Snapshot policies are configrable via an options object. |
| 56 | + @diffable({ snapshotPolicy: "every-change" }) |
| 57 | + #stateWithOptions = { count: 0 }; |
| 58 | + |
| 59 | + increment() { |
| 60 | + this.#state.count++; |
| 61 | + this.#stateWithOptions.count++; |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
19 | 66 | ## License
|
20 | 67 |
|
21 | 68 | Distributed under the MIT License. See [LICENSE](LICENSE) for more information.
|
0 commit comments