-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.ts
More file actions
156 lines (143 loc) Β· 4.04 KB
/
system.ts
File metadata and controls
156 lines (143 loc) Β· 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* System Configuration
*
* Define your stateful resources here. This example shows a simple
* counter and logger with a dependency relationship.
*/
import { defineResource, StartedResource } from "braided";
import { createSystemHooks, createSystemManager } from "braided-react";
const slowResource = defineResource({
start: () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Slow resource");
}, 300); // 300ms to simulate a slow resource
// this gives time for the suspense boundary to render
// obviously, you should not do this in production
});
},
halt: () => {
console.log("Slow resource halted");
},
});
/**
* Counter Resource - No dependencies
*
* A simple stateful counter that can be incremented.
* Uses useSyncExternalStore pattern with subscribe/getSnapshot.
*/
export const counterResource = defineResource({
start: () => {
console.log("π’ Counter starting...");
let count = 0;
const listeners = new Set<() => void>();
const notify = () => {
listeners.forEach((listener) => listener());
};
return {
// For useSyncExternalStore
subscribe(listener: () => void) {
listeners.add(listener);
return () => listeners.delete(listener);
},
getSnapshot() {
return count;
},
// Public API
increment() {
count++;
console.log(`Counter incremented to ${count}`);
notify();
},
reset() {
count = 0;
console.log("Counter reset to 0");
notify();
},
};
},
halt: (counter) => {
console.log(`π’ Counter halting (final count: ${counter.getSnapshot()})`);
},
});
/**
* Logger Resource - Depends on counter
*
* Logs messages and can access the counter to log its current value.
* Also uses subscribe/getSnapshot pattern for reactive logs.
*/
export const loggerResource = defineResource({
dependencies: ["counter"],
start: ({
counter,
}: {
counter: StartedResource<typeof counterResource>;
}) => {
console.log("π Logger starting...");
let logs: string[] = [];
const listeners = new Set<() => void>();
const notify = () => {
listeners.forEach((listener) => listener());
};
const logger = {
// For useSyncExternalStore
subscribe(listener: () => void) {
listeners.add(listener);
return () => listeners.delete(listener);
},
getSnapshot() {
// note, this is the same reference, if it were to change, react would enter an infinite loop
return logs;
},
// Public API
log(message: string) {
const timestamp = new Date().toLocaleTimeString();
const entry = `[${timestamp}] ${message}`;
logs = [...logs, entry];
console.log(`π ${entry}`);
notify();
},
logCount() {
const message = `Counter is at ${counter.getSnapshot()}`;
const timestamp = new Date().toLocaleTimeString();
const entry = `[${timestamp}] ${message}`;
logs = [...logs, entry];
console.log(`π ${entry}`);
notify();
},
clear() {
logs = [];
console.log("π Logs cleared");
notify();
},
};
return logger;
},
halt: (logger) => {
console.log(
`π Logger halting (${logger.getSnapshot().length} logs recorded)`
);
},
});
/**
* System Configuration
*
* Compose your resources into a system. The library will start them
* in dependency order (counter first, then logger).
*/
export const systemConfig = {
counter: counterResource,
logger: loggerResource,
slow: slowResource,
};
/**
* Create manager and hooks for our system configuration.
*
* These hooks provide full TypeScript inference:
* - useResource('counter') returns the exact counter type
* - useResource('logger') returns the exact logger type
* - useSystem() returns the complete system type
*/
export const manager = createSystemManager(systemConfig);
export const { useSystem, useResource, SystemProvider } =
createSystemHooks(manager);