@dra2020/baseclient 1.0.0

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 (113) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/dist/all/all.d.ts +18 -0
  4. package/dist/baseclient.js +9567 -0
  5. package/dist/baseclient.js.map +1 -0
  6. package/dist/context/all.d.ts +1 -0
  7. package/dist/context/context.d.ts +13 -0
  8. package/dist/filterexpr/all.d.ts +1 -0
  9. package/dist/filterexpr/filterexpr.d.ts +64 -0
  10. package/dist/fsm/all.d.ts +1 -0
  11. package/dist/fsm/fsm.d.ts +118 -0
  12. package/dist/logabstract/all.d.ts +1 -0
  13. package/dist/logabstract/log.d.ts +26 -0
  14. package/dist/logclient/all.d.ts +1 -0
  15. package/dist/logclient/log.d.ts +6 -0
  16. package/dist/ot-editutil/all.d.ts +2 -0
  17. package/dist/ot-editutil/oteditutil.d.ts +14 -0
  18. package/dist/ot-editutil/otmaputil.d.ts +21 -0
  19. package/dist/ot-js/all.d.ts +9 -0
  20. package/dist/ot-js/otarray.d.ts +111 -0
  21. package/dist/ot-js/otclientengine.d.ts +38 -0
  22. package/dist/ot-js/otcomposite.d.ts +37 -0
  23. package/dist/ot-js/otcounter.d.ts +17 -0
  24. package/dist/ot-js/otengine.d.ts +22 -0
  25. package/dist/ot-js/otmap.d.ts +19 -0
  26. package/dist/ot-js/otserverengine.d.ts +38 -0
  27. package/dist/ot-js/otsession.d.ts +111 -0
  28. package/dist/ot-js/ottypes.d.ts +29 -0
  29. package/dist/poly/all.d.ts +15 -0
  30. package/dist/poly/blend.d.ts +1 -0
  31. package/dist/poly/boundbox.d.ts +16 -0
  32. package/dist/poly/cartesian.d.ts +5 -0
  33. package/dist/poly/graham-scan.d.ts +8 -0
  34. package/dist/poly/hash.d.ts +1 -0
  35. package/dist/poly/matrix.d.ts +24 -0
  36. package/dist/poly/minbound.d.ts +1 -0
  37. package/dist/poly/poly.d.ts +52 -0
  38. package/dist/poly/polybin.d.ts +5 -0
  39. package/dist/poly/polylabel.d.ts +7 -0
  40. package/dist/poly/polypack.d.ts +30 -0
  41. package/dist/poly/polyround.d.ts +1 -0
  42. package/dist/poly/polysimplify.d.ts +1 -0
  43. package/dist/poly/quad.d.ts +48 -0
  44. package/dist/poly/selfintersect.d.ts +1 -0
  45. package/dist/poly/shamos.d.ts +1 -0
  46. package/dist/poly/simplify.d.ts +2 -0
  47. package/dist/poly/topo.d.ts +46 -0
  48. package/dist/poly/union.d.ts +48 -0
  49. package/dist/util/all.d.ts +5 -0
  50. package/dist/util/bintrie.d.ts +93 -0
  51. package/dist/util/countedhash.d.ts +19 -0
  52. package/dist/util/gradient.d.ts +15 -0
  53. package/dist/util/indexedarray.d.ts +15 -0
  54. package/dist/util/util.d.ts +68 -0
  55. package/docs/context.md +2 -0
  56. package/docs/fsm.md +243 -0
  57. package/docs/logabstract.md +2 -0
  58. package/docs/logclient.md +2 -0
  59. package/docs/ot-editutil.md +2 -0
  60. package/docs/ot-js.md +95 -0
  61. package/docs/poly.md +103 -0
  62. package/docs/util.md +2 -0
  63. package/lib/all/all.ts +19 -0
  64. package/lib/context/all.ts +1 -0
  65. package/lib/context/context.ts +82 -0
  66. package/lib/filterexpr/all.ts +1 -0
  67. package/lib/filterexpr/filterexpr.ts +625 -0
  68. package/lib/fsm/all.ts +1 -0
  69. package/lib/fsm/fsm.ts +549 -0
  70. package/lib/logabstract/all.ts +1 -0
  71. package/lib/logabstract/log.ts +55 -0
  72. package/lib/logclient/all.ts +1 -0
  73. package/lib/logclient/log.ts +105 -0
  74. package/lib/ot-editutil/all.ts +2 -0
  75. package/lib/ot-editutil/oteditutil.ts +180 -0
  76. package/lib/ot-editutil/otmaputil.ts +209 -0
  77. package/lib/ot-js/all.ts +9 -0
  78. package/lib/ot-js/otarray.ts +1168 -0
  79. package/lib/ot-js/otclientengine.ts +327 -0
  80. package/lib/ot-js/otcomposite.ts +247 -0
  81. package/lib/ot-js/otcounter.ts +145 -0
  82. package/lib/ot-js/otengine.ts +71 -0
  83. package/lib/ot-js/otmap.ts +144 -0
  84. package/lib/ot-js/otserverengine.ts +329 -0
  85. package/lib/ot-js/otsession.ts +199 -0
  86. package/lib/ot-js/ottypes.ts +98 -0
  87. package/lib/poly/all.ts +15 -0
  88. package/lib/poly/blend.ts +27 -0
  89. package/lib/poly/boundbox.ts +102 -0
  90. package/lib/poly/cartesian.ts +130 -0
  91. package/lib/poly/graham-scan.ts +401 -0
  92. package/lib/poly/hash.ts +15 -0
  93. package/lib/poly/matrix.ts +309 -0
  94. package/lib/poly/minbound.ts +211 -0
  95. package/lib/poly/poly.ts +767 -0
  96. package/lib/poly/polybin.ts +218 -0
  97. package/lib/poly/polylabel.ts +204 -0
  98. package/lib/poly/polypack.ts +458 -0
  99. package/lib/poly/polyround.ts +30 -0
  100. package/lib/poly/polysimplify.ts +24 -0
  101. package/lib/poly/quad.ts +272 -0
  102. package/lib/poly/selfintersect.ts +87 -0
  103. package/lib/poly/shamos.ts +297 -0
  104. package/lib/poly/simplify.ts +119 -0
  105. package/lib/poly/topo.ts +525 -0
  106. package/lib/poly/union.ts +371 -0
  107. package/lib/util/all.ts +5 -0
  108. package/lib/util/bintrie.ts +603 -0
  109. package/lib/util/countedhash.ts +83 -0
  110. package/lib/util/gradient.ts +108 -0
  111. package/lib/util/indexedarray.ts +80 -0
  112. package/lib/util/util.ts +695 -0
  113. package/package.json +52 -0
