@lunora/svelte 0.0.0 → 1.0.0-alpha.1
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.md +105 -0
- package/README.md +147 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +335 -0
- package/dist/index.d.ts +335 -0
- package/dist/index.mjs +10 -0
- package/dist/packem_shared/auth-D8EV6u02.mjs +28 -0
- package/dist/packem_shared/connectionStatus-CROr6jbn.mjs +14 -0
- package/dist/packem_shared/hydratePreloaded-BUv3k4-s.mjs +21 -0
- package/dist/packem_shared/is-function-reference-ycLAcI79.mjs +3 -0
- package/dist/packem_shared/mutation-CA3qEPCB.mjs +31 -0
- package/dist/packem_shared/paginatedQuery-CfCSITZv.mjs +196 -0
- package/dist/packem_shared/presence-Bf-y7QzR.mjs +66 -0
- package/dist/packem_shared/query-B14VE2Qg.mjs +36 -0
- package/dist/packem_shared/rateLimit-Cdw1kp8t.mjs +66 -0
- package/dist/packem_shared/setLunoraClient--bwSz7F6.mjs +16 -0
- package/dist/packem_shared/subscription-B3HDNcpq.mjs +46 -0
- package/dist/server.d.mts +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.mjs +1 -0
- package/dist/worker.d.mts +1 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.mjs +1 -0
- package/package.json +58 -17
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { auth } from './packem_shared/auth-D8EV6u02.mjs';
|
|
2
|
+
export { connectionStatus } from './packem_shared/connectionStatus-CROr6jbn.mjs';
|
|
3
|
+
export { getLunoraClient, setLunoraClient } from './packem_shared/setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
export { hydratePreloaded } from './packem_shared/hydratePreloaded-BUv3k4-s.mjs';
|
|
5
|
+
export { mutation } from './packem_shared/mutation-CA3qEPCB.mjs';
|
|
6
|
+
export { infiniteQuery, paginatedQuery } from './packem_shared/paginatedQuery-CfCSITZv.mjs';
|
|
7
|
+
export { presence } from './packem_shared/presence-Bf-y7QzR.mjs';
|
|
8
|
+
export { query } from './packem_shared/query-B14VE2Qg.mjs';
|
|
9
|
+
export { rateLimit } from './packem_shared/rateLimit-Cdw1kp8t.mjs';
|
|
10
|
+
export { subscription } from './packem_shared/subscription-B3HDNcpq.mjs';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getIdentityStore } from '@lunora/client/auth';
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
|
|
5
|
+
const auth = (explicitClient) => {
|
|
6
|
+
const client = explicitClient ?? getLunoraClient();
|
|
7
|
+
const store = getIdentityStore(client);
|
|
8
|
+
const token = readable(client.getAuthToken(), (set) => {
|
|
9
|
+
set(client.getAuthToken());
|
|
10
|
+
const unsub = client.onAuthTokenChange((next) => {
|
|
11
|
+
set(next);
|
|
12
|
+
});
|
|
13
|
+
return unsub;
|
|
14
|
+
});
|
|
15
|
+
const user = readable(store.getUser(), (set) => {
|
|
16
|
+
set(store.getUser());
|
|
17
|
+
const unsub = store.subscribe(() => {
|
|
18
|
+
set(store.getUser());
|
|
19
|
+
});
|
|
20
|
+
return unsub;
|
|
21
|
+
});
|
|
22
|
+
const setToken = (next) => {
|
|
23
|
+
client.setAuthToken(next);
|
|
24
|
+
};
|
|
25
|
+
return { setToken, token, user };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { auth };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readable } from 'svelte/store';
|
|
2
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
3
|
+
|
|
4
|
+
const connectionStatus = (client) => {
|
|
5
|
+
const resolved = client ?? getLunoraClient();
|
|
6
|
+
return readable(resolved.connectionStatus(), (set) => {
|
|
7
|
+
set(resolved.connectionStatus());
|
|
8
|
+
return resolved.onConnectionStatus((next) => {
|
|
9
|
+
set(next);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { connectionStatus };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { readable } from 'svelte/store';
|
|
2
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
3
|
+
|
|
4
|
+
const hydratePreloaded = (preloaded, client) => {
|
|
5
|
+
const resolvedClient = client ?? getLunoraClient();
|
|
6
|
+
const { args, functionPath, shardKey, value } = preloaded;
|
|
7
|
+
const functionRef = { __lunoraRef: functionPath };
|
|
8
|
+
return readable(
|
|
9
|
+
value,
|
|
10
|
+
(set) => resolvedClient.subscribe(
|
|
11
|
+
functionRef,
|
|
12
|
+
args,
|
|
13
|
+
(next) => {
|
|
14
|
+
set(next);
|
|
15
|
+
},
|
|
16
|
+
{ shardKey }
|
|
17
|
+
)
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { hydratePreloaded };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createMutationRunner } from '@lunora/client';
|
|
2
|
+
import { writable } from 'svelte/store';
|
|
3
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
|
|
5
|
+
function mutation(clientOrFunction, maybeFunction) {
|
|
6
|
+
const hasExplicitClient = maybeFunction !== void 0;
|
|
7
|
+
const client = hasExplicitClient ? clientOrFunction : getLunoraClient();
|
|
8
|
+
const functionRef = hasExplicitClient ? maybeFunction : clientOrFunction;
|
|
9
|
+
const data = writable();
|
|
10
|
+
const error = writable();
|
|
11
|
+
const pending = writable(false);
|
|
12
|
+
const mutate = createMutationRunner(client, functionRef, {
|
|
13
|
+
setError: (next) => {
|
|
14
|
+
error.set(next);
|
|
15
|
+
},
|
|
16
|
+
setPending: (next) => {
|
|
17
|
+
pending.set(next);
|
|
18
|
+
},
|
|
19
|
+
setResult: (result) => {
|
|
20
|
+
data.set(result);
|
|
21
|
+
error.set(void 0);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const reset = () => {
|
|
25
|
+
data.set(void 0);
|
|
26
|
+
error.set(void 0);
|
|
27
|
+
};
|
|
28
|
+
return { data, error, mutate, pending, reset };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { mutation };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { initialPages, derivePaginationStatus, rebalance, applyLoadMore } from '@lunora/client/pagination';
|
|
2
|
+
import { derived, writable, readable } from 'svelte/store';
|
|
3
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
import { i as isFunctionReference } from './is-function-reference-ycLAcI79.mjs';
|
|
5
|
+
|
|
6
|
+
const buildPageArgs = (page, baseArgs) => {
|
|
7
|
+
return {
|
|
8
|
+
...baseArgs,
|
|
9
|
+
paginationOpts: { cursor: page.lower, endCursor: page.upper, numItems: page.numItems }
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
const buildPageKey = (functionPath, pageArgs) => `${functionPath}::${JSON.stringify(pageArgs)}`;
|
|
13
|
+
const createPaginatedEngine = (client, function_, baseArgs, options) => {
|
|
14
|
+
const { initialNumItems, shardKey } = options;
|
|
15
|
+
const pagesStore = writable(initialPages(initialNumItems));
|
|
16
|
+
const pageResultsInternal = writable([]);
|
|
17
|
+
const resultsByKey = /* @__PURE__ */ new Map();
|
|
18
|
+
const activeSubs = /* @__PURE__ */ new Map();
|
|
19
|
+
const pendingPageKeys = /* @__PURE__ */ new Set();
|
|
20
|
+
let currentPages = initialPages(initialNumItems);
|
|
21
|
+
const currentBaseArgs = baseArgs;
|
|
22
|
+
pagesStore.subscribe((pages) => {
|
|
23
|
+
currentPages = pages;
|
|
24
|
+
});
|
|
25
|
+
const rebuildPageResults = () => {
|
|
26
|
+
if (currentBaseArgs === "skip") {
|
|
27
|
+
pageResultsInternal.set([]);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const updated = currentPages.map((page) => {
|
|
31
|
+
const key = buildPageKey(function_["__lunoraRef"], buildPageArgs(page, currentBaseArgs));
|
|
32
|
+
return resultsByKey.get(key);
|
|
33
|
+
});
|
|
34
|
+
pageResultsInternal.set(updated);
|
|
35
|
+
};
|
|
36
|
+
const syncSubscriptions = () => {
|
|
37
|
+
if (currentBaseArgs === "skip") {
|
|
38
|
+
for (const unsub of activeSubs.values()) {
|
|
39
|
+
unsub();
|
|
40
|
+
}
|
|
41
|
+
activeSubs.clear();
|
|
42
|
+
pageResultsInternal.set([]);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const baseArgsRecord = currentBaseArgs;
|
|
46
|
+
const wantedKeys = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const page of currentPages) {
|
|
48
|
+
wantedKeys.add(buildPageKey(function_["__lunoraRef"], buildPageArgs(page, baseArgsRecord)));
|
|
49
|
+
}
|
|
50
|
+
for (const [key, unsub] of activeSubs) {
|
|
51
|
+
if (!wantedKeys.has(key)) {
|
|
52
|
+
unsub();
|
|
53
|
+
activeSubs.delete(key);
|
|
54
|
+
pendingPageKeys.delete(key);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const page of currentPages) {
|
|
58
|
+
const pageArgs = buildPageArgs(page, baseArgsRecord);
|
|
59
|
+
const key = buildPageKey(function_["__lunoraRef"], pageArgs);
|
|
60
|
+
if (activeSubs.has(key)) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
pendingPageKeys.add(key);
|
|
64
|
+
const unsub = client.subscribe(
|
|
65
|
+
function_,
|
|
66
|
+
pageArgs,
|
|
67
|
+
(value) => {
|
|
68
|
+
resultsByKey.set(key, value);
|
|
69
|
+
pendingPageKeys.delete(key);
|
|
70
|
+
rebuildPageResults();
|
|
71
|
+
if (pendingPageKeys.size === 0) {
|
|
72
|
+
let updatedResults = [];
|
|
73
|
+
pageResultsInternal.subscribe((results) => {
|
|
74
|
+
updatedResults = results;
|
|
75
|
+
})();
|
|
76
|
+
const next = rebalance(currentPages, updatedResults);
|
|
77
|
+
if (next) {
|
|
78
|
+
pagesStore.set(next);
|
|
79
|
+
syncSubscriptions();
|
|
80
|
+
rebuildPageResults();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
{ shardKey }
|
|
85
|
+
);
|
|
86
|
+
activeSubs.set(key, unsub);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const teardownAll = () => {
|
|
90
|
+
for (const unsub of activeSubs.values()) {
|
|
91
|
+
unsub();
|
|
92
|
+
}
|
|
93
|
+
activeSubs.clear();
|
|
94
|
+
resultsByKey.clear();
|
|
95
|
+
pendingPageKeys.clear();
|
|
96
|
+
};
|
|
97
|
+
const pageResults = readable([], (set) => {
|
|
98
|
+
const unsubInternal = pageResultsInternal.subscribe(set);
|
|
99
|
+
if (currentBaseArgs !== "skip") {
|
|
100
|
+
syncSubscriptions();
|
|
101
|
+
rebuildPageResults();
|
|
102
|
+
}
|
|
103
|
+
return () => {
|
|
104
|
+
unsubInternal();
|
|
105
|
+
teardownAll();
|
|
106
|
+
pagesStore.set(initialPages(initialNumItems));
|
|
107
|
+
pageResultsInternal.set([]);
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
const status = derived(
|
|
111
|
+
pageResults,
|
|
112
|
+
(results) => derivePaginationStatus(currentBaseArgs === "skip", results).status
|
|
113
|
+
);
|
|
114
|
+
const loadMore = (numberItems) => {
|
|
115
|
+
if (currentBaseArgs === "skip") {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let currentResults = [];
|
|
119
|
+
pageResultsInternal.subscribe((results) => {
|
|
120
|
+
currentResults = results;
|
|
121
|
+
})();
|
|
122
|
+
const { nextCursor, status: currentStatus } = derivePaginationStatus(false, currentResults);
|
|
123
|
+
if (currentStatus !== "CanLoadMore") {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const next = applyLoadMore(currentPages, nextCursor, numberItems);
|
|
127
|
+
if (!next) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const oldTail = currentPages.at(-1);
|
|
131
|
+
const newPinnedPage = next.at(-2);
|
|
132
|
+
if (oldTail && newPinnedPage) {
|
|
133
|
+
const oldKey = buildPageKey(function_["__lunoraRef"], buildPageArgs(oldTail, currentBaseArgs));
|
|
134
|
+
const newKey = buildPageKey(function_["__lunoraRef"], buildPageArgs(newPinnedPage, currentBaseArgs));
|
|
135
|
+
if (oldKey !== newKey) {
|
|
136
|
+
const carried = resultsByKey.get(oldKey);
|
|
137
|
+
if (carried) {
|
|
138
|
+
resultsByKey.set(newKey, carried);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
pagesStore.set(next);
|
|
143
|
+
syncSubscriptions();
|
|
144
|
+
rebuildPageResults();
|
|
145
|
+
};
|
|
146
|
+
return { loadMore, pageResults, status };
|
|
147
|
+
};
|
|
148
|
+
function paginatedQuery(clientOrFunction, functionOrArguments, argumentsOrOptions, maybeOptions) {
|
|
149
|
+
const hasExplicitClient = !isFunctionReference(clientOrFunction);
|
|
150
|
+
const client = hasExplicitClient ? clientOrFunction : getLunoraClient();
|
|
151
|
+
const functionRef = hasExplicitClient ? functionOrArguments : clientOrFunction;
|
|
152
|
+
const args = hasExplicitClient ? argumentsOrOptions : functionOrArguments;
|
|
153
|
+
const options = hasExplicitClient ? maybeOptions : argumentsOrOptions;
|
|
154
|
+
const { loadMore, pageResults, status } = createPaginatedEngine(client, functionRef, args, options);
|
|
155
|
+
const results = derived(pageResults, (currentResults) => {
|
|
156
|
+
const items = [];
|
|
157
|
+
for (const page of currentResults) {
|
|
158
|
+
if (page) {
|
|
159
|
+
items.push(...page.page);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return items;
|
|
163
|
+
});
|
|
164
|
+
const isLoading = derived(
|
|
165
|
+
status,
|
|
166
|
+
(currentStatus) => currentStatus === "LoadingFirstPage" || currentStatus === "LoadingMore"
|
|
167
|
+
);
|
|
168
|
+
return { isLoading, loadMore, results, status };
|
|
169
|
+
}
|
|
170
|
+
function infiniteQuery(clientOrFunction, functionOrArguments, argumentsOrOptions, maybeOptions) {
|
|
171
|
+
const hasExplicitClient = !isFunctionReference(clientOrFunction);
|
|
172
|
+
const client = hasExplicitClient ? clientOrFunction : getLunoraClient();
|
|
173
|
+
const functionRef = hasExplicitClient ? functionOrArguments : clientOrFunction;
|
|
174
|
+
const args = hasExplicitClient ? argumentsOrOptions : functionOrArguments;
|
|
175
|
+
const options = hasExplicitClient ? maybeOptions : argumentsOrOptions;
|
|
176
|
+
const { initialNumItems } = options;
|
|
177
|
+
const { loadMore, pageResults, status } = createPaginatedEngine(client, functionRef, args, options);
|
|
178
|
+
const pages = derived(pageResults, (currentResults) => {
|
|
179
|
+
const result = [];
|
|
180
|
+
for (const page of currentResults) {
|
|
181
|
+
if (page) {
|
|
182
|
+
result.push(page.page);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
});
|
|
187
|
+
const isLoading = derived(status, (s) => s === "LoadingFirstPage");
|
|
188
|
+
const hasNextPage = derived(status, (s) => s === "CanLoadMore");
|
|
189
|
+
const isFetchingNextPage = derived(status, (s) => s === "LoadingMore");
|
|
190
|
+
const fetchNextPage = (numberItems) => {
|
|
191
|
+
loadMore(numberItems ?? initialNumItems);
|
|
192
|
+
};
|
|
193
|
+
return { fetchNextPage, hasNextPage, isFetchingNextPage, isLoading, pages, status };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export { infiniteQuery, paginatedQuery };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { readable } from 'svelte/store';
|
|
2
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
3
|
+
|
|
4
|
+
const makeSessionId = () => {
|
|
5
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
6
|
+
return crypto.randomUUID();
|
|
7
|
+
}
|
|
8
|
+
return `sess-${Math.random().toString(36).slice(2)}-${String(Date.now())}`;
|
|
9
|
+
};
|
|
10
|
+
const DEFAULT_INTERVAL_MS = 1e4;
|
|
11
|
+
const createPresenceHandle = (client, roomId, options) => {
|
|
12
|
+
const { heartbeat, intervalMs = DEFAULT_INTERVAL_MS, listPresent, shardKey } = options;
|
|
13
|
+
const sessionId = options.sessionId ?? makeSessionId();
|
|
14
|
+
let latestData = options.data;
|
|
15
|
+
const sendHeartbeat = () => {
|
|
16
|
+
const args = {
|
|
17
|
+
roomId,
|
|
18
|
+
sessionId,
|
|
19
|
+
...latestData === void 0 ? {} : { data: latestData }
|
|
20
|
+
};
|
|
21
|
+
client.mutation(heartbeat, args, { shardKey }).catch(() => void 0);
|
|
22
|
+
};
|
|
23
|
+
const setData = (next) => {
|
|
24
|
+
latestData = next;
|
|
25
|
+
sendHeartbeat();
|
|
26
|
+
};
|
|
27
|
+
sendHeartbeat();
|
|
28
|
+
const intervalHandle = setInterval(sendHeartbeat, intervalMs);
|
|
29
|
+
const onVisible = () => {
|
|
30
|
+
if (typeof document !== "undefined" && document.visibilityState === "visible") {
|
|
31
|
+
sendHeartbeat();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
if (typeof document !== "undefined") {
|
|
35
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
36
|
+
}
|
|
37
|
+
const releaseConnectionContext = client.acquireConnectionContext({ roomId, sessionId }, { shardKey });
|
|
38
|
+
const present = readable(void 0, (set) => {
|
|
39
|
+
const unsubscribe = client.subscribe(
|
|
40
|
+
listPresent,
|
|
41
|
+
{ roomId },
|
|
42
|
+
(value) => {
|
|
43
|
+
set(value);
|
|
44
|
+
},
|
|
45
|
+
{ shardKey }
|
|
46
|
+
);
|
|
47
|
+
return unsubscribe;
|
|
48
|
+
});
|
|
49
|
+
const teardown = () => {
|
|
50
|
+
clearInterval(intervalHandle);
|
|
51
|
+
if (typeof document !== "undefined") {
|
|
52
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
53
|
+
}
|
|
54
|
+
releaseConnectionContext();
|
|
55
|
+
};
|
|
56
|
+
return { present, sessionId, setData, teardown };
|
|
57
|
+
};
|
|
58
|
+
function presence(clientOrRoomId, roomIdOrOptions, maybeOptions) {
|
|
59
|
+
const hasExplicitClient = typeof clientOrRoomId !== "string";
|
|
60
|
+
const client = hasExplicitClient ? clientOrRoomId : getLunoraClient();
|
|
61
|
+
const roomId = hasExplicitClient ? roomIdOrOptions : clientOrRoomId;
|
|
62
|
+
const options = hasExplicitClient ? maybeOptions : roomIdOrOptions;
|
|
63
|
+
return createPresenceHandle(client, roomId, options);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { presence };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createQuerySubscription } from '@lunora/client/query';
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
import { i as isFunctionReference } from './is-function-reference-ycLAcI79.mjs';
|
|
5
|
+
|
|
6
|
+
function query(clientOrFunction, functionOrArguments, argumentsOrOptions, maybeOptions) {
|
|
7
|
+
const hasExplicitClient = !isFunctionReference(clientOrFunction);
|
|
8
|
+
const client = hasExplicitClient ? clientOrFunction : getLunoraClient();
|
|
9
|
+
const functionRef = hasExplicitClient ? functionOrArguments : clientOrFunction;
|
|
10
|
+
const args = hasExplicitClient ? argumentsOrOptions : functionOrArguments;
|
|
11
|
+
const options = (hasExplicitClient ? maybeOptions : argumentsOrOptions) ?? {};
|
|
12
|
+
return readable(
|
|
13
|
+
void 0,
|
|
14
|
+
(set) => (
|
|
15
|
+
// The shared `@lunora/client/query` state machine owns the subscribe +
|
|
16
|
+
// cleanup: it replays the last value synchronously when one exists and
|
|
17
|
+
// pushes every subsequent delta into the store, and its returned teardown
|
|
18
|
+
// is the store's stop callback, so the WS subscription closes when the
|
|
19
|
+
// last `$`-reader detaches.
|
|
20
|
+
createQuerySubscription(
|
|
21
|
+
client,
|
|
22
|
+
functionRef,
|
|
23
|
+
args,
|
|
24
|
+
{
|
|
25
|
+
onData: (value) => {
|
|
26
|
+
set(value);
|
|
27
|
+
},
|
|
28
|
+
onError: options.onError
|
|
29
|
+
},
|
|
30
|
+
{ shardKey: options.shardKey }
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { query };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { evaluate } from '@lunora/ratelimit';
|
|
2
|
+
import { writable, derived } from 'svelte/store';
|
|
3
|
+
|
|
4
|
+
const rateLimit = (config, options = {}) => {
|
|
5
|
+
const now = options.now ?? Date.now;
|
|
6
|
+
const tickMs = options.tickMs ?? 1e3;
|
|
7
|
+
let value;
|
|
8
|
+
const epoch = writable(0);
|
|
9
|
+
const bump = () => {
|
|
10
|
+
epoch.update((n) => n + 1);
|
|
11
|
+
};
|
|
12
|
+
const status = derived(epoch, () => evaluate(config, value, { consume: false, count: 1, now: now(), reserve: false }).status);
|
|
13
|
+
let intervalHandle;
|
|
14
|
+
const stopInterval = () => {
|
|
15
|
+
if (intervalHandle !== void 0) {
|
|
16
|
+
clearInterval(intervalHandle);
|
|
17
|
+
intervalHandle = void 0;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
let latestOk = true;
|
|
21
|
+
const unsubStatus = status.subscribe((s) => {
|
|
22
|
+
latestOk = s.ok;
|
|
23
|
+
});
|
|
24
|
+
const startIntervalIfThrottled = () => {
|
|
25
|
+
if (latestOk || intervalHandle !== void 0) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
intervalHandle = setInterval(() => {
|
|
29
|
+
bump();
|
|
30
|
+
if (latestOk) {
|
|
31
|
+
stopInterval();
|
|
32
|
+
}
|
|
33
|
+
}, tickMs);
|
|
34
|
+
};
|
|
35
|
+
startIntervalIfThrottled();
|
|
36
|
+
const consume = (count = 1) => {
|
|
37
|
+
const result = evaluate(config, value, { consume: true, count, now: now(), reserve: false });
|
|
38
|
+
if (result.value !== void 0) {
|
|
39
|
+
value = result.value;
|
|
40
|
+
}
|
|
41
|
+
bump();
|
|
42
|
+
startIntervalIfThrottled();
|
|
43
|
+
return result.status;
|
|
44
|
+
};
|
|
45
|
+
const check = (count = 1) => evaluate(config, value, { consume: false, count, now: now(), reserve: false }).status.ok;
|
|
46
|
+
const reset = () => {
|
|
47
|
+
value = void 0;
|
|
48
|
+
stopInterval();
|
|
49
|
+
bump();
|
|
50
|
+
};
|
|
51
|
+
const teardown = () => {
|
|
52
|
+
stopInterval();
|
|
53
|
+
unsubStatus();
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
check,
|
|
57
|
+
consume,
|
|
58
|
+
disabled: derived(status, (s) => !s.ok),
|
|
59
|
+
ok: derived(status, (s) => s.ok),
|
|
60
|
+
reset,
|
|
61
|
+
retryAfter: derived(status, (s) => s.retryAfter),
|
|
62
|
+
teardown
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export { rateLimit };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { setContext, getContext } from 'svelte';
|
|
2
|
+
|
|
3
|
+
const LUNORA_CONTEXT_KEY = /* @__PURE__ */ Symbol("lunora.client");
|
|
4
|
+
const setLunoraClient = (client) => {
|
|
5
|
+
setContext(LUNORA_CONTEXT_KEY, client);
|
|
6
|
+
return client;
|
|
7
|
+
};
|
|
8
|
+
const getLunoraClient = () => {
|
|
9
|
+
const client = getContext(LUNORA_CONTEXT_KEY);
|
|
10
|
+
if (!client) {
|
|
11
|
+
throw new Error("getLunoraClient(): no LunoraClient in context — call setLunoraClient(client) in an ancestor component first.");
|
|
12
|
+
}
|
|
13
|
+
return client;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { getLunoraClient, setLunoraClient };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createQuerySubscription } from '@lunora/client/query';
|
|
2
|
+
import { writable, readable } from 'svelte/store';
|
|
3
|
+
import { getLunoraClient } from './setLunoraClient--bwSz7F6.mjs';
|
|
4
|
+
import { i as isFunctionReference } from './is-function-reference-ycLAcI79.mjs';
|
|
5
|
+
|
|
6
|
+
function subscription(clientOrFunction, functionOrArgs, argsOrOptions, maybeOptions) {
|
|
7
|
+
const hasExplicitClient = !isFunctionReference(clientOrFunction);
|
|
8
|
+
const client = hasExplicitClient ? clientOrFunction : getLunoraClient();
|
|
9
|
+
const functionRef = hasExplicitClient ? functionOrArgs : clientOrFunction;
|
|
10
|
+
const args = hasExplicitClient ? argsOrOptions : functionOrArgs;
|
|
11
|
+
const options = (hasExplicitClient ? maybeOptions : argsOrOptions) ?? {};
|
|
12
|
+
const { shardKey, onError } = options;
|
|
13
|
+
const errorStore = writable();
|
|
14
|
+
const data = readable(void 0, (set) => {
|
|
15
|
+
if (args === "skip") {
|
|
16
|
+
return () => void 0;
|
|
17
|
+
}
|
|
18
|
+
const unsubscribe = createQuerySubscription(
|
|
19
|
+
client,
|
|
20
|
+
functionRef,
|
|
21
|
+
args,
|
|
22
|
+
{
|
|
23
|
+
onData: (value) => {
|
|
24
|
+
set(value);
|
|
25
|
+
errorStore.set(void 0);
|
|
26
|
+
},
|
|
27
|
+
onError: (subscriptionError) => {
|
|
28
|
+
const error = new Error(subscriptionError.message);
|
|
29
|
+
errorStore.set(error);
|
|
30
|
+
onError?.(error);
|
|
31
|
+
},
|
|
32
|
+
onReset: () => {
|
|
33
|
+
set(void 0);
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{ shardKey }
|
|
37
|
+
);
|
|
38
|
+
return () => {
|
|
39
|
+
unsubscribe();
|
|
40
|
+
errorStore.set(void 0);
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
return { data, error: { subscribe: errorStore.subscribe } };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { subscription };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ArgsOf, type AuthLike, type FunctionReference, type HeadersSource, type Preloaded, type ReturnOf, type ServerClientOptions, type ServerSession, createServerClient, deserializePreloaded, getServerSession, preloadQuery, preloadedQueryResult, serializePreloaded } from '@lunora/client/ssr';
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ArgsOf, type AuthLike, type FunctionReference, type HeadersSource, type Preloaded, type ReturnOf, type ServerClientOptions, type ServerSession, createServerClient, deserializePreloaded, getServerSession, preloadQuery, preloadedQueryResult, serializePreloaded } from '@lunora/client/ssr';
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createServerClient, deserializePreloaded, getServerSession, preloadQuery, preloadedQueryResult, serializePreloaded } from '@lunora/client/ssr';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ExecutionContextLike, type FrameworkWorkerOptions as LunoraWorkerOptions, type FrameworkWorkerOptionsInput as LunoraWorkerOptionsInput, type FrameworkHostHandler as SvelteKitWorker, withFrameworkWorker as withLunora } from '@lunora/runtime';
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ExecutionContextLike, type FrameworkWorkerOptions as LunoraWorkerOptions, type FrameworkWorkerOptionsInput as LunoraWorkerOptionsInput, type FrameworkHostHandler as SvelteKitWorker, withFrameworkWorker as withLunora } from '@lunora/runtime';
|
package/dist/worker.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { withFrameworkWorker as withLunora } from '@lunora/runtime';
|
package/package.json
CHANGED
|
@@ -1,31 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/svelte",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"description": "Svelte adapter for Lunora — live stores, optimistic mutations, and reactive loaders",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cloudflare",
|
|
7
|
+
"durable-objects",
|
|
8
|
+
"lunora",
|
|
9
|
+
"optimistic-updates",
|
|
10
|
+
"realtime",
|
|
11
|
+
"stores",
|
|
12
|
+
"svelte",
|
|
13
|
+
"workers"
|
|
14
|
+
],
|
|
6
15
|
"homepage": "https://lunora.sh",
|
|
16
|
+
"bugs": "https://github.com/anolilab/lunora/issues",
|
|
17
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Daniel Bannert",
|
|
20
|
+
"email": "d.bannert@anolilab.de"
|
|
21
|
+
},
|
|
7
22
|
"repository": {
|
|
8
23
|
"type": "git",
|
|
9
24
|
"url": "git+https://github.com/anolilab/lunora.git",
|
|
10
25
|
"directory": "packages/svelte"
|
|
11
26
|
},
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"cloudflare",
|
|
18
|
-
"workers",
|
|
19
|
-
"durable-objects",
|
|
20
|
-
"svelte",
|
|
21
|
-
"stores",
|
|
22
|
-
"realtime",
|
|
23
|
-
"optimistic-updates"
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE.md",
|
|
31
|
+
"__assets__"
|
|
24
32
|
],
|
|
33
|
+
"type": "module",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"main": "./dist/index.mjs",
|
|
36
|
+
"module": "./dist/index.mjs",
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./server": {
|
|
44
|
+
"types": "./dist/server.d.ts",
|
|
45
|
+
"import": "./dist/server.mjs"
|
|
46
|
+
},
|
|
47
|
+
"./worker": {
|
|
48
|
+
"types": "./dist/worker.d.ts",
|
|
49
|
+
"import": "./dist/worker.mjs"
|
|
50
|
+
},
|
|
51
|
+
"./package.json": "./package.json"
|
|
52
|
+
},
|
|
25
53
|
"publishConfig": {
|
|
26
54
|
"access": "public"
|
|
27
55
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@lunora/client": "1.0.0-alpha.1",
|
|
58
|
+
"@lunora/ratelimit": "1.0.0-alpha.1",
|
|
59
|
+
"@lunora/runtime": "1.0.0-alpha.1"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"svelte": "^5.0.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependenciesMeta": {
|
|
65
|
+
"svelte": {
|
|
66
|
+
"optional": false
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": "^22.15.0 || >=24.11.0"
|
|
71
|
+
}
|
|
31
72
|
}
|