@bjro/spriggan 1.0.0
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/LICENSE +201 -0
- package/README.md +232 -0
- package/package.json +56 -0
- package/src/spriggan.d.ts +251 -0
- package/src/spriggan.js +728 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spriggan - A minimal TEA-inspired framework
|
|
3
|
+
* TypeScript type definitions
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a new Spriggan instance
|
|
8
|
+
* @returns SprigganInstance with app and html functions
|
|
9
|
+
*/
|
|
10
|
+
declare function createSpriggan(): SprigganInstance;
|
|
11
|
+
|
|
12
|
+
export default createSpriggan;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Tagged template literal for HTML generation
|
|
16
|
+
* @example html`<div class="${className}">${content}</div>`
|
|
17
|
+
*/
|
|
18
|
+
export function html(
|
|
19
|
+
strings: TemplateStringsArray,
|
|
20
|
+
...values: HtmlValue[]
|
|
21
|
+
): string;
|
|
22
|
+
|
|
23
|
+
interface SprigganInstance {
|
|
24
|
+
app: typeof app;
|
|
25
|
+
html: typeof html;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type HtmlValue =
|
|
29
|
+
| string
|
|
30
|
+
| number
|
|
31
|
+
| boolean
|
|
32
|
+
| null
|
|
33
|
+
| undefined
|
|
34
|
+
| HtmlValue[]
|
|
35
|
+
| (() => HtmlValue)
|
|
36
|
+
| Message;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Message type - must have a type property
|
|
40
|
+
*/
|
|
41
|
+
interface Message {
|
|
42
|
+
type: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Dispatch function type - parameterized by message type
|
|
48
|
+
*/
|
|
49
|
+
type Dispatch<M extends Message = Message> = (msg: M) => void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Initialize a Spriggan application
|
|
53
|
+
* @param selector - CSS selector for the root element
|
|
54
|
+
* @param config - Application configuration
|
|
55
|
+
*/
|
|
56
|
+
declare function app<T, M extends Message>(
|
|
57
|
+
selector: string,
|
|
58
|
+
config: AppConfig<T, M>,
|
|
59
|
+
): AppApi<T, M>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Application configuration
|
|
63
|
+
*/
|
|
64
|
+
interface AppConfig<T, M extends Message> {
|
|
65
|
+
/** Initial state or function that returns initial state */
|
|
66
|
+
init: T | (() => T);
|
|
67
|
+
|
|
68
|
+
/** Update function that handles messages and returns new state */
|
|
69
|
+
update: UpdateFunction<T, M>;
|
|
70
|
+
|
|
71
|
+
/** View function that renders state to HTML string or DOM node */
|
|
72
|
+
view: ViewFunction<T, M>;
|
|
73
|
+
|
|
74
|
+
/** Custom effect handlers (merged with defaults) */
|
|
75
|
+
effects?: Record<string, EffectHandler<M>>;
|
|
76
|
+
|
|
77
|
+
/** Custom effect runner (replaces default) */
|
|
78
|
+
effectRunner?: EffectRunner<M>;
|
|
79
|
+
|
|
80
|
+
/** Subscription setup function for external event sources */
|
|
81
|
+
subscriptions?: SubscriptionFunction<M>;
|
|
82
|
+
|
|
83
|
+
/** Enable debug mode with logging and time-travel */
|
|
84
|
+
debug?: boolean;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Update function type
|
|
89
|
+
* Returns either new state, or [newState, ...effects] tuple
|
|
90
|
+
*/
|
|
91
|
+
type UpdateFunction<T, M extends Message> = (
|
|
92
|
+
state: T,
|
|
93
|
+
msg: M,
|
|
94
|
+
) => T | [T, ...Effect[]];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* View function type
|
|
98
|
+
* Returns HTML string, DOM Node, or undefined
|
|
99
|
+
*/
|
|
100
|
+
type ViewFunction<T, M extends Message> = (
|
|
101
|
+
state: T,
|
|
102
|
+
dispatch: Dispatch<M>,
|
|
103
|
+
) => string | Node | undefined;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Application API returned from app()
|
|
107
|
+
*/
|
|
108
|
+
interface AppApi<T, M extends Message = Message> {
|
|
109
|
+
/** Dispatch a message to trigger state update */
|
|
110
|
+
dispatch: Dispatch<M>;
|
|
111
|
+
|
|
112
|
+
/** Get current state */
|
|
113
|
+
getState: () => T | null;
|
|
114
|
+
|
|
115
|
+
/** Destroy the app and clean up resources */
|
|
116
|
+
destroy: () => void;
|
|
117
|
+
|
|
118
|
+
/** Debug tools (only present when debug: true) */
|
|
119
|
+
debug?: DebugTools<T, M>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Debug tools available on app instance when debug mode is enabled
|
|
124
|
+
*/
|
|
125
|
+
interface DebugTools<T, M extends Message = Message> {
|
|
126
|
+
/** History of state changes */
|
|
127
|
+
history: Array<{
|
|
128
|
+
msg: M;
|
|
129
|
+
state: T;
|
|
130
|
+
timestamp: number;
|
|
131
|
+
}>;
|
|
132
|
+
|
|
133
|
+
/** Travel to a specific history entry */
|
|
134
|
+
timeTravel: (index: number) => void;
|
|
135
|
+
|
|
136
|
+
/** Clear all history */
|
|
137
|
+
clearHistory: () => void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Subscription function - setup external event listeners
|
|
142
|
+
* Returns cleanup function or array of cleanup functions
|
|
143
|
+
*/
|
|
144
|
+
type SubscriptionFunction<M extends Message> = (
|
|
145
|
+
dispatch: Dispatch<M>,
|
|
146
|
+
) => CleanupFn | CleanupFn[] | undefined;
|
|
147
|
+
|
|
148
|
+
type CleanupFn = () => void;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Effect object - must have a type property
|
|
152
|
+
*/
|
|
153
|
+
interface Effect {
|
|
154
|
+
type: string;
|
|
155
|
+
[key: string]: unknown;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Effect handler function
|
|
160
|
+
*/
|
|
161
|
+
type EffectHandler<M extends Message> = (
|
|
162
|
+
effect: Effect,
|
|
163
|
+
dispatch: Dispatch<M>,
|
|
164
|
+
) => void;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Effect runner function
|
|
168
|
+
*/
|
|
169
|
+
type EffectRunner<M extends Message> = (
|
|
170
|
+
effect: Effect,
|
|
171
|
+
dispatch: Dispatch<M>,
|
|
172
|
+
handlers: Record<string, EffectHandler<M>>,
|
|
173
|
+
) => void;
|
|
174
|
+
|
|
175
|
+
// ============================================================================
|
|
176
|
+
// Built-in Effect Types
|
|
177
|
+
// ============================================================================
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* HTTP effect - make HTTP requests
|
|
181
|
+
*/
|
|
182
|
+
interface HttpEffect extends Effect {
|
|
183
|
+
type: "http";
|
|
184
|
+
url: string;
|
|
185
|
+
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
186
|
+
body?: unknown;
|
|
187
|
+
headers?: Record<string, string>;
|
|
188
|
+
onSuccess?: string;
|
|
189
|
+
onError?: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Delay effect - dispatch a message after a delay
|
|
194
|
+
*/
|
|
195
|
+
interface DelayEffect<M extends Message = Message> extends Effect {
|
|
196
|
+
type: "delay";
|
|
197
|
+
ms: number;
|
|
198
|
+
msg: M;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Storage effect - interact with localStorage
|
|
203
|
+
*/
|
|
204
|
+
interface StorageEffect extends Effect {
|
|
205
|
+
type: "storage";
|
|
206
|
+
action: "get" | "set" | "remove";
|
|
207
|
+
key: string;
|
|
208
|
+
value?: unknown;
|
|
209
|
+
onSuccess?: string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Function effect - execute a custom function
|
|
214
|
+
*/
|
|
215
|
+
interface FnEffect extends Effect {
|
|
216
|
+
type: "fn";
|
|
217
|
+
run: () => unknown;
|
|
218
|
+
onComplete?: string;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* DOM effect - perform DOM manipulations
|
|
223
|
+
*/
|
|
224
|
+
interface DomEffect extends Effect {
|
|
225
|
+
type: "dom";
|
|
226
|
+
action:
|
|
227
|
+
| "focus"
|
|
228
|
+
| "blur"
|
|
229
|
+
| "scrollIntoView"
|
|
230
|
+
| "setAttribute"
|
|
231
|
+
| "removeAttribute"
|
|
232
|
+
| "addClass"
|
|
233
|
+
| "removeClass"
|
|
234
|
+
| "toggleClass"
|
|
235
|
+
| "setProperty";
|
|
236
|
+
selector?: string;
|
|
237
|
+
name?: string;
|
|
238
|
+
value?: unknown;
|
|
239
|
+
options?: ScrollIntoViewOptions;
|
|
240
|
+
delay?: number;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Built-in effect types
|
|
245
|
+
*/
|
|
246
|
+
export type BuiltInEffect<M extends Message = Message> =
|
|
247
|
+
| HttpEffect
|
|
248
|
+
| DelayEffect<M>
|
|
249
|
+
| StorageEffect
|
|
250
|
+
| FnEffect
|
|
251
|
+
| DomEffect;
|