localdb-ces6q 0.0.5 → 0.2.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.
- package/LocalDB.d.ts +103 -102
- package/LocalDB.js +980 -966
- package/LocalDBDocument.d.ts +18 -18
- package/LocalDBDocument.js +55 -55
- package/LocalDBState.d.ts +14 -14
- package/LocalDBState.js +46 -46
- package/comparators.d.ts +15 -15
- package/comparators.js +105 -105
- package/hooks.d.ts +5 -5
- package/hooks.js +34 -34
- package/package.json +4 -4
- package/types.d.ts +104 -100
- package/types.js +1 -1
package/LocalDBDocument.d.ts
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
-
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
3
|
-
export declare class LocalDBDocument<Data = any> extends PubSub<[
|
4
|
-
Data,
|
5
|
-
Data,
|
6
|
-
Array<KeyOf<Data>>
|
7
|
-
]> {
|
8
|
-
private _data;
|
9
|
-
constructor(data: Data);
|
10
|
-
toJSON(): Data;
|
11
|
-
equals(other: LocalDBDocument<Data>): boolean;
|
12
|
-
destroy(): void;
|
13
|
-
get id(): string;
|
14
|
-
get data(): Data;
|
15
|
-
set data(next: Data);
|
16
|
-
set(next: Data): void;
|
17
|
-
update(update: Partial<Data>): void;
|
18
|
-
}
|
1
|
+
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
+
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
3
|
+
export declare class LocalDBDocument<Data = any> extends PubSub<[
|
4
|
+
Data,
|
5
|
+
Data,
|
6
|
+
Array<KeyOf<Data>>
|
7
|
+
]> {
|
8
|
+
private _data;
|
9
|
+
constructor(data: Data);
|
10
|
+
toJSON(): Data;
|
11
|
+
equals(other: LocalDBDocument<Data>): boolean;
|
12
|
+
destroy(): void;
|
13
|
+
get id(): string;
|
14
|
+
get data(): Data;
|
15
|
+
set data(next: Data);
|
16
|
+
set(next: Data): void;
|
17
|
+
update(update: Partial<Data>): void;
|
18
|
+
}
|
package/LocalDBDocument.js
CHANGED
@@ -1,55 +1,55 @@
|
|
1
|
-
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
-
import { deepEqual } from 'util-3gcvv/deepEqual.js';
|
3
|
-
import { objectKeys } from 'util-3gcvv/object.js';
|
4
|
-
export class LocalDBDocument extends PubSub {
|
5
|
-
constructor(data) {
|
6
|
-
super();
|
7
|
-
this._data = data;
|
8
|
-
}
|
9
|
-
toJSON() {
|
10
|
-
return this._data;
|
11
|
-
}
|
12
|
-
equals(other) {
|
13
|
-
return deepEqual(this._data, other._data);
|
14
|
-
}
|
15
|
-
destroy() {
|
16
|
-
super.destroy();
|
17
|
-
this._data = null;
|
18
|
-
}
|
19
|
-
get id() {
|
20
|
-
return this._data.id;
|
21
|
-
}
|
22
|
-
get data() {
|
23
|
-
return this._data;
|
24
|
-
}
|
25
|
-
set data(next) {
|
26
|
-
this.set(next);
|
27
|
-
}
|
28
|
-
set(next) {
|
29
|
-
return this.update(next);
|
30
|
-
}
|
31
|
-
update(update) {
|
32
|
-
const prev = this._data;
|
33
|
-
const keys = objectKeys(update);
|
34
|
-
const changed = {};
|
35
|
-
const changedKeys = [];
|
36
|
-
const next = { ...prev };
|
37
|
-
for (let i = 0, il = keys.length; i < il; i += 1) {
|
38
|
-
const key = keys[i];
|
39
|
-
if (!deepEqual(update[key], prev[key])) {
|
40
|
-
changed[key] = update[key];
|
41
|
-
if (update[key] === undefined) {
|
42
|
-
delete next[key];
|
43
|
-
}
|
44
|
-
else {
|
45
|
-
next[key] = update[key];
|
46
|
-
}
|
47
|
-
changedKeys.push(key);
|
48
|
-
}
|
49
|
-
}
|
50
|
-
if (changedKeys.length) {
|
51
|
-
this._data = next;
|
52
|
-
this.emit(next, prev, changedKeys);
|
53
|
-
}
|
54
|
-
}
|
55
|
-
}
|
1
|
+
import { PubSub } from 'util-3gcvv/class/PubSub.js';
|
2
|
+
import { deepEqual } from 'util-3gcvv/deepEqual.js';
|
3
|
+
import { objectKeys } from 'util-3gcvv/object.js';
|
4
|
+
export class LocalDBDocument extends PubSub {
|
5
|
+
constructor(data) {
|
6
|
+
super();
|
7
|
+
this._data = data;
|
8
|
+
}
|
9
|
+
toJSON() {
|
10
|
+
return this._data;
|
11
|
+
}
|
12
|
+
equals(other) {
|
13
|
+
return deepEqual(this._data, other._data);
|
14
|
+
}
|
15
|
+
destroy() {
|
16
|
+
super.destroy();
|
17
|
+
this._data = null;
|
18
|
+
}
|
19
|
+
get id() {
|
20
|
+
return this._data.id;
|
21
|
+
}
|
22
|
+
get data() {
|
23
|
+
return this._data;
|
24
|
+
}
|
25
|
+
set data(next) {
|
26
|
+
this.set(next);
|
27
|
+
}
|
28
|
+
set(next) {
|
29
|
+
return this.update(next);
|
30
|
+
}
|
31
|
+
update(update) {
|
32
|
+
const prev = this._data;
|
33
|
+
const keys = objectKeys(update);
|
34
|
+
const changed = {};
|
35
|
+
const changedKeys = [];
|
36
|
+
const next = { ...prev };
|
37
|
+
for (let i = 0, il = keys.length; i < il; i += 1) {
|
38
|
+
const key = keys[i];
|
39
|
+
if (!deepEqual(update[key], prev[key])) {
|
40
|
+
changed[key] = update[key];
|
41
|
+
if (update[key] === undefined) {
|
42
|
+
delete next[key];
|
43
|
+
}
|
44
|
+
else {
|
45
|
+
next[key] = update[key];
|
46
|
+
}
|
47
|
+
changedKeys.push(key);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
if (changedKeys.length) {
|
51
|
+
this._data = next;
|
52
|
+
this.emit(next, prev, changedKeys);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
package/LocalDBState.d.ts
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
import { SmartState } from 'util-3gcvv/class/SmartState.js';
|
2
|
-
import type { IDBOperationHistoryItem } from './types.js';
|
3
|
-
export interface ILocalDBStateProps {
|
4
|
-
history: IDBOperationHistoryItem[][];
|
5
|
-
historyIndex: number;
|
6
|
-
canRedo: boolean;
|
7
|
-
canUndo: boolean;
|
8
|
-
redoItem: IDBOperationHistoryItem[] | undefined;
|
9
|
-
undoItem: IDBOperationHistoryItem[] | undefined;
|
10
|
-
}
|
11
|
-
type ComputedKeys = 'canUndo' | 'canRedo' | 'redoItem' | 'undoItem';
|
12
|
-
export declare const LocalDBState: import("util-3gcvv/class/SmartState.js").SmartStateConstructor<ILocalDBStateProps, ComputedKeys, {}, import("util-3gcvv/class/SmartState.js").IEmptyInterface>;
|
13
|
-
export type LocalDBState = SmartState<ILocalDBStateProps, ComputedKeys, {}>;
|
14
|
-
export {};
|
1
|
+
import { SmartState } from 'util-3gcvv/class/SmartState.js';
|
2
|
+
import type { IDBOperationHistoryItem } from './types.js';
|
3
|
+
export interface ILocalDBStateProps {
|
4
|
+
history: IDBOperationHistoryItem[][];
|
5
|
+
historyIndex: number;
|
6
|
+
canRedo: boolean;
|
7
|
+
canUndo: boolean;
|
8
|
+
redoItem: IDBOperationHistoryItem[] | undefined;
|
9
|
+
undoItem: IDBOperationHistoryItem[] | undefined;
|
10
|
+
}
|
11
|
+
type ComputedKeys = 'canUndo' | 'canRedo' | 'redoItem' | 'undoItem';
|
12
|
+
export declare const LocalDBState: import("util-3gcvv/class/SmartState.js").SmartStateConstructor<ILocalDBStateProps, ComputedKeys, {}, import("util-3gcvv/class/SmartState.js").IEmptyInterface>;
|
13
|
+
export type LocalDBState = SmartState<ILocalDBStateProps, ComputedKeys, {}>;
|
14
|
+
export {};
|
package/LocalDBState.js
CHANGED
@@ -1,46 +1,46 @@
|
|
1
|
-
import { defineSmartState } from 'util-3gcvv/class/SmartState.js';
|
2
|
-
export const LocalDBState = defineSmartState({
|
3
|
-
statics: {
|
4
|
-
name: 'LocalDBState',
|
5
|
-
fromJSON: (json) => new LocalDBState(json.state, json.config),
|
6
|
-
},
|
7
|
-
properties: {
|
8
|
-
history: { type: 'array', item: 'object' },
|
9
|
-
historyIndex: { type: 'number' },
|
10
|
-
},
|
11
|
-
computed: {
|
12
|
-
// []
|
13
|
-
// [a, b, c, d]
|
14
|
-
canRedo: {
|
15
|
-
type: 'boolean',
|
16
|
-
deps: ['redoItem'],
|
17
|
-
get({ redoItem }) {
|
18
|
-
return redoItem != null;
|
19
|
-
},
|
20
|
-
},
|
21
|
-
canUndo: {
|
22
|
-
type: 'boolean',
|
23
|
-
deps: ['undoItem'],
|
24
|
-
get({ undoItem }) {
|
25
|
-
return undoItem != null;
|
26
|
-
},
|
27
|
-
},
|
28
|
-
redoItem: {
|
29
|
-
type: 'array',
|
30
|
-
item: 'object',
|
31
|
-
deps: ['history', 'historyIndex'],
|
32
|
-
get({ history, historyIndex }) {
|
33
|
-
return history[historyIndex];
|
34
|
-
},
|
35
|
-
},
|
36
|
-
undoItem: {
|
37
|
-
type: 'array',
|
38
|
-
item: 'object',
|
39
|
-
deps: ['history', 'historyIndex'],
|
40
|
-
get({ history, historyIndex }) {
|
41
|
-
return history[historyIndex - 1];
|
42
|
-
},
|
43
|
-
},
|
44
|
-
},
|
45
|
-
drafts: [],
|
46
|
-
});
|
1
|
+
import { defineSmartState } from 'util-3gcvv/class/SmartState.js';
|
2
|
+
export const LocalDBState = defineSmartState({
|
3
|
+
statics: {
|
4
|
+
name: 'LocalDBState',
|
5
|
+
fromJSON: (json) => new LocalDBState(json.state, json.config),
|
6
|
+
},
|
7
|
+
properties: {
|
8
|
+
history: { type: 'array', item: 'object' },
|
9
|
+
historyIndex: { type: 'number' },
|
10
|
+
},
|
11
|
+
computed: {
|
12
|
+
// []
|
13
|
+
// [a, b, c, d]
|
14
|
+
canRedo: {
|
15
|
+
type: 'boolean',
|
16
|
+
deps: ['redoItem'],
|
17
|
+
get({ redoItem }) {
|
18
|
+
return redoItem != null;
|
19
|
+
},
|
20
|
+
},
|
21
|
+
canUndo: {
|
22
|
+
type: 'boolean',
|
23
|
+
deps: ['undoItem'],
|
24
|
+
get({ undoItem }) {
|
25
|
+
return undoItem != null;
|
26
|
+
},
|
27
|
+
},
|
28
|
+
redoItem: {
|
29
|
+
type: 'array',
|
30
|
+
item: 'object',
|
31
|
+
deps: ['history', 'historyIndex'],
|
32
|
+
get({ history, historyIndex }) {
|
33
|
+
return history[historyIndex];
|
34
|
+
},
|
35
|
+
},
|
36
|
+
undoItem: {
|
37
|
+
type: 'array',
|
38
|
+
item: 'object',
|
39
|
+
deps: ['history', 'historyIndex'],
|
40
|
+
get({ history, historyIndex }) {
|
41
|
+
return history[historyIndex - 1];
|
42
|
+
},
|
43
|
+
},
|
44
|
+
},
|
45
|
+
drafts: [],
|
46
|
+
});
|
package/comparators.d.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
export declare const compareNil: (a: any, b: any) => number | null;
|
2
|
-
export declare const defaultComparators: {
|
3
|
-
string: {
|
4
|
-
asc: (a: string | null | undefined, b: string | null | undefined) => number;
|
5
|
-
desc: (a: string | null | undefined, b: string | null | undefined) => number;
|
6
|
-
};
|
7
|
-
number: {
|
8
|
-
asc: (a: number | null | undefined, b: number | null | undefined) => number;
|
9
|
-
desc: (a: number | null | undefined, b: number | null | undefined) => number;
|
10
|
-
};
|
11
|
-
boolean: {
|
12
|
-
asc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
13
|
-
desc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
14
|
-
};
|
15
|
-
};
|
1
|
+
export declare const compareNil: (a: any, b: any) => number | null;
|
2
|
+
export declare const defaultComparators: {
|
3
|
+
string: {
|
4
|
+
asc: (a: string | null | undefined, b: string | null | undefined) => number;
|
5
|
+
desc: (a: string | null | undefined, b: string | null | undefined) => number;
|
6
|
+
};
|
7
|
+
number: {
|
8
|
+
asc: (a: number | null | undefined, b: number | null | undefined) => number;
|
9
|
+
desc: (a: number | null | undefined, b: number | null | undefined) => number;
|
10
|
+
};
|
11
|
+
boolean: {
|
12
|
+
asc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
13
|
+
desc: (a: boolean | null | undefined, b: boolean | null | undefined) => number;
|
14
|
+
};
|
15
|
+
};
|
package/comparators.js
CHANGED
@@ -1,105 +1,105 @@
|
|
1
|
-
export const compareNil = (a, b) => {
|
2
|
-
if (a === b)
|
3
|
-
return 0;
|
4
|
-
if (a === undefined)
|
5
|
-
return 1;
|
6
|
-
if (b === undefined)
|
7
|
-
return -1;
|
8
|
-
if (a === null)
|
9
|
-
return 1;
|
10
|
-
if (b === null)
|
11
|
-
return -1;
|
12
|
-
return null;
|
13
|
-
};
|
14
|
-
export const defaultComparators = {
|
15
|
-
string: {
|
16
|
-
// a -> z
|
17
|
-
asc: (a, b) => {
|
18
|
-
if (a === b)
|
19
|
-
return 0;
|
20
|
-
if (a === undefined)
|
21
|
-
return 1;
|
22
|
-
if (b === undefined)
|
23
|
-
return -1;
|
24
|
-
if (a === null)
|
25
|
-
return 1;
|
26
|
-
if (b === null)
|
27
|
-
return -1;
|
28
|
-
return a.localeCompare(b);
|
29
|
-
},
|
30
|
-
// z -> a
|
31
|
-
desc: (a, b) => {
|
32
|
-
if (a === b)
|
33
|
-
return 0;
|
34
|
-
if (a === undefined)
|
35
|
-
return 1;
|
36
|
-
if (b === undefined)
|
37
|
-
return -1;
|
38
|
-
if (a === null)
|
39
|
-
return 1;
|
40
|
-
if (b === null)
|
41
|
-
return -1;
|
42
|
-
return b.localeCompare(a);
|
43
|
-
},
|
44
|
-
},
|
45
|
-
number: {
|
46
|
-
// 0 -> 9
|
47
|
-
asc: (a, b) => {
|
48
|
-
if (a === b)
|
49
|
-
return 0;
|
50
|
-
if (a === undefined)
|
51
|
-
return 1;
|
52
|
-
if (b === undefined)
|
53
|
-
return -1;
|
54
|
-
if (a === null)
|
55
|
-
return 1;
|
56
|
-
if (b === null)
|
57
|
-
return -1;
|
58
|
-
return a - b;
|
59
|
-
},
|
60
|
-
// 9 -> 0
|
61
|
-
desc: (a, b) => {
|
62
|
-
if (a === b)
|
63
|
-
return 0;
|
64
|
-
if (a === undefined)
|
65
|
-
return 1;
|
66
|
-
if (b === undefined)
|
67
|
-
return -1;
|
68
|
-
if (a === null)
|
69
|
-
return 1;
|
70
|
-
if (b === null)
|
71
|
-
return -1;
|
72
|
-
return b - a;
|
73
|
-
},
|
74
|
-
},
|
75
|
-
boolean: {
|
76
|
-
// false -> true
|
77
|
-
asc: (a, b) => {
|
78
|
-
if (a === b)
|
79
|
-
return 0;
|
80
|
-
if (a === undefined)
|
81
|
-
return 1;
|
82
|
-
if (b === undefined)
|
83
|
-
return -1;
|
84
|
-
if (a === null)
|
85
|
-
return 1;
|
86
|
-
if (b === null)
|
87
|
-
return -1;
|
88
|
-
return (a ? 1 : 0) - (b ? 1 : 0);
|
89
|
-
},
|
90
|
-
// true -> false
|
91
|
-
desc: (a, b) => {
|
92
|
-
if (a === b)
|
93
|
-
return 0;
|
94
|
-
if (a === undefined)
|
95
|
-
return 1;
|
96
|
-
if (b === undefined)
|
97
|
-
return -1;
|
98
|
-
if (a === null)
|
99
|
-
return 1;
|
100
|
-
if (b === null)
|
101
|
-
return -1;
|
102
|
-
return (b ? 1 : 0) - (a ? 1 : 0);
|
103
|
-
},
|
104
|
-
},
|
105
|
-
};
|
1
|
+
export const compareNil = (a, b) => {
|
2
|
+
if (a === b)
|
3
|
+
return 0;
|
4
|
+
if (a === undefined)
|
5
|
+
return 1;
|
6
|
+
if (b === undefined)
|
7
|
+
return -1;
|
8
|
+
if (a === null)
|
9
|
+
return 1;
|
10
|
+
if (b === null)
|
11
|
+
return -1;
|
12
|
+
return null;
|
13
|
+
};
|
14
|
+
export const defaultComparators = {
|
15
|
+
string: {
|
16
|
+
// a -> z
|
17
|
+
asc: (a, b) => {
|
18
|
+
if (a === b)
|
19
|
+
return 0;
|
20
|
+
if (a === undefined)
|
21
|
+
return 1;
|
22
|
+
if (b === undefined)
|
23
|
+
return -1;
|
24
|
+
if (a === null)
|
25
|
+
return 1;
|
26
|
+
if (b === null)
|
27
|
+
return -1;
|
28
|
+
return a.localeCompare(b);
|
29
|
+
},
|
30
|
+
// z -> a
|
31
|
+
desc: (a, b) => {
|
32
|
+
if (a === b)
|
33
|
+
return 0;
|
34
|
+
if (a === undefined)
|
35
|
+
return 1;
|
36
|
+
if (b === undefined)
|
37
|
+
return -1;
|
38
|
+
if (a === null)
|
39
|
+
return 1;
|
40
|
+
if (b === null)
|
41
|
+
return -1;
|
42
|
+
return b.localeCompare(a);
|
43
|
+
},
|
44
|
+
},
|
45
|
+
number: {
|
46
|
+
// 0 -> 9
|
47
|
+
asc: (a, b) => {
|
48
|
+
if (a === b)
|
49
|
+
return 0;
|
50
|
+
if (a === undefined)
|
51
|
+
return 1;
|
52
|
+
if (b === undefined)
|
53
|
+
return -1;
|
54
|
+
if (a === null)
|
55
|
+
return 1;
|
56
|
+
if (b === null)
|
57
|
+
return -1;
|
58
|
+
return a - b;
|
59
|
+
},
|
60
|
+
// 9 -> 0
|
61
|
+
desc: (a, b) => {
|
62
|
+
if (a === b)
|
63
|
+
return 0;
|
64
|
+
if (a === undefined)
|
65
|
+
return 1;
|
66
|
+
if (b === undefined)
|
67
|
+
return -1;
|
68
|
+
if (a === null)
|
69
|
+
return 1;
|
70
|
+
if (b === null)
|
71
|
+
return -1;
|
72
|
+
return b - a;
|
73
|
+
},
|
74
|
+
},
|
75
|
+
boolean: {
|
76
|
+
// false -> true
|
77
|
+
asc: (a, b) => {
|
78
|
+
if (a === b)
|
79
|
+
return 0;
|
80
|
+
if (a === undefined)
|
81
|
+
return 1;
|
82
|
+
if (b === undefined)
|
83
|
+
return -1;
|
84
|
+
if (a === null)
|
85
|
+
return 1;
|
86
|
+
if (b === null)
|
87
|
+
return -1;
|
88
|
+
return (a ? 1 : 0) - (b ? 1 : 0);
|
89
|
+
},
|
90
|
+
// true -> false
|
91
|
+
desc: (a, b) => {
|
92
|
+
if (a === b)
|
93
|
+
return 0;
|
94
|
+
if (a === undefined)
|
95
|
+
return 1;
|
96
|
+
if (b === undefined)
|
97
|
+
return -1;
|
98
|
+
if (a === null)
|
99
|
+
return 1;
|
100
|
+
if (b === null)
|
101
|
+
return -1;
|
102
|
+
return (b ? 1 : 0) - (a ? 1 : 0);
|
103
|
+
},
|
104
|
+
},
|
105
|
+
};
|
package/hooks.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { LocalDB } from './LocalDB.js';
|
2
|
-
import type { IDocument } from './types.js';
|
3
|
-
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
4
|
-
export declare const useLocalDbDoc: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, id: string, deps?: any[]) => T[K] | undefined;
|
5
|
-
export declare const useLocalDbDocs: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, ids: string[] | null | undefined, deps?: any[]) => (T[K] | undefined)[];
|
1
|
+
import type { LocalDB } from './LocalDB.js';
|
2
|
+
import type { IDocument } from './types.js';
|
3
|
+
import type { KeyOf } from 'util-3gcvv/types/types.js';
|
4
|
+
export declare const useLocalDbDoc: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, id: string, deps?: any[]) => T[K] | undefined;
|
5
|
+
export declare const useLocalDbDocs: <T extends Record<string, IDocument>, K extends KeyOf<T>>(db: LocalDB<T>, col: K, ids: string[] | null | undefined, deps?: any[]) => (T[K] | undefined)[];
|
package/hooks.js
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
import { useEffect, useRef, useState } from 'react';
|
2
|
-
import { arraysEqual } from 'util-3gcvv/array.js';
|
3
|
-
export const useLocalDbDoc = (db, col, id, deps = [db, col, id]) => {
|
4
|
-
const [doc, setDoc] = useState(() => db.doc(col, id));
|
5
|
-
useEffect(() => {
|
6
|
-
setDoc(db.doc(col, id));
|
7
|
-
return db.subToDoc(col, id, setDoc);
|
8
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
9
|
-
}, deps);
|
10
|
-
return doc;
|
11
|
-
};
|
12
|
-
export const useLocalDbDocs = (db, col, ids, deps = [db, col, ids]) => {
|
13
|
-
const [docs, setDocs] = useState(() => ids?.length ? db.docs(col, ids) : []);
|
14
|
-
const docsRef = useRef(docs);
|
15
|
-
useEffect(() => {
|
16
|
-
const newDocs = ids?.length ? db.docs(col, ids) : [];
|
17
|
-
if (!arraysEqual(docsRef.current, newDocs)) {
|
18
|
-
docsRef.current = newDocs;
|
19
|
-
setDocs(newDocs);
|
20
|
-
}
|
21
|
-
if (!ids?.length)
|
22
|
-
return;
|
23
|
-
return db.subToCol(col, (next, prev, change) => {
|
24
|
-
for (let i = 0, il = ids.length; i < il; i += 1) {
|
25
|
-
if (ids[i] in change) {
|
26
|
-
docsRef.current = ids.map((id) => next[id]);
|
27
|
-
return setDocs(docsRef.current);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
});
|
31
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
32
|
-
}, deps);
|
33
|
-
return docs;
|
34
|
-
};
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
2
|
+
import { arraysEqual } from 'util-3gcvv/array.js';
|
3
|
+
export const useLocalDbDoc = (db, col, id, deps = [db, col, id]) => {
|
4
|
+
const [doc, setDoc] = useState(() => db.doc(col, id));
|
5
|
+
useEffect(() => {
|
6
|
+
setDoc(db.doc(col, id));
|
7
|
+
return db.subToDoc(col, id, setDoc);
|
8
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
9
|
+
}, deps);
|
10
|
+
return doc;
|
11
|
+
};
|
12
|
+
export const useLocalDbDocs = (db, col, ids, deps = [db, col, ids]) => {
|
13
|
+
const [docs, setDocs] = useState(() => ids?.length ? db.docs(col, ids) : []);
|
14
|
+
const docsRef = useRef(docs);
|
15
|
+
useEffect(() => {
|
16
|
+
const newDocs = ids?.length ? db.docs(col, ids) : [];
|
17
|
+
if (!arraysEqual(docsRef.current, newDocs)) {
|
18
|
+
docsRef.current = newDocs;
|
19
|
+
setDocs(newDocs);
|
20
|
+
}
|
21
|
+
if (!ids?.length)
|
22
|
+
return;
|
23
|
+
return db.subToCol(col, (next, prev, change) => {
|
24
|
+
for (let i = 0, il = ids.length; i < il; i += 1) {
|
25
|
+
if (ids[i] in change) {
|
26
|
+
docsRef.current = ids.map((id) => next[id]);
|
27
|
+
return setDocs(docsRef.current);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
});
|
31
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
32
|
+
}, deps);
|
33
|
+
return docs;
|
34
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "localdb-ces6q",
|
3
|
-
"version": "0.0
|
3
|
+
"version": "0.2.0",
|
4
4
|
"description": "TypeScript ECMAScript Module Library Template",
|
5
5
|
"type": "module",
|
6
6
|
"module": "dist/index.js",
|
@@ -26,7 +26,7 @@
|
|
26
26
|
},
|
27
27
|
"homepage": "https://github.com/joonhocho/localdb#readme",
|
28
28
|
"dependencies": {
|
29
|
-
"bplustree-mq4uj": "^0.0
|
29
|
+
"bplustree-mq4uj": "^0.1.0",
|
30
30
|
"util-3gcvv": "^0.4.7"
|
31
31
|
},
|
32
32
|
"devDependencies": {
|
@@ -34,14 +34,14 @@
|
|
34
34
|
"@babel/preset-env": "^7.20.2",
|
35
35
|
"@babel/preset-typescript": "^7.21.0",
|
36
36
|
"@jest/globals": "^29.5.0",
|
37
|
-
"@types/jest": "^29.
|
37
|
+
"@types/jest": "^29.5.0",
|
38
38
|
"@types/react": "^18.0.28",
|
39
39
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
40
40
|
"prettier": "^2.8.4",
|
41
41
|
"react": "^18.2.0",
|
42
42
|
"shelljs": "^0.8.5",
|
43
43
|
"ts-jest": "^29.0.5",
|
44
|
-
"typescript": "^
|
44
|
+
"typescript": "^5.0.2"
|
45
45
|
},
|
46
46
|
"publishConfig": {
|
47
47
|
"access": "public"
|