@v1nt1248/3nclient-lib 0.0.13 → 0.0.15

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.
Files changed (28) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/libs/index.d.ts +3 -4
  3. package/dist/libs/ipc-service-caller.d.ts +2 -3
  4. package/dist/libs/ipc-service.d.ts +5 -6
  5. package/dist/libs/serialization-for-ipc/copy-json.d.ts +8 -0
  6. package/dist/libs/serialization-for-ipc/protobuf-type.d.ts +10 -0
  7. package/dist/libs/sqlite-on-3nstorage/deferred.d.ts +6 -0
  8. package/dist/libs/sqlite-on-3nstorage/index.d.ts +6 -6
  9. package/dist/libs/sqlite-on-3nstorage/synced.d.ts +1 -1
  10. package/dist/ui-3n-lib.js +11897 -7953
  11. package/package.json +8 -5
  12. package/src/libs/index.ts +11 -0
  13. package/src/libs/ipc-service-caller.ts +121 -0
  14. package/src/libs/ipc-service.ts +422 -0
  15. package/src/libs/serialization-for-ipc/copy-json.ts +55 -0
  16. package/src/libs/serialization-for-ipc/json-ipc.proto.d.ts +432 -0
  17. package/src/libs/serialization-for-ipc/json-ipc.proto.js +1093 -0
  18. package/src/libs/serialization-for-ipc/json-n-binary.ts +275 -0
  19. package/src/libs/serialization-for-ipc/protobuf-type.ts +61 -0
  20. package/src/libs/sqlite-on-3nstorage/deferred.ts +32 -0
  21. package/src/libs/sqlite-on-3nstorage/index.ts +291 -0
  22. package/{dist → src}/libs/sqlite-on-3nstorage/sqljs.d.ts +26 -26
  23. package/{dist/libs/sqlite-on-3nstorage/index.js → src/libs/sqlite-on-3nstorage/sqljs.js} +97 -2680
  24. package/src/libs/sqlite-on-3nstorage/synced.ts +194 -0
  25. package/dist/libs/index.js +0 -4
  26. package/dist/libs/ipc-service-caller.js +0 -6129
  27. package/dist/libs/ipc-service.js +0 -6337
  28. package/dist/libs/serialization-for-ipc/json-n-binary.js +0 -6040
