1
+ // ______ ______ _____ _ _ //
2
+ // | ____| ____| /\ / ____| (_) | | //
3
+ // | |__ | |__ / \ | (___ ___ ____ _ ____ | |_ //
4
+ // | __| | __| / /\ \ \___ \ / __| __| | _ \| __| //
5
+ // | | | |____ / ____ \ ____) | (__| | | | |_) | | //
6
+ // |_| |______/_/ \_\_____/ \___|_| |_| __/| | //
7
+ // | | | | //
8
+ // |_| | |_ //
9
+ // Website: https://feascript.com/ \__| //
10
+
1
11
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs" ;
2
12
import { basicLog } from "./utilities/utilitiesScript.js" ;
3
13
4
14
export class FEAWorkerScript {
15
+ /**
16
+ * Constructor to initialize the FEAWorkerScript class
17
+ * Sets up the worker and initializes the FEAWorkerWrapper.
18
+ */
5
19
constructor ( ) {
6
20
this . worker = null ;
7
21
this . feaWorker = null ;
@@ -10,6 +24,11 @@ export class FEAWorkerScript {
10
24
this . _initWorker ( ) ;
11
25
}
12
26
27
+ /**
28
+ * Initializes the web worker and wraps it using Comlink.
29
+ * @private
30
+ * @throws Will throw an error if the worker fails to initialize.
31
+ */
13
32
async _initWorker ( ) {
14
33
try {
15
34
this . worker = new Worker (
@@ -33,6 +52,12 @@ export class FEAWorkerScript {
33
52
}
34
53
}
35
54
55
+ /**
56
+ * Ensures that the worker is ready before performing any operations.
57
+ * @private
58
+ * @returns {Promise<void> } Resolves when the worker is ready.
59
+ * @throws Will throw an error if the worker is not ready within the timeout period.
60
+ */
36
61
async _ensureReady ( ) {
37
62
if ( this . isReady ) return Promise . resolve ( ) ;
38
63
@@ -54,18 +79,34 @@ export class FEAWorkerScript {
54
79
} ) ;
55
80
}
56
81
82
+ /**
83
+ * Sets the solver configuration in the worker.
84
+ * @param {string } solverConfig - The solver configuration to set.
85
+ * @returns {Promise<boolean> } Resolves when the configuration is set.
86
+ */
57
87
async setSolverConfig ( solverConfig ) {
58
88
await this . _ensureReady ( ) ;
59
89
basicLog ( `FEAWorkerScript: Setting solver config to: ${ solverConfig } ` ) ;
60
90
return this . feaWorker . setSolverConfig ( solverConfig ) ;
61
91
}
62
92
93
+ /**
94
+ * Sets the mesh configuration in the worker.
95
+ * @param {object } meshConfig - The mesh configuration to set.
96
+ * @returns {Promise<boolean> } Resolves when the configuration is set.
97
+ */
63
98
async setMeshConfig ( meshConfig ) {
64
99
await this . _ensureReady ( ) ;
65
100
basicLog ( `FEAWorkerScript: Setting mesh config` ) ;
66
101
return this . feaWorker . setMeshConfig ( meshConfig ) ;
67
102
}
68
103
104
+ /**
105
+ * Adds a boundary condition to the worker.
106
+ * @param {string } boundaryKey - The key identifying the boundary.
107
+ * @param {array } condition - The boundary condition to add.
108
+ * @returns {Promise<boolean> } Resolves when the boundary condition is added.
109
+ */
69
110
async addBoundaryCondition ( boundaryKey , condition ) {
70
111
await this . _ensureReady ( ) ;
71
112
basicLog (
@@ -74,12 +115,21 @@ export class FEAWorkerScript {
74
115
return this . feaWorker . addBoundaryCondition ( boundaryKey , condition ) ;
75
116
}
76
117
118
+ /**
119
+ * Sets the solver method in the worker.
120
+ * @param {string } solverMethod - The solver method to set.
121
+ * @returns {Promise<boolean> } Resolves when the solver method is set.
122
+ */
77
123
async setSolverMethod ( solverMethod ) {
78
124
await this . _ensureReady ( ) ;
79
125
basicLog ( `FEAWorkerScript: Setting solver method to: ${ solverMethod } ` ) ;
80
126
return this . feaWorker . setSolverMethod ( solverMethod ) ;
81
127
}
82
128
129
+ /**
130
+ * Requests the worker to solve the problem.
131
+ * @returns {Promise<object> } Resolves with the solution result.
132
+ */
83
133
async solve ( ) {
84
134
await this . _ensureReady ( ) ;
85
135
basicLog ( "FEAWorkerScript: Requesting solution from worker..." ) ;
@@ -97,16 +147,27 @@ export class FEAWorkerScript {
97
147
return result ;
98
148
}
99
149
150
+ /**
151
+ * Retrieves model information from the worker.
152
+ * @returns {Promise<object> } Resolves with the model information.
153
+ */
100
154
async getModelInfo ( ) {
101
155
await this . _ensureReady ( ) ;
102
156
return this . feaWorker . getModelInfo ( ) ;
103
157
}
104
158
159
+ /**
160
+ * Sends a ping request to the worker to check its availability.
161
+ * @returns {Promise<boolean> } Resolves if the worker responds.
162
+ */
105
163
async ping ( ) {
106
164
await this . _ensureReady ( ) ;
107
165
return this . feaWorker . ping ( ) ;
108
166
}
109
167
168
+ /**
169
+ * Terminates the worker and cleans up resources.
170
+ */
110
171
terminate ( ) {
111
172
if ( this . worker ) {
112
173
this . worker . terminate ( ) ;
0 commit comments