mutts 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.
Files changed (82) hide show
  1. package/README.md +150 -0
  2. package/dist/chunks/decorator-BXsign4Z.js +176 -0
  3. package/dist/chunks/decorator-BXsign4Z.js.map +1 -0
  4. package/dist/chunks/decorator-CPbZNnsX.esm.js +168 -0
  5. package/dist/chunks/decorator-CPbZNnsX.esm.js.map +1 -0
  6. package/dist/decorator.d.ts +50 -0
  7. package/dist/decorator.esm.js +2 -0
  8. package/dist/decorator.esm.js.map +1 -0
  9. package/dist/decorator.js +11 -0
  10. package/dist/decorator.js.map +1 -0
  11. package/dist/destroyable.d.ts +48 -0
  12. package/dist/destroyable.esm.js +91 -0
  13. package/dist/destroyable.esm.js.map +1 -0
  14. package/dist/destroyable.js +98 -0
  15. package/dist/destroyable.js.map +1 -0
  16. package/dist/eventful.d.ts +11 -0
  17. package/dist/eventful.esm.js +88 -0
  18. package/dist/eventful.esm.js.map +1 -0
  19. package/dist/eventful.js +90 -0
  20. package/dist/eventful.js.map +1 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.esm.js +7 -0
  23. package/dist/index.esm.js.map +1 -0
  24. package/dist/index.js +52 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/indexable.d.ts +31 -0
  27. package/dist/indexable.esm.js +85 -0
  28. package/dist/indexable.esm.js.map +1 -0
  29. package/dist/indexable.js +89 -0
  30. package/dist/indexable.js.map +1 -0
  31. package/dist/mutts.umd.js +2 -0
  32. package/dist/mutts.umd.js.map +1 -0
  33. package/dist/mutts.umd.min.js +2 -0
  34. package/dist/mutts.umd.min.js.map +1 -0
  35. package/dist/promiseChain.d.ts +11 -0
  36. package/dist/promiseChain.esm.js +72 -0
  37. package/dist/promiseChain.esm.js.map +1 -0
  38. package/dist/promiseChain.js +74 -0
  39. package/dist/promiseChain.js.map +1 -0
  40. package/dist/reactive.d.ts +114 -0
  41. package/dist/reactive.esm.js +1455 -0
  42. package/dist/reactive.esm.js.map +1 -0
  43. package/dist/reactive.js +1472 -0
  44. package/dist/reactive.js.map +1 -0
  45. package/dist/std-decorators.d.ts +17 -0
  46. package/dist/std-decorators.esm.js +161 -0
  47. package/dist/std-decorators.esm.js.map +1 -0
  48. package/dist/std-decorators.js +169 -0
  49. package/dist/std-decorators.js.map +1 -0
  50. package/docs/decorator.md +300 -0
  51. package/docs/destroyable.md +294 -0
  52. package/docs/events.md +225 -0
  53. package/docs/indexable.md +561 -0
  54. package/docs/promiseChain.md +218 -0
  55. package/docs/reactive.md +2072 -0
  56. package/docs/std-decorators.md +558 -0
  57. package/package.json +132 -0
  58. package/src/decorator.test.ts +495 -0
  59. package/src/decorator.ts +205 -0
  60. package/src/destroyable.test.ts +155 -0
  61. package/src/destroyable.ts +158 -0
  62. package/src/eventful.test.ts +380 -0
  63. package/src/eventful.ts +69 -0
  64. package/src/index.ts +7 -0
  65. package/src/indexable.test.ts +388 -0
  66. package/src/indexable.ts +124 -0
  67. package/src/promiseChain.test.ts +201 -0
  68. package/src/promiseChain.ts +99 -0
  69. package/src/reactive/array.test.ts +923 -0
  70. package/src/reactive/array.ts +352 -0
  71. package/src/reactive/core.test.ts +1663 -0
  72. package/src/reactive/core.ts +866 -0
  73. package/src/reactive/index.ts +28 -0
  74. package/src/reactive/interface.test.ts +1477 -0
  75. package/src/reactive/interface.ts +231 -0
  76. package/src/reactive/map.test.ts +866 -0
  77. package/src/reactive/map.ts +162 -0
  78. package/src/reactive/set.test.ts +289 -0
  79. package/src/reactive/set.ts +142 -0
  80. package/src/std-decorators.test.ts +679 -0
  81. package/src/std-decorators.ts +182 -0
  82. package/src/utils.ts +52 -0
