tool-db 2.5.3 → 2.5.5

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 (183) hide show
  1. package/README.md +82 -82
  2. package/bundle.js +1 -1
  3. package/dist/adapters-base/networkAdapter.d.ts +58 -58
  4. package/dist/adapters-base/networkAdapter.js +215 -215
  5. package/dist/adapters-base/storageAdapter.d.ts +11 -11
  6. package/dist/adapters-base/storageAdapter.js +35 -35
  7. package/dist/adapters-base/userAdapter.d.ts +15 -15
  8. package/dist/adapters-base/userAdapter.js +41 -41
  9. package/dist/crdt/baseCrdt.d.ts +9 -9
  10. package/dist/crdt/baseCrdt.js +26 -26
  11. package/dist/crdt/counterCrdt.d.ts +30 -30
  12. package/dist/crdt/counterCrdt.js +105 -105
  13. package/dist/crdt/listCrdt.d.ts +42 -42
  14. package/dist/crdt/listCrdt.js +158 -158
  15. package/dist/crdt/mapCrdt.d.ts +32 -32
  16. package/dist/crdt/mapCrdt.js +117 -117
  17. package/dist/index.d.ts +35 -34
  18. package/dist/index.js +84 -82
  19. package/dist/index.js.map +1 -1
  20. package/dist/logger.d.ts +2 -2
  21. package/dist/logger.js +28 -28
  22. package/dist/messageHandlers/handleCrdtGet.d.ts +2 -2
  23. package/dist/messageHandlers/handleCrdtGet.js +49 -28
  24. package/dist/messageHandlers/handleCrdtGet.js.map +1 -1
  25. package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
  26. package/dist/messageHandlers/handleCrdtPut.js +102 -92
  27. package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
  28. package/dist/messageHandlers/handleFunction.d.ts +2 -0
  29. package/dist/messageHandlers/handleFunction.js +56 -0
  30. package/dist/messageHandlers/handleFunction.js.map +1 -0
  31. package/dist/messageHandlers/handleGet.d.ts +2 -2
  32. package/dist/messageHandlers/handleGet.js +49 -28
  33. package/dist/messageHandlers/handleGet.js.map +1 -1
  34. package/dist/messageHandlers/handlePing.d.ts +2 -2
  35. package/dist/messageHandlers/handlePing.js +35 -35
  36. package/dist/messageHandlers/handlePing.js.map +1 -1
  37. package/dist/messageHandlers/handlePong.d.ts +2 -2
  38. package/dist/messageHandlers/handlePong.js +25 -25
  39. package/dist/messageHandlers/handlePut.d.ts +2 -2
  40. package/dist/messageHandlers/handlePut.js +77 -56
  41. package/dist/messageHandlers/handlePut.js.map +1 -1
  42. package/dist/messageHandlers/handleQuery.d.ts +2 -2
  43. package/dist/messageHandlers/handleQuery.js +43 -22
  44. package/dist/messageHandlers/handleQuery.js.map +1 -1
  45. package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
  46. package/dist/messageHandlers/handleSubscribe.js +43 -43
  47. package/dist/server.d.ts +1 -1
  48. package/dist/server.js +8 -8
  49. package/dist/shared.d.ts +2 -2
  50. package/dist/shared.js +6 -6
  51. package/dist/toolDbAnonSignIn.d.ts +2 -2
  52. package/dist/toolDbAnonSignIn.js +6 -6
  53. package/dist/toolDbClientOnMessage.d.ts +2 -2
  54. package/dist/toolDbClientOnMessage.js +61 -58
  55. package/dist/toolDbClientOnMessage.js.map +1 -1
  56. package/dist/toolDbCrdtGet.d.ts +9 -9
  57. package/dist/toolDbCrdtGet.js +76 -76
  58. package/dist/toolDbCrdtGet.js.map +1 -1
  59. package/dist/toolDbCrdtPut.d.ts +9 -9
  60. package/dist/toolDbCrdtPut.js +62 -62
  61. package/dist/toolDbCrdtPut.js.map +1 -1
  62. package/dist/toolDbFunction.d.ts +9 -0
  63. package/dist/toolDbFunction.js +38 -0
  64. package/dist/toolDbFunction.js.map +1 -0
  65. package/dist/toolDbGet.d.ts +9 -9
  66. package/dist/toolDbGet.js +76 -76
  67. package/dist/toolDbGet.js.map +1 -1
  68. package/dist/toolDbKeysSignIn.d.ts +2 -2
  69. package/dist/toolDbKeysSignIn.js +15 -15
  70. package/dist/toolDbPut.d.ts +9 -9
  71. package/dist/toolDbPut.js +68 -68
  72. package/dist/toolDbPut.js.map +1 -1
  73. package/dist/toolDbQueryKeys.d.ts +8 -8
  74. package/dist/toolDbQueryKeys.js +65 -65
  75. package/dist/toolDbQueryKeys.js.map +1 -1
  76. package/dist/toolDbSignIn.d.ts +2 -2
  77. package/dist/toolDbSignIn.js +26 -26
  78. package/dist/toolDbSignIn.js.map +1 -1
  79. package/dist/toolDbSignUp.d.ts +2 -2
  80. package/dist/toolDbSignUp.js +100 -100
  81. package/dist/toolDbSignUp.js.map +1 -1
  82. package/dist/toolDbSubscribe.d.ts +9 -9
  83. package/dist/toolDbSubscribe.js +49 -49
  84. package/dist/toolDbSubscribe.js.map +1 -1
  85. package/dist/toolDbVerificationWrapper.d.ts +2 -2
  86. package/dist/toolDbVerificationWrapper.js +97 -97
  87. package/dist/tooldb.d.ts +123 -108
  88. package/dist/tooldb.js +309 -285
  89. package/dist/tooldb.js.map +1 -1
  90. package/dist/types/message.d.ts +110 -100
  91. package/dist/types/message.js +16 -16
  92. package/dist/types/message.js.map +1 -1
  93. package/dist/types/tooldb.d.ts +110 -84
  94. package/dist/types/tooldb.js +2 -2
  95. package/dist/utils/catchReturn.d.ts +1 -1
  96. package/dist/utils/catchReturn.js +7 -7
  97. package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
  98. package/dist/utils/encoding/arrayBufferToHex.js +18 -18
  99. package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
  100. package/dist/utils/encoding/arrayBufferToString.js +11 -11
  101. package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
  102. package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
  103. package/dist/utils/encoding/hexToString.d.ts +1 -1
  104. package/dist/utils/encoding/hexToString.js +11 -11
  105. package/dist/utils/encoding/hexToUint8.d.ts +1 -1
  106. package/dist/utils/encoding/hexToUint8.js +7 -7
  107. package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
  108. package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
  109. package/dist/utils/getPeerSignature.d.ts +2 -2
  110. package/dist/utils/getPeerSignature.js +8 -8
  111. package/dist/utils/getTimestamp.d.ts +1 -1
  112. package/dist/utils/getTimestamp.js +6 -6
  113. package/dist/utils/proofOfWork.d.ts +4 -4
  114. package/dist/utils/proofOfWork.js +15 -15
  115. package/dist/utils/randomAnimal.d.ts +1 -1
  116. package/dist/utils/randomAnimal.js +76 -76
  117. package/dist/utils/sha1.d.ts +1 -1
  118. package/dist/utils/sha1.js +12 -12
  119. package/dist/utils/sha256.d.ts +3 -3
  120. package/dist/utils/sha256.js +12 -12
  121. package/dist/utils/textRandom.d.ts +1 -1
  122. package/dist/utils/textRandom.js +14 -14
  123. package/dist/utils/uniq.d.ts +1 -1
  124. package/dist/utils/uniq.js +6 -6
  125. package/dist/utils/verifyMessage.d.ts +8 -8
  126. package/dist/utils/verifyMessage.js +128 -128
  127. package/dist/utils/verifyPeer.d.ts +2 -2
  128. package/dist/utils/verifyPeer.js +14 -14
  129. package/lib/adapters-base/networkAdapter.ts +217 -217
  130. package/lib/adapters-base/storageAdapter.ts +35 -35
  131. package/lib/adapters-base/userAdapter.ts +49 -49
  132. package/lib/crdt/baseCrdt.ts +21 -21
  133. package/lib/crdt/counterCrdt.ts +111 -111
  134. package/lib/crdt/listCrdt.ts +190 -190
  135. package/lib/crdt/mapCrdt.ts +119 -119
  136. package/lib/index.ts +43 -42
  137. package/lib/logger.ts +30 -30
  138. package/lib/messageHandlers/handleCrdtGet.ts +34 -29
  139. package/lib/messageHandlers/handleCrdtPut.ts +123 -118
  140. package/lib/messageHandlers/handleFunction.ts +59 -0
  141. package/lib/messageHandlers/handleGet.ts +34 -29
  142. package/lib/messageHandlers/handlePing.ts +40 -40
  143. package/lib/messageHandlers/handlePong.ts +30 -30
  144. package/lib/messageHandlers/handlePut.ts +59 -54
  145. package/lib/messageHandlers/handleQuery.ts +30 -25
  146. package/lib/messageHandlers/handleSubscribe.ts +46 -46
  147. package/lib/server.ts +7 -7
  148. package/lib/shared.ts +5 -5
  149. package/lib/toolDbAnonSignIn.ts +5 -5
  150. package/lib/toolDbClientOnMessage.ts +79 -75
  151. package/lib/toolDbCrdtGet.ts +83 -82
  152. package/lib/toolDbCrdtPut.ts +78 -77
  153. package/lib/toolDbFunction.ts +49 -0
  154. package/lib/toolDbGet.ts +81 -80
  155. package/lib/toolDbKeysSignIn.ts +16 -16
  156. package/lib/toolDbPut.ts +84 -83
  157. package/lib/toolDbQueryKeys.ts +65 -64
  158. package/lib/toolDbSignIn.ts +32 -31
  159. package/lib/toolDbSignUp.ts +72 -71
  160. package/lib/toolDbSubscribe.ts +54 -53
  161. package/lib/toolDbVerificationWrapper.ts +55 -55
  162. package/lib/tooldb.ts +343 -316
  163. package/lib/types/message.ts +154 -133
  164. package/lib/types/tooldb.ts +139 -97
  165. package/lib/utils/catchReturn.ts +4 -4
  166. package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
  167. package/lib/utils/encoding/arrayBufferToString.ts +8 -8
  168. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
  169. package/lib/utils/encoding/hexToString.ts +8 -8
  170. package/lib/utils/encoding/hexToUint8.ts +5 -5
  171. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
  172. package/lib/utils/getPeerSignature.ts +12 -12
  173. package/lib/utils/getTimestamp.ts +3 -3
  174. package/lib/utils/proofOfWork.ts +16 -16
  175. package/lib/utils/randomAnimal.ts +77 -77
  176. package/lib/utils/sha1.ts +7 -7
  177. package/lib/utils/sha256.ts +7 -7
  178. package/lib/utils/textRandom.ts +11 -11
  179. package/lib/utils/uniq.ts +3 -3
  180. package/lib/utils/verifyMessage.ts +88 -88
  181. package/lib/utils/verifyPeer.ts +15 -15
  182. package/package.json +2 -2
  183. package/tsconfig.json +14 -14
