-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (75 loc) · 3.8 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (75 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { ExtensionWithFunctionality, MixinName, optionalMixins } from "./mixins/index";
import { ExtensionBase } from "./ExtensionBase";
import scratchInfo from "./mixins/base/scratchInfo";
import scratchVersions from "./mixins/base/scratchVersioning";
import supported from "./mixins/base/supported";
import { ExtensionMenuDisplayDetails, Writeable } from "$common/types";
import { tryCaptureDependencies } from "./mixins/dependencies";
import { tryCreateBundleTimeEvent } from "$common/utils";
export const extensionBundleEvent = tryCreateBundleTimeEvent<{ details: ExtensionMenuDisplayDetails, addOns: MixinName[] }>("extension");
/**
* Creates the base class that your Extension should 'extend' which is compatible with your request.
*
* Your request will have the following two parts:
* @param details The details about how your extension should display and behave within the Extensions Menu.
* Only the `name` field is required, but before your extension can be officially published,
* it will additionally need a `description`, `iconURL`, and `insetIconURL`
* @param addOns An optional collection of specifiers about what functionality this extension should have.
* In this way, the functionality your Extension has access to (through its base class) is configurable.
*
* To see what `addOns` you can specify, place your cursor after the details parameter and type a double quote (").
* Your IDE (code editor) should then suggest what values you can provide (e.g. `"ui"`, `"customArguments"`, `"customSaveData"`, etc.).
*
* **Note:** The order of the `addOns` does not matter.
* @returns
* @example Defining an extension with a name and description (and no add ons)
* ```ts
* export default class Example extends extension({ name: "Some Name", description: "Some description..." }) {
* ...
* }
* ```
* @example Defining an extension with a name and UI functionality
* ```ts
* export default class Example extends extension({ name: "Some Name" }, "ui") {
* ...
* }
* ```
* @example Defining an extension with a name and UI & custom arguments functionality
* ```ts
* export default class Example extends extension({ name: "Some Name" }, "ui", "customArguments") {
* ...
* }
* ```
*/
export const extension = <const TSupported extends readonly MixinName[]>(
details: ExtensionMenuDisplayDetails,
...addOns: Writeable<TSupported>
): ExtensionWithFunctionality<[...TSupported]> & typeof ExtensionBase => {
if (details) extensionBundleEvent?.fire({ details, addOns });
const Base = scratchVersions(scratchInfo(supported(ExtensionBase, addOns))) as ExtensionWithFunctionality<[...TSupported]>;
if (!addOns) return Base;
const { Result, allSupported } = recursivelyApplyMixinsAndDependencies(Base, addOns);
return supported(Result, Array.from(allSupported)) as typeof Result;
}
const recursivelyApplyMixinsAndDependencies = <const TSupported extends readonly MixinName[]>(
Base: ExtensionWithFunctionality<[...TSupported]>,
addons: TSupported,
alreadyAdded: Set<MixinName> = new Set()
): { Result: ExtensionWithFunctionality<[...TSupported]>, allSupported: Set<MixinName> } => {
const Result = addons
.filter(addon => !alreadyAdded.has(addon))
.map(key => {
alreadyAdded.add(key);
return key;
})
.map(key => optionalMixins[key])
.reduce((acc, mixin) => {
const { dependencies, MixedIn } = tryCaptureDependencies(() => mixin(acc));
return !dependencies
? MixedIn
: recursivelyApplyMixinsAndDependencies(MixedIn, dependencies, alreadyAdded).Result as typeof MixedIn;
}, Base);
return { Result, allSupported: alreadyAdded }
}
export type ExtensionConstructor<TSupported extends MixinName[] = []> = ReturnType<typeof extension<TSupported>>;
export type ExtensionInstance<TSupported extends MixinName[] = []> = InstanceType<ExtensionConstructor<TSupported>>;