tool-db 2.5.1 → 2.5.3

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 (159) 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 -213
  5. package/dist/adapters-base/networkAdapter.js.map +1 -1
  6. package/dist/adapters-base/storageAdapter.d.ts +11 -11
  7. package/dist/adapters-base/storageAdapter.js +35 -35
  8. package/dist/adapters-base/userAdapter.d.ts +15 -15
  9. package/dist/adapters-base/userAdapter.js +41 -41
  10. package/dist/crdt/baseCrdt.d.ts +9 -9
  11. package/dist/crdt/baseCrdt.js +26 -26
  12. package/dist/crdt/counterCrdt.d.ts +30 -30
  13. package/dist/crdt/counterCrdt.js +105 -105
  14. package/dist/crdt/listCrdt.d.ts +42 -42
  15. package/dist/crdt/listCrdt.js +158 -158
  16. package/dist/crdt/mapCrdt.d.ts +32 -32
  17. package/dist/crdt/mapCrdt.js +117 -117
  18. package/dist/index.d.ts +34 -34
  19. package/dist/index.js +82 -82
  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 +28 -28
  24. package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
  25. package/dist/messageHandlers/handleCrdtPut.js +92 -92
  26. package/dist/messageHandlers/handleGet.d.ts +2 -2
  27. package/dist/messageHandlers/handleGet.js +28 -28
  28. package/dist/messageHandlers/handlePing.d.ts +2 -2
  29. package/dist/messageHandlers/handlePing.js +35 -35
  30. package/dist/messageHandlers/handlePong.d.ts +2 -2
  31. package/dist/messageHandlers/handlePong.js +25 -25
  32. package/dist/messageHandlers/handlePut.d.ts +2 -2
  33. package/dist/messageHandlers/handlePut.js +56 -56
  34. package/dist/messageHandlers/handleQuery.d.ts +2 -2
  35. package/dist/messageHandlers/handleQuery.js +22 -22
  36. package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
  37. package/dist/messageHandlers/handleSubscribe.js +43 -43
  38. package/dist/server.d.ts +1 -1
  39. package/dist/server.js +8 -8
  40. package/dist/shared.d.ts +2 -2
  41. package/dist/shared.js +6 -6
  42. package/dist/toolDbAnonSignIn.d.ts +2 -2
  43. package/dist/toolDbAnonSignIn.js +6 -6
  44. package/dist/toolDbClientOnMessage.d.ts +2 -2
  45. package/dist/toolDbClientOnMessage.js +58 -58
  46. package/dist/toolDbCrdtGet.d.ts +9 -9
  47. package/dist/toolDbCrdtGet.js +76 -76
  48. package/dist/toolDbCrdtPut.d.ts +9 -9
  49. package/dist/toolDbCrdtPut.js +62 -62
  50. package/dist/toolDbGet.d.ts +9 -9
  51. package/dist/toolDbGet.js +76 -76
  52. package/dist/toolDbKeysSignIn.d.ts +2 -2
  53. package/dist/toolDbKeysSignIn.js +15 -15
  54. package/dist/toolDbPut.d.ts +9 -9
  55. package/dist/toolDbPut.js +68 -68
  56. package/dist/toolDbQueryKeys.d.ts +8 -8
  57. package/dist/toolDbQueryKeys.js +65 -65
  58. package/dist/toolDbSignIn.d.ts +2 -2
  59. package/dist/toolDbSignIn.js +26 -26
  60. package/dist/toolDbSignUp.d.ts +2 -2
  61. package/dist/toolDbSignUp.js +100 -100
  62. package/dist/toolDbSubscribe.d.ts +9 -9
  63. package/dist/toolDbSubscribe.js +49 -49
  64. package/dist/toolDbVerificationWrapper.d.ts +2 -2
  65. package/dist/toolDbVerificationWrapper.js +97 -97
  66. package/dist/tooldb.d.ts +108 -108
  67. package/dist/tooldb.js +285 -283
  68. package/dist/tooldb.js.map +1 -1
  69. package/dist/types/message.d.ts +100 -100
  70. package/dist/types/message.js +16 -16
  71. package/dist/types/tooldb.d.ts +84 -79
  72. package/dist/types/tooldb.js +2 -2
  73. package/dist/utils/catchReturn.d.ts +1 -1
  74. package/dist/utils/catchReturn.js +7 -7
  75. package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
  76. package/dist/utils/encoding/arrayBufferToHex.js +18 -18
  77. package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
  78. package/dist/utils/encoding/arrayBufferToString.js +11 -11
  79. package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
  80. package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
  81. package/dist/utils/encoding/hexToString.d.ts +1 -1
  82. package/dist/utils/encoding/hexToString.js +11 -11
  83. package/dist/utils/encoding/hexToUint8.d.ts +1 -1
  84. package/dist/utils/encoding/hexToUint8.js +7 -7
  85. package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
  86. package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
  87. package/dist/utils/getPeerSignature.d.ts +2 -2
  88. package/dist/utils/getPeerSignature.js +8 -8
  89. package/dist/utils/getTimestamp.d.ts +1 -1
  90. package/dist/utils/getTimestamp.js +6 -6
  91. package/dist/utils/proofOfWork.d.ts +4 -4
  92. package/dist/utils/proofOfWork.js +15 -15
  93. package/dist/utils/randomAnimal.d.ts +1 -1
  94. package/dist/utils/randomAnimal.js +76 -76
  95. package/dist/utils/sha1.d.ts +1 -1
  96. package/dist/utils/sha1.js +12 -12
  97. package/dist/utils/sha256.d.ts +3 -3
  98. package/dist/utils/sha256.js +12 -12
  99. package/dist/utils/textRandom.d.ts +1 -1
  100. package/dist/utils/textRandom.js +14 -14
  101. package/dist/utils/uniq.d.ts +1 -1
  102. package/dist/utils/uniq.js +6 -6
  103. package/dist/utils/verifyMessage.d.ts +8 -8
  104. package/dist/utils/verifyMessage.js +128 -128
  105. package/dist/utils/verifyPeer.d.ts +2 -2
  106. package/dist/utils/verifyPeer.js +14 -14
  107. package/lib/adapters-base/networkAdapter.ts +217 -215
  108. package/lib/adapters-base/storageAdapter.ts +35 -35
  109. package/lib/adapters-base/userAdapter.ts +49 -49
  110. package/lib/crdt/baseCrdt.ts +21 -21
  111. package/lib/crdt/counterCrdt.ts +111 -111
  112. package/lib/crdt/listCrdt.ts +190 -190
  113. package/lib/crdt/mapCrdt.ts +119 -119
  114. package/lib/index.ts +42 -42
  115. package/lib/logger.ts +30 -30
  116. package/lib/messageHandlers/handleCrdtGet.ts +29 -29
  117. package/lib/messageHandlers/handleCrdtPut.ts +118 -118
  118. package/lib/messageHandlers/handleGet.ts +29 -29
  119. package/lib/messageHandlers/handlePing.ts +40 -40
  120. package/lib/messageHandlers/handlePong.ts +30 -30
  121. package/lib/messageHandlers/handlePut.ts +54 -54
  122. package/lib/messageHandlers/handleQuery.ts +25 -25
  123. package/lib/messageHandlers/handleSubscribe.ts +46 -46
  124. package/lib/server.ts +7 -7
  125. package/lib/shared.ts +5 -5
  126. package/lib/toolDbAnonSignIn.ts +5 -5
  127. package/lib/toolDbClientOnMessage.ts +75 -75
  128. package/lib/toolDbCrdtGet.ts +82 -82
  129. package/lib/toolDbCrdtPut.ts +77 -77
  130. package/lib/toolDbGet.ts +80 -80
  131. package/lib/toolDbKeysSignIn.ts +16 -16
  132. package/lib/toolDbPut.ts +83 -83
  133. package/lib/toolDbQueryKeys.ts +64 -64
  134. package/lib/toolDbSignIn.ts +31 -31
  135. package/lib/toolDbSignUp.ts +71 -71
  136. package/lib/toolDbSubscribe.ts +53 -53
  137. package/lib/toolDbVerificationWrapper.ts +55 -55
  138. package/lib/tooldb.ts +316 -314
  139. package/lib/types/message.ts +133 -133
  140. package/lib/types/tooldb.ts +97 -89
  141. package/lib/utils/catchReturn.ts +4 -4
  142. package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
  143. package/lib/utils/encoding/arrayBufferToString.ts +8 -8
  144. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
  145. package/lib/utils/encoding/hexToString.ts +8 -8
  146. package/lib/utils/encoding/hexToUint8.ts +5 -5
  147. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
  148. package/lib/utils/getPeerSignature.ts +12 -12
  149. package/lib/utils/getTimestamp.ts +3 -3
  150. package/lib/utils/proofOfWork.ts +16 -16
  151. package/lib/utils/randomAnimal.ts +77 -77
  152. package/lib/utils/sha1.ts +7 -7
  153. package/lib/utils/sha256.ts +7 -7
  154. package/lib/utils/textRandom.ts +11 -11
  155. package/lib/utils/uniq.ts +3 -3
  156. package/lib/utils/verifyMessage.ts +88 -88
  157. package/lib/utils/verifyPeer.ts +15 -15
  158. package/package.json +2 -2
  159. package/tsconfig.json +14 -14