@@ -1,15 +1,15 @@
1
- import { ToolDb, VerificationData } from "..";
2
- export default class ToolDbUserAdapter {
3
- private _tooldb;
4
- get tooldb(): ToolDb;
5
- constructor(db: ToolDb);
6
- anonUser(): void;
7
- setUser(account: unknown | undefined, name: string): void;
8
- signData(data: string): Promise<string>;
9
- verifySignature(message: Partial<VerificationData<any>>): Promise<boolean>;
10
- getAccountFromPrivate(privateKey: string): Promise<unknown>;
11
- encryptAccount(password: string): Promise<unknown>;
12
- decryptAccount(acc: unknown, password: string): Promise<any>;
13
- getAddress(): string | undefined;
14
- getUsername(): string | undefined;
15
- }
1
+ import { ToolDb, VerificationData } from "..";
2
+ export default class ToolDbUserAdapter {
3
+ private _tooldb;
4
+ get tooldb(): ToolDb;
5
+ constructor(db: ToolDb);
6
+ anonUser(): void;
7
+ setUser(account: unknown | undefined, name: string): void;
8
+ signData(data: string): Promise<string>;
9
+ verifySignature(message: Partial<VerificationData<any>>): Promise<boolean>;
10
+ getAccountFromPrivate(privateKey: string): Promise<unknown>;
11
+ encryptAccount(password: string): Promise<unknown>;
12
+ decryptAccount(acc: unknown, password: string): Promise<any>;
13
+ getAddress(): string | undefined;
14
+ getUsername(): string | undefined;
15
+ }
@@ -1,42 +1,42 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var ToolDbUserAdapter = /** @class */ (function () {
4
- function ToolDbUserAdapter(db) {
5
- this._tooldb = db;
6
- }
7
- Object.defineProperty(ToolDbUserAdapter.prototype, "tooldb", {
8
- get: function () {
9
- return this._tooldb;
10
- },
11
- enumerable: false,
12
- configurable: true
13
- });
14
- ToolDbUserAdapter.prototype.anonUser = function () {
15
- return;
16
- };
17
- ToolDbUserAdapter.prototype.setUser = function (account, name) { };
18
- ToolDbUserAdapter.prototype.signData = function (data) {
19
- return Promise.resolve("");
20
- };
21
- ToolDbUserAdapter.prototype.verifySignature = function (message) {
22
- return Promise.resolve(false);
23
- };
24
- ToolDbUserAdapter.prototype.getAccountFromPrivate = function (privateKey) {
25
- return Promise.resolve(undefined);
26
- };
27
- ToolDbUserAdapter.prototype.encryptAccount = function (password) {
28
- return Promise.resolve(undefined);
29
- };
30
- ToolDbUserAdapter.prototype.decryptAccount = function (acc, password) {
31
- return Promise.resolve(undefined);
32
- };
33
- ToolDbUserAdapter.prototype.getAddress = function () {
34
- return "";
35
- };
36
- ToolDbUserAdapter.prototype.getUsername = function () {
37
- return "";
38
- };
39
- return ToolDbUserAdapter;
40
- }());
41
- exports.default = ToolDbUserAdapter;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var ToolDbUserAdapter = /** @class */ (function () {
4
+ function ToolDbUserAdapter(db) {
5
+ this._tooldb = db;
6
+ }
7
+ Object.defineProperty(ToolDbUserAdapter.prototype, "tooldb", {
8
+ get: function () {
9
+ return this._tooldb;
10
+ },
11
+ enumerable: false,
12
+ configurable: true
13
+ });
14
+ ToolDbUserAdapter.prototype.anonUser = function () {
15
+ return;
16
+ };
17
+ ToolDbUserAdapter.prototype.setUser = function (account, name) { };
18
+ ToolDbUserAdapter.prototype.signData = function (data) {
19
+ return Promise.resolve("");
20
+ };
21
+ ToolDbUserAdapter.prototype.verifySignature = function (message) {
22
+ return Promise.resolve(false);
23
+ };
24
+ ToolDbUserAdapter.prototype.getAccountFromPrivate = function (privateKey) {
25
+ return Promise.resolve(undefined);
26
+ };
27
+ ToolDbUserAdapter.prototype.encryptAccount = function (password) {
28
+ return Promise.resolve(undefined);
29
+ };
30
+ ToolDbUserAdapter.prototype.decryptAccount = function (acc, password) {
31
+ return Promise.resolve(undefined);
32
+ };
33
+ ToolDbUserAdapter.prototype.getAddress = function () {
34
+ return "";
35
+ };
36
+ ToolDbUserAdapter.prototype.getUsername = function () {
37
+ return "";
38
+ };
39
+ return ToolDbUserAdapter;
40
+ }());
41
+ exports.default = ToolDbUserAdapter;
42
42
  //# sourceMappingURL=userAdapter.js.map
