aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ScriptingGlobals.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2022-03-07 12:14:39 -0500
committerbobzel <zzzman@gmail.com>2022-03-07 12:14:39 -0500
commitc21919a2105bd1ed4f060be149624d064739a36c (patch)
treee88bbc4db244a08bd759e2985a31593a4b521911 /src/client/util/ScriptingGlobals.ts
parent7fa30c3edd851cc42cb68063d9dbdd7335fe7370 (diff)
got rid of include cycles for Scripting globals to make hot updates work better.
Diffstat (limited to 'src/client/util/ScriptingGlobals.ts')
-rw-r--r--src/client/util/ScriptingGlobals.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/client/util/ScriptingGlobals.ts b/src/client/util/ScriptingGlobals.ts
new file mode 100644
index 000000000..f151acd81
--- /dev/null
+++ b/src/client/util/ScriptingGlobals.ts
@@ -0,0 +1,81 @@
+
+import * as ts from "typescript";
+export { ts };
+
+export namespace ScriptingGlobals {
+ export function add(global: { name: string }): void;
+ export function add(name: string, global: any): void;
+ export function add(global: { name: string }, decription?: string, params?: string): void;
+ export function add(first: any, second?: any, third?: string) {
+ let n: any;
+ let obj: any;
+
+ if (second !== undefined) {
+ if (typeof first === "string") {
+ n = first;
+ obj = second;
+ } else {
+ obj = first;
+ n = first.name;
+ _scriptingDescriptions[n] = second;
+ if (third !== undefined) {
+ _scriptingParams[n] = third;
+ }
+ }
+ } else if (first && typeof first.name === "string") {
+ n = first.name;
+ obj = first;
+ } else {
+ throw new Error("Must either register an object with a name, or give a name and an object");
+ }
+ if (n === undefined || n === "undefined") {
+ return false;
+ } else if (_scriptingGlobals.hasOwnProperty(n)) {
+ throw new Error(`Global with name ${n} is already registered, choose another name`);
+ }
+ _scriptingGlobals[n] = obj;
+ }
+ export function makeMutableGlobalsCopy(globals?: { [name: string]: any }) {
+ return { ..._scriptingGlobals, ...(globals || {}) };
+ }
+
+ export function setScriptingGlobals(globals: { [key: string]: any }) {
+ scriptingGlobals = globals;
+ }
+
+ export function removeGlobal(name: string) {
+ if (getGlobals().includes(name)) {
+ delete _scriptingGlobals[name];
+ if (_scriptingDescriptions[name]) {
+ delete _scriptingDescriptions[name];
+ }
+ if (_scriptingParams[name]) {
+ delete _scriptingParams[name];
+ }
+ return true;
+ }
+ return false;
+ }
+
+ export function resetScriptingGlobals() { scriptingGlobals = _scriptingGlobals; }
+
+ // const types = Object.keys(ts.SyntaxKind).map(kind => ts.SyntaxKind[kind]);
+ export function printNodeType(node: any, indentation = "") { console.log(indentation + ts.SyntaxKind[node.kind]); }
+
+ export function getGlobals() { return Object.keys(_scriptingGlobals); }
+
+ export function getGlobalObj() { return _scriptingGlobals; }
+
+ export function getDescriptions() { return _scriptingDescriptions; }
+
+ export function getParameters() { return _scriptingParams; }
+}
+
+export function scriptingGlobal(constructor: { new(...args: any[]): any }) {
+ ScriptingGlobals.add(constructor);
+}
+
+const _scriptingGlobals: { [name: string]: any } = {};
+export let scriptingGlobals: { [name: string]: any } = _scriptingGlobals;
+const _scriptingDescriptions: { [name: string]: any } = {};
+const _scriptingParams: { [name: string]: any } = {}; \ No newline at end of file