crann 1.0.40 → 1.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -303,6 +303,7 @@ Actions are defined in your config alongside regular state items. The key differ
|
|
|
303
303
|
```typescript
|
|
304
304
|
// service-worker.ts
|
|
305
305
|
import { create } from "crann";
|
|
306
|
+
import { BrowserLocation } from "porter-source";
|
|
306
307
|
|
|
307
308
|
const crann = create({
|
|
308
309
|
// Regular state
|
|
@@ -313,9 +314,16 @@ const crann = create({
|
|
|
313
314
|
|
|
314
315
|
// RPC action
|
|
315
316
|
increment: {
|
|
316
|
-
handler: async (
|
|
317
|
+
handler: async (
|
|
318
|
+
state: any,
|
|
319
|
+
setState: (newState: Partial<any>) => Promise<void>,
|
|
320
|
+
target: BrowserLocation,
|
|
321
|
+
amount: number
|
|
322
|
+
) => {
|
|
317
323
|
// This runs in the service worker
|
|
318
|
-
|
|
324
|
+
const newCounter = state.counter + amount;
|
|
325
|
+
await setState({ counter: newCounter });
|
|
326
|
+
return { counter: newCounter };
|
|
319
327
|
},
|
|
320
328
|
validate: (amount: number) => {
|
|
321
329
|
if (amount < 0) throw new Error("Amount must be positive");
|
|
@@ -324,7 +332,12 @@ const crann = create({
|
|
|
324
332
|
|
|
325
333
|
// Another action example
|
|
326
334
|
fetchData: {
|
|
327
|
-
handler: async (
|
|
335
|
+
handler: async (
|
|
336
|
+
state: any,
|
|
337
|
+
setState: (newState: Partial<any>) => Promise<void>,
|
|
338
|
+
target: BrowserLocation,
|
|
339
|
+
url: string
|
|
340
|
+
) => {
|
|
328
341
|
// This runs in the service worker where we can make network requests
|
|
329
342
|
const response = await fetch(url);
|
|
330
343
|
const data = await response.json();
|
|
@@ -339,13 +352,51 @@ const crann = create({
|
|
|
339
352
|
|
|
340
353
|
// Action that returns the current time
|
|
341
354
|
getCurrentTime: {
|
|
342
|
-
handler: async (
|
|
355
|
+
handler: async (
|
|
356
|
+
state: any,
|
|
357
|
+
setState: (newState: Partial<any>) => Promise<void>,
|
|
358
|
+
target: BrowserLocation
|
|
359
|
+
) => {
|
|
343
360
|
return { time: new Date().toISOString() };
|
|
344
361
|
},
|
|
345
362
|
},
|
|
346
363
|
});
|
|
347
364
|
```
|
|
348
365
|
|
|
366
|
+
#### Understanding Action Handler Parameters
|
|
367
|
+
|
|
368
|
+
Action handlers receive four parameters that are automatically provided by Crann:
|
|
369
|
+
|
|
370
|
+
1. **`state`**: The current state object containing all shared and service state
|
|
371
|
+
2. **`setState`**: A function to update the state from within the action. Use this to persist changes made by your action
|
|
372
|
+
3. **`target`**: A `BrowserLocation` object that identifies which context called the action
|
|
373
|
+
4. **`...args`**: The arguments passed to the action when called via `callAction()`
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
// Example showing how to use each parameter
|
|
377
|
+
incrementWithLogging: {
|
|
378
|
+
handler: async (
|
|
379
|
+
state: any,
|
|
380
|
+
setState: (newState: Partial<any>) => Promise<void>,
|
|
381
|
+
target: BrowserLocation,
|
|
382
|
+
amount: number
|
|
383
|
+
) => {
|
|
384
|
+
// Read from state
|
|
385
|
+
const currentCount = state.counter;
|
|
386
|
+
|
|
387
|
+
// Log which context called this action
|
|
388
|
+
console.log(`Increment called from ${target.context} with amount ${amount}`);
|
|
389
|
+
|
|
390
|
+
// Update state
|
|
391
|
+
const newCount = currentCount + amount;
|
|
392
|
+
await setState({ counter: newCount });
|
|
393
|
+
|
|
394
|
+
// Return result (optional)
|
|
395
|
+
return { counter: newCount, previousValue: currentCount };
|
|
396
|
+
},
|
|
397
|
+
}
|
|
398
|
+
```
|
|
399
|
+
|
|
349
400
|
#### Using Actions in Service Worker
|
|
350
401
|
|
|
351
402
|
Actions can be called from any context that connects to Crann:
|