@fluid-experimental/sequence-deprecated 2.0.0-dev.2.2.0.111723

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 (63) hide show
  1. package/.editorconfig +7 -0
  2. package/.eslintrc.js +17 -0
  3. package/.vscode/launch.json +14 -0
  4. package/LICENSE +21 -0
  5. package/README.md +3 -0
  6. package/api-extractor.json +12 -0
  7. package/dist/index.d.ts +8 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +24 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/packageVersion.d.ts +9 -0
  12. package/dist/packageVersion.d.ts.map +1 -0
  13. package/dist/packageVersion.js +12 -0
  14. package/dist/packageVersion.js.map +1 -0
  15. package/dist/sequenceFactory.d.ts +95 -0
  16. package/dist/sequenceFactory.d.ts.map +1 -0
  17. package/dist/sequenceFactory.js +152 -0
  18. package/dist/sequenceFactory.js.map +1 -0
  19. package/dist/sharedNumberSequence.d.ts +50 -0
  20. package/dist/sharedNumberSequence.d.ts.map +1 -0
  21. package/dist/sharedNumberSequence.js +67 -0
  22. package/dist/sharedNumberSequence.js.map +1 -0
  23. package/dist/sharedObjectSequence.d.ts +50 -0
  24. package/dist/sharedObjectSequence.d.ts.map +1 -0
  25. package/dist/sharedObjectSequence.js +61 -0
  26. package/dist/sharedObjectSequence.js.map +1 -0
  27. package/dist/sparsematrix.d.ts +152 -0
  28. package/dist/sparsematrix.d.ts.map +1 -0
  29. package/dist/sparsematrix.js +345 -0
  30. package/dist/sparsematrix.js.map +1 -0
  31. package/lib/index.d.ts +8 -0
  32. package/lib/index.d.ts.map +1 -0
  33. package/lib/index.js +8 -0
  34. package/lib/index.js.map +1 -0
  35. package/lib/packageVersion.d.ts +9 -0
  36. package/lib/packageVersion.d.ts.map +1 -0
  37. package/lib/packageVersion.js +9 -0
  38. package/lib/packageVersion.js.map +1 -0
  39. package/lib/sequenceFactory.d.ts +95 -0
  40. package/lib/sequenceFactory.d.ts.map +1 -0
  41. package/lib/sequenceFactory.js +147 -0
  42. package/lib/sequenceFactory.js.map +1 -0
  43. package/lib/sharedNumberSequence.d.ts +50 -0
  44. package/lib/sharedNumberSequence.d.ts.map +1 -0
  45. package/lib/sharedNumberSequence.js +63 -0
  46. package/lib/sharedNumberSequence.js.map +1 -0
  47. package/lib/sharedObjectSequence.d.ts +50 -0
  48. package/lib/sharedObjectSequence.d.ts.map +1 -0
  49. package/lib/sharedObjectSequence.js +57 -0
  50. package/lib/sharedObjectSequence.js.map +1 -0
  51. package/lib/sparsematrix.d.ts +152 -0
  52. package/lib/sparsematrix.d.ts.map +1 -0
  53. package/lib/sparsematrix.js +336 -0
  54. package/lib/sparsematrix.js.map +1 -0
  55. package/package.json +97 -0
  56. package/src/index.ts +22 -0
  57. package/src/packageVersion.ts +9 -0
  58. package/src/sequenceFactory.ts +181 -0
  59. package/src/sharedNumberSequence.ts +68 -0
  60. package/src/sharedObjectSequence.ts +62 -0
  61. package/src/sparsematrix.ts +434 -0
  62. package/tsconfig.esnext.json +7 -0
  63. package/tsconfig.json +14 -0
