@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/LICENSE +21 -0
- package/LICENSE.md +21 -0
- package/README.md +114 -0
- package/dist/GunContext.d.ts +17 -0
- package/dist/GunContext.d.ts.map +1 -0
- package/dist/GunContext.js +180 -0
- package/dist/GunContext.js.map +1 -0
- package/dist/actions.d.ts +39 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +89 -0
- package/dist/actions.js.map +1 -0
- package/dist/gunComponent.d.ts +44 -0
- package/dist/gunComponent.d.ts.map +1 -0
- package/dist/gunComponent.js +205 -0
- package/dist/gunComponent.js.map +1 -0
- package/dist/gunify.d.ts +46 -0
- package/dist/gunify.d.ts.map +1 -0
- package/dist/gunify.js +294 -0
- package/dist/gunify.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +200 -0
- package/dist/index.js.map +1 -0
- package/dist/plures-helper.d.ts +41 -0
- package/dist/plures-helper.d.ts.map +1 -0
- package/dist/plures-helper.js +114 -0
- package/dist/plures-helper.js.map +1 -0
- package/dist/runes.d.ts +185 -0
- package/dist/runes.d.ts.map +1 -0
- package/dist/runes.js +327 -0
- package/dist/runes.js.map +1 -0
- package/dist/store.d.ts +26 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +154 -0
- package/dist/store.js.map +1 -0
- package/dist/universalGunBind.d.ts +219 -0
- package/dist/universalGunBind.d.ts.map +1 -0
- package/dist/universalGunBind.js +394 -0
- package/dist/universalGunBind.js.map +1 -0
- package/package.json +49 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* unum - PluresDB-powered Component Wrapper
|
|
3
|
+
*
|
|
4
|
+
* This module provides a higher-level component wrapper that simplifies
|
|
5
|
+
* binding Svelte components to PluresDB (pluresdb npm package) data.
|
|
6
|
+
*/
|
|
7
|
+
import { gun as gunStore } from './GunContext.js';
|
|
8
|
+
import { writable, get } from 'svelte/store';
|
|
9
|
+
/**
|
|
10
|
+
* Wraps any Svelte component with PluresDB integration
|
|
11
|
+
*
|
|
12
|
+
* @param {Component} Component - The Svelte component to wrap
|
|
13
|
+
* @param {Object} options - Configuration options
|
|
14
|
+
* @param {string} options.path - The PluresDB path to store data
|
|
15
|
+
* @param {string} [options.id] - Optional ID for this specific instance
|
|
16
|
+
* @param {Object} [options.defaultData] - Default data to use if PluresDB has no data
|
|
17
|
+
* @returns {Component} A new component that wraps the original with PluresDB integration
|
|
18
|
+
*/
|
|
19
|
+
export function gunComponent(Component, { path, id, defaultData = {} }) {
|
|
20
|
+
// Generate unique ID if not provided
|
|
21
|
+
const instanceId = id || `${path}_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
22
|
+
const gunPath = `${path}/${instanceId}`;
|
|
23
|
+
// Create a component that properly works with Svelte 5 runes mode
|
|
24
|
+
return function GunWrappedComponent(props) {
|
|
25
|
+
// Create a store for our props
|
|
26
|
+
const propsStore = writable({ ...defaultData, ...props });
|
|
27
|
+
// Flag to prevent circular updates
|
|
28
|
+
let isUpdatingFromGun = false;
|
|
29
|
+
// Gun reference and unsubscribe function
|
|
30
|
+
let gunNodeRef = null;
|
|
31
|
+
let gunUnsubscribe = null;
|
|
32
|
+
// Last known state for comparison
|
|
33
|
+
let lastKnownState = { ...defaultData, ...props };
|
|
34
|
+
// Setup Gun subscription
|
|
35
|
+
let cleanup = null;
|
|
36
|
+
// Set up our Gun subscription
|
|
37
|
+
function setup() {
|
|
38
|
+
const unsubGun = gunStore.subscribe($gun => {
|
|
39
|
+
if (!$gun)
|
|
40
|
+
return;
|
|
41
|
+
// Get the Gun node for this component
|
|
42
|
+
const gunNode = $gun.get(gunPath);
|
|
43
|
+
gunNodeRef = gunNode;
|
|
44
|
+
// Initialize with default data if empty
|
|
45
|
+
gunNode.once(data => {
|
|
46
|
+
if (!data || Object.keys(data).filter(k => k !== '_').length === 0) {
|
|
47
|
+
// Initialize with combined default and initial props
|
|
48
|
+
const initialData = { ...defaultData, ...props };
|
|
49
|
+
gunNode.put(initialData);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
// Subscribe to Gun data changes
|
|
53
|
+
if (gunUnsubscribe) {
|
|
54
|
+
gunUnsubscribe();
|
|
55
|
+
}
|
|
56
|
+
// Listen for updates
|
|
57
|
+
gunUnsubscribe = gunNode.on((data) => {
|
|
58
|
+
if (!data)
|
|
59
|
+
return;
|
|
60
|
+
// Filter out Gun metadata
|
|
61
|
+
const cleanData = {};
|
|
62
|
+
for (const key in data) {
|
|
63
|
+
if (key !== '_' && !key.startsWith('_')) {
|
|
64
|
+
cleanData[key] = data[key];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Prevent circular updates
|
|
68
|
+
if (JSON.stringify(cleanData) !== JSON.stringify(lastKnownState)) {
|
|
69
|
+
isUpdatingFromGun = true;
|
|
70
|
+
// Update store
|
|
71
|
+
propsStore.set(cleanData);
|
|
72
|
+
// Update last known state
|
|
73
|
+
lastKnownState = { ...cleanData };
|
|
74
|
+
// Reset update flag after a tick
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
isUpdatingFromGun = false;
|
|
77
|
+
}, 0);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
// Return cleanup function
|
|
82
|
+
return () => {
|
|
83
|
+
if (gunUnsubscribe) {
|
|
84
|
+
gunUnsubscribe();
|
|
85
|
+
gunUnsubscribe = null;
|
|
86
|
+
}
|
|
87
|
+
unsubGun();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Handle changes from the component
|
|
91
|
+
function handleUpdate(event) {
|
|
92
|
+
if (isUpdatingFromGun)
|
|
93
|
+
return;
|
|
94
|
+
const detail = event?.detail;
|
|
95
|
+
if (!detail)
|
|
96
|
+
return;
|
|
97
|
+
// Update our state
|
|
98
|
+
lastKnownState = { ...lastKnownState, ...detail };
|
|
99
|
+
// Update Gun
|
|
100
|
+
if (gunNodeRef) {
|
|
101
|
+
gunNodeRef.put(lastKnownState);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Update Gun when props change
|
|
105
|
+
function syncToGun() {
|
|
106
|
+
if (isUpdatingFromGun || !gunNodeRef)
|
|
107
|
+
return;
|
|
108
|
+
// Get current props
|
|
109
|
+
const currentProps = get(propsStore);
|
|
110
|
+
// Update Gun if different
|
|
111
|
+
if (JSON.stringify(currentProps) !== JSON.stringify(lastKnownState)) {
|
|
112
|
+
lastKnownState = { ...currentProps };
|
|
113
|
+
gunNodeRef.put(currentProps);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Create our component instance with proper lifecycle hooks
|
|
117
|
+
return {
|
|
118
|
+
// Set up when component is created
|
|
119
|
+
$$render() {
|
|
120
|
+
// Setup our Gun subscription if needed
|
|
121
|
+
if (!cleanup) {
|
|
122
|
+
cleanup = setup();
|
|
123
|
+
}
|
|
124
|
+
// Get derived props
|
|
125
|
+
const $props = get(propsStore);
|
|
126
|
+
// Sync to Gun when props change
|
|
127
|
+
syncToGun();
|
|
128
|
+
// Render the component with derived props
|
|
129
|
+
return Component.render
|
|
130
|
+
? Component.render($props, {}) // For Svelte compiler >= 5
|
|
131
|
+
: Component({ ...props, ...$props });
|
|
132
|
+
},
|
|
133
|
+
// Set up event handlers
|
|
134
|
+
$$events: {
|
|
135
|
+
update: handleUpdate,
|
|
136
|
+
change: handleUpdate,
|
|
137
|
+
input: handleUpdate,
|
|
138
|
+
},
|
|
139
|
+
// Clean up subscriptions when component is destroyed
|
|
140
|
+
$$destroy() {
|
|
141
|
+
if (cleanup) {
|
|
142
|
+
cleanup();
|
|
143
|
+
cleanup = null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a Svelte component that wraps another component with PluresDB integration
|
|
151
|
+
* This provides a cleaner API for creating PluresDB-powered components
|
|
152
|
+
*
|
|
153
|
+
* @param {Component} Component - The component to wrap
|
|
154
|
+
* @param {string} path - PluresDB path for data storage
|
|
155
|
+
* @param {Object} [options] - Additional options
|
|
156
|
+
* @param {string} [options.id] - Optional ID for this instance
|
|
157
|
+
* @param {Object} [options.defaultData] - Default data to use
|
|
158
|
+
* @returns {Component} A new Svelte component
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Create a PluresDB-powered TodoApp
|
|
162
|
+
* import { wrapWithGun } from '$lib/unum/gunComponent';
|
|
163
|
+
* import TodoApp from './TodoApp.svelte';
|
|
164
|
+
*
|
|
165
|
+
* // Simple API - component knows nothing about PluresDB
|
|
166
|
+
* export const PluresTodοApp = wrapWithGun(TodoApp, 'todos', {
|
|
167
|
+
* defaultData: { items: [] }
|
|
168
|
+
* });
|
|
169
|
+
*
|
|
170
|
+
* // In a parent component, use it like a normal component
|
|
171
|
+
* <PluresTodοApp title="My Todo List" />
|
|
172
|
+
*/
|
|
173
|
+
export function wrapWithGun(Component, path, options = {}) {
|
|
174
|
+
// Create a properly formatted component wrapper
|
|
175
|
+
const WrappedComponent = function (props) {
|
|
176
|
+
const wrapper = gunComponent(Component, {
|
|
177
|
+
path,
|
|
178
|
+
id: options.id,
|
|
179
|
+
defaultData: options.defaultData || {}
|
|
180
|
+
});
|
|
181
|
+
// Create a new instance of the wrapper with the props
|
|
182
|
+
const instance = new wrapper(props);
|
|
183
|
+
// Return the instance
|
|
184
|
+
return instance;
|
|
185
|
+
};
|
|
186
|
+
// Make it look like a Svelte component
|
|
187
|
+
WrappedComponent.$$render = function ($$props) {
|
|
188
|
+
const props = $$props || {};
|
|
189
|
+
const instance = new WrappedComponent(props);
|
|
190
|
+
return instance.$$render ? instance.$$render() : '';
|
|
191
|
+
};
|
|
192
|
+
// Add other important Svelte component properties
|
|
193
|
+
WrappedComponent.render = function (props, options) {
|
|
194
|
+
const wrapper = gunComponent(Component, {
|
|
195
|
+
path,
|
|
196
|
+
id: options?.id || options.id,
|
|
197
|
+
defaultData: options.defaultData || {}
|
|
198
|
+
});
|
|
199
|
+
// For Svelte 5
|
|
200
|
+
const instance = new wrapper(props);
|
|
201
|
+
return instance.$$render ? instance.$$render() : '';
|
|
202
|
+
};
|
|
203
|
+
return WrappedComponent;
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=gunComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gunComponent.js","sourceRoot":"","sources":["../src/gunComponent.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,GAAG,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,WAAW,GAAG,EAAE,EAAE;IACpE,qCAAqC;IACrC,MAAM,UAAU,GAAG,EAAE,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAC/F,MAAM,OAAO,GAAG,GAAG,IAAI,IAAI,UAAU,EAAE,CAAC;IAExC,kEAAkE;IAClE,OAAO,SAAS,mBAAmB,CAAC,KAAK;QACvC,+BAA+B;QAC/B,MAAM,UAAU,GAAG,QAAQ,CAAC,EAAE,GAAG,WAAW,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;QAE1D,mCAAmC;QACnC,IAAI,iBAAiB,GAAG,KAAK,CAAC;QAE9B,yCAAyC;QACzC,IAAI,UAAU,GAAG,IAAI,CAAC;QACtB,IAAI,cAAc,GAAG,IAAI,CAAC;QAE1B,kCAAkC;QAClC,IAAI,cAAc,GAAG,EAAE,GAAG,WAAW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d,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,EAAE;SACvC,CAAC,CAAC;QAEH,sDAAsD;QACtD,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;QAEpC,sBAAsB;QACtB,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,uCAAuC;IACvC,gBAAgB,CAAC,QAAQ,GAAG,UAAS,OAAO;QAC1C,MAAM,KAAK,GAAG,OAAO,IAAI,EAAE,CAAC;QAC5B,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC;QAC7C,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACtD,CAAC,CAAC;IAEF,kDAAkD;IAClD,gBAAgB,CAAC,MAAM,GAAG,UAAS,KAAK,EAAE,OAAO;QAC/C,MAAM,OAAO,GAAG,YAAY,CAAC,SAAS,EAAE;YACtC,IAAI;YACJ,EAAE,EAAE,OAAO,EAAE,EAAE,IAAI,OAAO,CAAC,EAAE;YAC7B,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,EAAE;SACvC,CAAC,CAAC;QAEH,eAAe;QACf,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;QACpC,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACtD,CAAC,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
|
package/dist/gunify.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Gun-powered version of any Svelte component
|
|
3
|
+
*
|
|
4
|
+
* This function takes a component and returns a new function component
|
|
5
|
+
* that automatically binds Gun.js data to the original component's props structure.
|
|
6
|
+
*
|
|
7
|
+
* @param {Component} Component - The Svelte component to enhance
|
|
8
|
+
* @param {Object} options - Options for the binding
|
|
9
|
+
* @param {string} options.path - Gun.js path for the data
|
|
10
|
+
* @param {string} [options.id] - Optional ID for specific component instance
|
|
11
|
+
* @param {Object} [options.defaultProps] - Default props to use when Gun data is not available
|
|
12
|
+
* @returns {Component} A new component that wraps the original with Gun.js binding
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Create a Gun-powered TodoList component
|
|
16
|
+
* import { unum } from '$lib/svgun/unum';
|
|
17
|
+
* import TodoList from './TodoList.svelte';
|
|
18
|
+
*
|
|
19
|
+
* export const GunTodoList = unum(TodoList, {
|
|
20
|
+
* path: 'todos',
|
|
21
|
+
* defaultProps: { items: [] }
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // In a parent component or route
|
|
25
|
+
* <GunTodoList />
|
|
26
|
+
*/
|
|
27
|
+
export function unum(Component: any, options: {
|
|
28
|
+
path: string;
|
|
29
|
+
id?: string;
|
|
30
|
+
defaultProps?: any;
|
|
31
|
+
}): any;
|
|
32
|
+
/**
|
|
33
|
+
* Universal binding between Gun.js and any component data
|
|
34
|
+
*
|
|
35
|
+
* This function creates a reactive binding that syncs component data
|
|
36
|
+
* to Gun.js in a generic way, without caring about the specific structure.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} path - Gun.js path for the data
|
|
39
|
+
* @param {Object} [options] - Configuration options
|
|
40
|
+
* @param {Object} [options.defaultData={}] - Default data to use when Gun data is not available
|
|
41
|
+
* @returns {Object} A binding object with subscriber, data object, and change handler
|
|
42
|
+
*/
|
|
43
|
+
export function connect(path: string, options?: {
|
|
44
|
+
defaultData?: any;
|
|
45
|
+
}): any;
|
|
46
|
+
//# sourceMappingURL=gunify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gunify.d.ts","sourceRoot":"","sources":["../src/gunify.js"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,8CAlBG;IAAwB,IAAI,EAApB,MAAM;IACW,EAAE,GAAnB,MAAM;IACW,YAAY;CACrC,OAsIF;AAED;;;;;;;;;;GAUG;AACH,8BALW,MAAM,YAEd;IAAyB,WAAW;CACpC,OAwKF"}
|
package/dist/gunify.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* unum - Component-agnostic Gun.js data binding for Svelte components
|
|
3
|
+
*
|
|
4
|
+
* This module provides a clean way to bind Gun.js data to Svelte components
|
|
5
|
+
* without the components knowing anything about Gun.js.
|
|
6
|
+
*/
|
|
7
|
+
import { getContext } from 'svelte';
|
|
8
|
+
import { writable, derived } from 'svelte/store';
|
|
9
|
+
import { gun as gunStore } from './GunContext.js';
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Gun-powered version of any Svelte component
|
|
12
|
+
*
|
|
13
|
+
* This function takes a component and returns a new function component
|
|
14
|
+
* that automatically binds Gun.js data to the original component's props structure.
|
|
15
|
+
*
|
|
16
|
+
* @param {Component} Component - The Svelte component to enhance
|
|
17
|
+
* @param {Object} options - Options for the binding
|
|
18
|
+
* @param {string} options.path - Gun.js path for the data
|
|
19
|
+
* @param {string} [options.id] - Optional ID for specific component instance
|
|
20
|
+
* @param {Object} [options.defaultProps] - Default props to use when Gun data is not available
|
|
21
|
+
* @returns {Component} A new component that wraps the original with Gun.js binding
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Create a Gun-powered TodoList component
|
|
25
|
+
* import { unum } from '$lib/svgun/unum';
|
|
26
|
+
* import TodoList from './TodoList.svelte';
|
|
27
|
+
*
|
|
28
|
+
* export const GunTodoList = unum(TodoList, {
|
|
29
|
+
* path: 'todos',
|
|
30
|
+
* defaultProps: { items: [] }
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // In a parent component or route
|
|
34
|
+
* <GunTodoList />
|
|
35
|
+
*/
|
|
36
|
+
export function unum(Component, options) {
|
|
37
|
+
const { path, id, defaultProps = {} } = options;
|
|
38
|
+
// Use id if provided, otherwise generate a unique one
|
|
39
|
+
const nodePath = id ? `${path}_${id}` : path;
|
|
40
|
+
// Return a new component function (Svelte 5 style)
|
|
41
|
+
return function GunifiedComponent($$props) {
|
|
42
|
+
const props = $$props || {};
|
|
43
|
+
// Create a store for the component props
|
|
44
|
+
const propsStore = writable({ ...defaultProps, ...props });
|
|
45
|
+
// Subscribe to the Gun instance
|
|
46
|
+
const gunStoreValue = getContext('gun') || gunStore;
|
|
47
|
+
let gun = null;
|
|
48
|
+
let unsubscribe = null;
|
|
49
|
+
// Subscribe to Gun updates when the component is created
|
|
50
|
+
const unsubscribeFromGun = gunStoreValue.subscribe($gun => {
|
|
51
|
+
if (!$gun)
|
|
52
|
+
return;
|
|
53
|
+
gun = $gun;
|
|
54
|
+
// Get the Gun node for this component
|
|
55
|
+
const node = gun.get(nodePath);
|
|
56
|
+
// Initialize Gun data with default props if empty
|
|
57
|
+
node.once(data => {
|
|
58
|
+
if (!data || Object.keys(data).length === 0) {
|
|
59
|
+
const initialData = { ...defaultProps, ...props };
|
|
60
|
+
node.put(initialData);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
// Subscribe to Gun data changes
|
|
64
|
+
unsubscribe = node.on((data) => {
|
|
65
|
+
if (!data)
|
|
66
|
+
return;
|
|
67
|
+
// Filter out Gun metadata
|
|
68
|
+
const cleanData = {};
|
|
69
|
+
for (const k in data) {
|
|
70
|
+
if (k !== '_' && !k.startsWith('_')) {
|
|
71
|
+
cleanData[k] = data[k];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Update props store
|
|
75
|
+
propsStore.update(current => ({
|
|
76
|
+
...current,
|
|
77
|
+
...cleanData
|
|
78
|
+
}));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
// Create the derived props for the component
|
|
82
|
+
const derivedProps = derived(propsStore, $props => $props);
|
|
83
|
+
// Handle component events
|
|
84
|
+
function handleEvent(event) {
|
|
85
|
+
const { detail } = event;
|
|
86
|
+
// If Gun instance is available
|
|
87
|
+
if (gun) {
|
|
88
|
+
const node = gun.get(nodePath);
|
|
89
|
+
if (node && detail) {
|
|
90
|
+
// Update Gun data with event detail
|
|
91
|
+
node.put(detail);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Also update the local props store
|
|
95
|
+
if (detail) {
|
|
96
|
+
propsStore.update(current => ({
|
|
97
|
+
...current,
|
|
98
|
+
...detail
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Listener for all update events
|
|
103
|
+
function addEventListeners(element) {
|
|
104
|
+
if (element) {
|
|
105
|
+
element.addEventListener('update', handleEvent);
|
|
106
|
+
}
|
|
107
|
+
return () => {
|
|
108
|
+
if (element) {
|
|
109
|
+
element.removeEventListener('update', handleEvent);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// Return the wrapped component
|
|
114
|
+
return {
|
|
115
|
+
component: Component,
|
|
116
|
+
props: derivedProps,
|
|
117
|
+
$$slot_def: {},
|
|
118
|
+
events: {
|
|
119
|
+
update: handleEvent
|
|
120
|
+
},
|
|
121
|
+
onMount(instance) {
|
|
122
|
+
if (instance && instance.$$root) {
|
|
123
|
+
// Add event listeners to the component's root element
|
|
124
|
+
return addEventListeners(instance.$$root);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
// Clean up when component is destroyed
|
|
128
|
+
destroy() {
|
|
129
|
+
if (unsubscribeFromGun) {
|
|
130
|
+
unsubscribeFromGun();
|
|
131
|
+
}
|
|
132
|
+
if (unsubscribe) {
|
|
133
|
+
unsubscribe();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Universal binding between Gun.js and any component data
|
|
141
|
+
*
|
|
142
|
+
* This function creates a reactive binding that syncs component data
|
|
143
|
+
* to Gun.js in a generic way, without caring about the specific structure.
|
|
144
|
+
*
|
|
145
|
+
* @param {string} path - Gun.js path for the data
|
|
146
|
+
* @param {Object} [options] - Configuration options
|
|
147
|
+
* @param {Object} [options.defaultData={}] - Default data to use when Gun data is not available
|
|
148
|
+
* @returns {Object} A binding object with subscriber, data object, and change handler
|
|
149
|
+
*/
|
|
150
|
+
export function connect(path, options = {}) {
|
|
151
|
+
const { defaultData = {} } = options;
|
|
152
|
+
// Create a reactive store
|
|
153
|
+
const dataStore = writable({ ...defaultData });
|
|
154
|
+
// Variable to prevent circular updates
|
|
155
|
+
let isUpdatingFromGun = false;
|
|
156
|
+
let isFirstUpdate = true;
|
|
157
|
+
// Use internal reference to track the current data state
|
|
158
|
+
let currentData = { ...defaultData };
|
|
159
|
+
// Setup Gun connection
|
|
160
|
+
const unsubscribeFromGun = gunStore.subscribe($gun => {
|
|
161
|
+
if (!$gun)
|
|
162
|
+
return;
|
|
163
|
+
// Get the Gun node
|
|
164
|
+
const node = $gun.get(path);
|
|
165
|
+
// Initialize data if empty
|
|
166
|
+
node.once(initialData => {
|
|
167
|
+
if (!initialData || Object.keys(initialData).filter(k => k !== '_').length === 0) {
|
|
168
|
+
// Initialize Gun with default data
|
|
169
|
+
node.put(defaultData);
|
|
170
|
+
// Update the store with default data
|
|
171
|
+
dataStore.set(defaultData);
|
|
172
|
+
currentData = { ...defaultData };
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// Filter out Gun metadata
|
|
176
|
+
const cleanData = {};
|
|
177
|
+
for (const k in initialData) {
|
|
178
|
+
if (k !== '_' && !k.startsWith('_')) {
|
|
179
|
+
cleanData[k] = initialData[k];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Update store with existing data
|
|
183
|
+
dataStore.set(cleanData);
|
|
184
|
+
currentData = { ...cleanData };
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
// Subscribe to Gun data changes
|
|
188
|
+
const unsubscribe = node.on((data) => {
|
|
189
|
+
if (!data)
|
|
190
|
+
return;
|
|
191
|
+
// Filter out Gun metadata
|
|
192
|
+
const cleanData = {};
|
|
193
|
+
for (const k in data) {
|
|
194
|
+
if (k !== '_' && !k.startsWith('_')) {
|
|
195
|
+
cleanData[k] = data[k];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (isFirstUpdate || JSON.stringify(cleanData) !== JSON.stringify(currentData)) {
|
|
199
|
+
isFirstUpdate = false;
|
|
200
|
+
isUpdatingFromGun = true;
|
|
201
|
+
// Update store with clean data
|
|
202
|
+
dataStore.set(cleanData);
|
|
203
|
+
// Update our internal reference
|
|
204
|
+
currentData = { ...cleanData };
|
|
205
|
+
// Reset flag after update
|
|
206
|
+
setTimeout(() => {
|
|
207
|
+
isUpdatingFromGun = false;
|
|
208
|
+
}, 0);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
return function cleanup() {
|
|
212
|
+
unsubscribe();
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
// Create reactive data object that syncs with Gun
|
|
216
|
+
const data = {};
|
|
217
|
+
// Set up a proxy to detect changes to data properties
|
|
218
|
+
const proxy = new Proxy(data, {
|
|
219
|
+
get(target, prop) {
|
|
220
|
+
// Get the latest data from the store
|
|
221
|
+
const storeData = currentData;
|
|
222
|
+
return storeData[prop];
|
|
223
|
+
},
|
|
224
|
+
set(target, prop, value) {
|
|
225
|
+
// Skip if we're in the middle of updating from Gun
|
|
226
|
+
if (isUpdatingFromGun)
|
|
227
|
+
return true;
|
|
228
|
+
// Create updated data
|
|
229
|
+
const updatedData = { ...currentData };
|
|
230
|
+
updatedData[prop] = value;
|
|
231
|
+
// Update Gun
|
|
232
|
+
gunStore.update($gun => {
|
|
233
|
+
if ($gun) {
|
|
234
|
+
$gun.get(path).put(updatedData);
|
|
235
|
+
}
|
|
236
|
+
return $gun;
|
|
237
|
+
});
|
|
238
|
+
// Update our internal reference
|
|
239
|
+
currentData = { ...updatedData };
|
|
240
|
+
// Update store
|
|
241
|
+
dataStore.set(updatedData);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
// Generic function to handle any component data changes
|
|
246
|
+
function handleComponentChange(newData) {
|
|
247
|
+
// Skip if we're in the middle of updating from Gun
|
|
248
|
+
if (isUpdatingFromGun)
|
|
249
|
+
return;
|
|
250
|
+
// Safety check for newData
|
|
251
|
+
if (!newData)
|
|
252
|
+
return;
|
|
253
|
+
// Update Gun with the new data
|
|
254
|
+
gunStore.update($gun => {
|
|
255
|
+
if ($gun) {
|
|
256
|
+
$gun.get(path).put(newData);
|
|
257
|
+
}
|
|
258
|
+
return $gun;
|
|
259
|
+
});
|
|
260
|
+
// Update our internal reference
|
|
261
|
+
currentData = { ...newData };
|
|
262
|
+
// Update store
|
|
263
|
+
dataStore.set(newData);
|
|
264
|
+
}
|
|
265
|
+
// Expose methods for updating data
|
|
266
|
+
return {
|
|
267
|
+
data: proxy,
|
|
268
|
+
subscribe: dataStore.subscribe,
|
|
269
|
+
set: handleComponentChange,
|
|
270
|
+
update(updater) {
|
|
271
|
+
// Skip if we're in the middle of updating from Gun
|
|
272
|
+
if (isUpdatingFromGun)
|
|
273
|
+
return;
|
|
274
|
+
// Apply updater to current data
|
|
275
|
+
const updatedData = updater(currentData);
|
|
276
|
+
handleComponentChange(updatedData);
|
|
277
|
+
},
|
|
278
|
+
updateField(field, value) {
|
|
279
|
+
// Skip if we're in the middle of updating from Gun
|
|
280
|
+
if (isUpdatingFromGun)
|
|
281
|
+
return;
|
|
282
|
+
// Update a specific field
|
|
283
|
+
const updatedData = { ...currentData };
|
|
284
|
+
updatedData[field] = value;
|
|
285
|
+
handleComponentChange(updatedData);
|
|
286
|
+
},
|
|
287
|
+
// Universal change handler for any component data
|
|
288
|
+
handleChange: handleComponentChange,
|
|
289
|
+
destroy() {
|
|
290
|
+
unsubscribeFromGun();
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
//# sourceMappingURL=gunify.js.map
|
|
@@ -0,0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O,CAAC,CAAC;IACzB,CAAC;IAED,mCAAmC;IACnC,OAAO;QACL,IAAI,EAAE,KAAK;QACX,SAAS,EAAE,SAAS,CAAC,SAAS;QAC9B,GAAG,EAAE,qBAAqB;QAC1B,MAAM,CAAC,OAAO;YACZ,mDAAmD;YACnD,IAAI,iBAAiB;gBAAE,OAAO;YAE9B,gCAAgC;YAChC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;YACzC,qBAAqB,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;QACD,WAAW,CAAC,KAAK,EAAE,KAAK;YACtB,mDAAmD;YACnD,IAAI,iBAAiB;gBAAE,OAAO;YAE9B,0BAA0B;YAC1B,MAAM,WAAW,GAAG,EAAE,GAAG,WAAW,EAAE,CAAC;YACvC,WAAW,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;YAC3B,qBAAqB,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;QACD,kDAAkD;QAClD,YAAY,EAAE,qBAAqB;QACnC,OAAO;YACL,kBAAkB,EAAE,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a complete PluresDB-powered component with automatic synchronization
|
|
3
|
+
*
|
|
4
|
+
* This higher-level function creates a wrapped component that automatically
|
|
5
|
+
* syncs with PluresDB (from the 'pluresdb' npm package), without requiring any knowledge of
|
|
6
|
+
* the component's props structure. Just specify the component and path, and
|
|
7
|
+
* it handles the rest.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} options - Configuration options
|
|
10
|
+
* @param {Function} options.component - The component to bind with PluresDB data
|
|
11
|
+
* @param {Object} options.db - PluresDB instance from 'pluresdb' package (can also use 'gun' for compatibility)
|
|
12
|
+
* @param {string} options.path - PluresDB path for data storage
|
|
13
|
+
* @param {string} [options.id] - Optional ID for multiple instances of the same component
|
|
14
|
+
* @param {Object} [options.defaultData] - Default data to use ONLY if no data exists yet
|
|
15
|
+
* @param {Object} [options.props] - Additional props to pass to the component (not synced with PluresDB)
|
|
16
|
+
* @returns {Function} A self-contained component that handles the synchronization
|
|
17
|
+
*/
|
|
18
|
+
export function pluresComponent(options: {
|
|
19
|
+
component: Function;
|
|
20
|
+
db: any;
|
|
21
|
+
path: string;
|
|
22
|
+
id?: string;
|
|
23
|
+
defaultData?: any;
|
|
24
|
+
props?: any;
|
|
25
|
+
}): Function;
|
|
26
|
+
export * from "./store.js";
|
|
27
|
+
export * from "./actions.js";
|
|
28
|
+
export * from "./plures-helper.js";
|
|
29
|
+
export * from "./plures-helper.js";
|
|
30
|
+
/**
|
|
31
|
+
* Creates a complete PluresDB-powered component with automatic synchronization
|
|
32
|
+
*
|
|
33
|
+
* This higher-level function creates a wrapped component that automatically
|
|
34
|
+
* syncs with PluresDB (from the 'pluresdb' npm package), without requiring any knowledge of
|
|
35
|
+
* the component's props structure. Just specify the component and path, and
|
|
36
|
+
* it handles the rest.
|
|
37
|
+
*
|
|
38
|
+
* @param {Object} options - Configuration options
|
|
39
|
+
* @param {Function} options.component - The component to bind with PluresDB data
|
|
40
|
+
* @param {Object} options.db - PluresDB instance from 'pluresdb' package (can also use 'gun' for compatibility)
|
|
41
|
+
* @param {string} options.path - PluresDB path for data storage
|
|
42
|
+
* @param {string} [options.id] - Optional ID for multiple instances of the same component
|
|
43
|
+
* @param {Object} [options.defaultData] - Default data to use ONLY if no data exists yet
|
|
44
|
+
* @param {Object} [options.props] - Additional props to pass to the component (not synced with PluresDB)
|
|
45
|
+
* @returns {Function} A self-contained component that handles the synchronization
|
|
46
|
+
*/
|
|
47
|
+
export function gunComponent(options: {
|
|
48
|
+
component: Function;
|
|
49
|
+
db: any;
|
|
50
|
+
path: string;
|
|
51
|
+
id?: string;
|
|
52
|
+
defaultData?: any;
|
|
53
|
+
props?: any;
|
|
54
|
+
}): Function;
|
|
55
|
+
import { gun } from './GunContext.js';
|
|
56
|
+
export { unum, connect, gun, gun as db, gun as plures };
|
|
57
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AA2BA;;;;;;;;;;;;;;;;GAgBG;AACH,yCARG;IAA0B,SAAS;IACX,EAAE;IACF,IAAI,EAApB,MAAM;IACW,EAAE,GAAnB,MAAM;IACW,WAAW;IACX,KAAK;CAC9B,YA+LF;;;;;AA9MD;;;;;;;;;;;;;;;;GAgBG;AACH,sCARG;IAA0B,SAAS;IACX,EAAE;IACF,IAAI,EAApB,MAAM;IACW,EAAE,GAAnB,MAAM;IACW,WAAW;IACX,KAAK;CAC9B,YA+LF;oBAnNmB,iBAAiB"}
|