-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathExtensionBase.ts
More file actions
76 lines (69 loc) · 2.69 KB
/
ExtensionBase.ts
File metadata and controls
76 lines (69 loc) · 2.69 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
import { AbstractConstructor, Environment, ExlcudeFirst, } from "$common/types";
import { Runtime } from "$common/types/scratch/vm";
export type ExtensionConstructorParams = ConstructorParameters<typeof ConstructableExtension>;
export type AuxiliaryExtensionInfoParams = ExlcudeFirst<ExtensionConstructorParams>;
export type ExtensionBaseConstructor = AbstractConstructor<ExtensionBase>;
export abstract class ConstructableExtension {
/**
* @summary This member function (or 'method') will be called when a user adds your extension via the Extensions Menu (i.e. when your extension is instantiated)
* @example
* // Initialize class field(s)
* private count: number;
*
* init() {
* count = 0;
* }
* @example
* // Interact with environment's runtime
* init(env: Environment) {
* env.runtime.emit(RuntimeEvent.ProjectStart);
* }
* @example
* // Nothing to initialize
* init() {}
* @description This function is intended to behave exactly like a constructor, used to initialize the state of your extension.
*
* The reason we use this function INSTEAD of a constructor is so that the base Extension class can manage the construction of this class.
*
* This also allows us to enable this method to be async (if you'd like).
* @param {Environment} env An object that allows your Extension to interact with the Scratch Environment. Currently is a little bare, but will be expanded soon.
* Can be ommitted if not needed.
*
* For Scratch developers: The `runtime` property on env is the same as the runtime passed to non-Typescript-Framework Extension constructors
*/
abstract init(env: Environment): void | Promise<void>;
protected async internal_init() {
const runtime = this.runtime;
const id = this.id;
return await Promise.resolve(this.init({
runtime,
get extensionManager() { return runtime.getExtensionManager() },
scrollIntoView() { runtime.emitScrollUpdate(id) }
}));
}
/**
*
* @param runtime The 'runtime' connected to the scratch-vm that enables your extension to interact with the scratch workspace
* @param name The name of this extension.
* @param id The ID of this extension.
* @param blockIconURI
*/
constructor(
readonly runtime: Runtime,
readonly name: string,
readonly id: string,
readonly blockIconURI: string,
readonly blockColor: string,
readonly menuColor: string,
readonly menuSelectColor: string
) {
}
}
export const extensionsMap = new Map<string, ExtensionBase>();
export abstract class ExtensionBase extends ConstructableExtension {
constructor(FORBIDDEN: never) {
// @ts-ignore
super(...arguments);
extensionsMap.set(this.id, this);
}
}