@@ -0,0 +1,180 @@
1
+ import * as LogAbstract from '../logabstract/all';
2
+ import * as OT from '../ot-js/all';
3
+
4
+ import * as DMP from "diff-match-patch";
5
+ let DiffMatchPatch = new DMP.diff_match_patch();
6
+ let DIFF_DELETE = DMP.DIFF_DELETE;
7
+ let DIFF_INSERT = DMP.DIFF_INSERT;
8
+ let DIFF_EQUAL = DMP.DIFF_EQUAL;
9
+
10
+ export class OTEditUtil
11
+ {
12
+ // Data members
13
+ ilog: LogAbstract.ILog;
14
+ resourceID: string;
15
+ resourceName: string;
16
+ clientID: string;
17
+
18
+ // Constructor
19
+ constructor(ilog: LogAbstract.ILog, rid: string, cid: string, name: string)
20
+ {
21
+ this.ilog = ilog;
22
+ this.resourceID = rid;
23
+ this.clientID = cid;
24
+ this.resourceName = name;
25
+ }
26
+
27
+ //
28
+ // Function: insertAtStart
29
+ //
30
+ // Description:
31
+ // Generate an OTCompositeResource to insert a string at the start of a buffer of the given size.
32
+ //
33
+
34
+ insertAtStart(s: any, nCurrentLen: number): OT.OTCompositeResource
35
+ {
36
+ let edit = new OT.OTCompositeResource(this.resourceID, this.clientID);
37
+ let sEdit = new OT.OTStringResource(this.resourceName);
38
+ sEdit.edits.push([ OT.OpInsert, s.length, s ]);
39
+ sEdit.edits.push([ OT.OpCursor, 0, '' ]);
40
+ sEdit.edits.push([ OT.OpRetain, nCurrentLen, '' ]);
41
+ edit.edits.push(sEdit);
42
+ return edit;
43
+ }
44
+
45
+ //
46
+ // Function: insertAtEnd
47
+ //
48
+ // Description:
49
+ // Generate an OTCompositeResource to insert a string at the end of a buffer of the given size.
50
+ //
51
+
52
+ insertAtEnd(s: any, nCurrentLen: number): OT.OTCompositeResource
53
+ {
54
+ let edit = new OT.OTCompositeResource(this.resourceID, this.clientID);
55
+ let sEdit = new OT.OTStringResource(this.resourceName);
56
+ sEdit.edits.push([ OT.OpRetain, nCurrentLen, '' ]);
57
+ sEdit.edits.push([ OT.OpInsert, s.length, s ]);
58
+ sEdit.edits.push([ OT.OpCursor, 0, '' ]);
59
+ edit.edits.push(sEdit);
60
+ return edit;
61
+ }
62
+
63
+ //
64
+ // Function: injectCursor
65
+ //
66
+ // Inject start/end cursor positions into an existing string resource.
67
+ //
68
+
69
+ injectCursor(edit: OT.OTCompositeResource, start: number, end?: number): void
70
+ {
71
+ if (start === undefined) return;
72
+ if (end === undefined) end = start;
73
+
74
+ let sEdit: OT.OTStringResource = edit.findResource(this.resourceName) as OT.OTStringResource;
75
+ if (sEdit == null)
76
+ return;
77
+ let cEdit: OT.OTStringResource = new OT.OTStringResource(this.resourceName);
78
+ if (start != 0)
79
+ cEdit.edits.push( [ OT.OpRetain, start, '' ] );
80
+ cEdit.edits.push( [ OT.OpCursor, 0, this.clientID ] );
81
+ if (end != start)
82
+ {
83
+ cEdit.edits.push( [ OT.OpRetain, end-start, '' ] );
84
+ cEdit.edits.push( [ OT.OpCursor, 1, this.clientID ] );
85
+ }
86
+ let nFinal: number = sEdit.finalLength();
87
+ if (end != nFinal)
88
+ cEdit.edits.push( [ OT.OpRetain, nFinal-end, '' ] );
89
+ sEdit.compose(cEdit);
90
+ }
91
+
92
+ //
93
+ // Function: extractCursor
94
+ //
95
+ // Extract cursor information by client. Returns an object indexed by clientID with an object with
96
+ // properties startSelection, endSelection.
97
+ //
98
+
99
+ extractCursor(edit: OT.OTCompositeResource): any
100
+ {
101
+ let cursors: any = { };
102
+ let sEdit: OT.OTStringResource = edit.findResource(this.resourceName) as OT.OTStringResource;
103
+ if (sEdit == null)
104
+ return cursors;
105
+ let pos: number = 0;
106
+ for (let i: number = 0; i < sEdit.length; i++)
107
+ {
108
+ let a: OT.OTSingleArrayEdit = sEdit.edits[i];
109
+ switch (a[0])
110
+ {
111
+ case OT.OpInsert:
112
+ pos += a[1];
113
+ break;
114
+ case OT.OpDelete:
115
+ break;
116
+ case OT.OpRetain:
117
+ pos += a[1];
118
+ break;
119
+ case OT.OpCursor:
120
+ if (a[2] != '') // should have clientID...
121
+ {
122
+ let sel: any = cursors[a[2]]; // a[2] is clientID
123
+ if (sel === undefined) { sel = { }; cursors[a[2]] = sel; }
124
+ if (a[1] == 0)
125
+ sel['selectionStart'] = pos; // 0 is selectionStart
126
+ else
127
+ sel['selectionEnd'] = pos; // 1 is selectionEnd
128
+ }
129
+ break;
130
+ case OT.OpSet:
131
+ pos += a[1];
132
+ break;
133
+ }
134
+ }
135
+ return cursors;
136
+ }
137
+
138
+ //
139
+ // Function: computeEdit
140
+ //
141
+ // Description:
142
+ // Given an old and new string, generate the (minimal) edits list necessary to convert the old
143
+ // string into the new string.
144
+ //
145
+ // This is useful if you're not actually tracking the specfic edit operations happening to the
146
+ // underlying string but rather just examining old and new values and trying to transmit
147
+ // minimal diffs.
148
+ //
149
+ // There are various good algorithms for computing the "edit distance" between two strings.
150
+ // Here I've used the google DiffMatchPatch algorithm.
151
+ //
152
+
153
+ computeEdit(sOld: string, sNew: string): OT.OTCompositeResource
154
+ {
155
+ let edit = new OT.OTCompositeResource(this.resourceID, this.clientID);
156
+ let sEdit = new OT.OTStringResource(this.resourceName);
157
+ let diffs: DMP.Diff[] = DiffMatchPatch.diff_main(sOld, sNew);
158
+ if (diffs)
159
+ for (let i: number = 0; i < diffs.length; i++)
160
+ {
161
+ let diff: DMP.Diff = diffs[i];
162
+ let s = diff[1];
163
+ switch (diff[0])
164
+ {
165
+ case DIFF_DELETE:
166
+ sEdit.edits.push([ OT.OpDelete, s.length, '' ]);
167
+ break;
168
+ case DIFF_INSERT:
169
+ sEdit.edits.push([ OT.OpInsert, s.length, s ]);
170
+ break;
171
+ case DIFF_EQUAL:
172
+ sEdit.edits.push([ OT.OpRetain, s.length, '' ]);
173
+ break;
174
+ }
175
+ }
176
+
177
+ edit.edits.push(sEdit);
178
+ return edit;
179
+ }
180
+ }
@@ -0,0 +1,209 @@
1
+ import * as OT from '../ot-js/all';
2
+ import * as Util from '../util/all';
3
+
4
+ export const MERGE: number = 0;
5
+ export const REPLACE: number = 1;
6
+
7
+ export class Editor
8
+ {
9
+ private engine: OT.OTEngine;
10
+ private root: OT.OTCompositeResource;
11
+ private stamps: OT.OTCounterResource;
12
+
13
+ constructor(engine: OT.OTEngine)
14
+ {
15
+ this.engine = engine;
16
+ this.root = null;
17
+ this.stamps = null;
18
+ }
19
+
20
+ startLocalEdit(): void
21
+ {
22
+ if (this.root == null)
23
+ this.root = this.engine.startLocalEdit();
24
+ }
25
+
26
+ stamp(rid: string, prop: string): void
27
+ {
28
+ if (this.stamps == null)
29
+ {
30
+ this.startLocalEdit();
31
+ this.stamps = new OT.OTCounterResource(rid);
32
+ this.root.edits.push(this.stamps);
33
+ }
34
+ this.stamps.edits.push([ OT.OpCounterAdd, prop, 1 ]);
35
+ }
36
+
37
+ set(rid: string, p: string, v: any): Editor
38
+ {
39
+ this.rawObject(rid).set(p, v);
40
+ return this;
41
+ }
42
+
43
+ del(rid: string, p: string): Editor
44
+ {
45
+ this.rawObject(rid).del(p);
46
+ return this;
47
+ }
48
+
49
+ rawObject(rid: string): OT.OTMapResource
50
+ {
51
+ this.startLocalEdit();
52
+ return this.root.map(rid);
53
+ }
54
+
55
+ rawArray(rid: string): OT.OTArrayResource
56
+ {
57
+ this.startLocalEdit();
58
+ return this.root.array(rid);
59
+ }
60
+
61
+ editCounter(rid: string, oNew: any, mode: number = MERGE): void
62
+ {
63
+ let oOld = this.engine.toPartialValue(rid);
64
+ let aDiff: string[] = keyDiff(oOld, oNew);
65
+
66
+ let mEdit = new OT.OTCounterResource(rid);
67
+
68
+ // Propagate sets
69
+ for (let i: number = 0; i < aDiff.length; i++)
70
+ mEdit.edits.push([ OT.OpCounterAdd, aDiff[i], oNew[aDiff[i]] ]);
71
+
72
+ // Also propagate deletes if REPLACE mode
73
+ if (mode == REPLACE)
74
+ {
75
+ for (var p in oOld) if (oOld.hasOwnProperty(p))
76
+ if (oNew[p] === undefined)
77
+ mEdit.edits.push([ OT.OpCounterDel, p, 0 ]);
78
+ }
79
+
80
+ if (mEdit.edits.length)
81
+ {
82
+ this.startLocalEdit();
83
+ this.root.edits.push(mEdit);
84
+ }
85
+ }
86
+
87
+ editObject(rid: string, oNew: any, mode: number = MERGE): void
88
+ {
89
+ if (rid === 'stamps')
90
+ {
91
+ this.editCounter(rid, oNew, mode);
92
+ return;
93
+ }
94
+
95
+ let oOld = this.engine.toPartialValue(rid);
96
+ let aDiff: string[] = keyDiff(oOld, oNew);
97
+
98
+ let mEdit = new OT.OTMapResource(rid);
99
+
100
+ // Propagate sets
101
+ for (let i: number = 0; i < aDiff.length; i++)
102
+ mEdit.edits.push([ OT.OpMapSet, aDiff[i], oNew[aDiff[i]] ]);
103
+
104
+ // Also propagate deletes if REPLACE mode
105
+ if (mode == REPLACE)
106
+ {
107
+ for (var p in oOld) if (oOld.hasOwnProperty(p))
108
+ if (oNew[p] === undefined)
109
+ mEdit.edits.push([ OT.OpMapDel, p, 0 ]);
110
+ }
111
+
112
+ if (mEdit.edits.length)
113
+ {
114
+ this.startLocalEdit();
115
+ this.root.edits.push(mEdit);
116
+ }
117
+ }
118
+
119
+ editArray(rid: string, aNew: any[]): void
120
+ {
121
+ let aOld = this.engine.toPartialValue(rid);
122
+ if (aOld == null) aOld = [];
123
+ if (Util.deepEqual(aNew, aOld))
124
+ return;
125
+
126
+ let aEdit = new OT.OTArrayResource(rid);
127
+
128
+ let i: number = 0;
129
+ let iRetain: number = 0;
130
+ for (; i < aNew.length && i < aOld.length; i++)
131
+ if (Util.deepEqual(aNew[i], aOld[i]))
132
+ iRetain++;
133
+ else
134
+ {
135
+ if (iRetain > 0)
136
+ {
137
+ aEdit.edits.push([ OT.OpRetain, iRetain, [] ] );
138
+ iRetain = 0;
139
+ }
140
+ aEdit.edits.push([ OT.OpSet, 1, [ aNew[i] ] ]);
141
+ }
142
+ // Preserve equal ones at end
143
+ if (iRetain > 0)
144
+ aEdit.edits.push([ OT.OpRetain, iRetain, [] ] );
145
+ // Add new ones
146
+ if (aNew.length > i)
147
+ aEdit.edits.push([ OT.OpInsert, aNew.length - i, aNew.slice(i) ]);
148
+ // Or delete old ones
149
+ else if (aOld.length > i)
150
+ aEdit.edits.push([ OT.OpDelete, aOld.length - i, [] ]);
151
+
152
+ if (aEdit.edits.length)
153
+ {
154
+ this.startLocalEdit();
155
+ this.root.edits.push(aEdit);
156
+ }
157
+ }
158
+
159
+ edit(oNew: any): boolean
160
+ {
161
+ for (var p in oNew) if (oNew.hasOwnProperty(p))
162
+ if (Array.isArray(oNew[p]))
163
+ this.editArray(p, oNew[p]);
164
+ else
165
+ this.editObject(p, oNew[p]);
166
+ return this.commit();
167
+ }
168
+
169
+ get length(): number
170
+ {
171
+ return this.root && this.root.edits ? this.root.edits.length : 0;
172
+ }
173
+
174
+ commit(): boolean
175
+ {
176
+ if (this.root)
177
+ {
178
+ try
179
+ {
180
+ if (this.root.edits.length)
181
+ this.engine.addLocalEdit(this.root);
182
+ this.root = null;
183
+ this.stamps = null;
184
+ return true;
185
+ }
186
+ catch (err)
187
+ {
188
+ // Really would like to log this, but effectively this is a programming error of mismatched bases.
189
+ return false;
190
+ }
191
+ }
192
+ }
193
+ }
194
+
195
+ function keyDiff(oOld: any, oNew: any): string[]
196
+ {
197
+ if (oNew == null)
198
+ return [];
199
+
200
+ let aDiff: string[] = [];
201
+ for (var p in oNew) if (oNew.hasOwnProperty(p))
202
+ {
203
+ if (oOld == null || oOld[p] === undefined)
204
+ aDiff.push(p)
205
+ else if (! Util.deepEqual(oNew[p], oOld[p]))
206
+ aDiff.push(p);
207
+ }
208
+ return aDiff;
209
+ }
@@ -0,0 +1,9 @@
1
+ export * from "./otarray";
2
+ export * from "./otengine";
3
+ export * from "./otclientengine";
4
+ export * from "./otcomposite";
5
+ export * from "./otmap";
6
+ export * from "./otcounter";
7
+ export * from "./otserverengine";
8
+ export * from "./ottypes";
9
+ export * from "./otsession";