@aiao/rxdb-plugin-workspace 0.0.5 → 0.0.7

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,4 +1,4 @@
1
- import { EntityType, RxDB, IRxDBPlugin, Plugin, RxDBPluginBase } from '@aiao/rxdb';
1
+ import { EntityType, RxDB, UUID, IRxDBPlugin, Plugin, RxDBEntityLocalCreatedEventData, RxDBEntityLocalEventData, RxDBPluginBase } from '../packages/rxdb/src/index.ts';
2
2
  interface RxDBPluginWorkspaceOptions {
3
3
  /**
4
4
  * 自动保存
@@ -8,31 +8,35 @@ interface RxDBPluginWorkspaceOptions {
8
8
  /**
9
9
  * 工作区
10
10
  * 所有实体会暂存 IndexedDB
11
+ * 1,当前只是把 new 的实体保存到 IndexedDB, remove 的实体从 IndexedDB 删除.
12
+ * 2. 用户关闭浏览器后重新打开时,会从 IndexedDB 读取实体数据到内存
11
13
  */
12
14
  export declare class RxDBPluginWorkspace extends RxDBPluginBase implements IRxDBPlugin {
13
15
  #private;
14
16
  readonly name: Uncapitalize<string>;
15
- constructor(rxdb: RxDB, options?: RxDBPluginWorkspaceOptions);
16
- install(): Promise<void>;
17
- destroy(): Promise<void>;
17
+ constructor(rxdb: RxDB, _options?: RxDBPluginWorkspaceOptions);
18
+ install(): void;
19
+ destroy(): void;
18
20
  /**
19
21
  * 添加实体到暂存区
20
22
  */
21
- stagedChange<T extends EntityType>(entity: InstanceType<T>): void;
23
+ stagedChange<_T extends EntityType>(entity: RxDBEntityLocalCreatedEventData): void;
22
24
  /**
23
25
  * 从暂存区移除实体
24
26
  */
25
- unstageChange<T extends EntityType>(entity: InstanceType<T>): void;
27
+ unstageChange<_T extends EntityType>(entity: RxDBEntityLocalCreatedEventData): void;
26
28
  /**
27
29
  * 提交变化
28
30
  */
29
31
  commit(msg: string): void;
30
32
  }
31
33
  export declare const rxDBPluginWorkspace: Plugin<RxDBPluginWorkspaceOptions>;
32
- declare module '@aiao/rxdb' {
34
+ declare module '../packages/rxdb/src/index.ts' {
33
35
  interface RxDB {
34
36
  workspace: RxDBPluginWorkspace;
35
37
  }
36
38
  }
39
+ type CacheId = `${string}:${string}:${UUID}`;
40
+ export declare const get_cache_id: (eventData: RxDBEntityLocalEventData) => CacheId;
37
41
  export {};
38
42
  //# sourceMappingURL=RxDBPluginWorkspace.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"RxDBPluginWorkspace.d.ts","sourceRoot":"","sources":["../src/RxDBPluginWorkspace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,UAAU,EAAW,IAAI,EAAyB,MAAM,YAAY,CAAC;AACnG,OAAO,EAKL,WAAW,EACX,MAAM,EACN,cAAc,EACf,MAAM,YAAY,CAAC;AAIpB,UAAU,0BAA0B;IAClC;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;GAGG;AACH,qBAAa,mBAAoB,SAAQ,cAAe,YAAW,WAAW;;IAiB5E,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAe;gBAEtC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,0BAA0B;IA0B5D,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB;;OAEG;IACH,YAAY,CAAC,CAAC,SAAS,UAAU,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IAO1D;;OAEG;IACH,aAAa,CAAC,CAAC,SAAS,UAAU,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IAK3D;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM;CAsDnB;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"}
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"}
package/dist/index.js CHANGED
@@ -1,86 +1,90 @@
1
- import { RxDBPluginBase as b, ENTITY_NEW_EVENT as d, ENTITY_REMOVE_EVENT as o, ENTITY_CREATE_EVENT as c, getEntityMetadata as n } from "@aiao/rxdb";
2
- import { createStore as f, entries as g, setMany as u, delMany as x } from "idb-keyval";
3
- import { Subject as _, debounceTime as y } from "rxjs";
4
- class T extends b {
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
5
  #s;
6
- #i = new _();
6
+ #i = new p();
7
7
  // 需要保存的实体
8
8
  #t = /* @__PURE__ */ new Map();
9
9
  #e = /* @__PURE__ */ new Set();
10
10
  // 工作区所有实体
11
- #a = /* @__PURE__ */ new Set();
12
- #n = /* @__PURE__ */ new Map();
11
+ #n = /* @__PURE__ */ new Set();
12
+ #a = /* @__PURE__ */ new Map();
13
13
  // 暂存区 ids
14
- #r = /* @__PURE__ */ new Set();
15
- #h = !1;
14
+ #h = /* @__PURE__ */ new Set();
15
+ #r = !1;
16
16
  name = "workspace";
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
18
  constructor(e, t) {
18
- super(e), this.rxdb.addEventListener(d, this.#d), this.rxdb.addEventListener(o, this.#d), this.rxdb.addEventListener(c, this.#d);
19
- const a = this.rxdb.options.dbName;
20
- this.#s = f(a, this.name), g(this.#s).then(
21
- (s) => s.forEach(([l, m]) => {
22
- const [E, p] = l.split(":"), h = this.rxdb.schemaManager.getEntityType(p, E);
23
- h && this.rxdb.entityManager.createEntityRef(h, m, { local: !1, remote: !1 });
24
- })
25
- ), this.#i.asObservable().pipe(y(0)).subscribe(() => this.#c());
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());
26
27
  }
27
28
  install() {
28
- return Promise.resolve();
29
29
  }
30
30
  destroy() {
31
- return Promise.resolve();
32
31
  }
33
32
  /**
34
33
  * 添加实体到暂存区
35
34
  */
35
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
36
  stagedChange(e) {
37
- const t = r(n(e), e);
38
- this.#n.has(t) && this.#r.add(t);
37
+ const t = a(e);
38
+ this.#a.has(t) && this.#h.add(t);
39
39
  }
40
40
  /**
41
41
  * 从暂存区移除实体
42
42
  */
43
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
44
  unstageChange(e) {
44
- const t = r(n(e), e);
45
- this.#r.delete(t);
45
+ const t = a(e);
46
+ this.#h.delete(t);
46
47
  }
47
48
  /**
48
49
  * 提交变化
49
50
  */
50
51
  commit(e) {
51
- this.#r.size > 0 && console.log("commit", e);
52
+ this.#h.size > 0 && console.log("commit", e);
52
53
  }
53
54
  #d = (e) => {
54
- const { entity: t } = e, a = n(t), s = r(a, t);
55
- switch (e.type) {
56
- case d:
57
- this.#a.add(s), this.#n.set(s, t), this.#t.set(s, t), this.#i.next();
58
- break;
59
- case c:
60
- this.#a.add(s), this.#e.add(s), this.#i.next();
61
- break;
62
- case o:
63
- this.#o(s);
64
- break;
65
- }
55
+ const { entities: t } = e;
56
+ t.forEach((n) => {
57
+ const s = a(n);
58
+ switch (e.type) {
59
+ case h:
60
+ this.#n.add(s), this.#a.set(s, e), this.#t.set(s, e), this.#i.next();
61
+ break;
62
+ case d:
63
+ this.#n.add(s), this.#e.add(s), this.#i.next();
64
+ break;
65
+ case r:
66
+ this.#c(s);
67
+ break;
68
+ }
69
+ });
66
70
  };
67
- #o(e) {
68
- this.#a.delete(e), this.#n.delete(e), this.#e.add(e), this.#i.next();
71
+ #c(e) {
72
+ this.#n.delete(e), this.#a.delete(e), this.#e.add(e), this.#i.next();
69
73
  }