package/docs/events.md ADDED
@@ -0,0 +1,225 @@
1
+ # Events
2
+
3
+ A TypeScript library that provides a type-safe event system built around the `Eventful` class.
4
+
5
+ ## Overview
6
+
7
+ The `events` module provides a clean, type-safe API for event handling with full TypeScript support. It allows you to define event types, register listeners, emit events, and manage event subscriptions with automatic cleanup.
8
+
9
+ ## API Reference
10
+
11
+ ### `Eventful<Events>`
12
+
13
+ A base class that provides event handling capabilities with type safety.
14
+
15
+ **Generic Parameters:**
16
+ - `Events`: A record type defining event names and their corresponding callback signatures
17
+
18
+ **Example:**
19
+ ```typescript
20
+ interface MyEvents {
21
+ userLogin: (userId: string, timestamp: Date) => void
22
+ dataUpdate: (data: any[]) => void
23
+ error: (error: Error) => void
24
+ }
25
+
26
+ class MyClass extends Eventful<MyEvents> {
27
+ // Your class implementation
28
+ }
29
+ ```
30
+
31
+ ### Methods
32
+
33
+ #### `on(events: Partial<Events>): void`
34
+ #### `on<EventType extends keyof Events>(event: EventType, cb: Events[EventType]): () => void`
35
+
36
+ Registers event listeners. Can be called with either a single event and callback, or an object containing multiple events and their callbacks.
37
+
38
+ **Parameters:**
39
+ - `event`: The event name (when using single event overload)
40
+ - `cb`: The callback function to execute when the event is emitted
41
+ - `events`: An object containing event names as keys and callbacks as values (when using bulk overload)
42
+
43
+ **Returns:** An unsubscribe function (single event overload) or void (bulk overload)
44
+
45
+ **Example:**
46
+ ```typescript
47
+ // Single event
48
+ const unsubscribe = myObject.on('userLogin', (userId, timestamp) => {
49
+ console.log(`User ${userId} logged in at ${timestamp}`)
50
+ })
51
+
52
+ // Multiple events
53
+ myObject.on({
54
+ userLogin: (userId, timestamp) => console.log('Login:', userId),
55
+ dataUpdate: (data) => console.log('Data updated:', data.length),
56
+ error(error) { console.error('Error:', error.message) }
57
+ })
58
+ ```
59
+
60
+ #### `off(events: Partial<Events>): void`
61
+ #### `off<EventType extends keyof Events>(event: EventType, cb?: Events[EventType]): void`
62
+
63
+ Removes event listeners. Can be called with either a single event (and optional callback), or an object containing multiple events.
64
+
65
+ **Parameters:**
66
+ - `event`: The event name (when using single event overload)
67
+ - `cb`: Optional callback function to remove (if not provided, removes all listeners for the event)
68
+ - `events`: An object containing event names as keys and callbacks as values (when using bulk overload)
69
+
70
+ **Example:**
71
+ ```typescript
72
+ // Remove specific callback
73
+ myObject.off('userLogin', myCallback)
74
+
75
+ // Remove all listeners for an event
76
+ myObject.off('userLogin')
77
+
78
+ // Remove multiple events
79
+ myObject.off({
80
+ userLogin: myCallback,
81
+ dataUpdate: myDataCallback
82
+ })
83
+ ```
84
+
85
+ #### `emit<EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>): void`
86
+
87
+ Emits an event, calling all registered listeners and global hooks.
88
+
89
+ **Parameters:**
90
+ - `event`: The event name to emit
91
+ - `...args`: Arguments to pass to the event listeners
92
+
93
+ **Example:**
94
+ ```typescript
95
+ myObject.emit('userLogin', 'user123', new Date())
96
+ myObject.emit('dataUpdate', [1, 2, 3])
97
+ myObject.emit('error', new Error('Something went wrong'))
98
+ ```
99
+
100
+ #### `hook(cb: <EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>) => void): () => void`
101
+
102
+ Registers a global hook that receives all events emitted by this instance.
103
+
104
+ **Parameters:**
105
+ - `cb`: A callback function that receives all events and their arguments
106
+
107
+ **Returns:** An unsubscribe function
108
+
109
+ **Example:**
110
+ ```typescript
111
+ const unsubscribe = myObject.hook((event, ...args) => {
112
+ console.log(`Event ${String(event)} emitted with args:`, args)
113
+ })
114
+ ```
115
+
116
+ ## Usage Examples
117
+
118
+ ### Basic Event Handling
119
+
120
+ ```typescript
121
+ import { Eventful } from 'mutts/eventful'
122
+
123
+ interface AppEvents {
124
+ ready: () => void
125
+ dataLoaded: (data: any[]) => void
126
+ error: (error: Error) => void
127
+ }
128
+
129
+ class App extends Eventful<AppEvents> {
130
+ async initialize() {
131
+ try {
132
+ // Do initialization work
133
+ this.emit('ready')
134
+ } catch (error) {
135
+ this.emit('error', error as Error)
136
+ }
137
+ }
138
+
139
+ async loadData() {
140
+ try {
141
+ const data = await fetchData()
142
+ this.emit('dataLoaded', data)
143
+ } catch (error) {
144
+ this.emit('error', error as Error)
145
+ }
146
+ }
147
+ }
148
+
149
+ // Usage
150
+ const app = new App()
151
+
152
+ app.on('ready', () => console.log('App is ready!'))
153
+ app.on('dataLoaded', (data) => console.log('Data loaded:', data.length))
154
+ app.on('error', (error) => console.error('App error:', error.message))
155
+
156
+ await app.initialize()
157
+ await app.loadData()
158
+ ```
159
+
160
+ ### Component Communication
161
+
162
+ ```typescript
163
+ interface ComponentEvents {
164
+ stateChange: (newState: any) => void
165
+ userAction: (action: string, payload: any) => void
166
+ }
167
+
168
+ class Component extends Eventful<ComponentEvents> {
169
+ private state: any = {}
170
+
171
+ setState(newState: any) {
172
+ this.state = { ...this.state, ...newState }
173
+ this.emit('stateChange', this.state)
174
+ }
175
+
176
+ handleUserAction(action: string, payload: any) {
177
+ this.emit('userAction', action, payload)
178
+ }
179
+ }
180
+
181
+ // Parent component listening to child events
182
+ const child = new Component()
183
+ child.on('stateChange', (newState) => {
184
+ console.log('Child state changed:', newState)
185
+ })
186
+ child.on('userAction', (action, payload) => {
187
+ console.log('User performed action:', action, payload)
188
+ })
189
+ ```
190
+
191
+ ### Global Event Logging
192
+
193
+ ```typescript
194
+ class Service extends Eventful<ServiceEvents> {
195
+ // ... service implementation
196
+ }
197
+
198
+ const service = new Service()
199
+
200
+ // Log all events for debugging
201
+ const unsubscribe = service.hook((event, ...args) => {
202
+ console.log(`[${new Date().toISOString()}] Service event: ${String(event)}`, args)
203
+ })
204
+
205
+ // Later, stop logging
206
+ unsubscribe()
207
+ ```
208
+
209
+ ## Key Features
210
+
211
+ - **Type Safety**: Full TypeScript support with compile-time event type checking
212
+ - **Multiple Listeners**: Support for multiple listeners per event type
213
+ - **Global Hooks**: Ability to listen to all events with a single hook
214
+ - **Automatic Cleanup**: Unsubscribe functions for easy memory management
215
+ - **Bulk Operations**: Register or remove multiple event listeners at once
216
+ - **Flexible API**: Support for both single events and bulk event operations
217
+
218
+ ## Use Cases
219
+
220
+ - **Component Communication**: Enable components to communicate through events
221
+ - **State Management**: Notify subscribers of state changes
222
+ - **Plugin Systems**: Allow plugins to hook into application events
223
+ - **Observer Pattern**: Implement the observer pattern with type safety
224
+ - **Debugging**: Global event logging and monitoring
225
+ - **Decoupled Architecture**: Create loosely coupled systems through event communication