Skip to content

Commit e5c14e0

Browse files
committed
feat: update docs
1 parent 2bb52dc commit e5c14e0

23 files changed

Lines changed: 129 additions & 89 deletions

examples/basic/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import React, { useSyncExternalStore } from "react";
12-
import { useResource } from "./hooks";
12+
import { useResource } from "./system";
1313

1414
/**
1515
* Counter Display Component

examples/basic/hooks.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/basic/main.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { StrictMode, Suspense } from "react";
99
import { createRoot } from "react-dom/client";
1010
import { App } from "./App";
11+
import React from "react";
1112

1213
/**
1314
* Mount React - system will start automatically via Suspense
@@ -18,7 +19,9 @@ function main() {
1819
const root = createRoot(document.getElementById("root")!);
1920
root.render(
2021
<StrictMode>
21-
<Suspense fallback={<div style={{ padding: "40px" }}>🚀 Starting system...</div>}>
22+
<Suspense
23+
fallback={<div style={{ padding: "40px" }}>🚀 Starting system...</div>}
24+
>
2225
<App />
2326
</Suspense>
2427
</StrictMode>

examples/basic/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"braided": "^0.0.4",
13-
"braided-react": "0.0.1",
13+
"braided-react": "^0.1.0",
1414
"react": "^18.3.1",
1515
"react-dom": "^18.3.1"
1616
},

examples/basic/system.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
*/
77

88
import { defineResource, StartedResource } from "braided";
9+
import { createSystemHooks, createSystemManager } from "braided-react";
10+
11+
const slowResource = defineResource({
12+
start: () => {
13+
return new Promise((resolve) => {
14+
setTimeout(() => {
15+
resolve("Slow resource");
16+
}, 300); // 300ms to simulate a slow resource
17+
// this gives time for the suspense boundary to render
18+
// obviously, you should not do this in production
19+
});
20+
},
21+
halt: () => {
22+
console.log("Slow resource halted");
23+
},
24+
});
925

1026
/**
1127
* Counter Resource - No dependencies
@@ -122,4 +138,19 @@ export const loggerResource = defineResource({
122138
export const systemConfig = {
123139
counter: counterResource,
124140
logger: loggerResource,
141+
slow: slowResource,
125142
};
143+
144+
/**
145+
* Create manager and hooks for our system configuration.
146+
*
147+
* These hooks provide full TypeScript inference:
148+
* - useResource('counter') returns the exact counter type
149+
* - useResource('logger') returns the exact logger type
150+
* - useSystem() returns the complete system type
151+
*/
152+
export const manager = createSystemManager(systemConfig);
153+
export const { useSystem, useResource, SystemProvider } =
154+
createSystemHooks(manager);
155+
156+

examples/error-boundary-example.tsx renamed to examples/error-boundary/error-boundary-example.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/**
22
* ErrorBoundary Integration Examples
3-
*
3+
*
44
* This file demonstrates how to use ErrorBoundary with braided-react
55
* to handle system startup errors gracefully.
6-
*
6+
*
77
* Install: npm install react-error-boundary
88
*/
99

1010
import { Suspense } from "react";
1111
import { ErrorBoundary } from "react-error-boundary";
1212
import { defineResource } from "braided";
1313
import { createSystemManager, createSystemHooks } from "braided-react";
14+
import React from "react";
1415

1516
// ============================================================================
1617
// Example 1: Basic ErrorBoundary with System Startup Errors
@@ -28,6 +29,7 @@ const basicConfig = {
2829
database: databaseResource,
2930
};
3031

32+
// @ts-expect-error - Type error
3133
const basicManager = createSystemManager(basicConfig);
3234
const { useSystem: useBasicSystem } = createSystemHooks(basicManager);
3335