@@ -1,9 +1,9 @@
1
- export declare const CRDT_MAP = "MAP";
2
- export declare const CRDT_LIST = "LIST";
3
- export declare const CRDT_COUNTER = "COUNTER";
4
- export declare class BaseCrdt<T = any, Changes = any, Value = any> {
5
- type: string;
6
- mergeChanges(changes: Changes[]): void;
7
- getChanges(): Changes[];
8
- get value(): Value;
9
- }
1
+ export declare const CRDT_MAP = "MAP";
2
+ export declare const CRDT_LIST = "LIST";
3
+ export declare const CRDT_COUNTER = "COUNTER";
4
+ export declare class BaseCrdt<T = any, Changes = any, Value = any> {
5
+ type: string;
6
+ mergeChanges(changes: Changes[]): void;
7
+ getChanges(): Changes[];
8
+ get value(): Value;
9
+ }
@@ -1,27 +1,27 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseCrdt = exports.CRDT_COUNTER = exports.CRDT_LIST = exports.CRDT_MAP = void 0;
4
- exports.CRDT_MAP = "MAP";
5
- exports.CRDT_LIST = "LIST";
6
- exports.CRDT_COUNTER = "COUNTER";
7
- var BaseCrdt = /** @class */ (function () {
8
- function BaseCrdt() {
9
- this.type = "";
10
- }
11
- BaseCrdt.prototype.mergeChanges = function (changes) {
12
- //
13
- };
14
- BaseCrdt.prototype.getChanges = function () {
15
- return [];
16
- };
17
- Object.defineProperty(BaseCrdt.prototype, "value", {
18
- get: function () {
19
- return "";
20
- },
21
- enumerable: false,
22
- configurable: true
23
- });
24
- return BaseCrdt;
25
- }());
26
- exports.BaseCrdt = BaseCrdt;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseCrdt = exports.CRDT_COUNTER = exports.CRDT_LIST = exports.CRDT_MAP = void 0;
4
+ exports.CRDT_MAP = "MAP";
5
+ exports.CRDT_LIST = "LIST";
6
+ exports.CRDT_COUNTER = "COUNTER";
7
+ var BaseCrdt = /** @class */ (function () {
8
+ function BaseCrdt() {
9
+ this.type = "";
10
+ }
11
+ BaseCrdt.prototype.mergeChanges = function (changes) {
12
+ //
13
+ };
14
+ BaseCrdt.prototype.getChanges = function () {
15
+ return [];
16
+ };
17
+ Object.defineProperty(BaseCrdt.prototype, "value", {
18
+ get: function () {
19
+ return "";
20
+ },
21
+ enumerable: false,
22
+ configurable: true
23
+ });
24
+ return BaseCrdt;
25
+ }());
26
+ exports.BaseCrdt = BaseCrdt;
27
27
  //# sourceMappingURL=baseCrdt.js.map
