@aiao/rxdb-plugin-workspace 0.0.9 → 0.0.11
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/README.md +6 -1
- package/dist/RxDBPluginWorkspace.d.ts +11 -24
- package/dist/RxDBPluginWorkspace.d.ts.map +1 -1
- package/dist/index.js +65 -61
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,34 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RxDB, UUID, IRxDBPlugin, Plugin, RxDBEntityLocalCreatedEventData, RxDBEntityLocalEventData, RxDBPluginBase } from '../packages/rxdb/src/index.ts';
|
|
2
2
|
interface RxDBPluginWorkspaceOptions {
|
|
3
|
-
/**
|
|
4
|
-
* 自动保存
|
|
5
|
-
*/
|
|
6
3
|
autoSave?: boolean;
|
|
7
4
|
}
|
|
8
|
-
/**
|
|
9
|
-
* 工作区
|
|
10
|
-
* 所有实体会暂存 IndexedDB
|
|
11
|
-
* 1,当前只是把 new 的实体保存到 IndexedDB, remove 的实体从 IndexedDB 删除.
|
|
12
|
-
* 2. 用户关闭浏览器后重新打开时,会从 IndexedDB 读取实体数据到内存
|
|
13
|
-
*/
|
|
14
5
|
export declare class RxDBPluginWorkspace extends RxDBPluginBase implements IRxDBPlugin {
|
|
15
6
|
#private;
|
|
16
7
|
readonly name: Uncapitalize<string>;
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
get stagedCount(): number;
|
|
9
|
+
get cacheCount(): number;
|
|
10
|
+
constructor(rxdb: RxDB);
|
|
11
|
+
install(): Promise<void>;
|
|
19
12
|
destroy(): void;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*/
|
|
27
|
-
unstageChange<_T extends EntityType>(entity: RxDBEntityLocalCreatedEventData): void;
|
|
28
|
-
/**
|
|
29
|
-
* 提交变化
|
|
30
|
-
*/
|
|
31
|
-
commit(msg: string): void;
|
|
13
|
+
stagedChange(entity: RxDBEntityLocalCreatedEventData): void;
|
|
14
|
+
unstageChange(entity: RxDBEntityLocalCreatedEventData): void;
|
|
15
|
+
commit(msg: string): Promise<{
|
|
16
|
+
message: string;
|
|
17
|
+
count: number;
|
|
18
|
+
} | undefined>;
|
|
32
19
|
}
|
|
33
20
|
export declare const rxDBPluginWorkspace: Plugin<RxDBPluginWorkspaceOptions>;
|
|
34
21
|
declare module '../packages/rxdb/src/index.ts' {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RxDBPluginWorkspace.d.ts","sourceRoot":"","sources":["../src/RxDBPluginWorkspace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"RxDBPluginWorkspace.d.ts","sourceRoot":"","sources":["../src/RxDBPluginWorkspace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAOL,WAAW,EACX,MAAM,EACN,+BAA+B,EAC/B,wBAAwB,EACxB,cAAc,EACf,MAAM,YAAY,CAAC;AAIpB,UAAU,0BAA0B;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,qBAAa,mBAAoB,SAAQ,cAAe,YAAW,WAAW;;IAe5E,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAe;IAElD,IAAI,WAAW,WAEd;IAED,IAAI,UAAU,WAEb;gBAEW,IAAI,EAAE,IAAI;IAehB,OAAO;IAcb,OAAO;IAaP,YAAY,CAAC,MAAM,EAAE,+BAA+B;IAOpD,aAAa,CAAC,MAAM,EAAE,+BAA+B;IAK/C,MAAM,CAAC,GAAG,EAAE,MAAM;;;;CA4EzB;AAED,eAAO,MAAM,mBAAmB,EAAE,MAAM,CAAC,0BAA0B,CAElE,CAAC;AAEF,OAAO,QAAQ,YAAY,CAAC;IAC1B,UAAU,IAAI;QACZ,SAAS,EAAE,mBAAmB,CAAC;KAChC;CACF;AAED,KAAK,OAAO,GAAG,GAAG,MAAM,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;AAE7C,eAAO,MAAM,YAAY,GAAI,WAAW,wBAAwB,KACS,OAAO,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,90 +1,94 @@
|
|
|
1
|
-
import { RxDBPluginBase as
|
|
2
|
-
import { createStore as
|
|
3
|
-
import { Subject as
|
|
4
|
-
class
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
#t = /* @__PURE__ */ new Map();
|
|
9
|
-
#e = /* @__PURE__ */ new Set();
|
|
10
|
-
// 工作区所有实体
|
|
11
|
-
#n = /* @__PURE__ */ new Set();
|
|
1
|
+
import { RxDBPluginBase as m, ENTITY_LOCAL_NEW_EVENT as c, ENTITY_LOCAL_REMOVE_EVENT as d, ENTITY_LOCAL_CREATE_EVENT as o } from "@aiao/rxdb";
|
|
2
|
+
import { createStore as p, entries as E, setMany as b, delMany as f } from "idb-keyval";
|
|
3
|
+
import { Subject as u, debounceTime as y } from "rxjs";
|
|
4
|
+
class g extends m {
|
|
5
|
+
#h;
|
|
6
|
+
#n = new u();
|
|
7
|
+
#d;
|
|
12
8
|
#a = /* @__PURE__ */ new Map();
|
|
13
|
-
|
|
14
|
-
#
|
|
15
|
-
#
|
|
9
|
+
#e = /* @__PURE__ */ new Set();
|
|
10
|
+
#s = /* @__PURE__ */ new Set();
|
|
11
|
+
#i = /* @__PURE__ */ new Map();
|
|
12
|
+
#t = /* @__PURE__ */ new Set();
|
|
13
|
+
#c = !1;
|
|
16
14
|
name = "workspace";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
), this.#
|
|
15
|
+
get stagedCount() {
|
|
16
|
+
return this.#t.size;
|
|
17
|
+
}
|
|
18
|
+
get cacheCount() {
|
|
19
|
+
return this.#s.size;
|
|
20
|
+
}
|
|
21
|
+
constructor(e) {
|
|
22
|
+
super(e), this.rxdb.addEventListener(c, this.#r), this.rxdb.addEventListener(d, this.#r), this.rxdb.addEventListener(o, this.#r);
|
|
23
|
+
const t = this.rxdb.config.dbName;
|
|
24
|
+
this.#h = p(t, this.name), this.#d = this.#n.asObservable().pipe(y(0)).subscribe(() => this.#l());
|
|
27
25
|
}
|
|
28
|
-
install() {
|
|
26
|
+
async install() {
|
|
27
|
+
const e = await E(this.#h);
|
|
28
|
+
for (const [t, s] of e) {
|
|
29
|
+
const [i, h] = t.split(":"), r = this.rxdb.schemaManager.getEntityType(h, i);
|
|
30
|
+
r && this.rxdb.entityManager.createEntityRef(r, s, { local: !1, remote: !1 });
|
|
31
|
+
const a = t;
|
|
32
|
+
this.#s.add(a), this.#i.set(a, s);
|
|
33
|
+
}
|
|
29
34
|
}
|
|
30
35
|
destroy() {
|
|
36
|
+
this.rxdb.removeEventListener(c, this.#r), this.rxdb.removeEventListener(d, this.#r), this.rxdb.removeEventListener(o, this.#r), this.#d?.unsubscribe(), this.#n.complete(), this.#s.clear(), this.#i.clear(), this.#t.clear(), this.#a.clear(), this.#e.clear();
|
|
31
37
|
}
|
|
32
|
-
/**
|
|
33
|
-
* 添加实体到暂存区
|
|
34
|
-
*/
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
38
|
stagedChange(e) {
|
|
37
|
-
const t =
|
|
38
|
-
this.#
|
|
39
|
+
const t = l(e);
|
|
40
|
+
this.#i.has(t) && this.#t.add(t);
|
|
39
41
|
}
|
|
40
|
-
/**
|
|
41
|
-
* 从暂存区移除实体
|
|
42
|
-
*/
|
|
43
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
44
42
|
unstageChange(e) {
|
|
45
|
-
const t =
|
|
46
|
-
this.#
|
|
43
|
+
const t = l(e);
|
|
44
|
+
this.#t.delete(t);
|
|
47
45
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
async commit(e) {
|
|
47
|
+
if (this.#t.size === 0) return;
|
|
48
|
+
const t = new Set(this.#t);
|
|
49
|
+
this.#t.clear();
|
|
50
|
+
for (const s of t) {
|
|
51
|
+
const i = this.#i.get(s);
|
|
52
|
+
if (!i) continue;
|
|
53
|
+
const [h, r] = s.split(":"), a = this.rxdb.schemaManager.getEntityType(r, h);
|
|
54
|
+
a && this.rxdb.entityManager.createEntityRef(a, i), this.#s.delete(s), this.#i.delete(s), this.#e.add(s);
|
|
55
|
+
}
|
|
56
|
+
return this.#n.next(), { message: e, count: t.size };
|
|
53
57
|
}
|
|
54
|
-
#
|
|
58
|
+
#r = (e) => {
|
|
55
59
|
const { entities: t } = e;
|
|
56
|
-
t.forEach((
|
|
57
|
-
const
|
|
60
|
+
t.forEach((s) => {
|
|
61
|
+
const i = l(s);
|
|
58
62
|
switch (e.type) {
|
|
59
|
-
case
|
|
60
|
-
this.#
|
|
63
|
+
case c:
|
|
64
|
+
this.#s.add(i), this.#i.set(i, s.patch), this.#a.set(i, s.patch), this.#n.next();
|
|
61
65
|
break;
|
|
62
|
-
case
|
|
63
|
-
this.#
|
|
66
|
+
case o:
|
|
67
|
+
this.#s.add(i), this.#e.add(i), this.#n.next();
|
|
64
68
|
break;
|
|
65
|
-
case
|
|
66
|
-
this.#
|
|
69
|
+
case d:
|
|
70
|
+
this.#o(i);
|
|
67
71
|
break;
|
|
68
72
|
}
|
|
69
73
|
});
|
|
70
74
|
};
|
|
71
|
-
#
|
|
72
|
-
this.#
|
|
75
|
+
#o(e) {
|
|
76
|
+
this.#s.delete(e), this.#i.delete(e), this.#t.delete(e), this.#e.add(e), this.#n.next();
|
|
73
77
|
}
|
|
74
|
-
async #
|
|
75
|
-
if (!(this.#
|
|
76
|
-
if (this.#
|
|
77
|
-
const e = Array.from(this.#
|
|
78
|
+
async #l() {
|
|
79
|
+
if (!(this.#c || this.#a.size === 0 && this.#e.size === 0)) {
|
|
80
|
+
if (this.#c = !0, this.#a.size) {
|
|
81
|
+
const e = Array.from(this.#a.entries()).map(([t, s]) => [
|
|
78
82
|
t,
|
|
79
|
-
{ ...
|
|
83
|
+
{ ...s }
|
|
80
84
|
]);
|
|
81
|
-
await
|
|
85
|
+
await b(e, this.#h), this.#a.clear();
|
|
82
86
|
}
|
|
83
|
-
this.#e.size && (await
|
|
87
|
+
this.#e.size && (await f(Array.from(this.#e), this.#h), this.#e.clear()), this.#c = !1;
|
|
84
88
|
}
|
|
85
89
|
}
|
|
86
90
|
}
|
|
87
|
-
const w = (
|
|
91
|
+
const w = (n) => new g(n), l = (n) => n.namespace + ":" + n.entity + ":" + n.id;
|
|
88
92
|
export {
|
|
89
93
|
w as rxDBPluginWorkspace
|
|
90
94
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiao/rxdb-plugin-workspace",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -30,6 +30,6 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"rxjs": "^7.8.2",
|
|
32
32
|
"idb-keyval": "^6.2.2",
|
|
33
|
-
"@aiao/rxdb": "0.0.
|
|
33
|
+
"@aiao/rxdb": "0.0.11"
|
|
34
34
|
}
|
|
35
35
|
}
|