@plures/unum 0.2.2

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/dist/runes.js ADDED
@@ -0,0 +1,327 @@
1
+ /**
2
+ * unum - Svelte 5 Runes API for PluresDB
3
+ *
4
+ * This module provides a seamless binding between PluresDB and Svelte 5 components
5
+ * using the new Runes reactivity system. Components can work with PluresDB data without
6
+ * knowing about PluresDB specifics.
7
+ */
8
+ import { gun as dbStore } from './GunContext.js';
9
+ /**
10
+ * Creates a reactive connection to a PluresDB path
11
+ * This is the primary API for Svelte 5 components to use PluresDB data
12
+ *
13
+ * @example
14
+ * ```svelte
15
+ * <script>
16
+ * import { pluresData } from 'unum';
17
+ *
18
+ * // Create a reactive connection to PluresDB data
19
+ * const todos = pluresData('todos');
20
+ *
21
+ * // Access a specific todo by ID
22
+ * const todo = pluresData('todos', 'specific-id');
23
+ *
24
+ * // Create a new todo
25
+ * function addTodo(text) {
26
+ * todos.add({ text, completed: false });
27
+ * }
28
+ *
29
+ * // Toggle a todo's completed status
30
+ * function toggleTodo(id) {
31
+ * todo.update(id, current => ({ ...current, completed: !current.completed }));
32
+ * }
33
+ *
34
+ * // Delete a todo
35
+ * function deleteTodo(id) {
36
+ * todos.remove(id);
37
+ * }
38
+ * </script>
39
+ *
40
+ * <!-- Use the data directly in the template -->
41
+ * <ul>
42
+ * {#each todos.list() as item}
43
+ * <li>{item.text}</li>
44
+ * {/each}
45
+ * </ul>
46
+ * ```
47
+ */
48
+ export function pluresData(path, id = null) {
49
+ // Using a plain object, not $state since this isn't a .svelte file
50
+ let state = {};
51
+ let listeners = [];
52
+ let db = null; // Reference to PluresDB instance
53
+ let unsubscribe = null; // Store unsubscription function
54
+ // Notify all listeners when state changes
55
+ function notifyListeners() {
56
+ for (const listener of listeners) {
57
+ listener(state);
58
+ }
59
+ }
60
+ // Create the subscription to PluresDB data
61
+ function initialize() {
62
+ // Unsubscribe from any existing subscription
63
+ if (unsubscribe) {
64
+ unsubscribe();
65
+ unsubscribe = null;
66
+ }
67
+ // Subscribe to PluresDB instance
68
+ const storeUnsubscribe = dbStore.subscribe(dbInstance => {
69
+ if (!dbInstance)
70
+ return;
71
+ // Store PluresDB instance reference
72
+ db = dbInstance;
73
+ // Create reference to the PluresDB path
74
+ let ref = db.get(path);
75
+ // If an ID is provided, narrow to that specific item
76
+ if (id) {
77
+ ref = ref.get(id);
78
+ // Set up subscription to a single item
79
+ const itemUnsubscribe = ref.on((data) => {
80
+ if (data) {
81
+ // Update state
82
+ state = { ...(data || {}) };
83
+ notifyListeners();
84
+ }
85
+ });
86
+ // Update unsubscribe function
87
+ unsubscribe = () => {
88
+ ref.off();
89
+ itemUnsubscribe();
90
+ storeUnsubscribe();
91
+ };
92
+ }
93
+ else {
94
+ // Set up subscription to a collection
95
+ const mapUnsubscribe = ref.map().on((data, key) => {
96
+ if (key === '_')
97
+ return; // Skip internal PluresDB keys
98
+ if (data === null) {
99
+ // Item was deleted - create a new object to trigger updates
100
+ const newState = { ...state };
101
+ delete newState[key];
102
+ state = newState;
103
+ notifyListeners();
104
+ }
105
+ else {
106
+ // Item was added or updated
107
+ state = {
108
+ ...state,
109
+ [key]: { ...(data || {}), id: key, text: data.text || '' }
110
+ };
111
+ notifyListeners();
112
+ }
113
+ });
114
+ // Update unsubscribe function
115
+ unsubscribe = () => {
116
+ ref.map().off();
117
+ mapUnsubscribe();
118
+ storeUnsubscribe();
119
+ };
120
+ }
121
+ });
122
+ }
123
+ // Start the initialization
124
+ initialize();
125
+ // For collections, get an array of items suitable for #each loops
126
+ function list() {
127
+ if (id)
128
+ return []; // Not applicable for single items
129
+ return Object.values(state)
130
+ .filter(item => item && typeof item === 'object')
131
+ .map(item => ({ ...item, text: item.text || '' }));
132
+ }
133
+ // Add a new item to a collection
134
+ function add(data) {
135
+ if (!db || id)
136
+ return; // Can't add to a single item reference
137
+ const itemId = data.id || Date.now().toString();
138
+ const newItem = {
139
+ ...data,
140
+ text: data.text || '' // Ensure text is never undefined
141
+ };
142
+ // Add to PluresDB directly
143
+ db.get(path).get(itemId).put(newItem);
144
+ // Also update local state for immediate UI updates
145
+ state = {
146
+ ...state,
147
+ [itemId]: { ...newItem, id: itemId }
148
+ };
149
+ notifyListeners();
150
+ }
151
+ // Update an item (or the current item if this is a single item reference)
152
+ function update(itemId, updater) {
153
+ if (!db)
154
+ return;
155
+ if (id) {
156
+ // This is a single item reference
157
+ const updatedData = typeof updater === 'function'
158
+ ? updater(state)
159
+ : updater;
160
+ db.get(path).get(id).put(updatedData);
161
+ // Update local state immediately
162
+ state = { ...state, ...updatedData };
163
+ notifyListeners();
164
+ }
165
+ else {
166
+ // This is a collection
167
+ const item = state[itemId];
168
+ if (!item)
169
+ return;
170
+ const updatedData = typeof updater === 'function'
171
+ ? updater(item)
172
+ : updater;
173
+ db.get(path).get(itemId).put(updatedData);
174
+ // Update local state immediately
175
+ state = {
176
+ ...state,
177
+ [itemId]: { ...item, ...updatedData }
178
+ };
179
+ notifyListeners();
180
+ }
181
+ }
182
+ // Remove an item (or all items if this is a collection and no id is provided)
183
+ function remove(itemId = null) {
184
+ if (!db)
185
+ return;
186
+ if (id) {
187
+ // This is a single item reference - null it out
188
+ db.get(path).get(id).put(null);
189
+ state = {};
190
+ notifyListeners();
191
+ }
192
+ else if (itemId) {
193
+ // Remove a specific item from the collection
194
+ db.get(path).get(itemId).put(null);
195
+ // Update local state immediately
196
+ const newState = { ...state };
197
+ delete newState[itemId];
198
+ state = newState;
199
+ notifyListeners();
200
+ }
201
+ }
202
+ // Subscribe to changes - this is needed for Svelte components to react
203
+ function subscribe(callback) {
204
+ listeners.push(callback);
205
+ callback(state); // Initial call with current state
206
+ // Return unsubscribe function
207
+ return () => {
208
+ listeners = listeners.filter(l => l !== callback);
209
+ };
210
+ }
211
+ // Cleanup function
212
+ function destroy() {
213
+ if (unsubscribe) {
214
+ unsubscribe();
215
+ unsubscribe = null;
216
+ }
217
+ listeners = [];
218
+ }
219
+ // Return methods and data
220
+ return {
221
+ // Data access
222
+ get state() { return state; },
223
+ get value() { return id ? state : list(); },
224
+ // Methods
225
+ list,
226
+ add,
227
+ update,
228
+ remove,
229
+ subscribe,
230
+ destroy
231
+ };
232
+ }
233
+ /**
234
+ * Creates a derived state from PluresDB data - allows transforming/filtering PluresDB data
235
+ *
236
+ * @example
237
+ * ```svelte
238
+ * <script>
239
+ * import { pluresData, pluresDerived } from 'unum';
240
+ *
241
+ * const todos = pluresData('todos');
242
+ * const completedTodos = pluresDerived(todos, items => items.filter(item => item.completed));
243
+ * </script>
244
+ *
245
+ * <h2>Completed Todos</h2>
246
+ * <ul>
247
+ * {#each completedTodos.value as item}
248
+ * <li>{item.text}</li>
249
+ * {/each}
250
+ * </ul>
251
+ * ```
252
+ */
253
+ export function pluresDerived(pluresData, transformer) {
254
+ let $state = []; // Derived state
255
+ // Create a computed value that updates when the source data changes
256
+ function compute() {
257
+ const sourceData = pluresData.list ? pluresData.list() : pluresData.value;
258
+ $state = transformer(sourceData);
259
+ }
260
+ // Initial computation
261
+ compute();
262
+ // Set up a watcher to recompute when source data changes
263
+ // This would be cleaner with Svelte 5's createEffect, but for now we'll use a workaround
264
+ let previousSourceData = JSON.stringify(pluresData.list ? pluresData.list() : pluresData.value);
265
+ // Check periodically for changes (not ideal, but works until Svelte 5 effects are stable)
266
+ const interval = setInterval(() => {
267
+ const currentSourceData = JSON.stringify(pluresData.list ? pluresData.list() : pluresData.value);
268
+ if (previousSourceData !== currentSourceData) {
269
+ previousSourceData = currentSourceData;
270
+ compute();
271
+ }
272
+ }, 100);
273
+ // Return reactive state and methods
274
+ return {
275
+ get value() { return $state; },
276
+ destroy() {
277
+ clearInterval(interval);
278
+ if (pluresData.destroy)
279
+ pluresData.destroy();
280
+ }
281
+ };
282
+ }
283
+ /**
284
+ * Creates a two-way binding between a form input and PluresDB data
285
+ *
286
+ * @example
287
+ * ```svelte
288
+ * <script>
289
+ * import { pluresBind } from 'unum';
290
+ *
291
+ * const userProfile = pluresData('userProfile');
292
+ * const nameBinding = pluresBind(userProfile, 'name');
293
+ * </script>
294
+ *
295
+ * <input type="text" bind:value={nameBinding.value} />
296
+ * ```
297
+ */
298
+ export function pluresBind(pluresData, field) {
299
+ let $value = pluresData.state && pluresData.state[field] || '';
300
+ // Set up a watcher for changes to the PluresDB data
301
+ let previousData = JSON.stringify(pluresData.state);
302
+ const interval = setInterval(() => {
303
+ const currentData = JSON.stringify(pluresData.state);
304
+ if (previousData !== currentData) {
305
+ previousData = currentData;
306
+ $value = pluresData.state && pluresData.state[field] || '';
307
+ }
308
+ }, 100);
309
+ return {
310
+ get value() { return $value; },
311
+ set value(newValue) {
312
+ $value = newValue;
313
+ // Update the PluresDB data
314
+ if (pluresData.update) {
315
+ pluresData.update({ [field]: newValue });
316
+ }
317
+ },
318
+ destroy() {
319
+ clearInterval(interval);
320
+ }
321
+ };
322
+ }
323
+ // Legacy exports for backward compatibility
324
+ export const gunData = pluresData;
325
+ export const gunDerived = pluresDerived;
326
+ export const gunBind = pluresBind;
327
+ //# sourceMappingURL=runes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runes.js","sourceRoot":"","sources":["../src/runes.js"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,UAAU,CAAC,IAAI,EAAE,EAAE,GAAG,IAAI;IACxC,mEAAmE;IACnE,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,SAAS,GAAG,EAAE,CAAC;IACnB,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,iCAAiC;IAChD,IAAI,WAAW,GAAG,IAAI,CAAC,CAAC,gCAAgC;IAExD,0CAA0C;IAC1C,SAAS,eAAe;QACtB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,QAAQ,CAAC,KAA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B,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;QACD,OAAO;YACL,aAAa,CAAC,QAAQ,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC;AACJ,CAAC;AAED,4CAA4C;AAC5C,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAAC;AAClC,MAAM,CAAC,MAAM,UAAU,GAAG,aAAa,CAAC;AACxC,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Creates a PluresStore with proper error handling
3
+ */
4
+ export function createPluresStore(db: any, options?: {}): PluresStore;
5
+ /**
6
+ * A Svelte-compatible store for PluresDB data
7
+ * This provides a reactive interface between PluresDB and Svelte
8
+ */
9
+ export class PluresStore {
10
+ constructor(db: any, options?: {});
11
+ db: any;
12
+ store: import("svelte/store").Writable<any>;
13
+ setupDbSubscription(): void;
14
+ unsubscribe: any;
15
+ ensureTextProperties(data: any): any;
16
+ subscribe(run: any): import("svelte/store").Unsubscriber;
17
+ set(value: any): void;
18
+ update(updater: any): void;
19
+ destroy(): void;
20
+ }
21
+ export const GunStore: typeof PluresStore;
22
+ /**
23
+ * Creates a PluresStore with proper error handling
24
+ */
25
+ export function createGunStore(db: any, options?: {}): PluresStore;
26
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.js"],"names":[],"mappings":"AAiJA;;GAEG;AACH,sEAUC;AAvJD;;;GAGG;AACH;IACE,mCAIC;IAHC,QAAY;IACZ,4CAA2C;IAI7C,4BA4CC;IArBG,iBAiBE;IAON,qCAqBC;IAED,yDAEC;IAED,sBAiBC;IAED,2BAqBC;IAED,gBAQC;CACF;AAkBD,0CAAoC;AAhBpC;;GAEG;AACH,mEAUC"}
package/dist/store.js ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * unum - Store implementation for Svelte support
3
+ *
4
+ * This provides a writable store implementation for PluresDB data.
5
+ */
6
+ import { writable } from 'svelte/store';
7
+ /**
8
+ * A Svelte-compatible store for PluresDB data
9
+ * This provides a reactive interface between PluresDB and Svelte
10
+ */
11
+ export class PluresStore {
12
+ constructor(db, options = {}) {
13
+ this.db = db;
14
+ this.store = writable(options.initialValue);
15
+ this.setupDbSubscription();
16
+ }
17
+ setupDbSubscription() {
18
+ if (!this.db || typeof this.db.on !== 'function') {
19
+ console.error('Invalid PluresDB instance provided to PluresStore');
20
+ return;
21
+ }
22
+ try {
23
+ // First, check if we can get a snapshot of the current data
24
+ this.db.once((data) => {
25
+ console.log('PluresDB.once initial data snapshot:', data);
26
+ if (data) {
27
+ // Ensure the data has text properties where needed
28
+ this.ensureTextProperties(data);
29
+ // Initialize the store with existing data
30
+ this.store.set(data);
31
+ }
32
+ else {
33
+ // If no data exists, set an empty object
34
+ console.log('No initial data, setting empty object');
35
+ this.store.set({});
36
+ }
37
+ });
38
+ // Then subscribe to ongoing updates
39
+ this.unsubscribe = this.db.on((data, key) => {
40
+ try {
41
+ console.log('PluresDB.on received data:', data, 'with key:', key);
42
+ if (data === null) {
43
+ console.log('Received null data, setting store to empty object');
44
+ this.store.set({});
45
+ }
46
+ else {
47
+ console.log('Setting store with data:', data);
48
+ // Ensure the data has text properties where needed
49
+ this.ensureTextProperties(data);
50
+ // Simple, clean update - let Svelte handle the reactivity
51
+ this.store.set(data);
52
+ }
53
+ }
54
+ catch (error) {
55
+ console.error('Error updating store with PluresDB data:', error);
56
+ }
57
+ });
58
+ }
59
+ catch (error) {
60
+ console.error('Error setting up PluresDB subscription:', error);
61
+ }
62
+ }
63
+ // Helper method to ensure text properties exist
64
+ ensureTextProperties(data) {
65
+ if (!data || typeof data !== 'object')
66
+ return data;
67
+ // Process all non-underscore properties
68
+ Object.keys(data).forEach(key => {
69
+ if (key === '_')
70
+ return; // Skip PluresDB metadata
71
+ const item = data[key];
72
+ if (item && typeof item === 'object') {
73
+ // If the item exists but has no text property, add one
74
+ if (!item.text || typeof item.text !== 'string') {
75
+ console.log(`Adding missing text property to item ${key}`);
76
+ data[key] = {
77
+ ...item,
78
+ text: `Item ${key.substring(0, 6)}`
79
+ };
80
+ }
81
+ }
82
+ });
83
+ return data;
84
+ }
85
+ subscribe(run) {
86
+ return this.store.subscribe(run);
87
+ }
88
+ set(value) {
89
+ if (!this.db || typeof this.db.put !== 'function') {
90
+ console.error('Cannot set data on invalid PluresDB instance');
91
+ return;
92
+ }
93
+ try {
94
+ console.log('PluresStore.set called with value:', value);
95
+ // Update PluresDB - this will trigger the .on() callback above
96
+ this.db.put(value);
97
+ // Also update the store directly for immediate UI feedback
98
+ this.store.set(value);
99
+ }
100
+ catch (error) {
101
+ console.error('Error setting PluresDB data:', error);
102
+ }
103
+ }
104
+ update(updater) {
105
+ if (!this.db || typeof this.db.put !== 'function') {
106
+ console.error('Cannot update data on invalid PluresDB instance');
107
+ return;
108
+ }
109
+ try {
110
+ this.store.update((currentValue) => {
111
+ if (currentValue === undefined) {
112
+ console.warn('Updating undefined PluresDB value');
113
+ const newValue = updater({});
114
+ this.db.put(newValue);
115
+ return newValue;
116
+ }
117
+ const newValue = updater(currentValue);
118
+ this.db.put(newValue);
119
+ return newValue;
120
+ });
121
+ }
122
+ catch (error) {
123
+ console.error('Error updating PluresDB data:', error);
124
+ }
125
+ }
126
+ destroy() {
127
+ if (this.unsubscribe) {
128
+ try {
129
+ this.unsubscribe();
130
+ }
131
+ catch (error) {
132
+ console.error('Error unsubscribing from PluresDB:', error);
133
+ }
134
+ }
135
+ }
136
+ }
137
+ /**
138
+ * Creates a PluresStore with proper error handling
139
+ */
140
+ export function createPluresStore(db, options = {}) {
141
+ if (!db) {
142
+ console.error('No PluresDB instance provided to createPluresStore');
143
+ // Return a dummy store that won't crash
144
+ return new PluresStore({
145
+ on: () => () => { },
146
+ put: () => { },
147
+ }, options);
148
+ }
149
+ return new PluresStore(db, options);
150
+ }
151
+ // Legacy exports for backward compatibility
152
+ export const GunStore = PluresStore;
153
+ export const createGunStore = createPluresStore;
154
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.js"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC;;;GAGG;AACH,MAAM,OAAO,WAAW;IACtB,YAAY,EAAE,EAAE,OAAO,GAAG,EAAE;QAC1B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;QAC5C,IAAI,CAAC,mBAAmB,EAAE,CAAC;IAC7B,CAAC;IAED,mBAAmB;QACjB,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC,EAAE,KAAK,UAAU,EAAE,CAAC;YACjD,OAAO,CAAC,KAAK,CAAC,mDAAmD,CAAC,CAAC;YACnE,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,4DAA4D;YAC5D,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;gBACpB,OAAO,CAAC,GAAG,CAAC,sCAAsC,EAAE,IAAI,CAAC,CAAC;gBAC1D,IAAI,IAAI,EAAE,CAAC;oBACT,mDAAmD;oBACnD,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;oBAChC,0CAA0C;oBAC1C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC;qBAAM,CAAC;oBACN,yCAAyC;oBACzC,OAAO,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;oBACrD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBACrB,CAAC;YACH,CAAC,CAAC,CAAC;YAEH,oCAAoC;YACpC,IAAI,CAAC,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