70
- async #c() {
71
- if (!(this.#h || this.#t.size === 0 && this.#e.size === 0)) {
72
- if (this.#h = !0, this.#t.size) {
73
- const e = Array.from(this.#t.entries()).map(([t, a]) => [
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]) => [
74
78
  t,
75
- { ...a }
79
+ { ...n }
76
80
  ]);
77
- await u(e, this.#s), this.#t.clear();
81
+ await E(e, this.#s), this.#t.clear();
78
82
  }
79
- this.#e.size && (await x(Array.from(this.#e), this.#s), this.#e.clear()), this.#h = !1;
83
+ this.#e.size && (await m(Array.from(this.#e), this.#s), this.#e.clear()), this.#r = !1;
80
84
  }
81
85
  }
82
86
  }
83
- const v = (i, e) => new T(i, e), r = (i, e) => i.namespace + ":" + i.name + ":" + e.id;
87
+ const w = (i, e) => new f(i, e), a = (i) => i.namespace + ":" + i.entity + ":" + i.id;
84
88
  export {
85
- v as rxDBPluginWorkspace
89
+ w as rxDBPluginWorkspace
86
90
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiao/rxdb-plugin-workspace",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -27,6 +27,6 @@
27
27
  "dependencies": {
28
28
  "rxjs": "^7.8.2",
29
29
  "idb-keyval": "^6.2.2",
30
- "@aiao/rxdb": "0.0.5"
30
+ "@aiao/rxdb": "0.0.7"
31
31
  }
32
32
  }