@encorejs/saaz 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 (50) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +1 -0
  3. package/dist/back/BackMemoryAdapter.d.ts +18 -0
  4. package/dist/back/BackMemoryAdapter.d.ts.map +1 -0
  5. package/dist/back/BackMemoryAdapter.js +69 -0
  6. package/dist/back/BackStorage.d.ts +20 -0
  7. package/dist/back/BackStorage.d.ts.map +1 -0
  8. package/dist/back/BackStorage.js +22 -0
  9. package/dist/back/SaazBack.d.ts +60 -0
  10. package/dist/back/SaazBack.d.ts.map +1 -0
  11. package/dist/back/SaazBack.js +188 -0
  12. package/dist/front/FrontIdbAdapter.d.ts +15 -0
  13. package/dist/front/FrontIdbAdapter.d.ts.map +1 -0
  14. package/dist/front/FrontIdbAdapter.js +159 -0
  15. package/dist/front/FrontMemoryAdapter.d.ts +16 -0
  16. package/dist/front/FrontMemoryAdapter.d.ts.map +1 -0
  17. package/dist/front/FrontMemoryAdapter.js +156 -0
  18. package/dist/front/FrontStorage.d.ts +23 -0
  19. package/dist/front/FrontStorage.d.ts.map +1 -0
  20. package/dist/front/FrontStorage.js +85 -0
  21. package/dist/front/SaazFront.d.ts +113 -0
  22. package/dist/front/SaazFront.d.ts.map +1 -0
  23. package/dist/front/SaazFront.js +756 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +9295 -0
  27. package/dist/index.js.map +7 -0
  28. package/dist/index.test.d.ts +2 -0
  29. package/dist/index.test.d.ts.map +1 -0
  30. package/dist/index.test.js +166 -0
  31. package/dist/rogue.d.ts +63 -0
  32. package/dist/rogue.d.ts.map +1 -0
  33. package/dist/rogue.js +548 -0
  34. package/dist/rogue.test.d.ts +2 -0
  35. package/dist/rogue.test.d.ts.map +1 -0
  36. package/dist/rogue.test.js +248 -0
  37. package/dist/shared/GeneratorSpy.d.ts +4 -0
  38. package/dist/shared/GeneratorSpy.d.ts.map +1 -0
  39. package/dist/shared/GeneratorSpy.js +41 -0
  40. package/dist/shared/transactions.d.ts +4 -0
  41. package/dist/shared/transactions.d.ts.map +1 -0
  42. package/dist/shared/transactions.js +131 -0
  43. package/dist/shared/utils.d.ts +5 -0
  44. package/dist/shared/utils.d.ts.map +1 -0
  45. package/dist/shared/utils.js +21 -0
  46. package/dist/tsdoc-metadata.json +11 -0
  47. package/dist/types.d.ts +240 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +1 -0
  50. package/package.json +54 -0
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,166 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { SaazFront } from './front/SaazFront';
11
+ import { FrontMemoryAdapter } from './front/FrontMemoryAdapter';
12
+ import SaazBack from './back/SaazBack';
13
+ import { BackMemoryAdapter } from './back/BackMemoryAdapter';
14
+ jest.setTimeout(1000);
15
+ describe(`saaz`, () => {
16
+ test('everything', () => __awaiter(void 0, void 0, void 0, function* () {
17
+ let randNum = 10;
18
+ const generators = {
19
+ rand: () => {
20
+ return randNum++;
21
+ },
22
+ };
23
+ const opEditors = {
24
+ increaseBy(state, generators, opts) {
25
+ var _a;
26
+ let count = (_a = state.opCount) !== null && _a !== void 0 ? _a : 0;
27
+ state.opCount = count + opts.by;
28
+ },
29
+ // this is a bad editor because it uses a random number generator, so it's not deterministic.
30
+ // we expect saaz.tx() to throw an error if we try to use it.
31
+ randomizeCountBadly(state, generators, opts) {
32
+ state.opCount = Math.random();
33
+ },
34
+ // this is a good editor because it uses a random number generator, but it's deterministic.
35
+ randomizeCountWell(state, generators, opts) {
36
+ state.opCount = generators.rand();
37
+ },
38
+ };
39
+ const schema = {
40
+ opShape: null,
41
+ // migrateOp(state: $IntentionalAny) {},
42
+ // migrateCell(s) {},
43
+ version: 1,
44
+ editors: opEditors,
45
+ generators: generators,
46
+ cellShape: null,
47
+ };
48
+ const mem = new FrontMemoryAdapter();
49
+ const backend = new SaazBack({
50
+ storageAdapter: new BackMemoryAdapter(),
51
+ dbName: 'test',
52
+ schema,
53
+ });
54
+ const saaz = new SaazFront({
55
+ schema,
56
+ backend,
57
+ peerId: 'peer1',
58
+ storageAdapter: mem,
59
+ dbName: 'test',
60
+ });
61
+ yield saaz.ready;
62
+ expect(saaz.state.op.opCount).toEqual(undefined);
63
+ expect(saaz.state.cell.cellCount).toEqual(undefined);
64
+ expect(() => saaz.tx((editors) => {
65
+ editors.randomizeCountBadly({});
66
+ })).toThrow();
67
+ expect(() => saaz.tx(undefined, (draft) => {
68
+ draft.cellCount = 1;
69
+ throw new Error('oops');
70
+ })).toThrow();
71
+ expect(saaz.state.op.opCount).toEqual(undefined);
72
+ expect(saaz.state.cell.cellCount).toEqual(undefined);
73
+ expect(() => saaz.tx((editors) => {
74
+ editors.increaseBy({ by: 1 });
75
+ throw new Error('oops');
76
+ })).toThrow();
77
+ expect(saaz.state.op.opCount).toEqual(undefined);
78
+ expect(() => saaz.tx((editors) => {
79
+ editors.randomizeCountWell({});
80
+ })).not.toThrow();
81
+ yield new Promise((resolve) => setTimeout(resolve, 100));
82
+ expect(saaz.state.op.opCount).toEqual(10);
83
+ saaz.tx(undefined, (draft) => {
84
+ draft.cellCount = 1;
85
+ });
86
+ expect(saaz.state.cell.cellCount).toEqual(1);
87
+ saaz.tx((editors) => {
88
+ editors.increaseBy({ by: 3 });
89
+ });
90
+ expect(saaz.state.op.opCount).toEqual(13);
91
+ yield saaz.waitForBackendSync();
92
+ yield saaz.waitForStorageSync();
93
+ expect(mem.export().sessions['peer1'].keyval.sessionState
94
+ .value.op).toEqual({ opCount: 13 });
95
+ // const fauxBackennd: SaazBackInterface = {
96
+ // async getUpdatesSinceClock() {
97
+ // return {clock: null, hasUpdates: false}
98
+ // },
99
+ // applyUpdates(opts) {
100
+ // return Promise.resolve({ok: true, hasUpdates: false})
101
+ // },
102
+ // updatePresence(opts) {
103
+ // return Promise.resolve({ok: true})
104
+ // },
105
+ // async subscribe() {
106
+ // return () => {}
107
+ // },
108
+ // }
109
+ const saaz2 = new SaazFront({
110
+ schema,
111
+ peerId: '2',
112
+ storageAdapter: new FrontMemoryAdapter(),
113
+ dbName: 'test',
114
+ backend: backend,
115
+ });
116
+ yield saaz2.ready;
117
+ yield saaz2.waitForBackendSync();
118
+ expect(saaz2.state).toEqual(saaz.state);
119
+ const saaz3 = new SaazFront({
120
+ schema,
121
+ peerId: '3',
122
+ storageAdapter: new FrontMemoryAdapter(),
123
+ dbName: 'test',
124
+ backend: backend,
125
+ });
126
+ yield saaz3.ready;
127
+ yield saaz3.waitForBackendSync();
128
+ expect(saaz3.state).toEqual(saaz.state);
129
+ saaz.tx(undefined, (draft) => {
130
+ draft.cellCount = 2;
131
+ });
132
+ expect(saaz.state.cell.cellCount).toEqual(2);
133
+ saaz.tx(undefined, (draft) => {
134
+ draft.cellCount = 3;
135
+ });
136
+ expect(saaz.state.cell.cellCount).toEqual(3);
137
+ yield saaz.waitForBackendSync();
138
+ yield saaz3.waitForBackendSync();
139
+ expect(saaz3.state).toEqual(saaz.state);
140
+ saaz.undo();
141
+ expect(saaz.state.cell.cellCount).toEqual(2);
142
+ yield saaz.waitForBackendSync();
143
+ yield saaz3.waitForBackendSync();
144
+ expect(saaz3.state).toEqual(saaz.state);
145
+ saaz.undo();
146
+ expect(saaz.state.cell.cellCount).toEqual(1);
147
+ saaz.redo();
148
+ expect(saaz.state.cell.cellCount).toEqual(2);
149
+ saaz.redo();
150
+ expect(saaz.state.cell.cellCount).toEqual(3);
151
+ saaz.undo();
152
+ saaz.undo();
153
+ expect(saaz.state.cell.cellCount).toEqual(1);
154
+ saaz.tx(undefined, (draft) => {
155
+ draft.cellCount = 4;
156
+ });
157
+ expect(saaz.state.cell.cellCount).toEqual(4);
158
+ saaz.redo();
159
+ expect(saaz.state.cell.cellCount).toEqual(4);
160
+ saaz.undo();
161
+ expect(saaz.state.cell.cellCount).toEqual(1);
162
+ saaz.teardown();
163
+ saaz2.teardown();
164
+ saaz3.teardown();
165
+ }));
166
+ });
@@ -0,0 +1,63 @@
1
+ type BranchName = 'base' | string;
2
+ type Branch = {
3
+ $boxedValue?: any;
4
+ $mapProps?: {
5
+ [key in string]?: Cell;
6
+ };
7
+ };
8
+ export type Cell = {
9
+ $type: [type: 'map' | 'boxed' | 'deleted', branchName: BranchName];
10
+ $branches?: {
11
+ [asOf in BranchName]?: Branch;
12
+ };
13
+ };
14
+ type CellToJSON<T extends Cell> = T extends {
15
+ $type: ['boxed', any];
16
+ } ? CellBoxedToJSON<T> : T extends {
17
+ $type: ['map'];
18
+ } ? MapCellToJSON<T> : never;
19
+ type CellBoxedToJSON<T extends Cell> = T['$branches'] extends {
20
+ [key: string]: {
21
+ $boxedValue: infer V;
22
+ };
23
+ } ? V : never;
24
+ type MapCellToJSON<T extends Cell> = T['$branches'] extends {
25
+ [key: string]: {
26
+ $mapProps: infer V;
27
+ };
28
+ } ? {
29
+ [Key in keyof V]: V[Key] extends Cell ? CellToJSON<V[Key]> : never;
30
+ } : never;
31
+ export type Root = Cell;
32
+ declare const NOT_DEFINED: {};
33
+ export type Ops = Op[];
34
+ type Op = ChnangeTypeOp | SetBoxedValue;
35
+ type ChnangeTypeOp = {
36
+ type: 'ChangeType';
37
+ path: Array<[branchName: BranchName, mapProp: string]>;
38
+ value: Cell['$type'];
39
+ };
40
+ type SetBoxedValue = {
41
+ type: 'SetBoxedValue';
42
+ path: Array<[branchName: BranchName, mapProp: string]>;
43
+ branchName: BranchName;
44
+ value: any;
45
+ };
46
+ export declare function makeDraft<S extends Cell>(base: any): [draft: any, finish: () => [cell: Cell, forwardOps: Ops, backwardOps: Ops]];
47
+ export declare function change(base: any, fn: (draft: any) => void): [cell: any, ops: Ops, backwardOps: Ops];
48
+ export declare function fromOps(base: any, ops: Ops): [cell: any];
49
+ export declare const current: <T extends {}>(draft: T) => T;
50
+ export declare function isPlainObject(value: any): boolean;
51
+ declare const BOXED: unique symbol;
52
+ export declare function boxed<V>(value: V): {
53
+ [BOXED]: true;
54
+ value: V;
55
+ };
56
+ declare const RESET: unique symbol;
57
+ export declare function reset<V>(value: V): {
58
+ [RESET]: true;
59
+ value: V;
60
+ };
61
+ export declare function jsonFromCell<V extends Cell | {}>(v: V): V extends Cell ? CellToJSON<V> : typeof NOT_DEFINED;
62
+ export {};
63
+ //# sourceMappingURL=rogue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rogue.d.ts","sourceRoot":"","sources":["../src/rogue.ts"],"names":[],"mappings":"AAMA,KAAK,UAAU,GAAG,MAAM,GAAG,MAAM,CAAA;AAEjC,KAAK,MAAM,GAAG;IACZ,WAAW,CAAC,EAAE,GAAG,CAAA;IACjB,SAAS,CAAC,EAAE;SACT,GAAG,IAAI,MAAM,CAAC,CAAC,EAAE,IAAI;KACvB,CAAA;CACF,CAAA;AAED,MAAM,MAAM,IAAI,GAAG;IACjB,KAAK,EAAE,CAAC,IAAI,EAAE,KAAK,GAAG,OAAO,GAAG,SAAS,EAAE,UAAU,EAAE,UAAU,CAAC,CAAA;IAClE,SAAS,CAAC,EAAE;SACT,IAAI,IAAI,UAAU,CAAC,CAAC,EAAE,MAAM;KAC9B,CAAA;CACF,CAAA;AAED,KAAK,UAAU,CAAC,CAAC,SAAS,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