@@ -0,0 +1,194 @@
1
+ /*
2
+ Copyright (C) 2015, 2017, 2019 - 2021 3NSoft Inc.
3
+
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU General Public License as published by the Free Software
6
+ Foundation, either version 3 of the License, or (at your option) any later
7
+ version.
8
+
9
+ This program is distributed in the hope that it will be useful, but
10
+ WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
+ See the GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License along with
15
+ this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { defer, Deferred } from "./deferred.js";
19
+
20
+ /**
21
+ * This represents a function that will create a promise, potentially starting
22
+ * some background process, only when it is called. Such wrap of code is needed
23
+ * for scheduling, as very often any start of an action must be postponed till
24
+ * later time. Scheduler needs a not-yet-started activity, as scheduler has
25
+ * control action's start.
26
+ */
27
+ export type Action<T> = () => Promise<T>;
28
+
29
+ /**
30
+ * This is a container of processes, labeled by some ids. It allows to track if
31
+ * there is a process already in progress with a given id. And, it allows to
32
+ * chain process, when needed.
33
+ *
34
+ * Common use of such class is to reuse getting of some expensive resource(s).
35
+ */
36
+ export class NamedProcs {
37
+
38
+ private processes = new Map<string, SingleProc>();
39
+
40
+ constructor() {
41
+ Object.freeze(this);
42
+ }
43
+
44
+ /**
45
+ * @param id is a string key of a process
46
+ * @return a promise of a process' completion, or undefined, if process with
47
+ * given id is unknown.
48
+ */
49
+ latestTaskAtThisMoment<T>(id: string): Promise<T> | undefined {
50
+ const proc = this.processes.get(id);
51
+ return (proc ? proc.latestTaskAtThisMoment() : undefined);
52
+ }
53
+
54
+ isProcessing(id: string): boolean {
55
+ const proc = this.processes.get(id);
56
+ return (proc ? proc.isProcessing() : false);
57
+ }
58
+
59
+ /**
60
+ * This method will add a promise of an already started process.
61
+ * @param id is a string key of a process
62
+ * @param promise of an already started process
63
+ * @return a promise of a process' completion.
64
+ */
65
+ addStarted<T>(id: string, promise: Promise<T>): Promise<T> {
66
+ if (this.isProcessing(id)) { throw new Error(
67
+ 'Process with id "'+id+'" is already in progress.'); }
68
+ return this.startOrChain(id, () => promise);
69
+ }
70
+
71
+ /**
72
+ * This method will start a given action only, if a process with a given id
73
+ * is not running.
74
+ * @param id is a string key of a process
75
+ * @param action is a function that starts some process
76
+ * @return a promise of a process' completion.
77
+ */
78
+ start<T>(id: string, action: Action<T>): Promise<T> {
79
+ if (this.isProcessing(id)) { throw new Error(
80
+ 'Process with id "'+id+'" is already in progress.'); }
81
+ return this.startOrChain(id, action);
82
+ }
83
+
84
+ /**
85
+ * This method will chain a given action to an already running process, or,
86
+ * if identified process is not running, this will start given action under
87
+ * a given id.
88
+ * @param id is a string key of a process
89
+ * @param action is a function that starts some process
90
+ * @return a promise of a process' completion.
91
+ */
92
+ startOrChain<T>(id: string, action: Action<T>): Promise<T> {
93
+ let proc = this.processes.get(id);
94
+ if (!proc) {
95
+ proc = new SingleProc(() => this.processes.delete(id));
96
+ this.processes.set(id, proc);
97
+ }
98
+ return proc.startOrChain(action);
99
+ }
100
+
101
+ }
102
+ Object.freeze(NamedProcs.prototype);
103
+ Object.freeze(NamedProcs);
104
+
105
+
106
+ /**
107
+ * This is a container of process. It allows to track if a process is already
108
+ * in progress. It also allows to chain process, when needed.
109
+ *
110
+ * Common use of such class is to reuse getting of some expensive resource, or
111
+ * do ning something as an exclusive process.
112
+ */
113
+ export class SingleProc {
114
+
115
+ private actions: {
116
+ action: Action<any>;
117
+ deferred: Deferred<any>;
118
+ }[] = [];
119
+ private running = false;
120
+
121
+ constructor(
122
+ private onGoingIdle?: () => void
123
+ ) {
124
+ Object.seal(this);
125
+ }
126
+
127
+ isProcessing(): boolean {
128
+ return (this.actions.length > 0);
129
+ }
130
+
131
+ latestTaskAtThisMoment<T>(): Promise<T>|undefined {
132
+ return ((this.actions.length === 0) ?
133
+ undefined :
134
+ this.actions[this.actions.length-1].deferred.promise);
135
+ }
136
+
137
+ addStarted<T>(promise: Promise<T>): Promise<T> {
138
+ if (this.isProcessing()) { throw new Error(
139
+ 'Process is already in progress.'); }
140
+ return this.startOrChain(() => promise);
141
+ }
142
+
143
+ start<T>(action: Action<T>): Promise<T> {
144
+ if (this.isProcessing()) { throw new Error(
145
+ 'Process is already in progress.'); }
146
+ return this.startOrChain(action);
147
+ }
148
+
149
+ private async runIfIdle(): Promise<void> {
150
+ if (this.running) { return; }
151
+ this.running = true;
152
+ while (this.actions.length > 0) {
153
+ const { action, deferred } = this.actions[0];
154
+ try {
155
+ const res = await action();
156
+ deferred.resolve(res);
157
+ } catch (err) {
158
+ deferred.reject(err);
159
+ }
160
+ this.actions.shift();
161
+ }
162
+ this.running = false;
163
+ // paranoid check after seeing LiquidCore 0.7.10 on iOS
164
+ if (this.actions.length > 0) {
165
+ return this.runIfIdle();
166
+ }
167
+ // when listener is used, like with NamedPcos
168
+ if (this.onGoingIdle) {
169
+ this.onGoingIdle();
170
+ }
171
+ }
172
+
173
+ startOrChain<T>(action: Action<T>): Promise<T> {
174
+ const deferred = defer<T>();
175
+ this.actions.push({ action, deferred });
176
+ this.runIfIdle();
177
+ return deferred.promise;
178
+ }
179
+
180
+ }
181
+ Object.freeze(SingleProc.prototype);
182
+ Object.freeze(SingleProc);
183
+
184
+
185
+ /**
186
+ * This wraps given function/method into syncing wrap.
187
+ */
188
+ export function makeSyncedFunc<T extends Function>(
189
+ syncProc: SingleProc, thisArg: any, func: T
190
+ ): T {
191
+ return ((
192
+ ...args: any[]
193
+ ) => syncProc.startOrChain(() => func.apply(thisArg, args))) as any as T;
194
+ }
@@ -1,4 +0,0 @@
1
- export * from './ipc-service'
2
- export * from './ipc-service-caller'
3
- export * from './serialization-for-ipc/json-n-binary'
4
- export * from './sqlite-on-3nstorage/index'