@lunora/solid 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/dist/index.mjs ADDED
@@ -0,0 +1,11 @@
1
+ export { LunoraContext, useLunora } from './packem_shared/LunoraContext-C9SpKj54.mjs';
2
+ export { AuthLoading, Authenticated, Unauthenticated, createAuth } from './packem_shared/Authenticated-RMT5Q_eE.mjs';
3
+ export { default as createConnectionStatus } from './packem_shared/createConnectionStatus-D8GPatZX.mjs';
4
+ export { createMutation, createMutationForClient } from './packem_shared/createMutationForClient-C7BxzO0y.mjs';
5
+ export { createInfiniteQuery, createPaginatedQuery } from './packem_shared/createInfiniteQuery-DyMvQ2Qy.mjs';
6
+ export { createPresence } from './packem_shared/createPresence-DiAak1Jw.mjs';
7
+ export { createQuery } from './packem_shared/createQuery-D8mdHfyQ.mjs';
8
+ export { createRateLimit } from './packem_shared/createRateLimit-BA2f8XyF.mjs';
9
+ export { createSubscription } from './packem_shared/createSubscription-BM2fw8hw.mjs';
10
+ export { default as hydratePreloaded } from './packem_shared/hydratePreloaded-CaT1kDBH.mjs';
11
+ export { LunoraProvider } from './packem_shared/LunoraProvider-B5BJFk3K.mjs';
@@ -0,0 +1,73 @@
1
+ import { createComponent, memo } from 'solid-js/web';
2
+ import { getIdentityStore } from '@lunora/client/auth';
3
+ import { Show, createSignal, onCleanup } from 'solid-js';
4
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
5
+
6
+ const createAuth = () => {
7
+ const client = useLunora();
8
+ const store = getIdentityStore(client);
9
+ const [token, setTokenSignal] = createSignal(client.getAuthToken());
10
+ const [user, setUserSignal] = createSignal(store.getUser());
11
+ const unsubToken = client.onAuthTokenChange((next) => {
12
+ setTokenSignal(() => next);
13
+ });
14
+ const unsubUser = store.subscribe(() => {
15
+ setUserSignal(() => store.getUser());
16
+ });
17
+ onCleanup(() => {
18
+ unsubToken();
19
+ unsubUser();
20
+ });
21
+ const setToken = (next) => {
22
+ client.setAuthToken(next);
23
+ };
24
+ return {
25
+ setToken,
26
+ token,
27
+ user
28
+ };
29
+ };
30
+ const Authenticated = (props) => {
31
+ const {
32
+ token,
33
+ user
34
+ } = createAuth();
35
+ return createComponent(Show, {
36
+ get when() {
37
+ return memo(() => token() === null)() ? false : user() !== null;
38
+ },
39
+ get children() {
40
+ return props.children;
41
+ }
42
+ });
43
+ };
44
+ const AuthLoading = (props) => {
45
+ const {
46
+ token,
47
+ user
48
+ } = createAuth();
49
+ return createComponent(Show, {
50
+ get when() {
51
+ return memo(() => token() === null)() ? false : user() === null;
52
+ },
53
+ get children() {
54
+ return props.children;
55
+ }
56
+ });
57
+ };
58
+ const Unauthenticated = (props) => {
59
+ const {
60
+ token,
61
+ user
62
+ } = createAuth();
63
+ return createComponent(Show, {
64
+ get when() {
65
+ return memo(() => token() === null)() ? user() === null : false;
66
+ },
67
+ get children() {
68
+ return props.children;
69
+ }
70
+ });
71
+ };
72
+
73
+ export { AuthLoading, Authenticated, Unauthenticated, createAuth };
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from 'solid-js';
2
+
3
+ const LunoraContext = createContext();
4
+ const useLunora = () => {
5
+ const client = useContext(LunoraContext);
6
+ if (!client) {
7
+ throw new Error("useLunora must be used inside <LunoraProvider />");
8
+ }
9
+ return client;
10
+ };
11
+
12
+ export { LunoraContext, useLunora };
@@ -0,0 +1,17 @@
1
+ import { createComponent } from 'solid-js/web';
2
+ import { LunoraContext } from './LunoraContext-C9SpKj54.mjs';
3
+
4
+ const LunoraProvider = (props) => (
5
+ // `props.client` is read lazily inside the JSX so Solid tracks it: swapping
6
+ // the client prop re-provides the new value to descendants.
7
+ createComponent(LunoraContext.Provider, {
8
+ get value() {
9
+ return props.client;
10
+ },
11
+ get children() {
12
+ return props.children;
13
+ }
14
+ })
15
+ );
16
+
17
+ export { LunoraProvider };
@@ -0,0 +1,14 @@
1
+ import { createSignal, onCleanup } from 'solid-js';
2
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
3
+
4
+ const createConnectionStatus = () => {
5
+ const client = useLunora();
6
+ const [status, setStatus] = createSignal(client.connectionStatus());
7
+ const unsubscribe = client.onConnectionStatus((next) => {
8
+ setStatus(next);
9
+ });
10
+ onCleanup(unsubscribe);
11
+ return status;
12
+ };
13
+
14
+ export { createConnectionStatus as default };
@@ -0,0 +1,196 @@
1
+ import { initialPages, derivePaginationStatus, rebalance, applyLoadMore } from '@lunora/client/pagination';
2
+ import { createMemo, createSignal, createEffect, on, onCleanup } from 'solid-js';
3
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
4
+
5
+ const buildPageArgs = (page, baseArgs) => {
6
+ return {
7
+ ...baseArgs,
8
+ paginationOpts: {
9
+ cursor: page.lower,
10
+ endCursor: page.upper,
11
+ numItems: page.numItems
12
+ }
13
+ };
14
+ };
15
+ const buildPageKey = (functionPath, pageArgs) => `${functionPath}::${JSON.stringify(pageArgs)}`;
16
+ const createPaginatedCore = (function_, args, options) => {
17
+ const client = useLunora();
18
+ const {
19
+ initialNumItems,
20
+ shardKey
21
+ } = options;
22
+ const resolveArgs = () => typeof args === "function" ? args() : args;
23
+ const [pages, setPages] = createSignal(initialPages(initialNumItems));
24
+ const resultsByKey = /* @__PURE__ */ new Map();
25
+ const [pageResults, setPageResults] = createSignal([]);
26
+ const activeSubs = /* @__PURE__ */ new Map();
27
+ const pendingPageKeys = /* @__PURE__ */ new Set();
28
+ const rebuildPageResults = (currentPages, baseArgs) => {
29
+ const updated = currentPages.map((page) => {
30
+ const key = buildPageKey(function_["__lunoraRef"], buildPageArgs(page, baseArgs));
31
+ return resultsByKey.get(key);
32
+ });
33
+ setPageResults(updated);
34
+ };
35
+ const syncSubscriptions = (currentPages, baseArgs) => {
36
+ const wantedKeys = /* @__PURE__ */ new Set();
37
+ for (const page of currentPages) {
38
+ wantedKeys.add(buildPageKey(function_["__lunoraRef"], buildPageArgs(page, baseArgs)));
39
+ }
40
+ for (const [key, unsub] of activeSubs) {
41
+ if (!wantedKeys.has(key)) {
42
+ unsub();
43
+ activeSubs.delete(key);
44
+ pendingPageKeys.delete(key);
45
+ }
46
+ }
47
+ for (const page of currentPages) {
48
+ const pageArgs = buildPageArgs(page, baseArgs);
49
+ const key = buildPageKey(function_["__lunoraRef"], pageArgs);
50
+ if (activeSubs.has(key)) {
51
+ continue;
52
+ }
53
+ pendingPageKeys.add(key);
54
+ const unsub = client.subscribe(function_, pageArgs, (value) => {
55
+ resultsByKey.set(key, value);
56
+ pendingPageKeys.delete(key);
57
+ const currentArgs = resolveArgs();
58
+ if (currentArgs === "skip") {
59
+ return;
60
+ }
61
+ rebuildPageResults(pages(), currentArgs);
62
+ if (pendingPageKeys.size === 0) {
63
+ const next = rebalance(pages(), pageResults());
64
+ if (next) {
65
+ setPages(next);
66
+ }
67
+ }
68
+ }, {
69
+ shardKey
70
+ });
71
+ activeSubs.set(key, unsub);
72
+ }
73
+ };
74
+ const teardownAll = () => {
75
+ for (const unsub of activeSubs.values()) {
76
+ unsub();
77
+ }
78
+ activeSubs.clear();
79
+ resultsByKey.clear();
80
+ pendingPageKeys.clear();
81
+ };
82
+ createEffect(on(resolveArgs, (current) => {
83
+ teardownAll();
84
+ setPages(initialPages(initialNumItems));
85
+ setPageResults([]);
86
+ if (current !== "skip") {
87
+ syncSubscriptions(pages(), current);
88
+ rebuildPageResults(pages(), current);
89
+ }
90
+ onCleanup(teardownAll);
91
+ }));
92
+ createEffect(on(pages, (currentPages) => {
93
+ const current = resolveArgs();
94
+ if (current !== "skip") {
95
+ syncSubscriptions(currentPages, current);
96
+ rebuildPageResults(currentPages, current);
97
+ }
98
+ }));
99
+ const status = createMemo(() => {
100
+ const skipped = resolveArgs() === "skip";
101
+ return derivePaginationStatus(skipped, pageResults()).status;
102
+ });
103
+ const loadMore = (numberItems) => {
104
+ const current = resolveArgs();
105
+ if (current === "skip") {
106
+ return;
107
+ }
108
+ const {
109
+ nextCursor,
110
+ status: currentStatus
111
+ } = derivePaginationStatus(false, pageResults());
112
+ if (currentStatus !== "CanLoadMore") {
113
+ return;
114
+ }
115
+ const next = applyLoadMore(pages(), nextCursor, numberItems);
116
+ if (!next) {
117
+ return;
118
+ }
119
+ const oldTail = pages().at(-1);
120
+ const newPinnedPage = next.at(-2);
121
+ if (oldTail && newPinnedPage) {
122
+ const oldKey = buildPageKey(function_["__lunoraRef"], buildPageArgs(oldTail, current));
123
+ const newKey = buildPageKey(function_["__lunoraRef"], buildPageArgs(newPinnedPage, current));
124
+ if (oldKey !== newKey) {
125
+ const carried = resultsByKey.get(oldKey);
126
+ if (carried) {
127
+ resultsByKey.set(newKey, carried);
128
+ }
129
+ }
130
+ }
131
+ setPages(next);
132
+ };
133
+ return {
134
+ loadMore,
135
+ pageResults,
136
+ status
137
+ };
138
+ };
139
+ const createPaginatedQuery = (function_, args, options) => {
140
+ const {
141
+ loadMore,
142
+ pageResults,
143
+ status
144
+ } = createPaginatedCore(function_, args, options);
145
+ const results = createMemo(() => {
146
+ const items = [];
147
+ for (const page of pageResults()) {
148
+ if (page) {
149
+ items.push(...page.page);
150
+ }
151
+ }
152
+ return items;
153
+ });
154
+ const isLoading = createMemo(() => status() === "LoadingFirstPage" || status() === "LoadingMore");
155
+ return {
156
+ isLoading,
157
+ loadMore,
158
+ results,
159
+ status
160
+ };
161
+ };
162
+ const createInfiniteQuery = (function_, args, options) => {
163
+ const {
164
+ initialNumItems
165
+ } = options;
166
+ const {
167
+ loadMore,
168
+ pageResults,
169
+ status
170
+ } = createPaginatedCore(function_, args, options);
171
+ const pages = createMemo(() => {
172
+ const result = [];
173
+ for (const page of pageResults()) {
174
+ if (page) {
175
+ result.push(page.page);
176
+ }
177
+ }
178
+ return result;
179
+ });
180
+ const isLoading = createMemo(() => status() === "LoadingFirstPage");
181
+ const hasNextPage = createMemo(() => status() === "CanLoadMore");
182
+ const isFetchingNextPage = createMemo(() => status() === "LoadingMore");
183
+ const fetchNextPage = (numberItems) => {
184
+ loadMore(numberItems ?? initialNumItems);
185
+ };
186
+ return {
187
+ fetchNextPage,
188
+ hasNextPage,
189
+ isFetchingNextPage,
190
+ isLoading,
191
+ pages,
192
+ status
193
+ };
194
+ };
195
+
196
+ export { createInfiniteQuery, createPaginatedQuery };
@@ -0,0 +1,36 @@
1
+ import { createMutationRunner } from '@lunora/client';
2
+ import { createSignal } from 'solid-js';
3
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
4
+
5
+ const createMutationForClient = (client, function_) => {
6
+ const [data, setData] = createSignal(void 0);
7
+ const [error, setError] = createSignal(void 0);
8
+ const [pending, setPending] = createSignal(false);
9
+ const mutate = createMutationRunner(client, function_, {
10
+ setError,
11
+ setPending,
12
+ setResult: (result) => {
13
+ setData(() => result);
14
+ setError(void 0);
15
+ }
16
+ });
17
+ const reset = () => {
18
+ setData(() => void 0);
19
+ setError(void 0);
20
+ };
21
+ return {
22
+ data,
23
+ error,
24
+ mutate,
25
+ pending,
26
+ reset
27
+ };
28
+ };
29
+ const createMutation = (function_) => {
30
+ const client = useLunora();
31
+ return createMutationForClient({
32
+ mutation: (reference, args, options) => client.mutation(reference, args, options)
33
+ }, function_);
34
+ };
35
+
36
+ export { createMutation, createMutationForClient };
@@ -0,0 +1,78 @@
1
+ import { createSignal, onMount, onCleanup } from 'solid-js';
2
+ import { useLunora } from './LunoraContext-C9SpKj54.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 createPresence = (roomId, options) => {
12
+ const client = useLunora();
13
+ const {
14
+ heartbeat,
15
+ intervalMs = DEFAULT_INTERVAL_MS,
16
+ listPresent,
17
+ shardKey
18
+ } = options;
19
+ const sessionId = options.sessionId ?? makeSessionId();
20
+ const [present, setPresent] = createSignal(void 0);
21
+ let latestData = options.data;
22
+ const sendHeartbeat = () => {
23
+ const args = {
24
+ roomId,
25
+ sessionId,
26
+ ...latestData === void 0 ? {} : {
27
+ data: latestData
28
+ }
29
+ };
30
+ client.mutation(heartbeat, args, {
31
+ shardKey
32
+ }).catch(() => void 0);
33
+ };
34
+ const setData = (next) => {
35
+ latestData = next;
36
+ sendHeartbeat();
37
+ };
38
+ onMount(() => {
39
+ sendHeartbeat();
40
+ const intervalHandle = setInterval(sendHeartbeat, intervalMs);
41
+ const onVisible = () => {
42
+ if (typeof document !== "undefined" && document.visibilityState === "visible") {
43
+ sendHeartbeat();
44
+ }
45
+ };
46
+ if (typeof document !== "undefined") {
47
+ document.addEventListener("visibilitychange", onVisible);
48
+ }
49
+ const releaseConnectionContext = client.acquireConnectionContext({
50
+ roomId,
51
+ sessionId
52
+ }, {
53
+ shardKey
54
+ });
55
+ const unsubscribe = client.subscribe(listPresent, {
56
+ roomId
57
+ }, (value) => {
58
+ setPresent(() => value);
59
+ }, {
60
+ shardKey
61
+ });
62
+ onCleanup(() => {
63
+ clearInterval(intervalHandle);
64
+ if (typeof document !== "undefined") {
65
+ document.removeEventListener("visibilitychange", onVisible);
66
+ }
67
+ releaseConnectionContext();
68
+ unsubscribe();
69
+ });
70
+ });
71
+ return {
72
+ present,
73
+ sessionId,
74
+ setData
75
+ };
76
+ };
77
+
78
+ export { createPresence };
@@ -0,0 +1,28 @@
1
+ import { createQuerySubscription } from '@lunora/client/query';
2
+ import { createSignal, createEffect, on, onCleanup } from 'solid-js';
3
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
4
+
5
+ const createQuery = (function_, args, options = {}) => {
6
+ const client = useLunora();
7
+ const {
8
+ shardKey
9
+ } = options;
10
+ const [value, setValue] = createSignal(void 0);
11
+ const resolveArgs = () => typeof args === "function" ? args() : args;
12
+ createEffect(on(resolveArgs, (current) => {
13
+ const unsubscribe = createQuerySubscription(client, function_, current, {
14
+ onData: (next) => {
15
+ setValue(() => next);
16
+ },
17
+ onReset: () => {
18
+ setValue(() => void 0);
19
+ }
20
+ }, {
21
+ shardKey
22
+ });
23
+ onCleanup(unsubscribe);
24
+ }));
25
+ return value;
26
+ };
27
+
28
+ export { createQuery };
@@ -0,0 +1,76 @@
1
+ import { evaluate } from '@lunora/ratelimit';
2
+ import { createSignal, createMemo, onCleanup } from 'solid-js';
3
+
4
+ const createRateLimit = (config, options = {}) => {
5
+ const now = options.now ?? Date.now;
6
+ const tickMs = options.tickMs ?? 1e3;
7
+ let value;
8
+ const [epoch, setEpoch] = createSignal(0);
9
+ const bump = () => {
10
+ setEpoch((n) => n + 1);
11
+ };
12
+ const status = createMemo(() => {
13
+ const ts = now() + epoch() * 0;
14
+ return evaluate(config, value, {
15
+ consume: false,
16
+ count: 1,
17
+ now: ts,
18
+ reserve: false
19
+ }).status;
20
+ });
21
+ let intervalHandle;
22
+ const stopInterval = () => {
23
+ if (intervalHandle !== void 0) {
24
+ clearInterval(intervalHandle);
25
+ intervalHandle = void 0;
26
+ }
27
+ };
28
+ const startIntervalIfThrottled = () => {
29
+ if (status().ok || intervalHandle !== void 0) {
30
+ return;
31
+ }
32
+ intervalHandle = setInterval(() => {
33
+ bump();
34
+ if (status().ok) {
35
+ stopInterval();
36
+ }
37
+ }, tickMs);
38
+ };
39
+ onCleanup(stopInterval);
40
+ startIntervalIfThrottled();
41
+ const consume = (count = 1) => {
42
+ const result = evaluate(config, value, {
43
+ consume: true,
44
+ count,
45
+ now: now(),
46
+ reserve: false
47
+ });
48
+ if (result.value !== void 0) {
49
+ value = result.value;
50
+ }
51
+ bump();
52
+ startIntervalIfThrottled();
53
+ return result.status;
54
+ };
55
+ const check = (count = 1) => evaluate(config, value, {
56
+ consume: false,
57
+ count,
58
+ now: now(),
59
+ reserve: false
60
+ }).status.ok;
61
+ const reset = () => {
62
+ value = void 0;
63
+ stopInterval();
64
+ bump();
65
+ };
66
+ return {
67
+ check,
68
+ consume,
69
+ disabled: () => !status().ok,
70
+ ok: () => status().ok,
71
+ reset,
72
+ retryAfter: () => status().retryAfter
73
+ };
74
+ };
75
+
76
+ export { createRateLimit };
@@ -0,0 +1,39 @@
1
+ import { createQuerySubscription } from '@lunora/client/query';
2
+ import { createSignal, createEffect, on, onCleanup } from 'solid-js';
3
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
4
+
5
+ const createSubscription = (function_, args, options = {}) => {
6
+ const client = useLunora();
7
+ const [data, setData] = createSignal(void 0);
8
+ const [error, setError] = createSignal(void 0);
9
+ const resolveArgs = typeof args === "function" ? args : () => args;
10
+ createEffect(on(resolveArgs, (currentArgs) => {
11
+ if (currentArgs === "skip") {
12
+ setData(() => void 0);
13
+ setError(() => void 0);
14
+ return;
15
+ }
16
+ const unsubscribe = createQuerySubscription(client, function_, currentArgs, {
17
+ onData: (value) => {
18
+ setData(() => value);
19
+ setError(() => void 0);
20
+ },
21
+ onError: (subscriptionError) => {
22
+ setError(() => new Error(subscriptionError.message));
23
+ setData(() => void 0);
24
+ },
25
+ onReset: () => {
26
+ setData(() => void 0);
27
+ }
28
+ }, {
29
+ shardKey: options.shardKey
30
+ });
31
+ onCleanup(unsubscribe);
32
+ }));
33
+ return {
34
+ data,
35
+ error
36
+ };
37
+ };
38
+
39
+ export { createSubscription };
@@ -0,0 +1,25 @@
1
+ import { createSignal, createEffect, onCleanup } from 'solid-js';
2
+ import { useLunora } from './LunoraContext-C9SpKj54.mjs';
3
+
4
+ const hydratePreloaded = (preloaded) => {
5
+ const client = useLunora();
6
+ const {
7
+ args,
8
+ functionPath,
9
+ shardKey,
10
+ value
11
+ } = preloaded;
12
+ const [data, setData] = createSignal(value);
13
+ const functionRef = {
14
+ __lunoraRef: functionPath
15
+ };
16
+ createEffect(() => {
17
+ const unsubscribe = client.subscribe(functionRef, args, (next) => setData(() => next), {
18
+ shardKey
19
+ });
20
+ onCleanup(unsubscribe);
21
+ });
22
+ return data;
23
+ };
24
+
25
+ export { hydratePreloaded as default };
@@ -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';
@@ -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';
@@ -0,0 +1 @@
1
+ export { createServerClient, deserializePreloaded, getServerSession, preloadQuery, preloadedQueryResult, serializePreloaded } from '@lunora/client/ssr';