@@ -7,11 +7,28 @@ const nameRegex = /^[a-z0-9 ]+$/i;
77const registry = new Map ( ) ;
88const modules = [ ] ;
99
10+ function load ( { name, mod, dependencies = [ ] , runs = 0 } , methods , local ) {
11+ if ( methods [ name ] !== undefined ) {
12+ console . error ( `Skipping "${ name } ": Already exists` ) ;
13+ return ;
14+ }
15+ const required = dependencies . filter ( ( module ) => methods [ module ] === undefined ) ;
16+ if ( required . length ) {
17+ if ( runs < 5 ) local . push ( { name, mod, dependencies : required , runs : runs + 1 } ) ;
18+ return ;
19+ }
20+ try {
21+ const val = mod ( methods ) ;
22+ if ( val !== undefined ) {
23+ methods [ name ] = val ;
24+ }
25+ } catch ( e ) {
26+ console . error ( `Error loading "${ name } ":` , e ) ;
27+ }
28+ }
29+
1030// TODO: RegisteredPlugin
1131export default function Plugin ( name = '' ) {
12- // eslint-disable-next-line no-param-reassign
13- name = name . trim ( ) ;
14- if ( name === '' ) throw new Error ( 'Plugin must have a name' ) ;
1532 if ( name . length > 20 ) throw new Error ( `Plugin name too long (${ name . length } /20)` ) ;
1633 if ( ! nameRegex . test ( name ) ) throw new Error ( 'Name contains illegal characters' ) ;
1734 if ( registry . has ( name ) ) throw new Error ( 'Name already registered' ) ;
@@ -21,28 +38,8 @@ export default function Plugin(name = '') {
2138 } ;
2239
2340 const local = [ ...modules ] ;
24- function load ( { name : prop , mod, dependencies = [ ] , runs = 0 } ) {
25- if ( methods [ prop ] !== undefined ) {
26- console . error ( `Skipping "${ prop } ": Already exists` ) ;
27- return ;
28- }
29- const required = dependencies . filter ( ( module ) => methods [ module ] === undefined ) ;
30- if ( required . length ) {
31- // eslint-disable-next-line no-param-reassign, no-plusplus
32- if ( runs ++ < 5 ) local . push ( { name : prop , mod, dependencies : required , runs } ) ;
33- return ;
34- }
35- try {
36- const val = mod ( methods ) ;
37- if ( val !== undefined ) {
38- methods [ prop ] = val ;
39- }
40- } catch ( e ) {
41- console . error ( `Error loading "${ prop } ":` , e ) ;
42- }
43- }
4441 for ( let i = 0 ; i < local . length ; i ++ ) {
45- load ( local [ i ] ) ;
42+ load ( local [ i ] , methods , local ) ;
4643 }
4744 local . filter ( ( { runs = 0 , dependencies = [ ] } ) => runs === 5 && dependencies . length )
4845 . forEach ( ( { name : prop , dependencies } ) => console . log ( `Failed to load module: ${ prop } [${ dependencies . join ( ', ' ) } ]` ) ) ;
@@ -52,7 +49,10 @@ export default function Plugin(name = '') {
5249 return plugin ;
5350}
5451
55- api . register ( 'plugin' , Plugin ) ;
52+ api . register ( 'plugin' , ( name ) => {
53+ if ( typeof name !== 'string' || ! name . trim ( ) ) throw new Error ( 'Plugin must have a name' ) ;
54+ return Plugin ( name . trim ( ) ) ;
55+ } ) ;
5656
5757export function registerModule ( name , mod , dependencies ) {
5858 if ( ! name ) throw new Error ( 'Module has no name' ) ;
0 commit comments