|
| 1 | +import Parse from "parse/node.js"; |
| 2 | +import React from "react"; |
| 3 | + |
| 4 | +type Result<T> = { |
| 5 | + result: T[]; |
| 6 | + count: number; |
| 7 | + loading: boolean; |
| 8 | + error: Error | undefined; |
| 9 | + reload: () => void; |
| 10 | +}; |
| 11 | + |
| 12 | +export function useParseQuery<T extends Parse.Object>( |
| 13 | + query: Parse.Query<T>, |
| 14 | + options?: { |
| 15 | + count?: boolean; |
| 16 | + live?: boolean; |
| 17 | + liveReload?: boolean; |
| 18 | + reloadThrottle?: number; |
| 19 | + } |
| 20 | +): Result<T> { |
| 21 | + const subscriptionRef = React.useRef<Parse.LiveQuerySubscription>(); |
| 22 | + const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(); |
| 23 | + const throttleRef = React.useRef<boolean>(false); |
| 24 | + |
| 25 | + const initialState = { |
| 26 | + result: [], |
| 27 | + count: 0, |
| 28 | + loading: true, |
| 29 | + error: undefined, |
| 30 | + reload: fetch, |
| 31 | + }; |
| 32 | + |
| 33 | + const [state, setState] = React.useState<Result<T>>(initialState); |
| 34 | + |
| 35 | + async function fetch(force: boolean = false) { |
| 36 | + if ( |
| 37 | + !force && |
| 38 | + options?.reloadThrottle && |
| 39 | + Number.isInteger(options?.reloadThrottle) |
| 40 | + ) { |
| 41 | + if (timeoutRef.current) { |
| 42 | + throttleRef.current = true; |
| 43 | + return; |
| 44 | + } else { |
| 45 | + timeoutRef.current = setTimeout(() => { |
| 46 | + timeoutRef.current = null; |
| 47 | + |
| 48 | + if (throttleRef.current) { |
| 49 | + fetch(); |
| 50 | + } |
| 51 | + |
| 52 | + throttleRef.current = false; |
| 53 | + }, options.reloadThrottle); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (query) { |
| 58 | + if (options?.live) { |
| 59 | + if (subscriptionRef.current) { |
| 60 | + await subscriptionRef.current.unsubscribe(); |
| 61 | + } |
| 62 | + |
| 63 | + subscriptionRef.current = await query.subscribe(); |
| 64 | + |
| 65 | + // subscriptionRef.current.on("open", () => {}); |
| 66 | + |
| 67 | + subscriptionRef.current.on("create", (object: T) => { |
| 68 | + if (options?.liveReload) { |
| 69 | + fetch(); |
| 70 | + } else { |
| 71 | + setState(mergeResult(object, true)); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + subscriptionRef.current.on("update", (object: T) => { |
| 76 | + if (options?.liveReload) { |
| 77 | + fetch(); |
| 78 | + } else { |
| 79 | + setState(mergeResult(object, true)); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + subscriptionRef.current.on("enter", (object: T) => { |
| 84 | + if (options?.liveReload) { |
| 85 | + fetch(); |
| 86 | + } else { |
| 87 | + setState(mergeResult(object, true)); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + subscriptionRef.current.on("leave", (object: T) => { |
| 92 | + if (options?.liveReload) { |
| 93 | + fetch(); |
| 94 | + } else { |
| 95 | + setState(mergeResult(object)); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + const requests: [Promise<T[]>, Promise<number>] = [ |
| 101 | + query.find(), |
| 102 | + options?.count ? query.count() : Promise.resolve(0), |
| 103 | + ]; |
| 104 | + |
| 105 | + Promise.all(requests).then( |
| 106 | + ([response, count]) => { |
| 107 | + if (Array.isArray(response)) { |
| 108 | + setState( |
| 109 | + mergeState({ |
| 110 | + result: response, |
| 111 | + count: count || 0, |
| 112 | + loading: false, |
| 113 | + }) |
| 114 | + ); |
| 115 | + } else { |
| 116 | + setState( |
| 117 | + mergeState({ error: new Error("Bad Response"), loading: false }) |
| 118 | + ); |
| 119 | + } |
| 120 | + }, |
| 121 | + (error: Error) => { |
| 122 | + setState(mergeState({ error, loading: false })); |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + function mergeState( |
| 129 | + nextState: Partial<Result<T>> |
| 130 | + ): (state: Result<T>) => Result<T> { |
| 131 | + return (state) => Object.assign({}, state, nextState); |
| 132 | + } |
| 133 | + |
| 134 | + function mergeResult( |
| 135 | + object: T, |
| 136 | + add: boolean = false |
| 137 | + ): (state: Result<T>) => Result<T> { |
| 138 | + return (state) => { |
| 139 | + const result = state.result.filter((e) => e.id !== object.id); |
| 140 | + |
| 141 | + if (add) { |
| 142 | + result.push(object); |
| 143 | + } |
| 144 | + |
| 145 | + return Object.assign({}, state, { result }); |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + React.useEffect(() => { |
| 150 | + setState(initialState); |
| 151 | + fetch(true); |
| 152 | + |
| 153 | + return () => { |
| 154 | + if (subscriptionRef.current) { |
| 155 | + subscriptionRef.current.unsubscribe(); |
| 156 | + } |
| 157 | + }; |
| 158 | + }, [query]); |
| 159 | + |
| 160 | + return state; |
| 161 | +} |
0 commit comments