@@ -1,30 +1,30 @@
1
- import { BaseCrdt } from "..";
2
- export declare type CounterOperations = "ADD" | "SUB";
3
- export interface ChangeCounterBase {
4
- t: CounterOperations;
5
- v: number;
6
- a: string;
7
- i: number;
8
- }
9
- export interface AddCounterChange extends ChangeCounterBase {
10
- t: "ADD";
11
- }
12
- export interface SubCounterChange extends ChangeCounterBase {
13
- t: "SUB";
14
- }
15
- export declare type CounterChanges = AddCounterChange | SubCounterChange;
16
- export declare class CounterCrdt<T> extends BaseCrdt<number, CounterChanges, number> {
17
- type: string;
18
- _changes: CounterChanges[];
19
- private _author;
20
- private _value;
21
- private _lastUpdateSize;
22
- constructor(author: string, changes?: CounterChanges[]);
23
- changesSort(a: CounterChanges, b: CounterChanges): 0 | 1 | -1;
24
- calculate(): void;
25
- get value(): number;
26
- mergeChanges(newChanges: CounterChanges[]): void;
27
- getChanges(): CounterChanges[];
28
- ADD(value: number): void;
29
- SUB(value: number): void;
30
- }
1
+ import { BaseCrdt } from "..";
2
+ export declare type CounterOperations = "ADD" | "SUB";
3
+ export interface ChangeCounterBase {
4
+ t: CounterOperations;
5
+ v: number;
6
+ a: string;
7
+ i: number;
8
+ }
9
+ export interface AddCounterChange extends ChangeCounterBase {
10
+ t: "ADD";
11
+ }
12
+ export interface SubCounterChange extends ChangeCounterBase {
13
+ t: "SUB";
14
+ }
15
+ export declare type CounterChanges = AddCounterChange | SubCounterChange;
16
+ export declare class CounterCrdt<T> extends BaseCrdt<number, CounterChanges, number> {
17
+ type: string;
18
+ _changes: CounterChanges[];
19
+ private _author;
20
+ private _value;
21
+ private _lastUpdateSize;
22
+ constructor(author: string, changes?: CounterChanges[]);
23
+ changesSort(a: CounterChanges, b: CounterChanges): 0 | 1 | -1;
24
+ calculate(): void;
25
+ get value(): number;
26
+ mergeChanges(newChanges: CounterChanges[]): void;
27
+ getChanges(): CounterChanges[];
28
+ ADD(value: number): void;
29
+ SUB(value: number): void;
30
+ }
@@ -1,106 +1,106 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.CounterCrdt = void 0;
19
- var __1 = require("..");
20
- var CounterCrdt = /** @class */ (function (_super) {
21
- __extends(CounterCrdt, _super);
22
- function CounterCrdt(author, changes) {
23
- var _this = _super.call(this) || this;
24
- _this.type = __1.CRDT_LIST;
25
- _this._changes = [];
26
- _this._author = "";
27
- _this._value = 0;
28
- _this._lastUpdateSize = 0;
29
- _this._author = author;
30
- if (changes) {
31
- _this.mergeChanges(changes);
32
- }
33
- _this.calculate();
34
- return _this;
35
- }
36
- CounterCrdt.prototype.changesSort = function (a, b) {
37
- if (a.i > b.i)
38
- return 1;
39
- if (a.i < b.i)
40
- return -1;
41
- return 0; // Should never be equal!
42
- };
43
- CounterCrdt.prototype.calculate = function () {
44
- var temp = 0;
45
- // Only update if we have new changes
46
- if (Object.values(this._changes).length !== this._lastUpdateSize) {
47
- this._changes.sort(this.changesSort).forEach(function (change) {
48
- if (change.t === "ADD") {
49
- temp += change.v;
50
- }
51
- else if (change.t === "SUB") {
52
- temp -= change.v;
53
- }
54
- });
55
- this._value = temp;
56
- }
57
- };
58
- Object.defineProperty(CounterCrdt.prototype, "value", {
59
- get: function () {
60
- this.calculate();
61
- return this._value;
62
- },
63
- enumerable: false,
64
- configurable: true
65
- });
66
- CounterCrdt.prototype.mergeChanges = function (newChanges) {
67
- var _this = this;
68
- newChanges.forEach(function (change) {
69
- // Filter by author and index
70
- var filtered = _this._changes.filter(function (c) { return c.i === change.i && c.a === change.a && c.t === change.t; });
71
- // Only add if there are not matches
72
- if (filtered.length === 0) {
73
- _this._changes.push(change);
74
- }
75
- });
76
- this.calculate();
77
- };
78
- CounterCrdt.prototype.getChanges = function () {
79
- return this._changes;
80
- };
81
- CounterCrdt.prototype.ADD = function (value) {
82
- var _this = this;
83
- var ourChanges = this._changes.filter(function (c) { return c.a === _this._author; });
84
- this._changes.push({
85
- t: "ADD",
86
- v: value,
87
- a: this._author,
88
- i: ourChanges.length,
89
- });
90
- this.calculate();
91
- };
92
- CounterCrdt.prototype.SUB = function (value) {
93
- var _this = this;
94
- var ourChanges = this._changes.filter(function (c) { return c.a === _this._author; });
95
- this._changes.push({
96
- t: "SUB",
97
- v: value,
98
- a: this._author,
99
- i: ourChanges.length,
100
- });
101
- this.calculate();
102
- };
103
- return CounterCrdt;
104
- }(__1.BaseCrdt));
105
- exports.CounterCrdt = CounterCrdt;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.CounterCrdt = void 0;
19
+ var __1 = require("..");
20
+ var CounterCrdt = /** @class */ (function (_super) {
21
+ __extends(CounterCrdt, _super);
22
+ function CounterCrdt(author, changes) {
23
+ var _this = _super.call(this) || this;
24
+ _this.type = __1.CRDT_LIST;
25
+ _this._changes = [];
26
+ _this._author = "";
27
+ _this._value = 0;
28
+ _this._lastUpdateSize = 0;
29
+ _this._author = author;
30
+ if (changes) {
31
+ _this.mergeChanges(changes);
32
+ }
33
+ _this.calculate();
34
+ return _this;
35
+ }
36
+ CounterCrdt.prototype.changesSort = function (a, b) {
37
+ if (a.i > b.i)
38
+ return 1;
39
+ if (a.i < b.i)
40
+ return -1;
41
+ return 0; // Should never be equal!
42
+ };
43
+ CounterCrdt.prototype.calculate = function () {
44
+ var temp = 0;
45
+ // Only update if we have new changes
46
+ if (Object.values(this._changes).length !== this._lastUpdateSize) {
47
+ this._changes.sort(this.changesSort).forEach(function (change) {
48
+ if (change.t === "ADD") {
49
+ temp += change.v;
50
+ }
51
+ else if (change.t === "SUB") {
52
+ temp -= change.v;
53
+ }
54
+ });
55
+ this._value = temp;
56
+ }
57
+ };
58
+ Object.defineProperty(CounterCrdt.prototype, "value", {
59
+ get: function () {
60
+ this.calculate();
61
+ return this._value;
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ CounterCrdt.prototype.mergeChanges = function (newChanges) {
67
+ var _this = this;
68
+ newChanges.forEach(function (change) {
69
+ // Filter by author and index
70
+ var filtered = _this._changes.filter(function (c) { return c.i === change.i && c.a === change.a && c.t === change.t; });
71
+ // Only add if there are not matches
72
+ if (filtered.length === 0) {
73
+ _this._changes.push(change);
74
+ }
75
+ });
76
+ this.calculate();
77
+ };
78
+ CounterCrdt.prototype.getChanges = function () {
79
+ return this._changes;
80
+ };
81
+ CounterCrdt.prototype.ADD = function (value) {
82
+ var _this = this;
83
+ var ourChanges = this._changes.filter(function (c) { return c.a === _this._author; });
84
+ this._changes.push({
85
+ t: "ADD",
86
+ v: value,
87
+ a: this._author,
88
+ i: ourChanges.length,
89
+ });
90
+ this.calculate();
91
+ };
92
+ CounterCrdt.prototype.SUB = function (value) {
93
+ var _this = this;
94
+ var ourChanges = this._changes.filter(function (c) { return c.a === _this._author; });
95
+ this._changes.push({
96
+ t: "SUB",
97
+ v: value,
98
+ a: this._author,
99
+ i: ourChanges.length,
100
+ });
101
+ this.calculate();
102
+ };
103
+ return CounterCrdt;
104
+ }(__1.BaseCrdt));
105
+ exports.CounterCrdt = CounterCrdt;
106
106
  //# sourceMappingURL=counterCrdt.js.map
@@ -1,42 +1,42 @@
1
- import { BaseCrdt } from "..";
2
- export declare type ListOperations = "INS" | "DEL";
3
- export interface ChangeListBase<T> {
4
- t: ListOperations;
5
- i: string;
6
- }
7
- export interface InsListChange<T> extends ChangeListBase<T> {
8
- t: "INS";
9
- v: T;
10
- p: string | undefined;
11
- n: string | undefined;
12
- }
13
- export interface DelListChange<T> extends ChangeListBase<T> {
14
- t: "DEL";
15
- v: string;
16
- }
17
- export declare type ListChanges<T> = InsListChange<T> | DelListChange<T>;
18
- interface ListTempCursor<T> {
19
- value: T;
20
- index: string;
21
- tomb: boolean;
22
- prev: string | undefined;
23
- next: string | undefined;
24
- }
25
- export declare class ListCrdt<T> extends BaseCrdt<T, ListChanges<T>, T[]> {
26
- type: string;
27
- _changes: ListChanges<T>[];
28
- private _author;
29
- private _value;
30
- _tempValues: ListTempCursor<T>[];
31
- private _lastUpdateSize;
32
- constructor(author: string, changes?: ListChanges<T>[]);
33
- changesSort(a: ListChanges<T>, b: ListChanges<T>): 0 | 1 | -1;
34
- calculate(): void;
35
- get value(): T[];
36
- mergeChanges(newChanges: ListChanges<T>[]): void;
37
- getChanges(): ListChanges<T>[];
38
- INS(value: T, index: number): void;
39
- PUSH(value: T): void;
40
- DEL(index: number): void;
41
- }
42
- export {};
1
+ import { BaseCrdt } from "..";
2
+ export declare type ListOperations = "INS" | "DEL";
3
+ export interface ChangeListBase<T> {
4
+ t: ListOperations;
5
+ i: string;
6
+ }
7
+ export interface InsListChange<T> extends ChangeListBase<T> {
8
+ t: "INS";
9
+ v: T;
10
+ p: string | undefined;
11
+ n: string | undefined;
12
+ }
13
+ export interface DelListChange<T> extends ChangeListBase<T> {
14
+ t: "DEL";
15
+ v: string;
16
+ }
17
+ export declare type ListChanges<T> = InsListChange<T> | DelListChange<T>;
18
+ interface ListTempCursor<T> {
19
+ value: T;
20
+ index: string;
21
+ tomb: boolean;
22
+ prev: string | undefined;
23
+ next: string | undefined;
24
+ }
25
+ export declare class ListCrdt<T> extends BaseCrdt<T, ListChanges<T>, T[]> {
26
+ type: string;
27
+ _changes: ListChanges<T>[];
28
+ private _author;
29
+ private _value;
30
+ _tempValues: ListTempCursor<T>[];
31
+ private _lastUpdateSize;
32
+ constructor(author: string, changes?: ListChanges<T>[]);
33
+ changesSort(a: ListChanges<T>, b: ListChanges<T>): 0 | 1 | -1;
34
+ calculate(): void;
35
+ get value(): T[];
36
+ mergeChanges(newChanges: ListChanges<T>[]): void;
37
+ getChanges(): ListChanges<T>[];
38
+ INS(value: T, index: number): void;
39
+ PUSH(value: T): void;
40
+ DEL(index: number): void;
41
+ }
42
+ export {};