@@ -1,36 +1,36 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var ToolDbStorageAdapter = /** @class */ (function () {
4
- function ToolDbStorageAdapter(db, forceStorageName) {
5
- this._tooldb = db;
6
- if (forceStorageName) {
7
- this._forceStorageName = forceStorageName;
8
- }
9
- }
10
- Object.defineProperty(ToolDbStorageAdapter.prototype, "tooldb", {
11
- get: function () {
12
- return this._tooldb;
13
- },
14
- enumerable: false,
15
- configurable: true
16
- });
17
- Object.defineProperty(ToolDbStorageAdapter.prototype, "storageName", {
18
- get: function () {
19
- return this._forceStorageName || this._tooldb.options.storageName;
20
- },
21
- enumerable: false,
22
- configurable: true
23
- });
24
- ToolDbStorageAdapter.prototype.put = function (key, data) {
25
- return Promise.resolve();
26
- };
27
- ToolDbStorageAdapter.prototype.get = function (key) {
28
- return Promise.resolve("");
29
- };
30
- ToolDbStorageAdapter.prototype.query = function (key) {
31
- return Promise.resolve([]);
32
- };
33
- return ToolDbStorageAdapter;
34
- }());
35
- exports.default = ToolDbStorageAdapter;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var ToolDbStorageAdapter = /** @class */ (function () {
4
+ function ToolDbStorageAdapter(db, forceStorageName) {
5
+ this._tooldb = db;
6
+ if (forceStorageName) {
7
+ this._forceStorageName = forceStorageName;
8
+ }
9
+ }
10
+ Object.defineProperty(ToolDbStorageAdapter.prototype, "tooldb", {
11
+ get: function () {
12
+ return this._tooldb;
13
+ },
14
+ enumerable: false,
15
+ configurable: true
16
+ });
17
+ Object.defineProperty(ToolDbStorageAdapter.prototype, "storageName", {
18
+ get: function () {
19
+ return this._forceStorageName || this._tooldb.options.storageName;
20
+ },
21
+ enumerable: false,
22
+ configurable: true
23
+ });
24
+ ToolDbStorageAdapter.prototype.put = function (key, data) {
25
+ return Promise.resolve();
26
+ };
27
+ ToolDbStorageAdapter.prototype.get = function (key) {
28
+ return Promise.resolve("");
29
+ };
30
+ ToolDbStorageAdapter.prototype.query = function (key) {
31
+ return Promise.resolve([]);
32
+ };
33
+ return ToolDbStorageAdapter;
34
+ }());
35
+ exports.default = ToolDbStorageAdapter;
36
36
  //# sourceMappingURL=storageAdapter.js.map
@@ -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 {};