Skip to content

Commit e5fc533

Browse files
authored
Create tw-extension-gui-api.js
1 parent eb3bbf4 commit e5fc533

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/lib/tw-extension-gui-api.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import LazyScratchBlocks from './tw-lazy-scratch-blocks';
2+
3+
/**
4+
* Implements Scratch.gui API for unsandboxed extensions.
5+
* @param {any} Scratch window.Scratch, mutated in place.
6+
*/
7+
const implementGuiAPI = Scratch => {
8+
Scratch.gui = {
9+
/**
10+
* Lazily get the internal ScratchBlocks object when it becomes available. It may never be
11+
* available if, for example, the user never enters the editor.
12+
*
13+
* ScratchBlocks becoming available does not necessarily mean the user is in the editor due
14+
* to getBlocklyEagerly() also existing.
15+
*
16+
* @returns {Promise<any>} Promise that may eventually resolve to ScratchBlocks
17+
*/
18+
getBlockly: () => new Promise(resolve => LazyScratchBlocks.onLoaded(resolve)),
19+
20+
/**
21+
* Get the internal ScratchBlocks object as soon as possible. This lets you access it even
22+
* if the user never enters the editor.
23+
*
24+
* This method is VERY SLOW and will cause A LOT OF CPU AND NETWORK ACTIVITY because it
25+
* downloads and evaluates all of scratch-blocks, a multi-megabyte JavaScript bundle.
26+
*
27+
* @returns {Promise<any>} Promise that will resolve to ScratchBlocks.
28+
*/
29+
getBlocklyEagerly: () => LazyScratchBlocks.load()
30+
};
31+
};
32+
33+
export default implementGuiAPI;

0 commit comments

Comments
 (0)