@@ -89,16 +91,16 @@ function MultiApp() {
8991
function CustomErrorFallback({ error }: { error: Error }) {
9092
// Parse the error message to extract individual failures
9193
const errorMessage = error.message;
92-
94+
9395
// Extract individual resource errors
9496
const resourceErrors: Array<{ resource: string; message: string }> = [];
95-
97+
9698
// Parse format: "System startup failed: api: error1, cache: error2"
9799
const match = errorMessage.match(/System startup failed: (.+)/);
98100
if (match) {
99101
const errorsStr = match[1];
100102
const parts = errorsStr.split(", ");
101-
103+
102104
parts.forEach((part) => {
103105
const [resource, ...messageParts] = part.split(": ");
104106
resourceErrors.push({
@@ -288,7 +290,9 @@ function CriticalFeature() {
288290

289291
function OptionalFeature() {
290292
return (
291-
<div style={{ padding: "20px", background: "#f0fdf4", borderRadius: "8px" }}>
293+
<div
294+
style={{ padding: "20px", background: "#f0fdf4", borderRadius: "8px" }}
295+
>
292296
<h3>✅ Optional Features</h3>
293297
<p>These features work even when critical systems fail.</p>
294298
</div>
@@ -393,7 +397,8 @@ function ProductionErrorFallback({ error }: { error: Error }) {
393397
Something went wrong
394398
</h1>
395399
<p style={{ color: "#6b7280", marginBottom: "30px" }}>
396-
We're having trouble starting the application. Our team has been notified.
400+
We're having trouble starting the application. Our team has been
401+
notified.
397402
</p>
398403
<div style={{ display: "flex", gap: "10px", justifyContent: "center" }}>
399404
<button
@@ -456,29 +461,28 @@ export function ProductionExample() {
456461

457462
/**
458463
* Key Patterns:
459-
*
464+
*
460465
* 1. Always wrap your app with ErrorBoundary + Suspense:
461466
* <ErrorBoundary FallbackComponent={ErrorUI}>
462467
* <Suspense fallback={<Loading />}>
463468
* <App />
464469
* </Suspense>
465470
* </ErrorBoundary>
466-
*
471+
*
467472
* 2. System startup errors are automatically thrown by useSystem()
468473
* - Suspense catches the loading state (thrown Promise)
469474
* - ErrorBoundary catches startup failures (thrown Error)
470-
*
475+
*
471476
* 3. Error messages include all failed resources:
472477
* "System startup failed: api: error1, cache: error2"
473-
*
478+
*
474479
* 4. Use nested ErrorBoundaries for graceful degradation:
475480
* - Outer boundary for complete failures
476481
* - Inner boundaries for optional features
477-
*
482+
*
478483
* 5. In production:
479484
* - Log errors to tracking service (Sentry, LogRocket)
480485
* - Provide user-friendly error messages
481486
* - Offer retry/reload options
482487
* - Include support contact information
483488
*/
484-

examples/lazy-start/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { useResource } from "./hooks";
1414
* Uses the Zustand store from the counterStore resource.
1515
*/
1616
function Counter() {
17-
const counterStore = useResource("counterStore");
18-
const { count, increment, decrement, reset } = counterStore.useCounterStore();
17+
const { useCounterStore } = useResource("counterStore");
18+
const { count, increment, decrement, reset } = useCounterStore();
1919

2020
return (
2121
<div

examples/lazy-start/hooks.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Typed Hooks for the System
33
*/
44

5-
import { createSystemManager, createSystemHooks } from 'braided-react'
6-
import { systemConfig } from './system'
5+
import { createSystemManager, createSystemHooks } from "braided-react";
6+
import { systemConfig } from "./system";
77

8-
export const manager = createSystemManager(systemConfig)
9-
export const { useSystem, useResource, SystemProvider } = createSystemHooks(manager)
8+
export const manager = createSystemManager(systemConfig);
9+
export const { useSystem, useResource, SystemProvider } =
10+
createSystemHooks(manager);

examples/lazy-start/main.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { StrictMode, Suspense } from "react";
88
import { createRoot } from "react-dom/client";
99
import { App } from "./App";
10+
import React from "react";
1011

1112
/**
1213
* Mount React - system will start automatically
@@ -17,7 +18,13 @@ function main() {
1718
const root = createRoot(document.getElementById("root")!);
1819
root.render(
1920
<StrictMode>
20-
<Suspense fallback={<div style={{ padding: "40px" }}>🚀 Starting system with Zustand stores...</div>}>
21+
<Suspense
22+
fallback={
23+
<div style={{ padding: "40px" }}>
24+
🚀 Starting system with Zustand stores...
25+
</div>
26+
}
27+
>
2128
<App />
2229
</Suspense>
2330
</StrictMode>

0 commit comments

Comments
 (0)