@aiao/rxdb-plugin-workspace 0.0.1

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 ADDED
@@ -0,0 +1 @@
1
+ # rxdb-plugin-workspace
@@ -0,0 +1,37 @@
1
+ import { EntityType, RxDB, IRxDBPlugin, Plugin, RxDBPluginBase } from '@aiao/rxdb';
2
+ interface RxDBPluginWorkspaceOptions {
3
+ /**
4
+ * 自动保存
5
+ */
6
+ autoSave?: boolean;
7
+ }
8
+ /**
9
+ * 工作区
10
+ * 所有实体会暂存 IndexedDB
11
+ */
12
+ export declare class RxDBPluginWorkspace extends RxDBPluginBase implements IRxDBPlugin {
13
+ #private;
14
+ readonly name: Uncapitalize<string>;
15
+ constructor(rxdb: RxDB, options?: RxDBPluginWorkspaceOptions);
16
+ install(): Promise<void>;
17
+ destroy(): Promise<void>;
18
+ /**
19
+ * 添加实体到暂存区
20
+ */
21
+ stagedChange<T extends EntityType>(entity: InstanceType<T>): void;
22
+ /**
23
+ * 从暂存区移除实体
24
+ */
25
+ unstageChange<T extends EntityType>(entity: InstanceType<T>): void;
26
+ /**
27
+ * 提交变化
28
+ */
29
+ commit(msg: string): void;
30
+ }
31
+ export declare const rxDBPluginWorkspace: Plugin<RxDBPluginWorkspaceOptions>;
32
+ declare module '@aiao/rxdb' {
33
+ interface RxDB {
34
+ workspace: RxDBPluginWorkspace;
35
+ }
36
+ }
37
+ export {};
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { rxDBPluginWorkspace } from './RxDBPluginWorkspace';
package/index.js ADDED
@@ -0,0 +1,86 @@
1
+ import { RxDBPluginBase as _, ENTITY_NEW_EVENT as d, ENTITY_REMOVE_SUCCESS_EVENT as o, ENTITY_CREATE_SUCCESS_EVENT as c, ENTITY_PATCH_EVENT as E, getEntityMetadata as n } from "@aiao/rxdb";
2
+ import { createStore as b, entries as f, setMany as x, delMany as u } from "idb-keyval";
3
+ import { Subject as g, debounceTime as N } from "rxjs";
4
+ class y extends _ {
5
+ #s;
6
+ #i = new g();
7
+ // 需要保存的实体
8
+ #t = /* @__PURE__ */ new Map();
9
+ #e = /* @__PURE__ */ new Set();
10
+ // 工作区所有实体
11
+ #n = /* @__PURE__ */ new Set();
12
+ #r = /* @__PURE__ */ new Map();
13
+ // 暂存区 ids
14
+ #h = /* @__PURE__ */ new Set();
15
+ #d = !1;
16
+ name = "workspace";
17
+ constructor(e, t) {
18
+ super(e), this.rxdb.addEventListener(d, this.#a), this.rxdb.addEventListener(o, this.#a), this.rxdb.addEventListener(c, this.#a), this.rxdb.addEventListener(E, this.#a);
19
+ const a = this.rxdb.options.dbName;
20
+ this.#s = b(a, this.name), f(this.#s).then(
21
+ (s) => s.forEach(([l, m]) => {
22
+ const [p, T] = l.split(":"), h = this.rxdb.schema.getEntityType(T, p);
23
+ h && this.rxdb.em.createEntityRef(h, m, { local: !1, remote: !1 });
24
+ })
25
+ ), this.#i.asObservable().pipe(N(0)).subscribe(() => this.#c());
26
+ }
27
+ install() {
28
+ return Promise.resolve();
29
+ }
30
+ destroy() {
31
+ return Promise.resolve();
32
+ }
33
+ /**
34
+ * 添加实体到暂存区
35
+ */
36
+ stagedChange(e) {
37
+ const t = r(n(e), e);
38
+ this.#r.has(t) && this.#h.add(t);
39
+ }
40
+ /**
41
+ * 从暂存区移除实体
42
+ */
43
+ unstageChange(e) {
44
+ const t = r(n(e), e);
45
+ this.#h.delete(t);
46
+ }
47
+ /**
48
+ * 提交变化
49
+ */
50
+ commit(e) {
51
+ this.#h.size > 0 && console.log("commit", e);
52
+ }
53
+ #a = (e) => {
54
+ const { entity: t } = e, a = n(t), s = r(a, t);
55
+ switch (e.type) {
56
+ case d:
57
+ this.#n.add(s), this.#r.set(s, t), this.#t.set(s, t), this.#i.next();
58
+ break;
59
+ case E:
60
+ console.log("ENTITY_PATCH_EVENT", e);
61
+ break;
62
+ case c:
63
+ this.#n.add(s), this.#e.add(s), this.#i.next();
64
+ break;
65
+ case o:
66
+ this.#o(s);
67
+ break;
68
+ }
69
+ };
70
+ #o(e) {
71
+ this.#n.delete(e), this.#r.delete(e), this.#e.add(e), this.#i.next();
72
+ }
73
+ async #c() {
74
+ if (!(this.#d || this.#t.size === 0 && this.#e.size === 0)) {
75
+ if (this.#d = !0, this.#t.size) {
76
+ const e = this.#t.entries().map(([t, a]) => [t, { ...a }]);
77
+ await x(e, this.#s), this.#t.clear();
78
+ }
79
+ this.#e.size && (await u(Array.from(this.#e), this.#s), this.#e.clear()), this.#d = !1;
80
+ }
81
+ }
82
+ }
83
+ const k = (i, e) => new y(i, e), r = (i, e) => i.namespace + ":" + i.name + ":" + e.id;
84
+ export {
85
+ k as rxDBPluginWorkspace
86
+ };
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@aiao/rxdb-plugin-workspace",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "dependencies": {
7
+ "@aiao/rxdb": "*",
8
+ "rxjs": "~7.8.0",
9
+ "idb-keyval": "^6.2.2"
10
+ },
11
+ ".": {
12
+ "import": "./index.js",
13
+ "types": "./index.d.ts"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ }
18
+ }