@bikky/replication 1.0.1

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 (140) hide show
  1. package/Constants/Errors.d.ts +27 -0
  2. package/Constants/Errors.js +75 -0
  3. package/Constants/Logging.d.ts +17 -0
  4. package/Constants/Logging.js +97 -0
  5. package/Constants/ReplicableRegistry.d.ts +37 -0
  6. package/Constants/ReplicableRegistry.js +234 -0
  7. package/Constants/SerialisationTypes.d.ts +82 -0
  8. package/Constants/SerialisationTypes.js +160 -0
  9. package/Constants/SourceMaps.d.ts +10 -0
  10. package/Constants/SourceMaps.js +12 -0
  11. package/Constants/TraversalStep.d.ts +5 -0
  12. package/Constants/TraversalStep.js +2 -0
  13. package/Constants/Versions.d.ts +15 -0
  14. package/Constants/Versions.js +63 -0
  15. package/Expressions/Compiler/BuiltinGrammar.d.ts +234 -0
  16. package/Expressions/Compiler/BuiltinGrammar.js +446 -0
  17. package/Expressions/Compiler/ExpressionGrammar.d.ts +89 -0
  18. package/Expressions/Compiler/ExpressionGrammar.js +70 -0
  19. package/Expressions/Compiler/Parser.d.ts +56 -0
  20. package/Expressions/Compiler/Parser.js +314 -0
  21. package/Expressions/Compiler/Tokenizer.d.ts +52 -0
  22. package/Expressions/Compiler/Tokenizer.js +222 -0
  23. package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.d.ts +1 -0
  24. package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.js +516 -0
  25. package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.d.ts +1 -0
  26. package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.js +68 -0
  27. package/Expressions/CreateEvaluator.d.ts +4 -0
  28. package/Expressions/CreateEvaluator.js +85 -0
  29. package/Expressions/EvaluatorChain.d.ts +19 -0
  30. package/Expressions/EvaluatorChain.js +137 -0
  31. package/Expressions/EvaluatorSteps.d.ts +19 -0
  32. package/Expressions/EvaluatorSteps.js +12 -0
  33. package/Expressions/EvaluatorString.d.ts +21 -0
  34. package/Expressions/EvaluatorString.js +26 -0
  35. package/Expressions/Expression.d.ts +36 -0
  36. package/Expressions/Expression.js +147 -0
  37. package/Expressions/Traverser.d.ts +28 -0
  38. package/Expressions/Traverser.js +348 -0
  39. package/Expressions/TypeRegistry/Accessors.d.ts +26 -0
  40. package/Expressions/TypeRegistry/Accessors.js +58 -0
  41. package/Expressions/TypeRegistry/ChainCollections.d.ts +51 -0
  42. package/Expressions/TypeRegistry/ChainCollections.js +134 -0
  43. package/Expressions/TypeRegistry/ChainTypes.d.ts +23 -0
  44. package/Expressions/TypeRegistry/ChainTypes.js +46 -0
  45. package/Expressions/TypeRegistry/CustomAPI.d.ts +36 -0
  46. package/Expressions/TypeRegistry/CustomAPI.js +181 -0
  47. package/Expressions/TypeRegistry/Primitive.d.ts +19 -0
  48. package/Expressions/TypeRegistry/Primitive.js +47 -0
  49. package/Expressions/TypeRegistry/Registry.d.ts +27 -0
  50. package/Expressions/TypeRegistry/Registry.js +270 -0
  51. package/Expressions/TypeRegistry/ReplAPI.d.ts +41 -0
  52. package/Expressions/TypeRegistry/ReplAPI.js +220 -0
  53. package/Expressions/TypeRegistry/Scope.d.ts +24 -0
  54. package/Expressions/TypeRegistry/Scope.js +44 -0
  55. package/Expressions/TypeRegistry/Types.d.ts +23 -0
  56. package/Expressions/TypeRegistry/Types.js +1 -0
  57. package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.d.ts +1 -0
  58. package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.js +31 -0
  59. package/Expressions/__tests__/ExpressionExamples.d.ts +28 -0
  60. package/Expressions/__tests__/ExpressionExamples.js +50 -0
  61. package/Expressions/__tests__/Replicable.Expressions.Expressions.test.d.ts +1 -0
  62. package/Expressions/__tests__/Replicable.Expressions.Expressions.test.js +166 -0
  63. package/IDPool.d.ts +18 -0
  64. package/IDPool.data.d.ts +17 -0
  65. package/IDPool.js +139 -0
  66. package/License.txt +1 -0
  67. package/Main.d.ts +13 -0
  68. package/Main.js +13 -0
  69. package/Networking.d.ts +60 -0
  70. package/Networking.js +626 -0
  71. package/Replicatable.d.ts +66 -0
  72. package/Replicatable.js +123 -0
  73. package/Tracking/Buffable.d.ts +68 -0
  74. package/Tracking/Buffable.js +194 -0
  75. package/Tracking/Class.d.ts +97 -0
  76. package/Tracking/Class.js +221 -0
  77. package/Tracking/Functions.d.ts +14 -0
  78. package/Tracking/Functions.js +27 -0
  79. package/Tracking/GlobalGroup.d.ts +5 -0
  80. package/Tracking/GlobalGroup.js +39 -0
  81. package/Tracking/Property.d.ts +95 -0
  82. package/Tracking/Property.js +125 -0
  83. package/Tracking/Types.d.ts +33 -0
  84. package/Tracking/Types.js +1 -0
  85. package/Tracking/__tests__/Replicable.Tracking.Decorator.test.d.ts +1 -0
  86. package/Tracking/__tests__/Replicable.Tracking.Decorator.test.js +151 -0
  87. package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.d.ts +1 -0
  88. package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.js +253 -0
  89. package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.d.ts +1 -0
  90. package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.js +135 -0
  91. package/Tracking/__tests__/Replicable.Tracking.Struct.test.d.ts +1 -0
  92. package/Tracking/__tests__/Replicable.Tracking.Struct.test.js +66 -0
  93. package/Tracking/__tests__/Replicable.Tracking.Type.test.d.ts +1 -0
  94. package/Tracking/__tests__/Replicable.Tracking.Type.test.js +67 -0
  95. package/Transformers/Configurer.d.ts +39 -0
  96. package/Transformers/Configurer.js +415 -0
  97. package/Transformers/Constructor.d.ts +12 -0
  98. package/Transformers/Constructor.js +44 -0
  99. package/Transformers/Definitions.d.ts +102 -0
  100. package/Transformers/Definitions.js +626 -0
  101. package/Transformers/Loader.d.ts +45 -0
  102. package/Transformers/Loader.js +350 -0
  103. package/Transformers/Progress.d.ts +32 -0
  104. package/Transformers/Progress.js +429 -0
  105. package/Transformers/Reference.d.ts +37 -0
  106. package/Transformers/Reference.js +212 -0
  107. package/Transformers/SchemaGenerator.d.ts +102 -0
  108. package/Transformers/SchemaGenerator.js +564 -0
  109. package/Transformers/Serialiser.d.ts +31 -0
  110. package/Transformers/Serialiser.js +366 -0
  111. package/Transformers/Utils.d.ts +33 -0
  112. package/Transformers/Utils.js +287 -0
  113. package/Transformers/__tests__/Examples.d.ts +168 -0
  114. package/Transformers/__tests__/Examples.js +263 -0
  115. package/Transformers/__tests__/Replicable.Transformers.Definitions.test.d.ts +1 -0
  116. package/Transformers/__tests__/Replicable.Transformers.Definitions.test.js +457 -0
  117. package/Transformers/__tests__/Replicable.Transformers.Loader.test.d.ts +1 -0
  118. package/Transformers/__tests__/Replicable.Transformers.Loader.test.js +339 -0
  119. package/Transformers/__tests__/Replicable.Transformers.Progress.test.d.ts +1 -0
  120. package/Transformers/__tests__/Replicable.Transformers.Progress.test.js +256 -0
  121. package/Transformers/__tests__/Replicable.Transformers.Reference.test.d.ts +1 -0
  122. package/Transformers/__tests__/Replicable.Transformers.Reference.test.js +167 -0
  123. package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.d.ts +1 -0
  124. package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.js +400 -0
  125. package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.d.ts +1 -0
  126. package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.js +441 -0
  127. package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.d.ts +1 -0
  128. package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.js +320 -0
  129. package/Transformers/__tests__/Replicable.Transformers.Utils.test.d.ts +1 -0
  130. package/Transformers/__tests__/Replicable.Transformers.Utils.test.js +534 -0
  131. package/__tests__/Replicable.Expressions.test.d.ts +1 -0
  132. package/__tests__/Replicable.Expressions.test.js +166 -0
  133. package/__tests__/Replicable.IDPool.test.d.ts +1 -0
  134. package/__tests__/Replicable.IDPool.test.js +11 -0
  135. package/__tests__/Replicable.ReplicableRegistry.test.d.ts +1 -0
  136. package/__tests__/Replicable.ReplicableRegistry.test.js +154 -0
  137. package/__tests__/Replicable.Serialisation.test.d.ts +1 -0
  138. package/__tests__/Replicable.Serialisation.test.js +283 -0
  139. package/package.json +14 -0
  140. package/tsconfig.json +19 -0
