Skip to content

Commit 5aa3545

Browse files
committed
refactor(plugin): internalize functions and enhance error handling
- Change exported functions to internal const in instance.js - Add server reachability check in WebSocket connect method - Update wrap method with initialization flag - Add build-time removal of export blocks in build.js
1 parent 58da2fe commit 5aa3545

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ try {
577577
.replaceAll("//<-- DOM_COMPONENT_ID -->", `DOM_COMPONENT_ID = "${config.id}";`)
578578
.replaceAll("//<-- C3_INSTANCE -->", `const C3 = ${sdk === 'v1' ? 'self' : 'globalThis'}.C3;`)
579579
.replaceAll("//<-- SDK_VERSION -->", `sdk = "${sdk}"`)
580+
.replaceAll(/\/\* REMOVE START \*\/[\s\S]*?\/\* REMOVE END \*\//gmi, '')
580581

581582
writeFileSync(`./${exportDir}/c3runtime/plugin.js`, pluginWithPluginInfo);
582583

src/instance.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class WebSocketClient {
3232
* @returns {Promise<WebSocket>} A promise that resolves with the WebSocket instance when connected.
3333
*/
3434
async connect() {
35+
const httpUrl = this.url.replace('ws://', 'http://');
36+
const errorMessage = 'Server not reachable. Make sure to export or preview with Pipelab.'
37+
try {
38+
const response = await fetch(httpUrl);
39+
if (!response.ok) {
40+
throw new Error(`${errorMessage}, status: ${response.status}`);
41+
}
42+
} catch (error) {
43+
console.error('error', error)
44+
throw new Error(errorMessage);
45+
}
46+
3547
return new Promise((resolve, reject) => {
3648
this.socket = new WebSocket(this.url);
3749

@@ -184,7 +196,7 @@ class WebSocketClient {
184196
}
185197
}
186198

187-
export const fullscreenC3StateToPipelabState = (/** @type {import("./sdk.js").IsFullScreenState} */ state) => {
199+
const fullscreenC3StateToPipelabState = (/** @type {import("./sdk.js").IsFullScreenState} */ state) => {
188200
switch (state) {
189201
case 0:
190202
return 'normal';
@@ -195,7 +207,7 @@ export const fullscreenC3StateToPipelabState = (/** @type {import("./sdk.js").Is
195207
}
196208
};
197209

198-
export const fullscreenPipelabStateToC3State = (/** @type {import('@pipelab/core').FullscreenStates} */ state) => {
210+
const fullscreenPipelabStateToC3State = (/** @type {import('@pipelab/core').FullscreenStates} */ state) => {
199211
switch (state) {
200212
case 'normal':
201213
return 0;
@@ -224,7 +236,7 @@ let config = {}
224236
*/
225237

226238
/** @type {import('./sdk.js').GetInstanceJSFn} */
227-
export function getInstanceJs(parentClass, addonTriggers, C3) {
239+
function getInstanceJs(parentClass, addonTriggers, C3) {
228240
// @ts-ignore
229241
return class Pipelab extends parentClass {
230242
_additionalLoadPromises = []
@@ -434,7 +446,11 @@ export function getInstanceJs(parentClass, addonTriggers, C3) {
434446
}
435447

436448
async unsupportedEngine() {
437-
console.warn(`Unable to execute action: unsupported engine`)
449+
console.warn(`Unable to execute action:
450+
- unsupported engine
451+
- server not reachable
452+
- plugin not initialized
453+
`)
438454
}
439455

440456
/**
@@ -461,12 +477,13 @@ export function getInstanceJs(parentClass, addonTriggers, C3) {
461477
* @param {(...params: Parameters<T>) => unknown} callback
462478
* @param {(...params: Parameters<T>) => unknown} [fallback]
463479
* @param {boolean} [force]
480+
* @param {boolean} [isInitialize]
464481
* @returns {T}
465482
*/
466-
wrap(base, callback, fallback, force) {
483+
wrap(base, callback, fallback, force, isInitialize) {
467484
// @ts-expect-error
468485
return (/** @type {Parameters<T>} */ ...args) => {
469-
if (!this._isInitialized) {
486+
if (!this._isInitialized && !isInitialize) {
470487
console.warn("Plugin has no been initialized. Please use the according action at the start of layout")
471488
}
472489

@@ -697,7 +714,7 @@ export function getInstanceJs(parentClass, addonTriggers, C3) {
697714
])
698715
}
699716

700-
}, this.unsupportedEngine, true)
717+
}, this.unsupportedEngine, true, true)
701718
_Initialize = this._InitializeBase
702719
_InitializeSync = this._InitializeBase
703720

@@ -2764,3 +2781,9 @@ export function getInstanceJs(parentClass, addonTriggers, C3) {
27642781
}
27652782
};
27662783
}
2784+
2785+
/* REMOVE START */
2786+
export {
2787+
getInstanceJs
2788+
}
2789+
/* REMOVE END */

0 commit comments

Comments
 (0)