@@ -0,0 +1,434 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { assert } from "@fluidframework/common-utils";
7
+ import { IFluidHandle } from "@fluidframework/core-interfaces";
8
+ import {
9
+ BaseSegment,
10
+ createGroupOp,
11
+ IJSONSegment,
12
+ IMergeTreeDeltaOp,
13
+ ISegment,
14
+ PropertySet,
15
+ } from "@fluidframework/merge-tree";
16
+ import {
17
+ IChannelAttributes,
18
+ IFluidDataStoreRuntime,
19
+ IChannelServices,
20
+ IChannelFactory,
21
+ Serializable,
22
+ Jsonable,
23
+ } from "@fluidframework/datastore-definitions";
24
+ import { SharedSegmentSequence, SubSequence } from "@fluidframework/sequence";
25
+ import { ISharedObject } from "@fluidframework/shared-object-base";
26
+ import { pkgVersion } from "./packageVersion";
27
+
28
+ /**
29
+ * An empty segment that occupies 'cachedLength' positions.
30
+ * {@link SparseMatrix} uses `PaddingSegment` to "pad" a run of unoccupied cells.
31
+ *
32
+ * @deprecated `PaddingSegment` is part of an abandoned prototype.
33
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
34
+ */
35
+ export class PaddingSegment extends BaseSegment {
36
+ public static readonly typeString = "PaddingSegment";
37
+ public static is(segment: ISegment): segment is PaddingSegment {
38
+ return segment.type === PaddingSegment.typeString;
39
+ }
40
+ public static fromJSONObject(spec: any) {
41
+ if (spec && typeof spec === "object" && "pad" in spec) {
42
+ const segment = new PaddingSegment(spec.pad);
43
+ if (spec.props) {
44
+ segment.addProperties(spec.props);
45
+ }
46
+ return segment;
47
+ }
48
+ return undefined;
49
+ }
50
+ public readonly type = PaddingSegment.typeString;
51
+
52
+ constructor(size: number) {
53
+ super();
54
+ this.cachedLength = size;
55
+ }
56
+
57
+ public toJSONObject() {
58
+ return { pad: this.cachedLength, props: this.properties };
59
+ }
60
+
61
+ public clone(start = 0, end?: number) {
62
+ const b = new PaddingSegment(this.cachedLength);
63
+ this.cloneInto(b);
64
+ return b;
65
+ }
66
+
67
+ public canAppend(segment: ISegment) {
68
+ return PaddingSegment.is(segment);
69
+ }
70
+
71
+ public toString() {
72
+ return `[padding: ${this.cachedLength}]`;
73
+ }
74
+
75
+ public append(segment: ISegment) {
76
+ assert(PaddingSegment.is(segment), "can only append padding segment");
77
+ super.append(segment);
78
+ }
79
+
80
+ // Returns true if entire run removed
81
+ public removeRange(start: number, end: number) {
82
+ this.cachedLength -= (end - start);
83
+ return (this.cachedLength === 0);
84
+ }
85
+
86
+ protected createSplitSegmentAt(pos: number) {
87
+ const leftLength = pos;
88
+ const rightLength = this.cachedLength - pos;
89
+
90
+ this.cachedLength = leftLength;
91
+ return new PaddingSegment(rightLength);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * @deprecated `SparseMatrixItem` is part of an abandoned prototype.
97
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
98
+ */
99
+ export type SparseMatrixItem = Serializable;
100
+
101
+ /**
102
+ * @deprecated `RunSegment` is part of an abandoned prototype.
103
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
104
+ */
105
+ export class RunSegment extends SubSequence<SparseMatrixItem> {
106
+ public static readonly typeString = "RunSegment";
107
+ public static is(segment: ISegment): segment is RunSegment {
108
+ return segment.type === RunSegment.typeString;
109
+ }
110
+ public static fromJSONObject(spec: any) {
111
+ if (spec && typeof spec === "object" && "items" in spec) {
112
+ const segment = new RunSegment(spec.items);
113
+ if (spec.props) {
114
+ segment.addProperties(spec.props);
115
+ }
116
+ return segment;
117
+ }
118
+ return undefined;
119
+ }
120
+ public readonly type = RunSegment.typeString;
121
+
122
+ private tags: any[];
123
+
124
+ constructor(public items: SparseMatrixItem[]) {
125
+ super(items);
126
+ this.tags = new Array(items.length).fill(undefined);
127
+ }
128
+
129
+ public clone(start = 0, end?: number) {
130
+ const b = new RunSegment(this.items.slice(start, end));
131
+ if (this.tags) {
132
+ b.tags = this.tags.slice(start, end);
133
+ }
134
+ this.cloneInto(b);
135
+ return b;
136
+ }
137
+
138
+ public append(segment: ISegment) {
139
+ super.append(segment);
140
+
141
+ const asRun = segment as RunSegment;
142
+ if (asRun.tags) {
143
+ if (this.tags) {
144
+ this.tags.splice(this.items.length, 0, ...asRun.tags);
145
+ }
146
+ }
147
+
148
+ return this;
149
+ }
150
+
151
+ // TODO: retain removed items for undo
152
+ // returns true if entire run removed
153
+ public removeRange(start: number, end: number) {
154
+ this.tags.splice(start, end - start);
155
+ return super.removeRange(start, end);
156
+ }
157
+
158
+ public getTag(pos: number) {
159
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
160
+ return this.tags[pos];
161
+ }
162
+
163
+ public setTag(pos: number, tag: any) {
164
+ this.tags[pos] = tag;
165
+ }
166
+
167
+ protected createSplitSegmentAt(pos: number) {
168
+ if (pos > 0) {
169
+ const remainingItems = this.items.slice(pos);
170
+ this.items = this.items.slice(0, pos);
171
+ this.cachedLength = this.items.length;
172
+
173
+ const leafSegment = new RunSegment(remainingItems);
174
+ leafSegment.tags = this.tags.slice(pos);
175
+ this.tags.length = pos;
176
+
177
+ return leafSegment;
178
+ }
179
+ }
180
+ }
181
+
182
+ /**
183
+ * @deprecated `MatrixSegment` is part of an abandoned prototype.
184
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
185
+ */
186
+ export type MatrixSegment = RunSegment | PaddingSegment;
187
+
188
+ /**
189
+ * @deprecated `maxCol` is part of an abandoned prototype.
190
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
191
+ */
192
+ export const maxCol = 0x200000; // X128 Excel maximum of 16,384 columns
193
+
194
+ /**
195
+ * @deprecated `maxCols` is part of an abandoned prototype.
196
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
197
+ */
198
+ export const maxCols = maxCol + 1;
199
+
200
+ /**
201
+ * @deprecated `maxRow` is part of an abandoned prototype.
202
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
203
+ */
204
+ export const maxRow = 0xFFFFFFFF; // X4096 Excel maximum of 1,048,576 rows
205
+
206
+ /**
207
+ * @deprecated `maxRows` is part of an abandoned prototype.
208
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
209
+ */
210
+ export const maxRows = maxRow + 1;
211
+
212
+ /**
213
+ * @deprecated `maxCellPosition` is part of an abandoned prototype.
214
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
215
+ */
216
+ export const maxCellPosition = maxCol * maxRow;
217
+
218
+ /**
219
+ * @deprecated `positionToRowCol` is part of an abandoned prototype.
220
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
221
+ */
222
+ export const rowColToPosition = (row: number, col: number) => row * maxCols + col;
223
+
224
+ /**
225
+ * @deprecated `positionToRowCol` is part of an abandoned prototype.
226
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
227
+ */
228
+ export function positionToRowCol(position: number) {
229
+ const row = Math.floor(position / maxCols);
230
+ const col = position - (row * maxCols);
231
+ return { row, col };
232
+ }
233
+
234
+ /**
235
+ * @deprecated `SparseMatrix` is an abandoned prototype.
236
+ * Use {@link @fluidframework/matrix#SharedMatrix} instead.
237
+ */
238
+ export class SparseMatrix extends SharedSegmentSequence<MatrixSegment> {
239
+ /**
240
+ * Create a new sparse matrix
241
+ *
242
+ * @param runtime - data store runtime the new sparse matrix belongs to
243
+ * @param id - optional name of the sparse matrix
244
+ * @returns newly create sparse matrix (but not attached yet)
245
+ */
246
+ public static create(runtime: IFluidDataStoreRuntime, id?: string) {
247
+ return runtime.createChannel(id, SparseMatrixFactory.Type) as SparseMatrix;
248
+ }
249
+
250
+ /**
251
+ * Get a factory for SharedMap to register with the data store.
252
+ *
253
+ * @returns a factory that creates and load SharedMap
254
+ */
255
+ public static getFactory(): IChannelFactory {
256
+ return new SparseMatrixFactory();
257
+ }
258
+
259
+ constructor(document: IFluidDataStoreRuntime, public id: string, attributes: IChannelAttributes) {
260
+ super(document, id, attributes, SparseMatrixFactory.segmentFromSpec);
261
+ }
262
+
263
+ public get numRows() {
264
+ return positionToRowCol(this.getLength()).row;
265
+ }
266
+
267
+ public setItems(
268
+ row: number,
269
+ col: number,
270
+ values: SparseMatrixItem[],
271
+ props?: PropertySet,
272
+ ) {
273
+ const start = rowColToPosition(row, col);
274
+ const end = start + values.length;
275
+ const segment = new RunSegment(values);
276
+ if (props) {
277
+ segment.addProperties(props);
278
+ }
279
+
280
+ this.replaceRange(start, end, segment);
281
+ }
282
+
283
+ public getItem(row: number, col: number):
284
+ // The return type is defined explicitly here to prevent TypeScript from generating dynamic imports
285
+ Jsonable<string | number | boolean | IFluidHandle> | undefined {
286
+ const pos = rowColToPosition(row, col);
287
+ const { segment, offset } = this.getContainingSegment(pos);
288
+ if (segment && RunSegment.is(segment)) {
289
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
290
+ return segment.items[offset ?? 0];
291
+ } else if (segment && PaddingSegment.is(segment)) {
292
+ return undefined;
293
+ }
294
+
295
+ throw new Error(`Unrecognized Segment type`);
296
+ }
297
+
298
+ public getTag(row: number, col: number) {
299
+ const { segment, offset } = this.getSegment(row, col);
300
+ if (segment && RunSegment.is(segment)) {
301
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
302
+ return segment.getTag(offset ?? 0);
303
+ }
304
+ return undefined;
305
+ }
306
+
307
+ public setTag(row: number, col: number, tag: any) {
308
+ const { segment, offset } = this.getSegment(row, col);
309
+ if (segment && RunSegment.is(segment)) {
310
+ segment.setTag(offset ?? 0, tag);
311
+ } else if (tag !== undefined) {
312
+ throw new Error(`Must not attempt to set tags on '${segment?.constructor.name}'.`);
313
+ }
314
+ }
315
+
316
+ public insertRows(row: number, numRows: number) {
317
+ const pos = rowColToPosition(row, 0);
318
+ const size = maxCols * numRows;
319
+ const segment = new PaddingSegment(size);
320
+
321
+ const insertOp = this.client.insertSegmentLocal(pos, segment);
322
+ if (insertOp) {
323
+ this.submitSequenceMessage(insertOp);
324
+ }
325
+ }
326
+
327
+ public removeRows(row: number, numRows: number) {
328
+ const pos = rowColToPosition(row, 0);
329
+ const size = maxCols * numRows;
330
+ this.removeRange(pos, pos + size);
331
+ }
332
+
333
+ public insertCols(col: number, numCols: number) {
334
+ this.moveAsPadding(maxCol - numCols, col, numCols);
335
+ }
336
+
337
+ public removeCols(col: number, numCols: number) {
338
+ this.moveAsPadding(col, maxCol - numCols, numCols);
339
+ }
340
+
341
+ public annotatePosition(row: number, col: number, props: PropertySet) {
342
+ const pos = rowColToPosition(row, col);
343
+ this.annotateRange(pos, pos + 1, props);
344
+ }
345
+
346
+ public getPositionProperties(row: number, col: number) {
347
+ const pos = rowColToPosition(row, col);
348
+ return this.getPropertiesAtPosition(pos);
349
+ }
350
+
351
+ // For each row, moves 'numCols' items starting from 'srcCol' and inserts 'numCols' padding
352
+ // at 'destCol'. Used by insertCols and removeCols.
353
+ private moveAsPadding(srcCol: number, destCol: number, numCols: number) {
354
+ const removeColStart = srcCol;
355
+ const removeColEnd = srcCol + numCols;
356
+ const ops: IMergeTreeDeltaOp[] = [];
357
+
358
+ for (let r = 0, rowStart = 0; r < this.numRows; r++, rowStart += maxCols) {
359
+ const removeMsg = this.client.removeRangeLocal(rowStart + removeColStart, rowStart + removeColEnd);
360
+ if (removeMsg) {
361
+ ops.push(removeMsg);
362
+ }
363
+ const insertPos = rowStart + destCol;
364
+ const segment = new PaddingSegment(numCols);
365
+ const insertMsg = this.client.insertSegmentLocal(insertPos, segment);
366
+ if (insertMsg) {
367
+ ops.push(insertMsg);
368
+ }
369
+ }
370
+
371
+ this.submitSequenceMessage(createGroupOp(...ops));
372
+ }
373
+
374
+ private getSegment(row: number, col: number) {
375
+ const pos = rowColToPosition(row, col);
376
+ return this.getContainingSegment(pos);
377
+ }
378
+ }
379
+
380
+ /**
381
+ * @deprecated `SparseMatrixFactory` is an abandoned prototype.
382
+ * Use {@link @fluidframework/matrix#SharedMatrixFactory} instead.
383
+ */
384
+ export class SparseMatrixFactory implements IChannelFactory {
385
+ public static Type = "https://graph.microsoft.com/types/mergeTree/sparse-matrix";
386
+
387
+ public static Attributes: IChannelAttributes = {
388
+ type: SparseMatrixFactory.Type,
389
+ snapshotFormatVersion: "0.1",
390
+ packageVersion: pkgVersion,
391
+ };
392
+
393
+ public static segmentFromSpec(spec: IJSONSegment): ISegment {
394
+ const maybePadding = PaddingSegment.fromJSONObject(spec);
395
+ if (maybePadding) {
396
+ return maybePadding;
397
+ }
398
+
399
+ const maybeRun = RunSegment.fromJSONObject(spec);
400
+ if (maybeRun) {
401
+ return maybeRun;
402
+ }
403
+
404
+ throw new Error(`Unrecognized IJSONObject: '${JSON.stringify(spec)}'`);
405
+ }
406
+
407
+ public get type() {
408
+ return SparseMatrixFactory.Type;
409
+ }
410
+
411
+ public get attributes() {
412
+ return SparseMatrixFactory.Attributes;
413
+ }
414
+
415
+ /**
416
+ * {@inheritDoc @fluidframework/datastore-definitions#IChannelFactory.load}
417
+ */
418
+ public async load(
419
+ runtime: IFluidDataStoreRuntime,
420
+ id: string,
421
+ services: IChannelServices,
422
+ attributes: IChannelAttributes,
423
+ ): Promise<ISharedObject> {
424
+ const sharedObject = new SparseMatrix(runtime, id, attributes);
425
+ await sharedObject.load(services);
426
+ return sharedObject;
427
+ }
428
+
429
+ public create(document: IFluidDataStoreRuntime, id: string): ISharedObject {
430
+ const sharedObject = new SparseMatrix(document, id, this.attributes);
431
+ sharedObject.initializeLocal();
432
+ return sharedObject;
433
+ }
434
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib",
5
+ "module": "esnext"
6
+ },
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "@fluidframework/build-common/ts-common-config.json",
3
+ "exclude": [
4
+ "src/test/**/*"
5
+ ],
6
+ "compilerOptions": {
7
+ "rootDir": "./src",
8
+ "outDir": "./dist",
9
+ "composite": true
10
+ },
11
+ "include": [
12
+ "src/**/*"
13
+ ]
14
+ }