@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 CHANGED
@@ -1 +1,6 @@
1
- # rxdb-plugin-workspace
1
+ # rxdb-plugin-workspace(未完成)
2
+
3
+ 把创建的所有对象暂存到 indexedDB 中,方便后续恢复。 帮助用户管理暂存的对象,包括恢复和删除。
4
+
5
+ 1. 如果这个对象保存了,就从 indexedDB 删除;
6
+ 2. 如果这个对象没有保存,用户可以选择恢复这个对象,或者删除它。
@@ -1,34 +1,21 @@
1
- import { EntityType, RxDB, UUID, IRxDBPlugin, Plugin, RxDBEntityLocalCreatedEventData, RxDBEntityLocalEventData, RxDBPluginBase } from '../packages/rxdb/src/index.ts';
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
- constructor(rxdb: RxDB, _options?: RxDBPluginWorkspaceOptions);
18
- install(): void;
8
+ get stagedCount(): number;
9
+ get cacheCount(): number;
10
+ constructor(rxdb: RxDB);
11
+ install(): Promise<void>;
19
12
  destroy(): void;
20
- /**
21
- * 添加实体到暂存区
22
- */
23
- stagedChange<_T extends EntityType>(entity: RxDBEntityLocalCreatedEventData): void;
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,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAOL,WAAW,EACX,MAAM,EACN,+BAA+B,EAC/B,wBAAwB,EACxB,cAAc,EACf,MAAM,YAAY,CAAC;AAIpB,UAAU,0BAA0B;IAClC;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AACH,qBAAa,mBAAoB,SAAQ,cAAe,YAAW,WAAW;;IAiB5E,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAe;gBAGtC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,EAAE,0BAA0B;IA6B7D,OAAO;IAIP,OAAO;IAIP;;OAEG;IAEH,YAAY,CAAC,EAAE,SAAS,UAAU,EAAE,MAAM,EAAE,+BAA+B;IAO3E;;OAEG;IAEH,aAAa,CAAC,EAAE,SAAS,UAAU,EAAE,MAAM,EAAE,+BAA+B;IAK5E;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM;CAwDnB;AAED,eAAO,MAAM,mBAAmB,EAAE,MAAM,CAAC,0BAA0B,CAKlE,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"}
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 o, ENTITY_LOCAL_NEW_EVENT as h, ENTITY_LOCAL_REMOVE_EVENT as r, ENTITY_LOCAL_CREATE_EVENT as d } from "@aiao/rxdb";
2
- import { createStore as _, entries as l, setMany as E, delMany as m } from "idb-keyval";
3
- import { Subject as p, debounceTime as b } from "rxjs";
4
- class f extends o {
5
- #s;
6
- #i = new p();
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
- // 暂存区 ids
14
- #h = /* @__PURE__ */ new Set();
15
- #r = !1;
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
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
- constructor(e, t) {
19
- super(e), this.rxdb.addEventListener(h, this.#d), this.rxdb.addEventListener(r, this.#d), this.rxdb.addEventListener(d, this.#d);
20
- const n = this.rxdb.config.dbName;
21
- this.#s = _(n, this.name), l(this.#s).then(
22
- (s) => (
23
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
- s.forEach(([u, c]) => c)
25
- )
26
- ), this.#i.asObservable().pipe(b(0)).subscribe(() => this.#o());
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 = a(e);
38
- this.#a.has(t) && this.#h.add(t);
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 = a(e);
46
- this.#h.delete(t);
43
+ const t = l(e);
44
+ this.#t.delete(t);
47
45
  }
48
- /**
49
- * 提交变化
50
- */
51
- commit(e) {
52
- this.#h.size > 0 && console.log("commit", e);
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
- #d = (e) => {
58
+ #r = (e) => {
55
59
  const { entities: t } = e;
56
- t.forEach((n) => {
57
- const s = a(n);
60
+ t.forEach((s) => {
61
+ const i = l(s);
58
62
  switch (e.type) {
59
- case h:
60
- this.#n.add(s), this.#a.set(s, e), this.#t.set(s, e), this.#i.next();
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 d:
63
- this.#n.add(s), this.#e.add(s), this.#i.next();
66
+ case o:
67
+ this.#s.add(i), this.#e.add(i), this.#n.next();
64
68
  break;
65
- case r:
66
- this.#c(s);
69
+ case d:
70
+ this.#o(i);
67
71
  break;
68
72
  }
69
73
  });
70
74
  };
71
- #c(e) {
72
- this.#n.delete(e), this.#a.delete(e), this.#e.add(e), this.#i.next();
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 #o() {
75
- if (!(this.#r || this.#t.size === 0 && this.#e.size === 0)) {
76
- if (this.#r = !0, this.#t.size) {
77
- const e = Array.from(this.#t.entries()).map(([t, n]) => [
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
- { ...n }
83
+ { ...s }
80
84
  ]);
81
- await E(e, this.#s), this.#t.clear();
85
+ await b(e, this.#h), this.#a.clear();
82
86
  }
83
- this.#e.size && (await m(Array.from(this.#e), this.#s), this.#e.clear()), this.#r = !1;
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 = (i, e) => new f(i, e), a = (i) => i.namespace + ":" + i.entity + ":" + i.id;
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.9",
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.9"
33
+ "@aiao/rxdb": "0.0.11"
34
34
  }
35
35
  }