@@ -2,90 +2,6 @@ import type { Program, ProgramRegistrant } from "./types";
22import type { AbstractFileSystem } from "./filesystem" ;
33import { ANSI , WrappedTerminal } from "./term_ctl" ;
44
5- export class ProgramRegistry {
6- _program_regs : Map < string , ProgramRegistrant > = new Map ( ) ;
7-
8-
9- registerProgram ( program_reg : ProgramRegistrant ) {
10- const program = program_reg . program ;
11-
12- if ( this . _program_regs . has ( program . name ) ) {
13- throw new Error ( `Program with name ${ program . name } already exists.` ) ;
14- }
15-
16- if ( globalThis . OLLIEOS_NODE && program . node_opt_out ) {
17- // don't register this program if it is not compatible with node.js
18- return ;
19- }
20-
21- this . _program_regs . set ( program . name , program_reg ) ;
22- }
23-
24-
25- getProgramRegistrant ( name : string ) : ProgramRegistrant | undefined {
26- return this . _program_regs . get ( name ) ;
27- }
28-
29- getProgram ( name : string ) : Program | undefined {
30- const program_reg = this . getProgramRegistrant ( name ) ;
31- if ( program_reg === undefined ) {
32- return undefined ;
33- }
34-
35- return program_reg . program ;
36- }
37-
38-
39- listProgramRegistrants ( includes_builtin = true , includes_mounted = false ) : ProgramRegistrant [ ] {
40- const arr = Array . from ( this . _program_regs . values ( ) ) ;
41-
42- if ( includes_builtin && includes_mounted ) {
43- return arr ;
44- }
45-
46- if ( includes_builtin && ! includes_mounted ) {
47- return arr . filter ( ( program_reg ) => program_reg . built_in ) ;
48- }
49-
50- if ( ! includes_builtin && includes_mounted ) {
51- return arr . filter ( ( program_reg ) => ! program_reg . built_in ) ;
52- }
53- }
54-
55- listProgramNames ( includes_builtin = true , includes_mounted = false ) : string [ ] {
56- const arr = Array . from ( this . _program_regs . keys ( ) ) ;
57-
58- if ( includes_builtin && includes_mounted ) {
59- return arr ;
60- }
61-
62- if ( includes_builtin && ! includes_mounted ) {
63- return arr . filter ( ( program_name ) => this . getProgramRegistrant ( program_name ) ?. built_in ) ;
64- }
65-
66- if ( ! includes_builtin && includes_mounted ) {
67- return arr . filter ( ( program_name ) => ! this . getProgramRegistrant ( program_name ) ?. built_in ) ;
68- }
69- }
70-
71- listPrograms ( includes_builtin = true , includes_mounted = false ) : Program [ ] {
72- return this . listProgramRegistrants ( includes_builtin , includes_mounted ) . map ( ( program_reg ) => program_reg . program ) ;
73- }
74-
75-
76- forceUnregister ( name : string ) {
77- this . _program_regs . delete ( name ) ;
78- }
79-
80- unregister ( name : string ) {
81- if ( ! this . _program_regs . has ( name ) ) {
82- throw new Error ( `Program with name ${ name } does not exist.` ) ;
83- }
84-
85- this . forceUnregister ( name ) ;
86- }
87- }
88-
895const encode_js_to_url = ( js_code : string ) : string => {
906 const encoded = encodeURIComponent ( js_code ) ;
917 return `data:text/javascript;charset=utf-8,${ encoded } ` ;
@@ -256,3 +172,106 @@ export const recurse_mount_and_register_with_output = async (fs: AbstractFileSys
256172}
257173
258174// TODO: these 2 methods are a bit messy! perhaps remove the output stuff and just have the user deal with it
175+
176+
177+ export class ProgramRegistry {
178+ _program_regs : Map < string , ProgramRegistrant > = new Map ( ) ;
179+
180+
181+ registerProgram ( program_reg : ProgramRegistrant ) {
182+ const program = program_reg . program ;
183+
184+ if ( this . _program_regs . has ( program . name ) ) {
185+ throw new Error ( `Program with name ${ program . name } already exists.` ) ;
186+ }
187+
188+ if ( globalThis . OLLIEOS_NODE && program . node_opt_out ) {
189+ // don't register this program if it is not compatible with node.js
190+ return ;
191+ }
192+
193+ this . _program_regs . set ( program . name , program_reg ) ;
194+ }
195+
196+
197+ getProgramRegistrant ( name : string ) : ProgramRegistrant | undefined {
198+ return this . _program_regs . get ( name ) ;
199+ }
200+
201+ getProgram ( name : string ) : Program | undefined {
202+ const program_reg = this . getProgramRegistrant ( name ) ;
203+ if ( program_reg === undefined ) {
204+ return undefined ;
205+ }
206+
207+ return program_reg . program ;
208+ }
209+
210+
211+ listProgramRegistrants ( includes_builtin = true , includes_mounted = false ) : ProgramRegistrant [ ] {
212+ const arr = Array . from ( this . _program_regs . values ( ) ) ;
213+
214+ if ( includes_builtin && includes_mounted ) {
215+ return arr ;
216+ }
217+
218+ if ( includes_builtin && ! includes_mounted ) {
219+ return arr . filter ( ( program_reg ) => program_reg . built_in ) ;
220+ }
221+
222+ if ( ! includes_builtin && includes_mounted ) {
223+ return arr . filter ( ( program_reg ) => ! program_reg . built_in ) ;
224+ }
225+ }
226+
227+ listProgramNames ( includes_builtin = true , includes_mounted = false ) : string [ ] {
228+ const arr = Array . from ( this . _program_regs . keys ( ) ) ;
229+
230+ if ( includes_builtin && includes_mounted ) {
231+ return arr ;
232+ }
233+
234+ if ( includes_builtin && ! includes_mounted ) {
235+ return arr . filter ( ( program_name ) => this . getProgramRegistrant ( program_name ) ?. built_in ) ;
236+ }
237+
238+ if ( ! includes_builtin && includes_mounted ) {
239+ return arr . filter ( ( program_name ) => ! this . getProgramRegistrant ( program_name ) ?. built_in ) ;
240+ }
241+ }
242+
243+ listPrograms ( includes_builtin = true , includes_mounted = false ) : Program [ ] {
244+ return this . listProgramRegistrants ( includes_builtin , includes_mounted ) . map ( ( program_reg ) => program_reg . program ) ;
245+ }
246+
247+
248+ forceUnregister ( name : string ) {
249+ this . _program_regs . delete ( name ) ;
250+ }
251+
252+ unregister ( name : string ) {
253+ if ( ! this . _program_regs . has ( name ) ) {
254+ throw new Error ( `Program with name ${ name } does not exist.` ) ;
255+ }
256+
257+ this . forceUnregister ( name ) ;
258+ }
259+
260+ // TODO: move usage of above methods to use class methods instead of the standalone functions
261+
262+ static async build_registrant_from_js ( js_code : string , built_in = false ) : Promise < ProgramRegistrant > {
263+ return build_registrant_from_js ( js_code , built_in ) ;
264+ }
265+
266+ static async determine_program_name_from_js ( js_code : string ) : Promise < string > {
267+ return determine_program_name_from_js ( js_code ) ;
268+ }
269+
270+ async mount_and_register_with_output ( filename : string , content : string , term : WrappedTerminal , output_success = false ) {
271+ return mount_and_register_with_output ( filename , content , this , term , output_success ) ;
272+ }
273+
274+ async recurse_mount_and_register_with_output ( fs : AbstractFileSystem , dir_path : string , term : WrappedTerminal ) {
275+ return recurse_mount_and_register_with_output ( fs , dir_path , this , term ) ;
276+ }
277+ }
0 commit comments