houdini-react 1.2.0-react.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 +21 -0
- package/README.md +36 -0
- package/build/next/index.d.ts +2 -0
- package/build/next-cjs/index.js +50 -0
- package/build/next-cjs/package.json +1 -0
- package/build/next-esm/index.js +26 -0
- package/build/next-esm/package.json +1 -0
- package/build/plugin/config.d.ts +3 -0
- package/build/plugin/extract.d.ts +6 -0
- package/build/plugin/index.d.ts +3 -0
- package/build/plugin/transform.d.ts +6 -0
- package/build/plugin-cjs/index.js +78313 -0
- package/build/plugin-cjs/package.json +1 -0
- package/build/plugin-esm/index.js +78307 -0
- package/build/plugin-esm/package.json +1 -0
- package/build/runtime/context.d.ts +7 -0
- package/build/runtime/hooks/index.d.ts +8 -0
- package/build/runtime/hooks/useDeepCompareEffect.d.ts +35 -0
- package/build/runtime/hooks/useDocumentHandle.d.ts +34 -0
- package/build/runtime/hooks/useDocumentStore.d.ts +11 -0
- package/build/runtime/hooks/useDocumentSubscription.d.ts +14 -0
- package/build/runtime/hooks/useFragment.d.ts +15 -0
- package/build/runtime/hooks/useFragmentHandle.d.ts +9 -0
- package/build/runtime/hooks/useHoudiniClient.d.ts +2 -0
- package/build/runtime/hooks/useIsMounted.d.ts +3 -0
- package/build/runtime/hooks/useMutation.d.ts +10 -0
- package/build/runtime/hooks/useQuery.d.ts +5 -0
- package/build/runtime/hooks/useQueryHandle.d.ts +10 -0
- package/build/runtime/hooks/useSubscription.d.ts +4 -0
- package/build/runtime/hooks/useSubscriptionHandle.d.ts +25 -0
- package/build/runtime/index.d.ts +2 -0
- package/build/runtime/lib/cache.d.ts +62 -0
- package/build/runtime-cjs/context.d.ts +7 -0
- package/build/runtime-cjs/context.js +41 -0
- package/build/runtime-cjs/hooks/index.d.ts +8 -0
- package/build/runtime-cjs/hooks/index.js +45 -0
- package/build/runtime-cjs/hooks/useDeepCompareEffect.d.ts +35 -0
- package/build/runtime-cjs/hooks/useDeepCompareEffect.js +72 -0
- package/build/runtime-cjs/hooks/useDocumentHandle.d.ts +34 -0
- package/build/runtime-cjs/hooks/useDocumentHandle.js +127 -0
- package/build/runtime-cjs/hooks/useDocumentStore.d.ts +11 -0
- package/build/runtime-cjs/hooks/useDocumentStore.js +63 -0
- package/build/runtime-cjs/hooks/useDocumentSubscription.d.ts +14 -0
- package/build/runtime-cjs/hooks/useDocumentSubscription.js +65 -0
- package/build/runtime-cjs/hooks/useFragment.d.ts +15 -0
- package/build/runtime-cjs/hooks/useFragment.js +93 -0
- package/build/runtime-cjs/hooks/useFragmentHandle.d.ts +9 -0
- package/build/runtime-cjs/hooks/useFragmentHandle.js +47 -0
- package/build/runtime-cjs/hooks/useHoudiniClient.d.ts +2 -0
- package/build/runtime-cjs/hooks/useHoudiniClient.js +42 -0
- package/build/runtime-cjs/hooks/useIsMounted.d.ts +3 -0
- package/build/runtime-cjs/hooks/useIsMounted.js +38 -0
- package/build/runtime-cjs/hooks/useMutation.d.ts +10 -0
- package/build/runtime-cjs/hooks/useMutation.js +48 -0
- package/build/runtime-cjs/hooks/useQuery.d.ts +5 -0
- package/build/runtime-cjs/hooks/useQuery.js +32 -0
- package/build/runtime-cjs/hooks/useQueryHandle.d.ts +10 -0
- package/build/runtime-cjs/hooks/useQueryHandle.js +125 -0
- package/build/runtime-cjs/hooks/useSubscription.d.ts +4 -0
- package/build/runtime-cjs/hooks/useSubscription.js +32 -0
- package/build/runtime-cjs/hooks/useSubscriptionHandle.d.ts +25 -0
- package/build/runtime-cjs/hooks/useSubscriptionHandle.js +42 -0
- package/build/runtime-cjs/index.d.ts +2 -0
- package/build/runtime-cjs/index.js +30 -0
- package/build/runtime-cjs/lib/cache.d.ts +62 -0
- package/build/runtime-cjs/lib/cache.js +71 -0
- package/build/runtime-cjs/package.json +1 -0
- package/build/runtime-esm/context.d.ts +7 -0
- package/build/runtime-esm/context.js +10 -0
- package/build/runtime-esm/hooks/index.d.ts +8 -0
- package/build/runtime-esm/hooks/index.js +16 -0
- package/build/runtime-esm/hooks/useDeepCompareEffect.d.ts +35 -0
- package/build/runtime-esm/hooks/useDeepCompareEffect.js +41 -0
- package/build/runtime-esm/hooks/useDocumentHandle.d.ts +34 -0
- package/build/runtime-esm/hooks/useDocumentHandle.js +99 -0
- package/build/runtime-esm/hooks/useDocumentStore.d.ts +11 -0
- package/build/runtime-esm/hooks/useDocumentStore.js +33 -0
- package/build/runtime-esm/hooks/useDocumentSubscription.d.ts +14 -0
- package/build/runtime-esm/hooks/useDocumentSubscription.js +35 -0
- package/build/runtime-esm/hooks/useFragment.d.ts +15 -0
- package/build/runtime-esm/hooks/useFragment.js +62 -0
- package/build/runtime-esm/hooks/useFragmentHandle.d.ts +9 -0
- package/build/runtime-esm/hooks/useFragmentHandle.js +23 -0
- package/build/runtime-esm/hooks/useHoudiniClient.d.ts +2 -0
- package/build/runtime-esm/hooks/useHoudiniClient.js +12 -0
- package/build/runtime-esm/hooks/useIsMounted.d.ts +3 -0
- package/build/runtime-esm/hooks/useIsMounted.js +14 -0
- package/build/runtime-esm/hooks/useMutation.d.ts +10 -0
- package/build/runtime-esm/hooks/useMutation.js +24 -0
- package/build/runtime-esm/hooks/useQuery.d.ts +5 -0
- package/build/runtime-esm/hooks/useQuery.js +8 -0
- package/build/runtime-esm/hooks/useQueryHandle.d.ts +10 -0
- package/build/runtime-esm/hooks/useQueryHandle.js +95 -0
- package/build/runtime-esm/hooks/useSubscription.d.ts +4 -0
- package/build/runtime-esm/hooks/useSubscription.js +8 -0
- package/build/runtime-esm/hooks/useSubscriptionHandle.d.ts +25 -0
- package/build/runtime-esm/hooks/useSubscriptionHandle.js +18 -0
- package/build/runtime-esm/index.d.ts +2 -0
- package/build/runtime-esm/index.js +5 -0
- package/build/runtime-esm/lib/cache.d.ts +62 -0
- package/build/runtime-esm/lib/cache.js +47 -0
- package/build/runtime-esm/package.json +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useHoudiniClient } from "./useHoudiniClient";
|
|
3
|
+
import { useIsMountedRef } from "./useIsMounted";
|
|
4
|
+
function useDocumentStore({
|
|
5
|
+
artifact,
|
|
6
|
+
...observeParams
|
|
7
|
+
}) {
|
|
8
|
+
const client = useHoudiniClient();
|
|
9
|
+
const isMountedRef = useIsMountedRef();
|
|
10
|
+
let [observer, setObserver] = React.useState(
|
|
11
|
+
() => client.observe({
|
|
12
|
+
artifact,
|
|
13
|
+
...observeParams
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
const box = React.useRef(observer.state);
|
|
17
|
+
const subscribe = React.useCallback(
|
|
18
|
+
(fn) => {
|
|
19
|
+
return observer.subscribe((val) => {
|
|
20
|
+
box.current = val;
|
|
21
|
+
if (isMountedRef.current) {
|
|
22
|
+
fn();
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
[observer]
|
|
27
|
+
);
|
|
28
|
+
const storeValue = React.useSyncExternalStore(subscribe, () => box.current);
|
|
29
|
+
return [storeValue, observer, setObserver];
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
useDocumentStore
|
|
33
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DocumentArtifact, QueryResult } from '$houdini/lib/types';
|
|
2
|
+
import type { DocumentStore, SendParams } from '$houdini/runtime/client';
|
|
3
|
+
import { GraphQLObject } from 'houdini';
|
|
4
|
+
import { type UseDocumentStoreParams } from './useDocumentStore';
|
|
5
|
+
export declare function useDocumentSubscription<_Artifact extends DocumentArtifact = DocumentArtifact, _Data extends GraphQLObject = GraphQLObject, _Input extends {} = {}>({ artifact, variables, send, ...observeParams }: UseDocumentStoreParams<_Artifact, _Data> & {
|
|
6
|
+
variables: _Input;
|
|
7
|
+
send?: Partial<SendParams>;
|
|
8
|
+
}): [
|
|
9
|
+
QueryResult<_Data, _Input> & {
|
|
10
|
+
parent?: string | null;
|
|
11
|
+
},
|
|
12
|
+
DocumentStore<_Data, _Input>,
|
|
13
|
+
(store: DocumentStore<_Data, _Input>) => void
|
|
14
|
+
];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import useDeepCompareEffect from "./useDeepCompareEffect";
|
|
2
|
+
import { useDocumentStore } from "./useDocumentStore";
|
|
3
|
+
function useDocumentSubscription({
|
|
4
|
+
artifact,
|
|
5
|
+
variables,
|
|
6
|
+
send,
|
|
7
|
+
...observeParams
|
|
8
|
+
}) {
|
|
9
|
+
const [storeValue, observer, setObserver] = useDocumentStore({
|
|
10
|
+
artifact,
|
|
11
|
+
...observeParams
|
|
12
|
+
});
|
|
13
|
+
useDeepCompareEffect(() => {
|
|
14
|
+
observer.send({
|
|
15
|
+
variables,
|
|
16
|
+
session: {},
|
|
17
|
+
metadata: {},
|
|
18
|
+
...send
|
|
19
|
+
});
|
|
20
|
+
return () => {
|
|
21
|
+
observer.cleanup();
|
|
22
|
+
};
|
|
23
|
+
}, [observer, variables ?? {}, send ?? {}]);
|
|
24
|
+
return [
|
|
25
|
+
{
|
|
26
|
+
parent: send?.stuff?.parentID,
|
|
27
|
+
...storeValue
|
|
28
|
+
},
|
|
29
|
+
observer,
|
|
30
|
+
setObserver
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
useDocumentSubscription
|
|
35
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { fragmentKey } from '$houdini/runtime/lib/types';
|
|
2
|
+
import type { GraphQLObject, FragmentArtifact } from '$houdini/runtime/lib/types';
|
|
3
|
+
export declare function useFragment<_Data extends GraphQLObject, _ReferenceType extends {}, _Input extends {} = {}>(reference: _Data | {
|
|
4
|
+
[fragmentKey]: _ReferenceType;
|
|
5
|
+
} | null, document: {
|
|
6
|
+
artifact: FragmentArtifact;
|
|
7
|
+
}): _Data | null;
|
|
8
|
+
export declare function fragmentReference<_Data extends GraphQLObject, _Input, _ReferenceType extends {}>(reference: _Data | {
|
|
9
|
+
[fragmentKey]: _ReferenceType;
|
|
10
|
+
} | null, document: {
|
|
11
|
+
artifact: FragmentArtifact;
|
|
12
|
+
}): {
|
|
13
|
+
variables: _Input;
|
|
14
|
+
parent: string;
|
|
15
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import cache from "$houdini/runtime/cache";
|
|
2
|
+
import { deepEquals } from "$houdini/runtime/lib/deepEquals";
|
|
3
|
+
import { fragmentKey } from "$houdini/runtime/lib/types";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { useDeepCompareMemoize } from "./useDeepCompareEffect";
|
|
6
|
+
import { useDocumentSubscription } from "./useDocumentSubscription";
|
|
7
|
+
function useFragment(reference, document) {
|
|
8
|
+
const { parent, variables } = fragmentReference(
|
|
9
|
+
reference,
|
|
10
|
+
document
|
|
11
|
+
);
|
|
12
|
+
let cachedValue = reference;
|
|
13
|
+
if (reference && parent) {
|
|
14
|
+
cachedValue = cache.read({
|
|
15
|
+
selection: document.artifact.selection,
|
|
16
|
+
parent,
|
|
17
|
+
variables
|
|
18
|
+
}).data;
|
|
19
|
+
}
|
|
20
|
+
const observeParams = {
|
|
21
|
+
artifact: document.artifact,
|
|
22
|
+
variables,
|
|
23
|
+
initialValue: cachedValue,
|
|
24
|
+
send: {
|
|
25
|
+
stuff: {
|
|
26
|
+
parentID: parent
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const [storeValue] = useDocumentSubscription(observeParams);
|
|
31
|
+
const lastReference = React.useRef(null);
|
|
32
|
+
return React.useMemo(() => {
|
|
33
|
+
const parentChange = storeValue.parent !== parent || !deepEquals({ parent, variables }, lastReference.current);
|
|
34
|
+
if (parentChange) {
|
|
35
|
+
lastReference.current = { parent, variables: { ...variables } };
|
|
36
|
+
return cachedValue;
|
|
37
|
+
}
|
|
38
|
+
return storeValue.data;
|
|
39
|
+
}, [
|
|
40
|
+
useDeepCompareMemoize({
|
|
41
|
+
parent,
|
|
42
|
+
variables,
|
|
43
|
+
cachedValue,
|
|
44
|
+
storeValue: storeValue.data,
|
|
45
|
+
storeParent: storeValue.parent
|
|
46
|
+
})
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
function fragmentReference(reference, document) {
|
|
50
|
+
const { variables, parent } = reference?.[fragmentKey]?.[document.artifact.name] ?? {};
|
|
51
|
+
if (reference && fragmentKey in reference && (!variables || !parent)) {
|
|
52
|
+
console.warn(
|
|
53
|
+
`\u26A0\uFE0F Parent does not contain the information for this fragment. Something is wrong.
|
|
54
|
+
Please ensure that you have passed a record that has ${document.artifact.name} mixed into it.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return { variables, parent };
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
fragmentReference,
|
|
61
|
+
useFragment
|
|
62
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GraphQLObject, FragmentArtifact, QueryArtifact } from '$houdini/runtime/lib/types';
|
|
2
|
+
import { fragmentKey } from '$houdini/runtime/lib/types';
|
|
3
|
+
import { type DocumentHandle } from './useDocumentHandle';
|
|
4
|
+
export declare function useFragmentHandle<_Artifact extends FragmentArtifact, _Data extends GraphQLObject, _ReferenceType extends {}, _PaginationArtifact extends QueryArtifact, _Input extends {} = {}>(reference: _Data | {
|
|
5
|
+
[fragmentKey]: _ReferenceType;
|
|
6
|
+
} | null, document: {
|
|
7
|
+
artifact: FragmentArtifact;
|
|
8
|
+
refetchArtifact?: QueryArtifact;
|
|
9
|
+
}): DocumentHandle<_PaginationArtifact, _Data, _Input>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useDocumentHandle } from "./useDocumentHandle";
|
|
2
|
+
import { useDocumentStore } from "./useDocumentStore";
|
|
3
|
+
import { fragmentReference, useFragment } from "./useFragment";
|
|
4
|
+
function useFragmentHandle(reference, document) {
|
|
5
|
+
const data = useFragment(reference, document);
|
|
6
|
+
const { variables } = fragmentReference(reference, document);
|
|
7
|
+
const [handleValue, handleObserver] = useDocumentStore({
|
|
8
|
+
artifact: document.refetchArtifact ?? document.artifact
|
|
9
|
+
});
|
|
10
|
+
const handle = useDocumentHandle({
|
|
11
|
+
observer: handleObserver,
|
|
12
|
+
storeValue: handleValue,
|
|
13
|
+
artifact: document.refetchArtifact ?? document.artifact
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
...handle,
|
|
17
|
+
variables,
|
|
18
|
+
data
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
useFragmentHandle
|
|
23
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { HoudiniContext } from "../context";
|
|
3
|
+
function useHoudiniClient() {
|
|
4
|
+
const client = React.useContext(HoudiniContext);
|
|
5
|
+
if (!client) {
|
|
6
|
+
throw new Error("Could not find client");
|
|
7
|
+
}
|
|
8
|
+
return client;
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
useHoudiniClient
|
|
12
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useRef, useEffect } from "react";
|
|
2
|
+
function useIsMountedRef() {
|
|
3
|
+
const isMountedRef = useRef(true);
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
isMountedRef.current = true;
|
|
6
|
+
return () => {
|
|
7
|
+
isMountedRef.current = false;
|
|
8
|
+
};
|
|
9
|
+
}, []);
|
|
10
|
+
return isMountedRef;
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
useIsMountedRef
|
|
14
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MutationArtifact, GraphQLObject, QueryResult } from '$houdini/runtime/lib/types';
|
|
2
|
+
export type MutationHandler<_Result, _Input, _Optimistic extends GraphQLObject> = (args: {
|
|
3
|
+
variables: _Input;
|
|
4
|
+
metadata?: App.Metadata;
|
|
5
|
+
fetch?: typeof globalThis.fetch;
|
|
6
|
+
optimisticResponse?: _Optimistic;
|
|
7
|
+
}) => Promise<QueryResult<_Result, _Input>>;
|
|
8
|
+
export declare function useMutation<_Result extends GraphQLObject, _Input extends {}, _Optimistic extends GraphQLObject>({ artifact, }: {
|
|
9
|
+
artifact: MutationArtifact;
|
|
10
|
+
}): [boolean, MutationHandler<_Result, _Input, _Optimistic>];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useDocumentStore } from "./useDocumentStore";
|
|
2
|
+
function useMutation({
|
|
3
|
+
artifact
|
|
4
|
+
}) {
|
|
5
|
+
const [storeValue, observer] = useDocumentStore({ artifact });
|
|
6
|
+
const pending = storeValue.fetching;
|
|
7
|
+
const mutate = ({
|
|
8
|
+
metadata,
|
|
9
|
+
fetch,
|
|
10
|
+
variables,
|
|
11
|
+
...mutationConfig
|
|
12
|
+
}) => observer.send({
|
|
13
|
+
variables,
|
|
14
|
+
metadata,
|
|
15
|
+
session: {},
|
|
16
|
+
stuff: {
|
|
17
|
+
...mutationConfig
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return [pending, mutate];
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
useMutation
|
|
24
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type QueryArtifact, GraphQLObject } from '$houdini/runtime/lib/types';
|
|
2
|
+
import { UseQueryConfig } from './useQueryHandle';
|
|
3
|
+
export declare function useQuery<_Artifact extends QueryArtifact, _Data extends GraphQLObject = GraphQLObject, _Input extends {} = []>(document: {
|
|
4
|
+
artifact: QueryArtifact;
|
|
5
|
+
}, variables?: any, config?: UseQueryConfig): _Data;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type QueryArtifact, GraphQLObject, CachePolicies } from '$houdini/runtime/lib/types';
|
|
2
|
+
import { DocumentHandle } from './useDocumentHandle';
|
|
3
|
+
export declare function useQueryHandle<_Artifact extends QueryArtifact, _Data extends GraphQLObject = GraphQLObject, _Input extends {} = []>({ artifact }: {
|
|
4
|
+
artifact: QueryArtifact;
|
|
5
|
+
}, variables?: any, config?: UseQueryConfig): DocumentHandle<_Artifact, _Data, _Input>;
|
|
6
|
+
export type UseQueryConfig = {
|
|
7
|
+
policy?: CachePolicies;
|
|
8
|
+
metadata?: App.Metadata;
|
|
9
|
+
fetchKey?: any;
|
|
10
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { createCache } from "../lib/cache";
|
|
3
|
+
import { useDocumentHandle } from "./useDocumentHandle";
|
|
4
|
+
import { useHoudiniClient } from "./useHoudiniClient";
|
|
5
|
+
import { useIsMountedRef } from "./useIsMounted";
|
|
6
|
+
const promiseCache = createCache();
|
|
7
|
+
function useQueryHandle({ artifact }, variables = null, config = {}) {
|
|
8
|
+
const identifier = queryIdentifier({ artifact, variables, config });
|
|
9
|
+
const suspenseValue = promiseCache.get(identifier);
|
|
10
|
+
const client = useHoudiniClient();
|
|
11
|
+
const isMountedRef = useIsMountedRef();
|
|
12
|
+
let [observer] = React.useState(
|
|
13
|
+
client.observe({
|
|
14
|
+
artifact,
|
|
15
|
+
initialValue: suspenseValue?.resolved?.data ?? {}
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
const suspenseTracker = React.useRef(false);
|
|
19
|
+
const box = React.useRef(observer.state);
|
|
20
|
+
const subscribe = React.useCallback(
|
|
21
|
+
(fn) => {
|
|
22
|
+
return observer.subscribe((val) => {
|
|
23
|
+
box.current = val;
|
|
24
|
+
if (isMountedRef.current && !suspenseTracker.current) {
|
|
25
|
+
fn();
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
[observer]
|
|
30
|
+
);
|
|
31
|
+
const storeValue = React.useSyncExternalStore(subscribe, () => box.current);
|
|
32
|
+
const handle = useDocumentHandle({
|
|
33
|
+
artifact,
|
|
34
|
+
observer,
|
|
35
|
+
storeValue
|
|
36
|
+
});
|
|
37
|
+
React.useEffect(() => {
|
|
38
|
+
return () => {
|
|
39
|
+
promiseCache.delete(identifier);
|
|
40
|
+
};
|
|
41
|
+
}, [identifier]);
|
|
42
|
+
React.useEffect(() => {
|
|
43
|
+
return () => {
|
|
44
|
+
observer.cleanup();
|
|
45
|
+
};
|
|
46
|
+
}, [observer]);
|
|
47
|
+
let result = storeValue.data;
|
|
48
|
+
if (!suspenseValue) {
|
|
49
|
+
let resolve = () => {
|
|
50
|
+
};
|
|
51
|
+
const loadPromise = new Promise((r) => resolve = r);
|
|
52
|
+
const suspenseUnit = {
|
|
53
|
+
then: loadPromise.then.bind(loadPromise),
|
|
54
|
+
resolve,
|
|
55
|
+
variables
|
|
56
|
+
};
|
|
57
|
+
promiseCache.set(identifier, suspenseUnit);
|
|
58
|
+
handle.fetch({
|
|
59
|
+
variables,
|
|
60
|
+
stuff: {
|
|
61
|
+
silenceLoading: true
|
|
62
|
+
}
|
|
63
|
+
}).then((value) => {
|
|
64
|
+
suspenseUnit.resolved = {
|
|
65
|
+
...handle,
|
|
66
|
+
data: value.data,
|
|
67
|
+
partia: value.partial,
|
|
68
|
+
artifact
|
|
69
|
+
};
|
|
70
|
+
suspenseUnit.resolve();
|
|
71
|
+
});
|
|
72
|
+
suspenseTracker.current = true;
|
|
73
|
+
throw suspenseUnit;
|
|
74
|
+
}
|
|
75
|
+
if (!result && suspenseValue && !suspenseValue.resolved) {
|
|
76
|
+
suspenseTracker.current = true;
|
|
77
|
+
throw suspenseValue;
|
|
78
|
+
}
|
|
79
|
+
if (!result && suspenseValue?.resolved) {
|
|
80
|
+
return suspenseValue.resolved;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
...handle,
|
|
84
|
+
variables: storeValue.variables,
|
|
85
|
+
data: result
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function queryIdentifier(args) {
|
|
89
|
+
args.fetchKey ??= 0;
|
|
90
|
+
const { artifact, variables, fetchKey } = args;
|
|
91
|
+
return [artifact.name, JSON.stringify(variables), fetchKey].join("@@");
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
useQueryHandle
|
|
95
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { SubscriptionArtifact, GraphQLObject } from '$houdini/runtime/lib/types';
|
|
2
|
+
export declare function useSubscription<_Result extends GraphQLObject, _Input extends {}>(document: {
|
|
3
|
+
artifact: SubscriptionArtifact;
|
|
4
|
+
}, variables: _Input): import("houdini").GraphQLObject | null;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SubscriptionArtifact, GraphQLObject } from '$houdini/runtime/lib/types';
|
|
2
|
+
export type SubscriptionHandle<_Result extends GraphQLObject, _Input extends {} | null> = {
|
|
3
|
+
data: _Result | null;
|
|
4
|
+
errors: {
|
|
5
|
+
message: string;
|
|
6
|
+
}[] | null;
|
|
7
|
+
variables: _Input;
|
|
8
|
+
listen: (args: {
|
|
9
|
+
variables?: _Input;
|
|
10
|
+
}) => void;
|
|
11
|
+
unlisten: () => void;
|
|
12
|
+
fetching: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare function useSubscriptionHandle<_Result extends GraphQLObject, _Input extends {}>({ artifact }: {
|
|
15
|
+
artifact: SubscriptionArtifact;
|
|
16
|
+
}, variables: _Input): {
|
|
17
|
+
data: import("houdini").GraphQLObject | null;
|
|
18
|
+
errors: {
|
|
19
|
+
message: string;
|
|
20
|
+
}[] | null;
|
|
21
|
+
fetching: boolean;
|
|
22
|
+
variables: _Input;
|
|
23
|
+
unlisten: () => Promise<void>;
|
|
24
|
+
listen: ({ metadata, session, fetch, variables, policy, stuff, cacheParams, setup, silenceEcho, }?: import("$houdini/lib").SendParams) => Promise<import("$houdini/runtime/lib/types").QueryResult<import("houdini").GraphQLObject, _Input>>;
|
|
25
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useDocumentSubscription } from "./useDocumentSubscription";
|
|
2
|
+
function useSubscriptionHandle({ artifact }, variables) {
|
|
3
|
+
const [storeValue, observer] = useDocumentSubscription({
|
|
4
|
+
artifact,
|
|
5
|
+
variables
|
|
6
|
+
});
|
|
7
|
+
return {
|
|
8
|
+
data: storeValue.data,
|
|
9
|
+
errors: storeValue.errors,
|
|
10
|
+
fetching: storeValue.fetching,
|
|
11
|
+
variables,
|
|
12
|
+
unlisten: observer.cleanup,
|
|
13
|
+
listen: observer.send
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
useSubscriptionHandle
|
|
18
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is a copy and paste of a very simple and effective LRU cache
|
|
3
|
+
* using javascript maps. It was copied under the MIT license found at the
|
|
4
|
+
* bottom of the page.
|
|
5
|
+
*/
|
|
6
|
+
export interface Cache<T> {
|
|
7
|
+
get(key: string): T | null;
|
|
8
|
+
set(key: string, value: T): void;
|
|
9
|
+
has(key: string): boolean;
|
|
10
|
+
delete(key: string): void;
|
|
11
|
+
size(): number;
|
|
12
|
+
capacity(): number;
|
|
13
|
+
clear(): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* JS maps (both plain objects and Map) maintain key insertion
|
|
17
|
+
* order, which means there is an easy way to simulate LRU behavior
|
|
18
|
+
* that should also perform quite well:
|
|
19
|
+
*
|
|
20
|
+
* To insert a new value, first delete the key from the inner _map,
|
|
21
|
+
* then _map.set(k, v). By deleting and reinserting, you ensure that the
|
|
22
|
+
* map sees the key as the last inserted key.
|
|
23
|
+
*
|
|
24
|
+
* Get does the same: if the key is present, delete and reinsert it.
|
|
25
|
+
*/
|
|
26
|
+
declare class LRUCache<T> implements Cache<T> {
|
|
27
|
+
_capacity: number;
|
|
28
|
+
_map: Map<string, T>;
|
|
29
|
+
constructor(capacity?: number);
|
|
30
|
+
set(key: string, value: T): void;
|
|
31
|
+
get(key: string): T | null;
|
|
32
|
+
has(key: string): boolean;
|
|
33
|
+
delete(key: string): void;
|
|
34
|
+
size(): number;
|
|
35
|
+
capacity(): number;
|
|
36
|
+
clear(): void;
|
|
37
|
+
}
|
|
38
|
+
export declare function createCache<T>(capacity?: number): LRUCache<T>;
|
|
39
|
+
export {};
|
|
40
|
+
/**
|
|
41
|
+
MIT License
|
|
42
|
+
|
|
43
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
44
|
+
|
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
46
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
47
|
+
in the Software without restriction, including without limitation the rights
|
|
48
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
49
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
50
|
+
furnished to do so, subject to the following conditions:
|
|
51
|
+
|
|
52
|
+
The above copyright notice and this permission notice shall be included in all
|
|
53
|
+
copies or substantial portions of the Software.
|
|
54
|
+
|
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
56
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
57
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
58
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
59
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
60
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
61
|
+
SOFTWARE.
|
|
62
|
+
*/
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
class LRUCache {
|
|
2
|
+
_capacity;
|
|
3
|
+
_map;
|
|
4
|
+
constructor(capacity = 1e3) {
|
|
5
|
+
this._capacity = capacity;
|
|
6
|
+
this._map = /* @__PURE__ */ new Map();
|
|
7
|
+
}
|
|
8
|
+
set(key, value) {
|
|
9
|
+
this._map.delete(key);
|
|
10
|
+
this._map.set(key, value);
|
|
11
|
+
if (this._map.size > this._capacity) {
|
|
12
|
+
const firstKey = this._map.keys().next();
|
|
13
|
+
if (!firstKey.done) {
|
|
14
|
+
this._map.delete(firstKey.value);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
get(key) {
|
|
19
|
+
const value = this._map.get(key);
|
|
20
|
+
if (value != null) {
|
|
21
|
+
this._map.delete(key);
|
|
22
|
+
this._map.set(key, value);
|
|
23
|
+
}
|
|
24
|
+
return value ?? null;
|
|
25
|
+
}
|
|
26
|
+
has(key) {
|
|
27
|
+
return this._map.has(key);
|
|
28
|
+
}
|
|
29
|
+
delete(key) {
|
|
30
|
+
this._map.delete(key);
|
|
31
|
+
}
|
|
32
|
+
size() {
|
|
33
|
+
return this._map.size;
|
|
34
|
+
}
|
|
35
|
+
capacity() {
|
|
36
|
+
return this._capacity - this._map.size;
|
|
37
|
+
}
|
|
38
|
+
clear() {
|
|
39
|
+
this._map.clear();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function createCache(capacity = 1e3) {
|
|
43
|
+
return new LRUCache(capacity);
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
createCache
|
|
47
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "houdini-react",
|
|
3
|
+
"version": "1.2.0-react.1",
|
|
4
|
+
"description": "The React plugin for houdini",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"react",
|
|
8
|
+
"graphql",
|
|
9
|
+
"graphql-client"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/HoudiniGraphql/houdini",
|
|
12
|
+
"funding": "https://github.com/sponsors/HoudiniGraphql",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/HoudiniGraphql/houdini.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/estraverse": "^5.1.2",
|
|
20
|
+
"@types/next": "^9.0.0",
|
|
21
|
+
"next": "^13.0.1",
|
|
22
|
+
"scripts": "^1.0.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@babel/parser": "^7.19.3",
|
|
26
|
+
"@types/react": "^18.0.28",
|
|
27
|
+
"@types/rollup": "^0.54.0",
|
|
28
|
+
"estraverse": "^5.3.0",
|
|
29
|
+
"graphql": "^15.8.0",
|
|
30
|
+
"react": "^18.2.0",
|
|
31
|
+
"recast": "^0.23.1",
|
|
32
|
+
"rollup": "^3.7.4",
|
|
33
|
+
"use-deep-compare-effect": "^1.8.1",
|
|
34
|
+
"houdini": "^1.2.0-react.1"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"build"
|
|
38
|
+
],
|
|
39
|
+
"exports": {
|
|
40
|
+
"./package.json": "./package.json",
|
|
41
|
+
"./next": {
|
|
42
|
+
"import": "./build/next-esm/index.js",
|
|
43
|
+
"require": "./build/next-cjs/index.js"
|
|
44
|
+
},
|
|
45
|
+
".": {
|
|
46
|
+
"import": "./build/plugin-esm/index.js",
|
|
47
|
+
"require": "./build/plugin-cjs/index.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"typesVersions": {
|
|
51
|
+
"*": {
|
|
52
|
+
"next": [
|
|
53
|
+
"build/next/index.d.ts"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"main": "./build/plugin-cjs/index.js",
|
|
58
|
+
"types": "./build/plugin/index.d.ts",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"compile": "scripts build --plugin",
|
|
61
|
+
"typedefs": "scripts typedefs --plugin"
|
|
62
|
+
}
|
|
63
|
+
}
|