@@ -0,0 +1,27 @@
1
+ import { EAST_Token, ExpressionGrammar } from "../Expressions/Compiler/ExpressionGrammar.js";
2
+ import { ExpressionAST } from "../Expressions/Compiler/BuiltinGrammar.js";
3
+ import ExpressionNode = ExpressionAST.ExpressionNode;
4
+ import Node = ExpressionGrammar.Node;
5
+ import { SourceMap } from "./SourceMaps.js";
6
+ export declare class ErrorStack {
7
+ errorStack: string[];
8
+ errorLocations: string[];
9
+ source: SourceMap;
10
+ constructor(source: SourceMap);
11
+ hasErrors(): boolean;
12
+ add(code: string, msg: string): this;
13
+ toString(locCode: string): string;
14
+ appendWithoutIndent(other: ErrorStack): this;
15
+ appendAndIndent(other: ErrorStack): this;
16
+ }
17
+ export interface ExpressionSource {
18
+ text: string;
19
+ file: string;
20
+ line: number;
21
+ }
22
+ export declare class ExpressionSyntaxError extends Error {
23
+ source: ExpressionSource;
24
+ location: number;
25
+ constructor(msg: string, source: ExpressionSource, location: number);
26
+ constructor(msg: string, node: EAST_Token | ExpressionNode | Node);
27
+ }
@@ -0,0 +1,75 @@
1
+ import { BikPath } from "@bikky/path";
2
+ let stacks = new Map();
3
+ export class ErrorStack {
4
+ constructor(source) {
5
+ this.errorLocations = [];
6
+ this.source = source;
7
+ this.errorStack = [];
8
+ }
9
+ hasErrors() {
10
+ return this.errorStack.length > 0;
11
+ }
12
+ add(code, msg) {
13
+ this.errorStack.push(msg);
14
+ if (this.errorLocations.length == 0) {
15
+ let newTrace = stacks.get(code);
16
+ if (!newTrace) {
17
+ stacks.set(code, newTrace = new Error().stack);
18
+ }
19
+ newTrace = newTrace.replace(/^(Error: )\n.*/, "$1").replace(/\.js/g, ".ts");
20
+ this.errorLocations.push(newTrace);
21
+ }
22
+ return this;
23
+ }
24
+ toString(locCode) {
25
+ if (this.errorStack.length == 0)
26
+ return "";
27
+ if (this.errorLocations.length == 0) {
28
+ let localErr = stacks.get(locCode);
29
+ if (!localErr) {
30
+ localErr = new Error().stack;
31
+ stacks.set(locCode, localErr);
32
+ }
33
+ localErr = localErr.replace(/^(Error: )\n.*/, "$1").replace(/\.js/g, ".ts");
34
+ this.errorLocations.push(localErr);
35
+ }
36
+ return this.errorStack.join("\n") + "\n\n"
37
+ + " at Mod (" + BikPath.fsStyle(this.source.SourceFile).replace(".js", ".ts") + ":0:0)\n\n"
38
+ + (this.errorLocations.join("\n\n"))
39
+ + "\n";
40
+ }
41
+ appendWithoutIndent(other) {
42
+ this.errorStack = this.errorStack.concat(...other.errorStack);
43
+ this.errorLocations.push(...other.errorLocations);
44
+ return this;
45
+ }
46
+ appendAndIndent(other) {
47
+ this.errorStack = this.errorStack.concat(...other.errorStack.map((e) => "\t" + e));
48
+ this.errorLocations.push(...other.errorLocations);
49
+ return this;
50
+ }
51
+ }
52
+ function MarkIncidentLocation(source, location) {
53
+ return ` ---> ${source.text}\n ${"".padStart(location, " ")}^^\nFound in file ${source.file} on line ${source.line} at ${location}.\n`;
54
+ }
55
+ export class ExpressionSyntaxError extends Error {
56
+ constructor(msg, source, location) {
57
+ if (location) {
58
+ super(msg + `\n${MarkIncidentLocation(source, location)}`);
59
+ this.source = source;
60
+ this.location = location;
61
+ }
62
+ else {
63
+ let node = source;
64
+ let sourceDetails = {
65
+ file: node.file.sourceLocation,
66
+ version: node.file.version,
67
+ text: node.file.text,
68
+ line: node.line
69
+ };
70
+ super(msg + `\n${MarkIncidentLocation(sourceDetails, node.column)}`);
71
+ this.source = sourceDetails;
72
+ this.location = node.column;
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,17 @@
1
+ declare global {
2
+ interface Console {
3
+ logLevel: Logging.Verbosity | "none" | "errors" | "warnings" | "info" | "debug";
4
+ }
5
+ }
6
+ export declare namespace Logging {
7
+ enum Verbosity {
8
+ None = 0,
9
+ Log = 1,
10
+ Error = 2,
11
+ Warning = 3,
12
+ Info = 4,
13
+ Debug = 5
14
+ }
15
+ function SetVerbosityLevel(level: Verbosity, path?: string): void;
16
+ function SetVerbosityLevelForDirectory(level: Verbosity, directory: string): void;
17
+ }
@@ -0,0 +1,97 @@
1
+ import { BikPath } from "@bikky/path";
2
+ export var Logging;
3
+ (function (Logging) {
4
+ let Verbosity;
5
+ (function (Verbosity) {
6
+ Verbosity[Verbosity["None"] = 0] = "None";
7
+ Verbosity[Verbosity["Log"] = 1] = "Log";
8
+ Verbosity[Verbosity["Error"] = 2] = "Error";
9
+ Verbosity[Verbosity["Warning"] = 3] = "Warning";
10
+ Verbosity[Verbosity["Info"] = 4] = "Info";
11
+ Verbosity[Verbosity["Debug"] = 5] = "Debug";
12
+ })(Verbosity = Logging.Verbosity || (Logging.Verbosity = {}));
13
+ const OriginalLog = console.log;
14
+ const OriginalWarn = console.warn;
15
+ const OriginalError = console.error;
16
+ const OriginalInfo = console.info;
17
+ const OriginalDebug = console.debug;
18
+ const OriginalAssert = console.assert;
19
+ const DirectoryVerbosityLevel = new Map();
20
+ const FileVerbosityLevel = new Map();
21
+ function ComputeVerbosity() {
22
+ var _a;
23
+ let file = (_a = BikPath.GetStackFrame(2)) === null || _a === void 0 ? void 0 : _a.file;
24
+ if (!file) {
25
+ return Verbosity.Warning;
26
+ }
27
+ if (FileVerbosityLevel.has(file)) {
28
+ return FileVerbosityLevel.get(file);
29
+ }
30
+ let directories = file.split("/");
31
+ for (let i = directories.length; i > 0; i--) {
32
+ let directory = directories[i];
33
+ if (DirectoryVerbosityLevel.has(directory)) {
34
+ return DirectoryVerbosityLevel.get(directory);
35
+ }
36
+ }
37
+ return Verbosity.Warning;
38
+ }
39
+ Object.defineProperty(console, "logLevel", {
40
+ get() {
41
+ return ComputeVerbosity();
42
+ },
43
+ set(value) {
44
+ if (typeof value === "number") {
45
+ return FileVerbosityLevel.set(BikPath.getCallerFilepath(), value);
46
+ }
47
+ if (value === value.toLowerCase()) {
48
+ value = value[0].toUpperCase() + value.slice(1);
49
+ }
50
+ return FileVerbosityLevel.set(BikPath.getCallerFilepath(), Verbosity[value]);
51
+ }
52
+ });
53
+ function SetVerbosityLevel(level, path) {
54
+ let file = BikPath.getCallerFilepath();
55
+ if (path) {
56
+ file = BikPath.join(file, path);
57
+ }
58
+ FileVerbosityLevel.set(file, level);
59
+ }
60
+ Logging.SetVerbosityLevel = SetVerbosityLevel;
61
+ function SetVerbosityLevelForDirectory(level, directory) {
62
+ DirectoryVerbosityLevel.set(directory, level);
63
+ }
64
+ Logging.SetVerbosityLevelForDirectory = SetVerbosityLevelForDirectory;
65
+ console.log = function (...args) {
66
+ if (ComputeVerbosity() >= Verbosity.Log) {
67
+ OriginalLog(...args);
68
+ }
69
+ };
70
+ console.warn = function (...args) {
71
+ if (ComputeVerbosity() >= Verbosity.Warning) {
72
+ OriginalWarn(...args);
73
+ }
74
+ };
75
+ console.error = function (...args) {
76
+ if (ComputeVerbosity() >= Verbosity.Error) {
77
+ OriginalError(...args);
78
+ }
79
+ };
80
+ console.info = function (...args) {
81
+ if (ComputeVerbosity() >= Verbosity.Info) {
82
+ OriginalInfo(...args);
83
+ }
84
+ };
85
+ console.debug = function (...args) {
86
+ if (ComputeVerbosity() >= Verbosity.Debug) {
87
+ OriginalDebug(...args);
88
+ }
89
+ };
90
+ console.assert = function (condition, ...args) {
91
+ if (!condition) {
92
+ if (ComputeVerbosity() >= Verbosity.Error) {
93
+ OriginalAssert(condition, ...args);
94
+ }
95
+ }
96
+ };
97
+ })(Logging || (Logging = {}));
@@ -0,0 +1,37 @@
1
+ import { Class } from "@bikky/smart-collections";
2
+ import { Primitive } from "./SerialisationTypes.js";
3
+ import type { ReplicableClass } from "../Tracking/Class.js";
4
+ import type { CustomClass } from "../Tracking/Types.js";
5
+ export declare namespace ReplicableRegistry {
6
+ function Initialise(): Promise<void>;
7
+ /**
8
+ * This function is only for testing!
9
+ */
10
+ function triggerLoadCompleteForTests(): Promise<void>;
11
+ function ResetForTests(): Promise<void>;
12
+ /**
13
+ * Add idToMap as an alias for the given replicable class. E.g. adding
14
+ * Array as a pointer to a custom array resolver would have Array as the first value, then the resolver
15
+ * (or it's name) as the second. From then-on you could use type: Array in @Property and get the resolver.
16
+ * @param idToMap the name or object you want to use as an alias for another class.
17
+ * @param replicableValue the class you want to use to resolve loading/saving and replicating the value.
18
+ */
19
+ function RegisterTypeAlias(idToMap: string | {
20
+ name?: string;
21
+ }, replicableValue: Class<any> | AbstractClass<any> | CustomClass | string | Primitive): void;
22
+ function ReplaceClass(oldClass: any, newClass: any): void;
23
+ function MarkLoadStarted(markerName: string): Promise<void>;
24
+ function MarkLoadComplete(markerName: string): Promise<void>;
25
+ function __GetReplicablesFor(type: any): (ReplicableClass | Primitive)[] | undefined;
26
+ function __GetReplOrPrimFor(type: any): (ReplicableClass | Primitive)[] | undefined;
27
+ function __GetReplicableForDataName(type: string): ReplicableClass | undefined;
28
+ function __GetAllReplicables(): ReplicableClass[];
29
+ function __TypesToReplicables(types: (Class<any> | string)[]): any[];
30
+ function GetDataNameOfObjectType(obj: object): string;
31
+ function GetDataName(type: Class<any>): string;
32
+ function GetTypesForName(type: string): any;
33
+ function GetTypeForDataName(type: string): ReplicableClass | undefined;
34
+ function HasType(type: Class<any>): any;
35
+ function IsType<T>(object: any, type: Class<T>): object is T;
36
+ function IsType(object: any, type: string): boolean;
37
+ }
@@ -0,0 +1,234 @@
1
+ import { ArrayMap, InheritableArrayMetastore, OpenPromise, SmartArray, } from "@bikky/smart-collections";
2
+ import { describeObject, getPrimitiveClass, isPrimitiveType, TypeSymbol, } from "./SerialisationTypes.js";
3
+ import { Progress } from "../Transformers/Progress.js";
4
+ import { Logging } from "./Logging.js";
5
+ const InheritanceMap = new InheritableArrayMetastore("TypeParent");
6
+ //This is an array because you can have type aliases that link to multiple types
7
+ // E.g. Location -> InventorySlot, Entity, Tile, null, etc.
8
+ const TypeMap = new ArrayMap();
9
+ const DataNameMap = new Map();
10
+ const ParentSymbol = Symbol("parent");
11
+ export var ReplicableRegistry;
12
+ (function (ReplicableRegistry) {
13
+ console.logLevel = Logging.Verbosity.None;
14
+ //Keep a list of all the repliable types that have been registered,
15
+ // as they finish load mark them off the list.
16
+ // When the list is empty or the list hasn't changed for a period of time.
17
+ // Mark load as complete and complain about all of the types that haven't been loaded.
18
+ let pendingTypes = new SmartArray();
19
+ let loadComplete = new OpenPromise();
20
+ let loadBegun = false;
21
+ let loadTimeout = null;
22
+ let externalLoadingMarkers = [];
23
+ function OnLoadComplete() {
24
+ if (externalLoadingMarkers.length > 0) {
25
+ ResetLoadTimeout();
26
+ return;
27
+ }
28
+ clearTimeout(loadTimeout);
29
+ loadBegun = true;
30
+ Progress.FinishedAllLoading();
31
+ if (pendingTypes.length > 0) {
32
+ let typeNames = new Set(pendingTypes.map((type) => typeof type === "string" ? type.replace("Definition", "") :
33
+ typeof type === "function" ? type.name : "" + type));
34
+ console.warn("Types that failed to load because their dependancies didn't load: ", [...typeNames].join(", "));
35
+ // console.log("Replacing all types that didn't load with unknown.");
36
+ // let unknown = __getReplicableFor("unknown")!;
37
+ // for (let type of pendingTypes) {
38
+ // typeMap.set(type, unknown);
39
+ // typeFinishedLoad.resolve(type, unknown);
40
+ // }
41
+ }
42
+ console.info("Replicable Types load complete.");
43
+ loadComplete.complete();
44
+ }
45
+ function ResetLoadTimeout() {
46
+ if (loadTimeout !== null)
47
+ clearTimeout(loadTimeout);
48
+ loadTimeout = setTimeout(() => {
49
+ //If load finishes immediately (e.g. because of tests) this can still trigger.
50
+ if (loadBegun === false) {
51
+ OnLoadComplete();
52
+ }
53
+ }, 100);
54
+ }
55
+ async function Initialise() {
56
+ OnLoadComplete();
57
+ await loadComplete;
58
+ }
59
+ ReplicableRegistry.Initialise = Initialise;
60
+ /**
61
+ * This function is only for testing!
62
+ */
63
+ async function triggerLoadCompleteForTests() {
64
+ if (typeof TestingMode === "boolean" && TestingMode) {
65
+ OnLoadComplete();
66
+ }
67
+ }
68
+ ReplicableRegistry.triggerLoadCompleteForTests = triggerLoadCompleteForTests;
69
+ async function ResetForTests() {
70
+ loadComplete = new OpenPromise();
71
+ loadBegun = false;
72
+ externalLoadingMarkers.length = 0;
73
+ // pendingTypes = new SmartArray<any>();
74
+ // TypeMap.clear();
75
+ // DataNameMap.clear();
76
+ Progress.ClearCaches();
77
+ }
78
+ ReplicableRegistry.ResetForTests = ResetForTests;
79
+ function OnTypeLoaded(type, originalClass) {
80
+ console.info(`${Date.now()}: Finished loading ${type.opts.typescriptTypeName}`);
81
+ //Support for @DelayUntil classes.
82
+ if (!TypeMap.includes(originalClass, type)) {
83
+ TypeMap.push(originalClass, type);
84
+ }
85
+ if (!TypeMap.includes(type.classConstructor, type)) {
86
+ TypeMap.push(type.classConstructor, type);
87
+ }
88
+ if (!TypeMap.includes(type.opts.dataTypeName, type)) {
89
+ TypeMap.push(type.opts.dataTypeName, type);
90
+ }
91
+ DataNameMap.set(type.opts.dataTypeName, type);
92
+ if (!TypeMap.includes(type.opts.typescriptTypeName, type)) {
93
+ TypeMap.push(type.opts.typescriptTypeName, type);
94
+ }
95
+ if (!isPrimitiveType(type.classConstructor)) {
96
+ InheritanceMap.add(type.classConstructor, ParentSymbol, type);
97
+ }
98
+ ResetLoadTimeout();
99
+ }
100
+ function OnAliasLoaded(alias, types) {
101
+ for (let type of types) {
102
+ if (!TypeMap.includes(alias, type)) {
103
+ TypeMap.push(alias, type);
104
+ }
105
+ }
106
+ pendingTypes.removeAll(alias, alias.name);
107
+ ResetLoadTimeout();
108
+ }
109
+ Progress.OnAnyReplicableReady(OnTypeLoaded);
110
+ Progress.OnAnyPrimitiveReady((alias, prim) => {
111
+ TypeMap.push(alias, prim);
112
+ pendingTypes.removeAll(alias);
113
+ ResetLoadTimeout();
114
+ });
115
+ Progress.OnAnyAliasReady(OnAliasLoaded);
116
+ /**
117
+ * Add idToMap as an alias for the given replicable class. E.g. adding
118
+ * Array as a pointer to a custom array resolver would have Array as the first value, then the resolver
119
+ * (or it's name) as the second. From then-on you could use type: Array in @Property and get the resolver.
120
+ * @param idToMap the name or object you want to use as an alias for another class.
121
+ * @param replicableValue the class you want to use to resolve loading/saving and replicating the value.
122
+ */
123
+ function RegisterTypeAlias(idToMap, replicableValue) {
124
+ Progress.RegisterAlias(idToMap, replicableValue);
125
+ }
126
+ ReplicableRegistry.RegisterTypeAlias = RegisterTypeAlias;
127
+ OnBikDelayCompleted(function UpdateRegistryClasses(old, upd) {
128
+ RegisterTypeAlias(old, upd);
129
+ let mapping = TypeMap.get(old);
130
+ if (mapping) {
131
+ TypeMap.set(upd, mapping);
132
+ }
133
+ });
134
+ function ReplaceClass(oldClass, newClass) {
135
+ let oldTypes = TypeMap.get(oldClass), newTypes = TypeMap.get(newClass);
136
+ if (!newTypes) {
137
+ throw new Error(`Cannot override type ${oldClass.name} with ${newClass.name} as ${newClass.name} is not registered.`);
138
+ }
139
+ if (!oldTypes) {
140
+ throw new Error(`Cannot override type ${oldClass.name} with ${newClass.name} as ${oldClass.name} is not registered.`);
141
+ }
142
+ let oldType = oldTypes.find((e) => !isPrimitiveType(e) && e.classConstructor == oldClass);
143
+ let newType = newTypes.find((e) => !isPrimitiveType(e) && e.classConstructor == oldClass);
144
+ TypeMap.remove(oldType.classConstructor, oldType);
145
+ TypeMap.push(oldType.classConstructor, newType);
146
+ TypeMap.remove(oldType.opts.dataTypeName, oldType);
147
+ TypeMap.push(oldType.opts.dataTypeName, newType);
148
+ DataNameMap.set(oldType.opts.dataTypeName, newType);
149
+ TypeMap.remove(oldType.opts.typescriptTypeName, oldType);
150
+ TypeMap.push(oldType.opts.typescriptTypeName, newType);
151
+ TypeMap.set(oldType.opts.typescriptTypeName, new SmartArray(newType));
152
+ }
153
+ ReplicableRegistry.ReplaceClass = ReplaceClass;
154
+ async function MarkLoadStarted(markerName) {
155
+ externalLoadingMarkers.push(markerName);
156
+ console.debug("Started loading", markerName);
157
+ }
158
+ ReplicableRegistry.MarkLoadStarted = MarkLoadStarted;
159
+ async function MarkLoadComplete(markerName) {
160
+ if (!externalLoadingMarkers.includes(markerName)) {
161
+ throw new Error("Can't mark loading as complete for marker that hasn't started.");
162
+ }
163
+ console.debug("Completed loading", markerName);
164
+ externalLoadingMarkers.splice(externalLoadingMarkers.indexOf(markerName));
165
+ if (externalLoadingMarkers.length == 0) {
166
+ ResetLoadTimeout();
167
+ }
168
+ }
169
+ ReplicableRegistry.MarkLoadComplete = MarkLoadComplete;
170
+ // eslint-disable-next-line @typescript-eslint/naming-convention
171
+ function __GetReplicablesFor(type) {
172
+ if (!TypeMap.has(type)) {
173
+ console.warn(`Unable to find type ${describeObject(type)} in type map, is it missing a @Type or @Struct decorator?`);
174
+ }
175
+ let result = TypeMap.get(type);
176
+ return result;
177
+ }
178
+ ReplicableRegistry.__GetReplicablesFor = __GetReplicablesFor;
179
+ // eslint-disable-next-line @typescript-eslint/naming-convention
180
+ function __GetReplOrPrimFor(type) {
181
+ let result = isPrimitiveType(type) ? [type] : TypeMap.get(type);
182
+ if (!result) {
183
+ console.warn(`Unable to find type ${describeObject(type)} in type map, is it missing a @Type or @Struct decorator?`);
184
+ }
185
+ return result;
186
+ }
187
+ ReplicableRegistry.__GetReplOrPrimFor = __GetReplOrPrimFor;
188
+ // eslint-disable-next-line @typescript-eslint/naming-convention
189
+ function __GetReplicableForDataName(type) {
190
+ return DataNameMap.get(type);
191
+ }
192
+ ReplicableRegistry.__GetReplicableForDataName = __GetReplicableForDataName;
193
+ // eslint-disable-next-line @typescript-eslint/naming-convention
194
+ function __GetAllReplicables() {
195
+ return Array.from(new Set(TypeMap.values()))
196
+ .flat()
197
+ .filter((e) => !!e && !isPrimitiveType(e));
198
+ }
199
+ ReplicableRegistry.__GetAllReplicables = __GetAllReplicables;
200
+ // eslint-disable-next-line @typescript-eslint/naming-convention
201
+ function __TypesToReplicables(types) {
202
+ return types.flatMap((e) => { var _a; return (_a = TypeMap.get(e)) !== null && _a !== void 0 ? _a : []; });
203
+ }
204
+ ReplicableRegistry.__TypesToReplicables = __TypesToReplicables;
205
+ function GetDataNameOfObjectType(obj) {
206
+ var _a;
207
+ return (_a = obj[TypeSymbol]) === null || _a === void 0 ? void 0 : _a.opts.dataTypeName;
208
+ }
209
+ ReplicableRegistry.GetDataNameOfObjectType = GetDataNameOfObjectType;
210
+ function GetDataName(type) {
211
+ var _a;
212
+ return (_a = type.prototype[TypeSymbol]) === null || _a === void 0 ? void 0 : _a.opts.dataTypeName;
213
+ }
214
+ ReplicableRegistry.GetDataName = GetDataName;
215
+ function GetTypesForName(type) {
216
+ var _a;
217
+ return (_a = TypeMap.get(type)) === null || _a === void 0 ? void 0 : _a.map((e) => isPrimitiveType(e) ? e : e.classConstructor);
218
+ }
219
+ ReplicableRegistry.GetTypesForName = GetTypesForName;
220
+ function GetTypeForDataName(type) {
221
+ return DataNameMap.get(type);
222
+ }
223
+ ReplicableRegistry.GetTypeForDataName = GetTypeForDataName;
224
+ function HasType(type) {
225
+ return TypeMap.has(type);
226
+ }
227
+ ReplicableRegistry.HasType = HasType;
228
+ function IsType(object, type) {
229
+ let typeDefs = TypeMap.get(type);
230
+ let isPrimitive = typeof object !== "object" || object == null;
231
+ return !!(typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.some((e) => isPrimitiveType(e) ? isPrimitive ? getPrimitiveClass(object) === e : false : e === null || e === void 0 ? void 0 : e.isObjectOfThisType(object)));
232
+ }
233
+ ReplicableRegistry.IsType = IsType;
234
+ })(ReplicableRegistry || (ReplicableRegistry = {}));
@@ -0,0 +1,82 @@
1
+ import { Class } from "@bikky/smart-collections";
2
+ import { Pointer } from "../Transformers/Reference.js";
3
+ import { Versions } from "./Versions.js";
4
+ import { TraversalStep } from "./TraversalStep.js";
5
+ import { ReplicableClass } from "../Tracking/Class.js";
6
+ export declare function getClassOfObject<T extends any>(obj: T): Class<T>;
7
+ export declare function generateDataName(name: string): string;
8
+ export declare function describeObject(type: any): string;
9
+ export declare function describeObjects(types: any[], brackets?: boolean): string;
10
+ export type DataType<T extends object = {}> = StructDataType<T> & {
11
+ ID: string;
12
+ Class: string;
13
+ Version: Versions | string;
14
+ SourceFile: string;
15
+ };
16
+ export type StructDataType<T extends object = {}> = Omit<{
17
+ [K in keyof T as T[K] extends Function ? never : Capitalize<string & K>]?: DataTSType<T[K]> | string;
18
+ }, "Id">;
19
+ export type DataTSType<T = {}> = T extends string | number | boolean | void | undefined ? T : T extends Pointer<infer J> ? DataTSType<J> : T extends object ? {
20
+ [K in keyof T as K extends string ? Capitalize<K> : never]?: DataTSType<T[K]> | T[K];
21
+ } | T : T extends Function ? T extends Function & {
22
+ prototype: infer J;
23
+ } ? DataTSType<J> | J : T extends {
24
+ new (...args: any[]): infer J;
25
+ } ? DataTSType<J> | J : never : never;
26
+ export type getObjectReferenceType<T extends Object> = TraversalStep | SerialisedType<T>;
27
+ export type SerialisedType<T extends Object> = {
28
+ [K in Exclude<keyof T, "id"> as T[K] extends Function ? never : Capitalize<string & K>]: SerialisedTSType<T[K]>;
29
+ } & {
30
+ INST_ID?: string;
31
+ INST_REF: [type: string, ...extra: any[]];
32
+ Version: string;
33
+ };
34
+ export type RuntimeStructData<T extends Object> = Omit<{
35
+ [K in keyof T as T[K] extends Function ? never : Capitalize<string & K>]?: T[K] | string;
36
+ }, "Id">;
37
+ export type SerialisedTSType<T = {}> = [
38
+ string,
39
+ T extends string | number | boolean | void | undefined ? T : T extends (infer J)[] ? (J extends {
40
+ __proto__: any;
41
+ } ? SerialisedType<J> : J)[] : T extends JSON ? T : T extends Object ? SerialisedType<T> : T
42
+ ];
43
+ export declare const TypeSymbol: unique symbol;
44
+ export declare const DataSymbol: unique symbol;
45
+ export declare enum ClassCategory {
46
+ Primitive = 0,
47
+ Struct = 1,
48
+ Inlined = 2,
49
+ Pointer = 4,
50
+ Custom = 8
51
+ }
52
+ export declare namespace ClassCategory {
53
+ function isType(type: ClassCategory, category: ClassCategory): boolean;
54
+ function isTypeOr(type: ClassCategory, ...categories: ClassCategory[]): boolean;
55
+ function isTypeAnd(type: ClassCategory, ...categories: ClassCategory[]): boolean;
56
+ }
57
+ export declare enum PropertyTypes {
58
+ AsIs = 0,// No modifications
59
+ Definition = 1,// Storing the class' data instead of value.
60
+ Serialised = 2,// Storing the class' serialised data instead of value.
61
+ ForceInline = 3,// Turns a reference into a value when serialising.
62
+ Expression = 4,
63
+ Array = 8,
64
+ Array2 = 16,
65
+ Dictionary = 32,//Made with {}
66
+ Map = 64,//Made with the Map class
67
+ Solo = 128
68
+ }
69
+ export declare namespace PropertyTypes {
70
+ function isType(type: PropertyTypes, category: PropertyTypes): boolean;
71
+ function isTypeOr(type: PropertyTypes, ...categories: PropertyTypes[]): boolean;
72
+ function isTypeAnd(type: PropertyTypes, ...categories: PropertyTypes[]): boolean;
73
+ function ModOnly(type: PropertyTypes): number;
74
+ function allAreType(category: PropertyTypes, ...types: PropertyTypes[]): boolean;
75
+ }
76
+ export type Primitive = typeof String | typeof Number | typeof Boolean | null | undefined;
77
+ export declare function isPrimitiveType(type: any): type is Primitive;
78
+ export declare function primitiveTypeName(type: any): string;
79
+ export declare function GetTypeName(type: Primitive | ReplicableClass): string;
80
+ export declare function isPrimitiveValue(object: any): boolean;
81
+ export declare function getPrimitiveClass(object: any): NumberConstructor | StringConstructor | BooleanConstructor | null | undefined;
82
+ export declare function ValueIsPrimitiveType(type: Primitive, value: any): boolean;