@colyseus/schema 1.1.0-alpha.0 → 2.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.
- package/README.md +1 -4
- package/build/cjs/index.js +1194 -1001
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +350 -311
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +371 -335
- package/lib/Reflection.d.ts +21 -0
- package/lib/Reflection.js +198 -0
- package/lib/Reflection.js.map +1 -0
- package/lib/Schema.d.ts +66 -0
- package/lib/Schema.js +853 -0
- package/lib/Schema.js.map +1 -0
- package/lib/annotations.d.ts +96 -0
- package/lib/annotations.js +316 -0
- package/lib/annotations.js.map +1 -0
- package/lib/changes/ChangeTree.d.ts +53 -0
- package/lib/changes/ChangeTree.js +209 -0
- package/lib/changes/ChangeTree.js.map +1 -0
- package/lib/changes/ReferenceTracker.d.ts +14 -0
- package/lib/changes/ReferenceTracker.js +77 -0
- package/lib/changes/ReferenceTracker.js.map +1 -0
- package/lib/codegen/api.d.ts +7 -0
- package/lib/codegen/api.js +36 -0
- package/lib/codegen/api.js.map +1 -0
- package/lib/codegen/argv.d.ts +6 -0
- package/lib/codegen/argv.js +41 -0
- package/lib/codegen/argv.js.map +1 -0
- package/lib/codegen/cli.d.ts +1 -0
- package/lib/codegen/cli.js +49 -0
- package/lib/codegen/cli.js.map +1 -0
- package/lib/codegen/languages/cpp.d.ts +3 -0
- package/lib/codegen/languages/cpp.js +213 -0
- package/lib/codegen/languages/cpp.js.map +1 -0
- package/lib/codegen/languages/csharp.d.ts +4 -0
- package/lib/codegen/languages/csharp.js +130 -0
- package/lib/codegen/languages/csharp.js.map +1 -0
- package/lib/codegen/languages/haxe.d.ts +3 -0
- package/lib/codegen/languages/haxe.js +93 -0
- package/lib/codegen/languages/haxe.js.map +1 -0
- package/lib/codegen/languages/java.d.ts +6 -0
- package/lib/codegen/languages/java.js +92 -0
- package/lib/codegen/languages/java.js.map +1 -0
- package/lib/codegen/languages/js.d.ts +3 -0
- package/lib/codegen/languages/js.js +89 -0
- package/lib/codegen/languages/js.js.map +1 -0
- package/lib/codegen/languages/lua.d.ts +3 -0
- package/lib/codegen/languages/lua.js +97 -0
- package/lib/codegen/languages/lua.js.map +1 -0
- package/lib/codegen/languages/ts.d.ts +3 -0
- package/lib/codegen/languages/ts.js +125 -0
- package/lib/codegen/languages/ts.js.map +1 -0
- package/lib/codegen/parser.d.ts +5 -0
- package/lib/codegen/parser.js +196 -0
- package/lib/codegen/parser.js.map +1 -0
- package/lib/codegen/types.d.ts +44 -0
- package/lib/codegen/types.js +122 -0
- package/lib/codegen/types.js.map +1 -0
- package/lib/encoding/decode.d.ts +48 -0
- package/lib/encoding/decode.js +267 -0
- package/lib/encoding/decode.js.map +1 -0
- package/lib/encoding/encode.d.ts +38 -0
- package/lib/encoding/encode.js +281 -0
- package/lib/encoding/encode.js.map +1 -0
- package/lib/events/EventEmitter.d.ts +13 -0
- package/lib/events/EventEmitter.js +62 -0
- package/lib/events/EventEmitter.js.map +1 -0
- package/lib/filters/index.d.ts +8 -0
- package/lib/filters/index.js +25 -0
- package/lib/filters/index.js.map +1 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.js +46 -0
- package/lib/index.js.map +1 -0
- package/lib/spec.d.ts +13 -0
- package/lib/spec.js +42 -0
- package/lib/spec.js.map +1 -0
- package/lib/types/ArraySchema.d.ts +230 -0
- package/lib/types/ArraySchema.js +559 -0
- package/lib/types/ArraySchema.js.map +1 -0
- package/lib/types/CollectionSchema.d.ts +35 -0
- package/lib/types/CollectionSchema.js +158 -0
- package/lib/types/CollectionSchema.js.map +1 -0
- package/lib/types/HelperTypes.d.ts +28 -0
- package/lib/types/HelperTypes.js +3 -0
- package/lib/types/HelperTypes.js.map +1 -0
- package/lib/types/MapSchema.d.ts +37 -0
- package/lib/types/MapSchema.js +214 -0
- package/lib/types/MapSchema.js.map +1 -0
- package/lib/types/SetSchema.d.ts +32 -0
- package/lib/types/SetSchema.js +171 -0
- package/lib/types/SetSchema.js.map +1 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/index.js +13 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/typeRegistry.d.ts +5 -0
- package/lib/types/typeRegistry.js +13 -0
- package/lib/types/typeRegistry.js.map +1 -0
- package/lib/types/utils.d.ts +9 -0
- package/lib/types/utils.js +50 -0
- package/lib/types/utils.js.map +1 -0
- package/lib/utils.d.ts +2 -0
- package/lib/utils.js +26 -0
- package/lib/utils.js.map +1 -0
- package/package.json +4 -8
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ChangeTree } from "../changes/ChangeTree";
|
|
2
|
+
import { SchemaDecoderCallbacks } from "../Schema";
|
|
3
|
+
import { DataChange } from "..";
|
|
4
|
+
declare type K = number;
|
|
5
|
+
export declare class CollectionSchema<V = any> implements SchemaDecoderCallbacks {
|
|
6
|
+
protected $changes: ChangeTree;
|
|
7
|
+
protected $items: Map<number, V>;
|
|
8
|
+
protected $indexes: Map<number, number>;
|
|
9
|
+
protected $refId: number;
|
|
10
|
+
$callbacks: {
|
|
11
|
+
[operation: number]: Array<(item: V, key: string) => void>;
|
|
12
|
+
};
|
|
13
|
+
onAdd(callback: (item: V, key: string) => void, triggerAll?: boolean): () => boolean;
|
|
14
|
+
onRemove(callback: (item: V, key: string) => void): () => boolean;
|
|
15
|
+
onChange(callback: (item: V, key: string) => void): () => boolean;
|
|
16
|
+
static is(type: any): boolean;
|
|
17
|
+
constructor(initialValues?: Array<V>);
|
|
18
|
+
add(value: V): number;
|
|
19
|
+
at(index: number): V | undefined;
|
|
20
|
+
entries(): IterableIterator<[number, V]>;
|
|
21
|
+
delete(item: V): boolean;
|
|
22
|
+
clear(changes?: DataChange[]): void;
|
|
23
|
+
has(value: V): boolean;
|
|
24
|
+
forEach(callbackfn: (value: V, key: K, collection: CollectionSchema<V>) => void): void;
|
|
25
|
+
values(): IterableIterator<V>;
|
|
26
|
+
get size(): number;
|
|
27
|
+
protected setIndex(index: number, key: number): void;
|
|
28
|
+
protected getIndex(index: number): number;
|
|
29
|
+
protected getByIndex(index: number): V;
|
|
30
|
+
protected deleteByIndex(index: number): void;
|
|
31
|
+
toArray(): V[];
|
|
32
|
+
toJSON(): V[];
|
|
33
|
+
clone(isDecoding?: boolean): CollectionSchema<V>;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CollectionSchema = void 0;
|
|
4
|
+
var ChangeTree_1 = require("../changes/ChangeTree");
|
|
5
|
+
var spec_1 = require("../spec");
|
|
6
|
+
var utils_1 = require("./utils");
|
|
7
|
+
var CollectionSchema = /** @class */ (function () {
|
|
8
|
+
function CollectionSchema(initialValues) {
|
|
9
|
+
var _this = this;
|
|
10
|
+
this.$changes = new ChangeTree_1.ChangeTree(this);
|
|
11
|
+
this.$items = new Map();
|
|
12
|
+
this.$indexes = new Map();
|
|
13
|
+
this.$refId = 0;
|
|
14
|
+
if (initialValues) {
|
|
15
|
+
initialValues.forEach(function (v) { return _this.add(v); });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
CollectionSchema.prototype.onAdd = function (callback, triggerAll) {
|
|
19
|
+
if (triggerAll === void 0) { triggerAll = true; }
|
|
20
|
+
return utils_1.addCallback((this.$callbacks || (this.$callbacks = [])), spec_1.OPERATION.ADD, callback, (triggerAll)
|
|
21
|
+
? this.$items
|
|
22
|
+
: undefined);
|
|
23
|
+
};
|
|
24
|
+
CollectionSchema.prototype.onRemove = function (callback) { return utils_1.addCallback(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.DELETE, callback); };
|
|
25
|
+
CollectionSchema.prototype.onChange = function (callback) { return utils_1.addCallback(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.REPLACE, callback); };
|
|
26
|
+
CollectionSchema.is = function (type) {
|
|
27
|
+
return type['collection'] !== undefined;
|
|
28
|
+
};
|
|
29
|
+
CollectionSchema.prototype.add = function (value) {
|
|
30
|
+
// set "index" for reference.
|
|
31
|
+
var index = this.$refId++;
|
|
32
|
+
var isRef = (value['$changes']) !== undefined;
|
|
33
|
+
if (isRef) {
|
|
34
|
+
value['$changes'].setParent(this, this.$changes.root, index);
|
|
35
|
+
}
|
|
36
|
+
this.$changes.indexes[index] = index;
|
|
37
|
+
this.$indexes.set(index, index);
|
|
38
|
+
this.$items.set(index, value);
|
|
39
|
+
this.$changes.change(index);
|
|
40
|
+
return index;
|
|
41
|
+
};
|
|
42
|
+
CollectionSchema.prototype.at = function (index) {
|
|
43
|
+
var key = Array.from(this.$items.keys())[index];
|
|
44
|
+
return this.$items.get(key);
|
|
45
|
+
};
|
|
46
|
+
CollectionSchema.prototype.entries = function () {
|
|
47
|
+
return this.$items.entries();
|
|
48
|
+
};
|
|
49
|
+
CollectionSchema.prototype.delete = function (item) {
|
|
50
|
+
var entries = this.$items.entries();
|
|
51
|
+
var index;
|
|
52
|
+
var entry;
|
|
53
|
+
while (entry = entries.next()) {
|
|
54
|
+
if (entry.done) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
if (item === entry.value[1]) {
|
|
58
|
+
index = entry.value[0];
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (index === undefined) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
this.$changes.delete(index);
|
|
66
|
+
this.$indexes.delete(index);
|
|
67
|
+
return this.$items.delete(index);
|
|
68
|
+
};
|
|
69
|
+
CollectionSchema.prototype.clear = function (changes) {
|
|
70
|
+
// discard previous operations.
|
|
71
|
+
this.$changes.discard(true, true);
|
|
72
|
+
this.$changes.indexes = {};
|
|
73
|
+
// clear previous indexes
|
|
74
|
+
this.$indexes.clear();
|
|
75
|
+
//
|
|
76
|
+
// When decoding:
|
|
77
|
+
// - enqueue items for DELETE callback.
|
|
78
|
+
// - flag child items for garbage collection.
|
|
79
|
+
//
|
|
80
|
+
if (changes) {
|
|
81
|
+
utils_1.removeChildRefs.call(this, changes);
|
|
82
|
+
}
|
|
83
|
+
// clear items
|
|
84
|
+
this.$items.clear();
|
|
85
|
+
this.$changes.operation({ index: 0, op: spec_1.OPERATION.CLEAR });
|
|
86
|
+
// touch all structures until reach root
|
|
87
|
+
this.$changes.touchParents();
|
|
88
|
+
};
|
|
89
|
+
CollectionSchema.prototype.has = function (value) {
|
|
90
|
+
return Array.from(this.$items.values()).some(function (v) { return v === value; });
|
|
91
|
+
};
|
|
92
|
+
CollectionSchema.prototype.forEach = function (callbackfn) {
|
|
93
|
+
var _this = this;
|
|
94
|
+
this.$items.forEach(function (value, key, _) { return callbackfn(value, key, _this); });
|
|
95
|
+
};
|
|
96
|
+
CollectionSchema.prototype.values = function () {
|
|
97
|
+
return this.$items.values();
|
|
98
|
+
};
|
|
99
|
+
Object.defineProperty(CollectionSchema.prototype, "size", {
|
|
100
|
+
get: function () {
|
|
101
|
+
return this.$items.size;
|
|
102
|
+
},
|
|
103
|
+
enumerable: false,
|
|
104
|
+
configurable: true
|
|
105
|
+
});
|
|
106
|
+
CollectionSchema.prototype.setIndex = function (index, key) {
|
|
107
|
+
this.$indexes.set(index, key);
|
|
108
|
+
};
|
|
109
|
+
CollectionSchema.prototype.getIndex = function (index) {
|
|
110
|
+
return this.$indexes.get(index);
|
|
111
|
+
};
|
|
112
|
+
CollectionSchema.prototype.getByIndex = function (index) {
|
|
113
|
+
return this.$items.get(this.$indexes.get(index));
|
|
114
|
+
};
|
|
115
|
+
CollectionSchema.prototype.deleteByIndex = function (index) {
|
|
116
|
+
var key = this.$indexes.get(index);
|
|
117
|
+
this.$items.delete(key);
|
|
118
|
+
this.$indexes.delete(index);
|
|
119
|
+
};
|
|
120
|
+
CollectionSchema.prototype.toArray = function () {
|
|
121
|
+
return Array.from(this.$items.values());
|
|
122
|
+
};
|
|
123
|
+
CollectionSchema.prototype.toJSON = function () {
|
|
124
|
+
var values = [];
|
|
125
|
+
this.forEach(function (value, key) {
|
|
126
|
+
values.push((typeof (value['toJSON']) === "function")
|
|
127
|
+
? value['toJSON']()
|
|
128
|
+
: value);
|
|
129
|
+
});
|
|
130
|
+
return values;
|
|
131
|
+
};
|
|
132
|
+
//
|
|
133
|
+
// Decoding utilities
|
|
134
|
+
//
|
|
135
|
+
CollectionSchema.prototype.clone = function (isDecoding) {
|
|
136
|
+
var cloned;
|
|
137
|
+
if (isDecoding) {
|
|
138
|
+
// client-side
|
|
139
|
+
cloned = Object.assign(new CollectionSchema(), this);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
// server-side
|
|
143
|
+
cloned = new CollectionSchema();
|
|
144
|
+
this.forEach(function (value) {
|
|
145
|
+
if (value['$changes']) {
|
|
146
|
+
cloned.add(value['clone']());
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
cloned.add(value);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return cloned;
|
|
154
|
+
};
|
|
155
|
+
return CollectionSchema;
|
|
156
|
+
}());
|
|
157
|
+
exports.CollectionSchema = CollectionSchema;
|
|
158
|
+
//# sourceMappingURL=CollectionSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CollectionSchema.js","sourceRoot":"","sources":["../../src/types/CollectionSchema.ts"],"names":[],"mappings":";;;AAAA,oDAAmD;AACnD,gCAAoC;AAEpC,iCAAuD;AAKvD;IA6BI,0BAAa,aAAwB;QAArC,iBAIC;QAhCS,aAAQ,GAAe,IAAI,uBAAU,CAAC,IAAI,CAAC,CAAC;QAE5C,WAAM,GAAmB,IAAI,GAAG,EAAa,CAAC;QAC9C,aAAQ,GAAwB,IAAI,GAAG,EAAkB,CAAC;QAE1D,WAAM,GAAW,CAAC,CAAC;QAwBzB,IAAI,aAAa,EAAE;YACf,aAAa,CAAC,OAAO,CAAC,UAAC,CAAC,IAAK,OAAA,KAAI,CAAC,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QACtB,IAAI,MAAwB,CAAC;QAE7B,IAAI,UAAU,EAAE;YACZ,cAAc;YACd,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,gBAAgB,EAAE,EAAE,IAAI,CAAC,CAAC;SAExD;aAAM;YACH,cAAc;YACd,MAAM,GAAG,IAAI,gBAAgB,EAAE,CAAC;YAChC,IAAI,CAAC,OAAO,CAAC,UAAC,KAAK;gBACf,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;oBACnB,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;iBAChC;qBAAM;oBACH,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;iBACrB;YACL,CAAC,CAAC,CAAA;SACL;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;IAEL,uBAAC;AAAD,CAAC,AA9LD,IA8LC;AA9LY,4CAAgB","sourcesContent":["import { ChangeTree } from \"../changes/ChangeTree\";\nimport { OPERATION } from \"../spec\";\nimport { SchemaDecoderCallbacks } from \"../Schema\";\nimport { addCallback, removeChildRefs } from \"./utils\";\nimport { DataChange } from \"..\";\n\ntype K = number; // TODO: allow to specify K generic on MapSchema.\n\nexport class CollectionSchema<V=any> implements SchemaDecoderCallbacks {\n protected $changes: ChangeTree = new ChangeTree(this);\n\n protected $items: Map<number, V> = new Map<number, V>();\n protected $indexes: Map<number, number> = new Map<number, number>();\n\n protected $refId: number = 0;\n\n //\n // Decoding callbacks\n //\n public $callbacks: { [operation: number]: Array<(item: V, key: string) => void> };\n public onAdd(callback: (item: V, key: string) => void, triggerAll: boolean = true) {\n return addCallback(\n (this.$callbacks || (this.$callbacks = [])),\n OPERATION.ADD,\n callback,\n (triggerAll)\n ? this.$items\n : undefined\n );\n }\n public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }\n public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }\n\n static is(type: any) {\n return type['collection'] !== undefined;\n }\n\n constructor (initialValues?: Array<V>) {\n if (initialValues) {\n initialValues.forEach((v) => this.add(v));\n }\n }\n\n add(value: V) {\n // set \"index\" for reference.\n const index = this.$refId++;\n\n const isRef = (value['$changes']) !== undefined;\n if (isRef) {\n (value['$changes'] as ChangeTree).setParent(this, this.$changes.root, index);\n }\n\n this.$changes.indexes[index] = index;\n\n this.$indexes.set(index, index);\n this.$items.set(index, value);\n\n this.$changes.change(index);\n\n return index;\n }\n\n at(index: number): V | undefined {\n const key = Array.from(this.$items.keys())[index];\n return this.$items.get(key);\n }\n\n entries() {\n return this.$items.entries();\n }\n\n delete(item: V) {\n const entries = this.$items.entries();\n\n let index: K;\n let entry: IteratorResult<[number, V]>;\n while (entry = entries.next()) {\n if (entry.done) { break; }\n\n if (item === entry.value[1]) {\n index = entry.value[0];\n break;\n }\n }\n\n if (index === undefined) {\n return false;\n }\n\n this.$changes.delete(index);\n this.$indexes.delete(index);\n\n return this.$items.delete(index);\n }\n\n clear(changes?: DataChange[]) {\n // discard previous operations.\n this.$changes.discard(true, true);\n this.$changes.indexes = {};\n\n // clear previous indexes\n this.$indexes.clear();\n\n //\n // When decoding:\n // - enqueue items for DELETE callback.\n // - flag child items for garbage collection.\n //\n if (changes) {\n removeChildRefs.call(this, changes);\n }\n\n // clear items\n this.$items.clear();\n\n this.$changes.operation({ index: 0, op: OPERATION.CLEAR });\n\n // touch all structures until reach root\n this.$changes.touchParents();\n }\n\n has (value: V): boolean {\n return Array.from(this.$items.values()).some((v) => v === value);\n }\n\n forEach(callbackfn: (value: V, key: K, collection: CollectionSchema<V>) => void) {\n this.$items.forEach((value, key, _) => callbackfn(value, key, this));\n }\n\n values() {\n return this.$items.values();\n }\n\n get size () {\n return this.$items.size;\n }\n\n protected setIndex(index: number, key: number) {\n this.$indexes.set(index, key);\n }\n\n protected getIndex(index: number) {\n return this.$indexes.get(index);\n }\n\n protected getByIndex(index: number) {\n return this.$items.get(this.$indexes.get(index));\n }\n\n protected deleteByIndex(index: number) {\n const key = this.$indexes.get(index);\n this.$items.delete(key);\n this.$indexes.delete(index);\n }\n\n toArray() {\n return Array.from(this.$items.values());\n }\n\n toJSON() {\n const values: V[] = [];\n\n this.forEach((value, key) => {\n values.push(\n (typeof (value['toJSON']) === \"function\")\n ? value['toJSON']()\n : value\n );\n });\n\n return values;\n }\n\n //\n // Decoding utilities\n //\n clone(isDecoding?: boolean): CollectionSchema<V> {\n let cloned: CollectionSchema;\n\n if (isDecoding) {\n // client-side\n cloned = Object.assign(new CollectionSchema(), this);\n\n } else {\n // server-side\n cloned = new CollectionSchema();\n this.forEach((value) => {\n if (value['$changes']) {\n cloned.add(value['clone']());\n } else {\n cloned.add(value);\n }\n })\n }\n\n return cloned;\n }\n\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare type Bool = 'true' | 'false';
|
|
2
|
+
declare type Key = string | number | symbol;
|
|
3
|
+
declare type Not<X extends Bool> = {
|
|
4
|
+
true: 'false';
|
|
5
|
+
false: 'true';
|
|
6
|
+
}[X];
|
|
7
|
+
declare type HaveIntersection<S1 extends string, S2 extends string> = ({
|
|
8
|
+
[K in S1]: 'true';
|
|
9
|
+
} & {
|
|
10
|
+
[key: string]: 'false';
|
|
11
|
+
})[S2];
|
|
12
|
+
declare type IsNeverWorker<S extends Key> = ({
|
|
13
|
+
[K in S]: 'false';
|
|
14
|
+
} & {
|
|
15
|
+
[key: string]: 'true';
|
|
16
|
+
})[S];
|
|
17
|
+
declare type IsNever<T extends Key> = Not<HaveIntersection<IsNeverWorker<T>, 'false'>>;
|
|
18
|
+
declare type IsFunction<T> = IsNever<keyof T>;
|
|
19
|
+
export declare type NonFunctionProps<T> = {
|
|
20
|
+
[K in keyof T]: {
|
|
21
|
+
'false': K;
|
|
22
|
+
'true': never;
|
|
23
|
+
}[IsFunction<T[K]>];
|
|
24
|
+
}[keyof T];
|
|
25
|
+
export declare type NonFunctionPropNames<T> = {
|
|
26
|
+
[K in keyof T]: T[K] extends Function ? never : K;
|
|
27
|
+
}[keyof T];
|
|
28
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["type Bool = 'true' | 'false'\ntype Key = string | number | symbol;\n\ntype Not<X extends Bool> = {\n true: 'false',\n false: 'true'\n}[X]\n\ntype HaveIntersection<S1 extends string, S2 extends string> = (\n { [K in S1]: 'true' } &\n { [key: string]: 'false' }\n)[S2]\n\ntype IsNeverWorker<S extends Key> = (\n { [K in S]: 'false' } &\n { [key: string]: 'true' }\n)[S]\n\n// Worker needed because of https://github.com/Microsoft/TypeScript/issues/18118\ntype IsNever<T extends Key> = Not<HaveIntersection<IsNeverWorker<T>, 'false'>>\n\ntype IsFunction<T> = IsNever<keyof T>\n\nexport type NonFunctionProps<T> = {\n [K in keyof T]: {\n 'false': K,\n 'true': never\n }[IsFunction<T[K]>]\n}[keyof T];\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ChangeTree } from "../changes/ChangeTree";
|
|
2
|
+
import { SchemaDecoderCallbacks } from "../Schema";
|
|
3
|
+
import { DataChange } from "..";
|
|
4
|
+
export declare function getMapProxy(value: MapSchema): MapSchema<any>;
|
|
5
|
+
export declare class MapSchema<V = any> implements Map<string, V>, SchemaDecoderCallbacks {
|
|
6
|
+
protected $changes: ChangeTree;
|
|
7
|
+
protected $items: Map<string, V>;
|
|
8
|
+
protected $indexes: Map<number, string>;
|
|
9
|
+
protected $refId: number;
|
|
10
|
+
$callbacks: {
|
|
11
|
+
[operation: number]: Array<(item: V, key: string) => void>;
|
|
12
|
+
};
|
|
13
|
+
onAdd(callback: (item: V, key: string) => void, triggerAll?: boolean): () => boolean;
|
|
14
|
+
onRemove(callback: (item: V, key: string) => void): () => boolean;
|
|
15
|
+
onChange(callback: (item: V, key: string) => void): () => boolean;
|
|
16
|
+
static is(type: any): boolean;
|
|
17
|
+
constructor(initialValues?: Map<string, V> | any);
|
|
18
|
+
/** Iterator */
|
|
19
|
+
[Symbol.iterator](): IterableIterator<[string, V]>;
|
|
20
|
+
get [Symbol.toStringTag](): string;
|
|
21
|
+
set(key: string, value: V): this;
|
|
22
|
+
get(key: string): V | undefined;
|
|
23
|
+
delete(key: string): boolean;
|
|
24
|
+
clear(changes?: DataChange[]): void;
|
|
25
|
+
has(key: string): boolean;
|
|
26
|
+
forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void): void;
|
|
27
|
+
entries(): IterableIterator<[string, V]>;
|
|
28
|
+
keys(): IterableIterator<string>;
|
|
29
|
+
values(): IterableIterator<V>;
|
|
30
|
+
get size(): number;
|
|
31
|
+
protected setIndex(index: number, key: string): void;
|
|
32
|
+
protected getIndex(index: number): string;
|
|
33
|
+
protected getByIndex(index: number): V;
|
|
34
|
+
protected deleteByIndex(index: number): void;
|
|
35
|
+
toJSON(): any;
|
|
36
|
+
clone(isDecoding?: boolean): MapSchema<V>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MapSchema = exports.getMapProxy = void 0;
|
|
4
|
+
var ChangeTree_1 = require("../changes/ChangeTree");
|
|
5
|
+
var spec_1 = require("../spec");
|
|
6
|
+
var utils_1 = require("./utils");
|
|
7
|
+
function getMapProxy(value) {
|
|
8
|
+
value['$proxy'] = true;
|
|
9
|
+
value = new Proxy(value, {
|
|
10
|
+
get: function (obj, prop) {
|
|
11
|
+
if (typeof (prop) !== "symbol" && // accessing properties
|
|
12
|
+
typeof (obj[prop]) === "undefined") {
|
|
13
|
+
return obj.get(prop);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return obj[prop];
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
set: function (obj, prop, setValue) {
|
|
20
|
+
if (typeof (prop) !== "symbol" &&
|
|
21
|
+
(prop.indexOf("$") === -1 &&
|
|
22
|
+
prop !== "onAdd" &&
|
|
23
|
+
prop !== "onRemove" &&
|
|
24
|
+
prop !== "onChange")) {
|
|
25
|
+
obj.set(prop, setValue);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
obj[prop] = setValue;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
deleteProperty: function (obj, prop) {
|
|
33
|
+
obj.delete(prop);
|
|
34
|
+
return true;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
exports.getMapProxy = getMapProxy;
|
|
40
|
+
var MapSchema = /** @class */ (function () {
|
|
41
|
+
function MapSchema(initialValues) {
|
|
42
|
+
var _this = this;
|
|
43
|
+
this.$changes = new ChangeTree_1.ChangeTree(this);
|
|
44
|
+
this.$items = new Map();
|
|
45
|
+
this.$indexes = new Map();
|
|
46
|
+
this.$refId = 0;
|
|
47
|
+
if (initialValues) {
|
|
48
|
+
if (initialValues instanceof Map) {
|
|
49
|
+
initialValues.forEach(function (v, k) { return _this.set(k, v); });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
for (var k in initialValues) {
|
|
53
|
+
this.set(k, initialValues[k]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
MapSchema.prototype.onAdd = function (callback, triggerAll) {
|
|
59
|
+
if (triggerAll === void 0) { triggerAll = true; }
|
|
60
|
+
return utils_1.addCallback((this.$callbacks || (this.$callbacks = [])), spec_1.OPERATION.ADD, callback, (triggerAll)
|
|
61
|
+
? this.$items
|
|
62
|
+
: undefined);
|
|
63
|
+
};
|
|
64
|
+
MapSchema.prototype.onRemove = function (callback) { return utils_1.addCallback(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.DELETE, callback); };
|
|
65
|
+
MapSchema.prototype.onChange = function (callback) { return utils_1.addCallback(this.$callbacks || (this.$callbacks = []), spec_1.OPERATION.REPLACE, callback); };
|
|
66
|
+
MapSchema.is = function (type) {
|
|
67
|
+
return type['map'] !== undefined;
|
|
68
|
+
};
|
|
69
|
+
/** Iterator */
|
|
70
|
+
MapSchema.prototype[Symbol.iterator] = function () { return this.$items[Symbol.iterator](); };
|
|
71
|
+
Object.defineProperty(MapSchema.prototype, Symbol.toStringTag, {
|
|
72
|
+
get: function () { return this.$items[Symbol.toStringTag]; },
|
|
73
|
+
enumerable: false,
|
|
74
|
+
configurable: true
|
|
75
|
+
});
|
|
76
|
+
MapSchema.prototype.set = function (key, value) {
|
|
77
|
+
if (value === undefined || value === null) {
|
|
78
|
+
throw new Error("MapSchema#set('" + key + "', " + value + "): trying to set " + value + " value on '" + key + "'.");
|
|
79
|
+
}
|
|
80
|
+
// get "index" for this value.
|
|
81
|
+
var hasIndex = typeof (this.$changes.indexes[key]) !== "undefined";
|
|
82
|
+
var index = (hasIndex)
|
|
83
|
+
? this.$changes.indexes[key]
|
|
84
|
+
: this.$refId++;
|
|
85
|
+
var operation = (hasIndex)
|
|
86
|
+
? spec_1.OPERATION.REPLACE
|
|
87
|
+
: spec_1.OPERATION.ADD;
|
|
88
|
+
var isRef = (value['$changes']) !== undefined;
|
|
89
|
+
if (isRef) {
|
|
90
|
+
value['$changes'].setParent(this, this.$changes.root, index);
|
|
91
|
+
}
|
|
92
|
+
//
|
|
93
|
+
// (encoding)
|
|
94
|
+
// set a unique id to relate directly with this key/value.
|
|
95
|
+
//
|
|
96
|
+
if (!hasIndex) {
|
|
97
|
+
this.$changes.indexes[key] = index;
|
|
98
|
+
this.$indexes.set(index, key);
|
|
99
|
+
}
|
|
100
|
+
else if (isRef && // if is schema, force ADD operation if value differ from previous one.
|
|
101
|
+
this.$items.get(key) !== value) {
|
|
102
|
+
operation = spec_1.OPERATION.ADD;
|
|
103
|
+
}
|
|
104
|
+
this.$items.set(key, value);
|
|
105
|
+
this.$changes.change(key, operation);
|
|
106
|
+
return this;
|
|
107
|
+
};
|
|
108
|
+
MapSchema.prototype.get = function (key) {
|
|
109
|
+
return this.$items.get(key);
|
|
110
|
+
};
|
|
111
|
+
MapSchema.prototype.delete = function (key) {
|
|
112
|
+
//
|
|
113
|
+
// TODO: add a "purge" method after .encode() runs, to cleanup removed `$indexes`
|
|
114
|
+
//
|
|
115
|
+
// We don't remove $indexes to allow setting the same key in the same patch
|
|
116
|
+
// (See "should allow to remove and set an item in the same place" test)
|
|
117
|
+
//
|
|
118
|
+
// // const index = this.$changes.indexes[key];
|
|
119
|
+
// // this.$indexes.delete(index);
|
|
120
|
+
this.$changes.delete(key);
|
|
121
|
+
return this.$items.delete(key);
|
|
122
|
+
};
|
|
123
|
+
MapSchema.prototype.clear = function (changes) {
|
|
124
|
+
// discard previous operations.
|
|
125
|
+
this.$changes.discard(true, true);
|
|
126
|
+
this.$changes.indexes = {};
|
|
127
|
+
// clear previous indexes
|
|
128
|
+
this.$indexes.clear();
|
|
129
|
+
//
|
|
130
|
+
// When decoding:
|
|
131
|
+
// - enqueue items for DELETE callback.
|
|
132
|
+
// - flag child items for garbage collection.
|
|
133
|
+
//
|
|
134
|
+
if (changes) {
|
|
135
|
+
utils_1.removeChildRefs.call(this, changes);
|
|
136
|
+
}
|
|
137
|
+
// clear items
|
|
138
|
+
this.$items.clear();
|
|
139
|
+
this.$changes.operation({ index: 0, op: spec_1.OPERATION.CLEAR });
|
|
140
|
+
// touch all structures until reach root
|
|
141
|
+
this.$changes.touchParents();
|
|
142
|
+
};
|
|
143
|
+
MapSchema.prototype.has = function (key) {
|
|
144
|
+
return this.$items.has(key);
|
|
145
|
+
};
|
|
146
|
+
MapSchema.prototype.forEach = function (callbackfn) {
|
|
147
|
+
this.$items.forEach(callbackfn);
|
|
148
|
+
};
|
|
149
|
+
MapSchema.prototype.entries = function () {
|
|
150
|
+
return this.$items.entries();
|
|
151
|
+
};
|
|
152
|
+
MapSchema.prototype.keys = function () {
|
|
153
|
+
return this.$items.keys();
|
|
154
|
+
};
|
|
155
|
+
MapSchema.prototype.values = function () {
|
|
156
|
+
return this.$items.values();
|
|
157
|
+
};
|
|
158
|
+
Object.defineProperty(MapSchema.prototype, "size", {
|
|
159
|
+
get: function () {
|
|
160
|
+
return this.$items.size;
|
|
161
|
+
},
|
|
162
|
+
enumerable: false,
|
|
163
|
+
configurable: true
|
|
164
|
+
});
|
|
165
|
+
MapSchema.prototype.setIndex = function (index, key) {
|
|
166
|
+
this.$indexes.set(index, key);
|
|
167
|
+
};
|
|
168
|
+
MapSchema.prototype.getIndex = function (index) {
|
|
169
|
+
return this.$indexes.get(index);
|
|
170
|
+
};
|
|
171
|
+
MapSchema.prototype.getByIndex = function (index) {
|
|
172
|
+
return this.$items.get(this.$indexes.get(index));
|
|
173
|
+
};
|
|
174
|
+
MapSchema.prototype.deleteByIndex = function (index) {
|
|
175
|
+
var key = this.$indexes.get(index);
|
|
176
|
+
this.$items.delete(key);
|
|
177
|
+
this.$indexes.delete(index);
|
|
178
|
+
};
|
|
179
|
+
MapSchema.prototype.toJSON = function () {
|
|
180
|
+
var map = {};
|
|
181
|
+
this.forEach(function (value, key) {
|
|
182
|
+
map[key] = (typeof (value['toJSON']) === "function")
|
|
183
|
+
? value['toJSON']()
|
|
184
|
+
: value;
|
|
185
|
+
});
|
|
186
|
+
return map;
|
|
187
|
+
};
|
|
188
|
+
//
|
|
189
|
+
// Decoding utilities
|
|
190
|
+
//
|
|
191
|
+
MapSchema.prototype.clone = function (isDecoding) {
|
|
192
|
+
var cloned;
|
|
193
|
+
if (isDecoding) {
|
|
194
|
+
// client-side
|
|
195
|
+
cloned = Object.assign(new MapSchema(), this);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
// server-side
|
|
199
|
+
cloned = new MapSchema();
|
|
200
|
+
this.forEach(function (value, key) {
|
|
201
|
+
if (value['$changes']) {
|
|
202
|
+
cloned.set(key, value['clone']());
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
cloned.set(key, value);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return cloned;
|
|
210
|
+
};
|
|
211
|
+
return MapSchema;
|
|
212
|
+
}());
|
|
213
|
+
exports.MapSchema = MapSchema;
|
|
214
|
+
//# sourceMappingURL=MapSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MapSchema.js","sourceRoot":"","sources":["../../src/types/MapSchema.ts"],"names":[],"mappings":";;;AAAA,oDAAmD;AACnD,gCAAoC;AAEpC,iCAAuD;AAGvD,SAAgB,WAAW,CAAC,KAAgB;IACxC,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;IAEvB,KAAK,GAAG,IAAI,KAAK,CAAC,KAAK,EAAE;QACrB,GAAG,EAAE,UAAC,GAAG,EAAE,IAAI;YACX,IACI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ,IAAI,uBAAuB;gBACrD,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,WAAW,EACpC;gBACE,OAAO,GAAG,CAAC,GAAG,CAAC,IAAc,CAAC,CAAC;aAElC;iBAAM;gBACH,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;aACpB;QACL,CAAC;QAED,GAAG,EAAE,UAAC,GAAG,EAAE,IAAI,EAAE,QAAQ;YACrB,IACI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ;gBAC1B,CACK,IAAe,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;oBACpC,IAAI,KAAK,OAAO;oBAChB,IAAI,KAAK,UAAU;oBACnB,IAAI,KAAK,UAAU,CACtB,EACH;gBACE,GAAG,CAAC,GAAG,CAAC,IAAc,EAAE,QAAQ,CAAC,CAAC;aAErC;iBAAM;gBACH,GAAG,CAAC,IAAI,CAAC,GAAG,QAAQ,C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AM,GAAG,IAAI,SAAS,EAAE,CAAC;YACzB,IAAI,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG;gBACpB,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;oBACnB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;iBACrC;qBAAM;oBACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;iBAC1B;YACL,CAAC,CAAC,CAAA;SACL;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;IAEL,gBAAC;AAAD,CAAC,AAvND,IAuNC;AAvNY,8BAAS","sourcesContent":["import { ChangeTree } from \"../changes/ChangeTree\";\nimport { OPERATION } from \"../spec\";\nimport { SchemaDecoderCallbacks, Schema } from \"../Schema\";\nimport { addCallback, removeChildRefs } from \"./utils\";\nimport { DataChange } from \"..\";\n\nexport function getMapProxy(value: MapSchema) {\n value['$proxy'] = true;\n\n value = new Proxy(value, {\n get: (obj, prop) => {\n if (\n typeof (prop) !== \"symbol\" && // accessing properties\n typeof (obj[prop]) === \"undefined\"\n ) {\n return obj.get(prop as string);\n\n } else {\n return obj[prop];\n }\n },\n\n set: (obj, prop, setValue) => {\n if (\n typeof (prop) !== \"symbol\" &&\n (\n (prop as string).indexOf(\"$\") === -1 &&\n prop !== \"onAdd\" &&\n prop !== \"onRemove\" &&\n prop !== \"onChange\"\n )\n ) {\n obj.set(prop as string, setValue);\n\n } else {\n obj[prop] = setValue;\n }\n return true;\n },\n\n deleteProperty: (obj, prop) => {\n obj.delete(prop as string);\n return true;\n },\n });\n\n return value;\n}\n\nexport class MapSchema<V=any> implements Map<string, V>, SchemaDecoderCallbacks {\n protected $changes: ChangeTree = new ChangeTree(this);\n\n protected $items: Map<string, V> = new Map<string, V>();\n protected $indexes: Map<number, string> = new Map<number, string>();\n\n protected $refId: number = 0;\n\n //\n // Decoding callbacks\n //\n public $callbacks: { [operation: number]: Array<(item: V, key: string) => void> };\n public onAdd(callback: (item: V, key: string) => void, triggerAll: boolean = true) {\n return addCallback(\n (this.$callbacks || (this.$callbacks = [])),\n OPERATION.ADD,\n callback,\n (triggerAll)\n ? this.$items\n : undefined\n );\n }\n public onRemove(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.DELETE, callback); }\n public onChange(callback: (item: V, key: string) => void) { return addCallback(this.$callbacks || (this.$callbacks = []), OPERATION.REPLACE, callback); }\n\n static is(type: any) {\n return type['map'] !== undefined;\n }\n\n constructor (initialValues?: Map<string, V> | any) {\n if (initialValues) {\n if (initialValues instanceof Map) {\n initialValues.forEach((v, k) => this.set(k, v));\n\n } else {\n for (const k in initialValues) {\n this.set(k, initialValues[k]);\n }\n }\n }\n }\n\n /** Iterator */\n [Symbol.iterator](): IterableIterator<[string, V]> { return this.$items[Symbol.iterator](); }\n get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }\n\n set(key: string, value: V) {\n if (value === undefined || value === null) {\n throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);\n }\n\n // get \"index\" for this value.\n const hasIndex = typeof(this.$changes.indexes[key]) !== \"undefined\";\n const index = (hasIndex)\n ? this.$changes.indexes[key]\n : this.$refId++;\n\n let operation: OPERATION = (hasIndex)\n ? OPERATION.REPLACE\n : OPERATION.ADD;\n\n const isRef = (value['$changes']) !== undefined;\n if (isRef) {\n (value['$changes'] as ChangeTree).setParent(\n this,\n this.$changes.root,\n index\n );\n }\n\n //\n // (encoding)\n // set a unique id to relate directly with this key/value.\n //\n if (!hasIndex) {\n this.$changes.indexes[key] = index;\n this.$indexes.set(index, key);\n\n } else if (\n isRef && // if is schema, force ADD operation if value differ from previous one.\n this.$items.get(key) !== value\n ) {\n operation = OPERATION.ADD;\n }\n\n this.$items.set(key, value);\n\n this.$changes.change(key, operation);\n\n return this;\n }\n\n get(key: string): V | undefined {\n return this.$items.get(key);\n }\n\n delete(key: string) {\n //\n // TODO: add a \"purge\" method after .encode() runs, to cleanup removed `$indexes`\n //\n // We don't remove $indexes to allow setting the same key in the same patch\n // (See \"should allow to remove and set an item in the same place\" test)\n //\n // // const index = this.$changes.indexes[key];\n // // this.$indexes.delete(index);\n\n this.$changes.delete(key);\n return this.$items.delete(key);\n }\n\n clear(changes?: DataChange[]) {\n // discard previous operations.\n this.$changes.discard(true, true);\n this.$changes.indexes = {};\n\n // clear previous indexes\n this.$indexes.clear();\n\n //\n // When decoding:\n // - enqueue items for DELETE callback.\n // - flag child items for garbage collection.\n //\n if (changes) {\n removeChildRefs.call(this, changes);\n }\n\n // clear items\n this.$items.clear();\n\n this.$changes.operation({ index: 0, op: OPERATION.CLEAR });\n\n // touch all structures until reach root\n this.$changes.touchParents();\n }\n\n has (key: string) {\n return this.$items.has(key);\n }\n\n forEach(callbackfn: (value: V, key: string, map: Map<string, V>) => void) {\n this.$items.forEach(callbackfn);\n }\n\n entries () {\n return this.$items.entries();\n }\n\n keys () {\n return this.$items.keys();\n }\n\n values() {\n return this.$items.values();\n }\n\n get size () {\n return this.$items.size;\n }\n\n protected setIndex(index: number, key: string) {\n this.$indexes.set(index, key);\n }\n\n protected getIndex(index: number) {\n return this.$indexes.get(index);\n }\n\n protected getByIndex(index: number) {\n return this.$items.get(this.$indexes.get(index));\n }\n\n protected deleteByIndex(index: number) {\n const key = this.$indexes.get(index);\n this.$items.delete(key);\n this.$indexes.delete(index);\n }\n\n toJSON() {\n const map: any = {};\n\n this.forEach((value, key) => {\n map[key] = (typeof (value['toJSON']) === \"function\")\n ? value['toJSON']()\n : value;\n });\n\n return map;\n }\n\n //\n // Decoding utilities\n //\n clone(isDecoding?: boolean): MapSchema<V> {\n let cloned: MapSchema;\n\n if (isDecoding) {\n // client-side\n cloned = Object.assign(new MapSchema(), this);\n\n } else {\n // server-side\n cloned = new MapSchema();\n this.forEach((value, key) => {\n if (value['$changes']) {\n cloned.set(key, value['clone']());\n } else {\n cloned.set(key, value);\n }\n })\n }\n\n return cloned;\n }\n\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ChangeTree } from "../changes/ChangeTree";
|
|
2
|
+
import { SchemaDecoderCallbacks } from "../Schema";
|
|
3
|
+
import { DataChange } from "..";
|
|
4
|
+
export declare class SetSchema<V = any> implements SchemaDecoderCallbacks {
|
|
5
|
+
protected $changes: ChangeTree;
|
|
6
|
+
protected $items: Map<number, V>;
|
|
7
|
+
protected $indexes: Map<number, number>;
|
|
8
|
+
protected $refId: number;
|
|
9
|
+
$callbacks: {
|
|
10
|
+
[operation: number]: Array<(item: V, key: string) => void>;
|
|
11
|
+
};
|
|
12
|
+
onAdd(callback: (item: V, key: string) => void, triggerAll?: boolean): () => boolean;
|
|
13
|
+
onRemove(callback: (item: V, key: string) => void): () => boolean;
|
|
14
|
+
onChange(callback: (item: V, key: string) => void): () => boolean;
|
|
15
|
+
static is(type: any): boolean;
|
|
16
|
+
constructor(initialValues?: Array<V>);
|
|
17
|
+
add(value: V): number | false;
|
|
18
|
+
entries(): IterableIterator<[number, V]>;
|
|
19
|
+
delete(item: V): boolean;
|
|
20
|
+
clear(changes?: DataChange[]): void;
|
|
21
|
+
has(value: V): boolean;
|
|
22
|
+
forEach(callbackfn: (value: V, key: number, collection: SetSchema<V>) => void): void;
|
|
23
|
+
values(): IterableIterator<V>;
|
|
24
|
+
get size(): number;
|
|
25
|
+
protected setIndex(index: number, key: number): void;
|
|
26
|
+
protected getIndex(index: number): number;
|
|
27
|
+
protected getByIndex(index: number): V;
|
|
28
|
+
protected deleteByIndex(index: number): void;
|
|
29
|
+
toArray(): V[];
|
|
30
|
+
toJSON(): V[];
|
|
31
|
+
clone(isDecoding?: boolean): SetSchema<V>;
|
|
32
|
+
}
|