react-firebase-ql 2.2.0 → 3.0.0
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.
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { BaseModel, whereClause } from 'firebase-client-ql';
|
|
2
|
+
type UseStreamResult<T> = {
|
|
3
|
+
data: T[];
|
|
4
|
+
loading: boolean;
|
|
5
|
+
error: unknown;
|
|
6
|
+
};
|
|
2
7
|
export declare const useStream: <T extends BaseModel>(param: {
|
|
3
|
-
model:
|
|
8
|
+
model: any;
|
|
4
9
|
where?: whereClause[];
|
|
5
10
|
reference?: string;
|
|
6
11
|
filter?: {
|
|
@@ -10,4 +15,5 @@ export declare const useStream: <T extends BaseModel>(param: {
|
|
|
10
15
|
};
|
|
11
16
|
offset?: string;
|
|
12
17
|
};
|
|
13
|
-
}
|
|
18
|
+
}) => UseStreamResult<T>;
|
|
19
|
+
export {};
|
package/dist/client/useStream.js
CHANGED
|
@@ -4,38 +4,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.useStream = void 0;
|
|
5
5
|
const firebase_client_ql_1 = require("firebase-client-ql");
|
|
6
6
|
const react_1 = require("react");
|
|
7
|
-
const useStream = (param
|
|
7
|
+
const useStream = (param) => {
|
|
8
|
+
const [data, setData] = (0, react_1.useState)([]);
|
|
9
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
10
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
8
11
|
(0, react_1.useEffect)(() => {
|
|
9
12
|
let unsubscribe;
|
|
13
|
+
setLoading(true);
|
|
10
14
|
try {
|
|
11
15
|
const { model, reference, where, filter } = param;
|
|
12
16
|
if (reference) {
|
|
13
|
-
unsubscribe = model.stream((
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
unsubscribe = model.stream((result) => {
|
|
18
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
19
|
+
setLoading(false);
|
|
16
20
|
}, reference);
|
|
17
21
|
}
|
|
18
22
|
else if (where) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
24
|
+
setData([]);
|
|
25
|
+
setLoading(false);
|
|
22
26
|
return;
|
|
23
27
|
}
|
|
24
|
-
unsubscribe = model.streamWhere(where, (
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
unsubscribe = model.streamWhere(where, (result) => {
|
|
29
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
30
|
+
setLoading(false);
|
|
27
31
|
}, 100, filter === null || filter === void 0 ? void 0 : filter.orderBy, filter === null || filter === void 0 ? void 0 : filter.offset);
|
|
28
32
|
}
|
|
29
33
|
}
|
|
30
|
-
catch (
|
|
31
|
-
(0, firebase_client_ql_1.errorLogger)('useStream error:',
|
|
34
|
+
catch (err) {
|
|
35
|
+
(0, firebase_client_ql_1.errorLogger)('useStream error:', err);
|
|
36
|
+
setError(err);
|
|
37
|
+
setLoading(false);
|
|
32
38
|
}
|
|
33
39
|
return () => {
|
|
34
|
-
if (typeof unsubscribe === 'function')
|
|
40
|
+
if (typeof unsubscribe === 'function')
|
|
35
41
|
unsubscribe();
|
|
36
|
-
}
|
|
37
42
|
};
|
|
38
|
-
}, [
|
|
43
|
+
}, [
|
|
44
|
+
JSON.stringify(param.where),
|
|
45
|
+
JSON.stringify(param.filter),
|
|
46
|
+
param.reference,
|
|
47
|
+
]);
|
|
48
|
+
return { data, loading, error };
|
|
39
49
|
};
|
|
40
50
|
exports.useStream = useStream;
|
|
41
51
|
//# sourceMappingURL=useStream.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;AAEb,2DAAyE;AACzE,
|
|
1
|
+
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":";AAAA,YAAY,CA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ACtB,UAAU,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,EACD,GAAG,EACH,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EACf,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CACf,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAA,gCAAW,EAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;YACrC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACd,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QAED,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,WAAW,KAAK,UAAU;gBAAE,WAAW,EAAE,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC,EAAE;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;QAC5B,KAAK,CAAC,SAAS;KAChB,CAAC,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC,CAAC;AA9DW,QAAA,SAAS,aA8DpB"}
|
package/esm/client/useStream.js
CHANGED
|
@@ -1,37 +1,47 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { errorLogger } from 'firebase-client-ql';
|
|
3
|
-
import { useEffect } from 'react';
|
|
4
|
-
export const useStream = (param
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
export const useStream = (param) => {
|
|
5
|
+
const [data, setData] = useState([]);
|
|
6
|
+
const [loading, setLoading] = useState(true);
|
|
7
|
+
const [error, setError] = useState(null);
|
|
5
8
|
useEffect(() => {
|
|
6
9
|
let unsubscribe;
|
|
10
|
+
setLoading(true);
|
|
7
11
|
try {
|
|
8
12
|
const { model, reference, where, filter } = param;
|
|
9
13
|
if (reference) {
|
|
10
|
-
unsubscribe = model.stream((
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
unsubscribe = model.stream((result) => {
|
|
15
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
16
|
+
setLoading(false);
|
|
13
17
|
}, reference);
|
|
14
18
|
}
|
|
15
19
|
else if (where) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
21
|
+
setData([]);
|
|
22
|
+
setLoading(false);
|
|
19
23
|
return;
|
|
20
24
|
}
|
|
21
|
-
unsubscribe = model.streamWhere(where, (
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
unsubscribe = model.streamWhere(where, (result) => {
|
|
26
|
+
setData(result !== null && result !== void 0 ? result : []);
|
|
27
|
+
setLoading(false);
|
|
24
28
|
}, 100, filter === null || filter === void 0 ? void 0 : filter.orderBy, filter === null || filter === void 0 ? void 0 : filter.offset);
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
|
-
catch (
|
|
28
|
-
errorLogger('useStream error:',
|
|
31
|
+
catch (err) {
|
|
32
|
+
errorLogger('useStream error:', err);
|
|
33
|
+
setError(err);
|
|
34
|
+
setLoading(false);
|
|
29
35
|
}
|
|
30
36
|
return () => {
|
|
31
|
-
if (typeof unsubscribe === 'function')
|
|
37
|
+
if (typeof unsubscribe === 'function')
|
|
32
38
|
unsubscribe();
|
|
33
|
-
}
|
|
34
39
|
};
|
|
35
|
-
}, [
|
|
40
|
+
}, [
|
|
41
|
+
JSON.stringify(param.where),
|
|
42
|
+
JSON.stringify(param.filter),
|
|
43
|
+
param.reference,
|
|
44
|
+
]);
|
|
45
|
+
return { data, loading, error };
|
|
36
46
|
};
|
|
37
47
|
//# sourceMappingURL=useStream.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStream.js","sourceRoot":"","sources":["../../src/client/useStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAa,WAAW,EAAe,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,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AC,CAAC;oBAClB,OAAO;gBACT,CAAC;gBAED,WAAW,GAAG,KAAK,CAAC,WAAW,CAC7B,KAAK,EACL,CAAC,MAAW,EAAE,EAAE;oBACd,OAAO,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CAAC;oBACtB,UAAU,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,EACD,GAAG,EACH,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,EACf,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,CACf,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,WAAW,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;YACrC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACd,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QAED,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,WAAW,KAAK,UAAU;gBAAE,WAAW,EAAE,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC,EAAE;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;QAC5B,KAAK,CAAC,SAAS;KAChB,CAAC,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/client/useStream.ts
CHANGED
|
@@ -1,57 +1,74 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { BaseModel, errorLogger, whereClause } from 'firebase-client-ql';
|
|
4
|
-
import { useEffect } from 'react';
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
5
|
|
|
6
|
+
type UseStreamResult<T> = {
|
|
7
|
+
data: T[];
|
|
8
|
+
loading: boolean;
|
|
9
|
+
error: unknown;
|
|
10
|
+
};
|
|
6
11
|
|
|
7
12
|
export const useStream = <T extends BaseModel>(
|
|
8
13
|
param: {
|
|
9
|
-
model:
|
|
14
|
+
model: any;
|
|
10
15
|
where?: whereClause[];
|
|
11
16
|
reference?: string;
|
|
12
17
|
filter?: {
|
|
13
18
|
orderBy?: { parameter: string; direction?: 'asc' | 'desc' };
|
|
14
19
|
offset?: string;
|
|
15
20
|
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
}
|
|
22
|
+
): UseStreamResult<T> => {
|
|
23
|
+
const [data, setData] = useState<T[]>([]);
|
|
24
|
+
const [loading, setLoading] = useState(true);
|
|
25
|
+
const [error, setError] = useState<unknown>(null);
|
|
26
|
+
|
|
19
27
|
useEffect(() => {
|
|
20
28
|
let unsubscribe: (() => void) | undefined;
|
|
29
|
+
setLoading(true);
|
|
21
30
|
|
|
22
31
|
try {
|
|
23
|
-
const {model, reference, where, filter } = param;
|
|
32
|
+
const { model, reference, where, filter } = param;
|
|
24
33
|
|
|
25
34
|
if (reference) {
|
|
26
|
-
unsubscribe = model.stream((
|
|
27
|
-
|
|
35
|
+
unsubscribe = model.stream((result: T[]) => {
|
|
36
|
+
setData(result ?? []);
|
|
37
|
+
setLoading(false);
|
|
28
38
|
}, reference);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
39
|
+
}
|
|
40
|
+
else if (where) {
|
|
41
|
+
if (where.length > 0 && where.every(w => w.value === undefined)) {
|
|
42
|
+
setData([]);
|
|
43
|
+
setLoading(false);
|
|
33
44
|
return;
|
|
34
45
|
}
|
|
35
46
|
|
|
36
47
|
unsubscribe = model.streamWhere(
|
|
37
48
|
where,
|
|
38
|
-
(
|
|
39
|
-
|
|
49
|
+
(result: T[]) => {
|
|
50
|
+
setData(result ?? []);
|
|
51
|
+
setLoading(false);
|
|
40
52
|
},
|
|
41
53
|
100,
|
|
42
54
|
filter?.orderBy,
|
|
43
55
|
filter?.offset
|
|
44
56
|
);
|
|
45
57
|
}
|
|
46
|
-
} catch (
|
|
47
|
-
errorLogger('useStream error:',
|
|
58
|
+
} catch (err) {
|
|
59
|
+
errorLogger('useStream error:', err);
|
|
60
|
+
setError(err);
|
|
61
|
+
setLoading(false);
|
|
48
62
|
}
|
|
49
63
|
|
|
50
|
-
// Proper cleanup
|
|
51
64
|
return () => {
|
|
52
|
-
if (typeof unsubscribe === 'function')
|
|
53
|
-
unsubscribe();
|
|
54
|
-
}
|
|
65
|
+
if (typeof unsubscribe === 'function') unsubscribe();
|
|
55
66
|
};
|
|
56
|
-
}, [
|
|
67
|
+
}, [
|
|
68
|
+
JSON.stringify(param.where),
|
|
69
|
+
JSON.stringify(param.filter),
|
|
70
|
+
param.reference,
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
return { data, loading, error };
|
|
57
74
|
};
|