1
|
{"version":3,"file":"NamespacedValue.js","names":["NamespacedValue","constructor","stable","unstable","Error","name","altName","names","push","matches","val","findIn","obj","undefined","includedIn","arr","included","includes","exports","ServerControlledNamespacedValue","args","_defineProperty2","default","setPreferUnstable","preferUnstable","UnstableValue"],"sources":["../src/NamespacedValue.ts"],"sourcesContent":["/*\nCopyright 2021 - 2022 The Matrix.org Foundation C.I.C.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport { Optional } from \"matrix-events-sdk/lib/types\";\n\n/**\n * Represents a simple Matrix namespaced value. This will assume that if a stable prefix\n * is provided that the stable prefix should be used when representing the identifier.\n */\nexport class NamespacedValue<S extends string, U extends string> {\n // Stable is optional, but one of the two parameters is required, hence the weird-looking types.\n // Goal is to to have developers explicitly say there is no stable value (if applicable).\n public constructor(stable: S, unstable: U);\n public constructor(stable: S, unstable?: U);\n public constructor(stable: null | undefined, unstable: U);\n public constructor(public readonly stable?: S | null, public readonly unstable?: U) {\n if (!this.unstable && !this.stable) {\n throw new Error(\"One of stable or unstable values must be supplied\");\n }\n }\n\n public get name(): U | S {\n if (this.stable) {\n return this.stable;\n }\n return this.unstable!;\n }\n\n public get altName(): U | S | null | undefined {\n if (!this.stable) {\n return null;\n }\n return this.unstable;\n }\n\n public get names(): (U | S)[] {\n const names = [this.name];\n const altName = this.altName;\n if (altName) names.push(altName);\n return names;\n }\n\n public matches(val: string): boolean {\n return this.name === val || this.altName === val;\n }\n\n // this desperately wants https://github.com/microsoft/TypeScript/pull/26349 at the top level of the class\n // so we can instantiate `NamespacedValue<string, _, _>` as a default type for that namespace.\n public findIn<T>(obj: any): Optional<T> {\n let val: T | undefined = undefined;\n if (this.name) {\n val = obj?.[this.name];\n }\n if (!val && this.altName) {\n val = obj?.[this.altName];\n }\n return val;\n }\n\n public includedIn(arr: any[]): boolean {\n let included = false;\n if (this.name) {\n included = arr.includes(this.name);\n }\n if (!included && this.altName) {\n included = arr.includes(this.altName);\n }\n return included;\n }\n}\n\nexport class ServerControlledNamespacedValue<S extends string, U extends string> extends NamespacedValue<S, U> {\n private preferUnstable = false;\n\n public setPreferUnstable(preferUnstable: boolean): void {\n this.preferUnstable = preferUnstable;\n }\n\n public get name(): U | S {\n if (this.stable && !this.preferUnstable) {\n return this.stable;\n }\n return this.unstable!;\n }\n}\n\n/**\n * Represents a namespaced value which prioritizes the unstable value over the stable\n * value.\n */\nexport class UnstableValue<S extends string, U extends string> extends NamespacedValue<S, U> {\n // Note: Constructor difference is that `unstable` is *required*.\n public constructor(stable: S, unstable: U) {\n super(stable, unstable);\n if (!this.unstable) {\n throw new Error(\"Unstable value must be supplied\");\n }\n }\n\n public get name(): U {\n return this.unstable!;\n }\n\n public get altName(): S {\n return this.stable!;\n }\n}\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACO,MAAMA,eAAe,CAAqC;EAC7D;EACA;;EAIOC,WAAWA,CAAiBC,MAAiB,EAAkBC,QAAY,EAAE;IAAA,KAAjDD,MAAiB,GAAjBA,MAAiB;IAAA,KAAkBC,QAAY,GAAZA,QAAY;IAC9E,IAAI,CAAC,IAAI,CAACA,QAAQ,IAAI,CAAC,IAAI,CAACD,MAAM,EAAE;MAChC,MAAM,IAAIE,KAAK,CAAC,mDAAmD,CAAC;IACxE;EACJ;EAEA,IAAWC,IAAIA,CAAA,EAAU;IACrB,IAAI,IAAI,CAACH,MAAM,EAAE;MACb,OAAO,IAAI,CAACA,MAAM;IACtB;IACA,OAAO,IAAI,CAACC,QAAQ;EACxB;EAEA,IAA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