1+ import EventBus from './event-bus.js'
12import Media from './media.js'
2- import Publisher from './publisher.js'
3- import Subscriber from './subscriber.js'
43import Parameter from './parameter.js'
54import Readme from './readme.js'
65import Manifest from './manifest.js'
@@ -17,6 +16,7 @@ export default class Extension {
1716 #callbacks = { }
1817 #category = null
1918 #demo = false
19+ #eventBus = null
2020 #features = [ ]
2121 #functions = [ ]
2222 #functionSchemas = [ ]
@@ -27,81 +27,58 @@ export default class Extension {
2727 #parameters = [ ]
2828 #softwareVersion = null
2929 #started = false
30- #token = null
3130 #website = null
3231
33- #publisher = null
34-
35- /**
36- * @param {String } token - The token of the extension.
37- * @returns {Extension } The created extension instance.
38- */
39- constructor ( token ) {
40- this . #token = token || process . argv [ 2 ] || process . env . TALKOPS_TOKEN
41- }
42-
4332 async #setup( ) {
44- if ( this . #token) {
45- const mercure = JSON . parse ( Buffer . from ( this . #token, 'base64' ) . toString ( ) )
46- this . #publisher = new Publisher (
47- ( ) => {
48- return {
49- mercure,
50- }
51- } ,
52- ( ) => {
53- return {
54- category : this . #category,
55- demo : this . #demo,
56- icon : this . #icon,
57- installationSteps : this . #installationSteps,
58- instructions : this . #instructions,
59- name : this . #name,
60- parameters : this . #parameters,
61- sdk : {
62- name : 'nodejs' ,
63- version : pkg . version ,
64- } ,
65- softwareVersion : this . #softwareVersion,
66- functionSchemas : this . #functionSchemas,
67- }
68- } ,
69- )
70- new Subscriber ( ( ) => {
71- return {
72- callbacks : this . #callbacks,
73- extension : this ,
74- functions : this . #functions,
75- mercure,
76- parameters : this . #parameters,
77- publisher : this . #publisher,
78- }
79- } )
80- }
81-
82- if ( process . env . NODE_ENV && process . env . NODE_ENV === 'development' ) {
83- new Readme ( ( ) => {
84- return {
85- features : this . #features,
86- name : this . #name,
87- }
88- } )
89- new Manifest ( ( ) => {
33+ this . #eventBus = new EventBus (
34+ ( ) => {
9035 return {
9136 category : this . #category,
9237 demo : this . #demo,
93- features : this . #features,
9438 icon : this . #icon,
39+ installationSteps : this . #installationSteps,
40+ instructions : this . #instructions,
9541 name : this . #name,
42+ parameters : this . #parameters,
9643 sdk : {
9744 name : 'nodejs' ,
9845 version : pkg . version ,
9946 } ,
10047 softwareVersion : this . #softwareVersion,
101- website : this . #website ,
48+ functionSchemas : this . #functionSchemas ,
10249 }
103- } )
104- }
50+ } ,
51+ ( ) => {
52+ return {
53+ callbacks : this . #callbacks,
54+ functions : this . #functions,
55+ parameters : this . #parameters,
56+ }
57+ } ,
58+ )
59+
60+ new Readme ( ( ) => {
61+ return {
62+ features : this . #features,
63+ name : this . #name,
64+ }
65+ } )
66+
67+ new Manifest ( ( ) => {
68+ return {
69+ category : this . #category,
70+ demo : this . #demo,
71+ features : this . #features,
72+ icon : this . #icon,
73+ name : this . #name,
74+ sdk : {
75+ name : 'nodejs' ,
76+ version : pkg . version ,
77+ } ,
78+ softwareVersion : this . #softwareVersion,
79+ website : this . #website,
80+ }
81+ } )
10582 }
10683
10784 /**
@@ -298,7 +275,7 @@ export default class Extension {
298275 * Enables alarm.
299276 */
300277 enableAlarm ( ) {
301- this . #publisher . publishEvent ( { type : 'alarm' } )
278+ this . #eventBus . publishEvent ( { type : 'alarm' } )
302279 }
303280
304281 /**
@@ -310,7 +287,7 @@ export default class Extension {
310287 if ( ! medias . every ( ( item ) => item instanceof Media ) ) {
311288 throw new Error ( 'medias must be an array of Media instances.' )
312289 }
313- this . #publisher . publishEvent ( {
290+ this . #eventBus . publishEvent ( {
314291 type : 'medias' ,
315292 medias : medias . map ( ( media ) => media . toJSON ( ) ) ,
316293 } )
@@ -324,7 +301,7 @@ export default class Extension {
324301 if ( typeof text !== 'string' || text . trim ( ) === '' ) {
325302 throw new TypeError ( 'text must be a non-empty string.' )
326303 }
327- this . #publisher . publishEvent ( { type : 'message' , text } )
304+ this . #eventBus . publishEvent ( { type : 'message' , text } )
328305 }
329306
330307 /**
@@ -335,6 +312,6 @@ export default class Extension {
335312 if ( typeof text !== 'string' || text . trim ( ) === '' ) {
336313 throw new TypeError ( 'text must be a non-empty string.' )
337314 }
338- this . #publisher . publishEvent ( { type : 'notification' , text } )
315+ this . #eventBus . publishEvent ( { type : 'notification' , text } )
339316 }
340317}
0 commit comments