1- <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
1+ ### Import :
2+ ``` js
3+ brackets .getModule (" NodeConnector" )
4+ ```
25
3- ## NodeConnector
6+ < a name = " module_NodeConnector " ></ a >
47
8+ ## NodeConnector
59Node Connector Communication Module
610
711This module simplifies communication between Node.js and Phoenix (phcode). A ` NodeConnector ` acts as an intermediary,
@@ -15,28 +19,29 @@ To establish communication between two modules, such as `x.js` in Phoenix and `y
1519
1620### Create ` NodeConnector ` in Phoenix (` x.js ` )
1721
22+ ** Example**
1823``` js
1924const NodeConnector = require (' NodeConnector' );
2025const XY_NODE_CONNECTOR_ID = ' ext_x_y' ; // Use a unique ID
2126let nodeConnector = NodeConnector .createNodeConnector (XY_NODE_CONNECTOR_ID , exports );
2227
23- exports .modifyImage = async function (imageName , imageArrayBuffer ) \ {
28+ exports .modifyImage = async function (imageName , imageArrayBuffer ) {
2429 // Perform image operations with the imageArrayBuffer
2530 // To return an ArrayBuffer, return an object with a `buffer` key.
26- return \ {
31+ return {
2732 operationDone: ' colored, cropped' ,
2833 buffer: imageArrayBuffer,
2934 };
3035};
3136```
3237
3338### Create ` NodeConnector ` in Node.js (` y.js ` )
34-
39+ ** Example **
3540``` js
3641const XY_NODE_CONNECTOR_ID = ' ext_x_y' ; // Use the same unique ID
3742let nodeConnector = global .createNodeConnector (XY_NODE_CONNECTOR_ID , exports );
3843
39- exports .getPWDRelative = async function (subPath ) \ {
44+ exports .getPWDRelative = async function (subPath ) {
4045 return process .cwd + ' /' + subPath;
4146};
4247```
@@ -46,44 +51,44 @@ With these steps, a `NodeConnector` is set up, enabling two-way communication.
4651## Executing Functions
4752
4853To call a Node.js function from Phoenix, use the ` execPeer ` method.
49-
54+ ** Example **
5055``` js
5156// In `x.js` (Phoenix)
5257const fullPath = await nodeConnector .execPeer (' getPWDRelative' , ' sub/path.html' );
5358```
5459
5560To execute a Phoenix function from Node.js and transfer binary data, pass an optional ArrayBuffer.
56-
61+ ** Example **
5762``` js
5863// In `y.js` (Node.js)
59- const \ { operationDone , buffer } = await nodeConnector .execPeer (' modifyImage' , \ {name: ' theHills.png' }, imageAsArrayBuffer);
64+ const { operationDone , buffer } = await nodeConnector .execPeer (' modifyImage' , {name: ' theHills.png' }, imageAsArrayBuffer);
6065```
6166
6267## Event Handling
6368
6469The ` NodeConnector ` object implements all the APIs supported by ` utils/EventDispatcher ` . You can trigger and listen
6570to events between Node.js and Phoenix using the ` triggerPeer ` and (` on ` , ` one ` or ` off ` ) methods.
66-
71+ ** Example **
6772``` js
6873// In `y.js` (Node.js)
69- nodeConnector .on (' phoenixProjectOpened' , (_event , projectPath ) => \ {
74+ nodeConnector .on (' phoenixProjectOpened' , (_event , projectPath ) => {
7075 console .log (projectPath);
7176});
7277
73- nodeConnector .one (' phoenixProjectOpened' , (_event , projectPath ) => \ {
78+ nodeConnector .one (' phoenixProjectOpened' , (_event , projectPath ) => {
7479 console .log (projectPath + " will be received only once" );
7580});
7681```
7782
7883To raise an event from Phoenix to Node.js:
79-
84+ ** Example **
8085``` js
8186// In `x.js` (Phoenix)
8287nodeConnector .triggerPeer (' phoenixProjectOpened' , ' /x/project/folder' );
8388```
8489
8590To Switch off events
86-
91+ ** Example **
8792``` js
8893nodeConnector .off (' phoenixProjectOpened' ); // will switch off all event handlers of that name.
8994```
@@ -97,37 +102,43 @@ When executing functions that send or receive binary data, ensure that the funct
97102optional ArrayBuffer as a parameter. To return binary data, use an object with a ` buffer ` key.
98103
99104Example of calling a function in Node.js with binary data transfer:
100-
105+ ** Example **
101106``` js
102107// In `y.js` (Node.js)
103- const \ { operationDone , buffer } = await nodeConnector .execPeer (' modifyImage' , \ {name: ' name.png' }, imageArrayBuffer);
108+ const { operationDone , buffer } = await nodeConnector .execPeer (' modifyImage' , {name: ' name.png' }, imageArrayBuffer);
104109```
105110
106111### Handling ArrayBuffer Data in Event Handling
107112
108113Use the ` triggerPeer ` method to send binary data in events. Include the ArrayBuffer as an optional parameter.
109114
110115Example of sending binary data in an event from Phoenix to Node.js:
111-
116+ ** Example **
112117``` js
113118// In `x.js` (Phoenix)
114119const imageArrayBuffer = getSomeImageArrayBuffer (); // Get the ArrayBuffer
115120nodeConnector .triggerPeer (' imageEdited' , ' name.png' , imageArrayBuffer);
116121```
117-
118- * ## Caveats
119-
120- <!-- -->
121-
122- * Be cautious when sending large binary data, as it may affect performance and memory usage. Transferring large
123- data is fully supported, but be mindful of performance.
124- * Functions called with ` execPeer ` and ` triggerPeer ` must be asynchronous and accept a single argument. An optional
125- second argument can be used to transfer large binary data as an ArrayBuffer.
122+ ## Caveats
123+ - Be cautious when sending large binary data, as it may affect performance and memory usage. Transferring large
124+ data is fully supported, but be mindful of performance.
125+ - Functions called with ` execPeer ` and ` triggerPeer ` must be asynchronous and accept a single argument. An optional
126+ second argument can be used to transfer large binary data as an ArrayBuffer.
126127
127128For more event handling operations and details, refer to the documentation for the ` utils/EventDispatcher ` module.
128129
129- ## createNodeConnector
130+ * [ NodeConnector] ( #module_NodeConnector )
131+ * [ .createNodeConnector(nodeConnectorID, moduleExports)] ( #module_NodeConnector..createNodeConnector ) ⇒ <code >Object</code >
132+ * [ .isNodeAvailable()] ( #module_NodeConnector..isNodeAvailable ) ⇒ <code >boolean</code >
133+ * [ .isNodeReady()] ( #module_NodeConnector..isNodeReady ) ⇒ <code >boolean</code >
134+ * [ .terminateNode()] ( #module_NodeConnector..terminateNode ) ⇒ <code >Promise</code >
135+ * [ .setInspectEnabled(enabled)] ( #module_NodeConnector..setInspectEnabled )
136+ * [ .isInspectEnabled()] ( #module_NodeConnector..isInspectEnabled ) ⇒ <code >boolean</code >
137+ * [ .getInspectPort()] ( #module_NodeConnector..getInspectPort ) ⇒ <code >number</code >
138+
139+ <a name =" module_NodeConnector..createNodeConnector " ></a >
130140
141+ ### NodeConnector.createNodeConnector(nodeConnectorID, moduleExports) ⇒ <code >Object</code >
131142Creates a new node connector with the specified ID and module exports.
132143
133144Returns a NodeConnector Object (which is an EventDispatcher with
@@ -142,69 +153,64 @@ If the connector is not created within this timeout period, all queued `execPeer
142153and all queued events will be dropped. It is recommended to call the ` createNodeConnector ` API on both ends
143154within a timeframe of less than 10 seconds(ideally same time) for seamless communication.
144155
145- * execPeer: A function that executes a peer function with specified parameters.
146- * triggerPeer: A function that triggers an event to be sent to a peer.
147- * Also contains all the APIs supported by ` utils/EventDispatcher ` module.
156+ - execPeer: A function that executes a peer function with specified parameters.
157+ - triggerPeer: A function that triggers an event to be sent to a peer.
158+ - Also contains all the APIs supported by ` utils/EventDispatcher ` module.
148159
149- ### Parameters
160+ ** Kind** : inner method of [ <code >NodeConnector</code >] ( #module_NodeConnector )
161+ ** Returns** : <code >Object</code > - - A NodeConnector Object. Also contains all the APIs supported by ` utils/EventDispatcher ` module.
162+ ** Throws** :
150163
151- * ` nodeConnectorID ` ** [ string] [ 1 ] ** The unique identifier for the new node connector.
152- * ` moduleExports ` ** [ Object] [ 2 ] ** The exports of the module that contains the functions to be executed on the other side.
164+ - <code >Error</code > - If a node connector with the same ID already exists/invalid args passed.
153165
154- <!-- -->
155166
156- * Throws ** [ Error] [ 3 ] ** If a node connector with the same ID already exists/invalid args passed.
167+ | Param | Type | Description |
168+ | --- | --- | --- |
169+ | nodeConnectorID | <code >string</code > | The unique identifier for the new node connector. |
170+ | moduleExports | <code >Object</code > | The exports of the module that contains the functions to be executed on the other side. |
157171
158- Returns ** \{ execPeer: [ function] [ 4 ] , triggerPeer: [ function] [ 4 ] , trigger: [ function] [ 4 ] , on: [ function] [ 4 ] , off: [ function] [ 4 ] , one: [ function] [ 4 ] }** A NodeConnector Object. Also contains all the APIs supported by ` utils/EventDispatcher ` module.
159-
160- ## isNodeAvailable
172+ <a name =" module_NodeConnector..isNodeAvailable " ></a >
161173
174+ ### NodeConnector.isNodeAvailable() ⇒ <code >boolean</code >
162175Checks if Node.js Engine is available. (returns true even if the node instance is terminated)
163176
164- Returns ** [ boolean ] [ 5 ] ** Returns true if Node.js Engine is available.
165-
166- ## isNodeReady
177+ ** Kind ** : inner method of [ < code >NodeConnector</ code > ] ( #module_NodeConnector )
178+ ** Returns ** : < code >boolean</ code > - Returns true if Node.js Engine is available.
179+ < a name = " module_NodeConnector.. isNodeReady" ></ a >
167180
181+ ### NodeConnector.isNodeReady() ⇒ <code >boolean</code >
168182Node is available and is ready to exec requests
169183
170- Returns ** [ boolean] [ 5 ] **
171-
172- ## terminateNode
184+ ** Kind** : inner method of [ <code >NodeConnector</code >] ( #module_NodeConnector )
185+ <a name =" module_NodeConnector..terminateNode " ></a >
173186
187+ ### NodeConnector.terminateNode() ⇒ <code >Promise</code >
174188Terminate the PhNodeEngine node if it is available. Else does nothing.
175189
176- Returns ** [ Promise ] [ 6 ] ** promise that resolves when node process is terminated and exits.
177-
178- ## setInspectEnabled
190+ ** Kind ** : inner method of [ < code >NodeConnector</ code > ] ( #module_NodeConnector )
191+ ** Returns ** : < code >Promise</ code > - promise that resolves when node process is terminated and exits.
192+ < a name = " module_NodeConnector.. setInspectEnabled" ></ a >
179193
194+ ### NodeConnector.setInspectEnabled(enabled)
180195Sets weather to enable node inspector in next boot.
181196
182- ### Parameters
197+ ** Kind ** : inner method of [ < code >NodeConnector</ code > ] ( #module_NodeConnector )
183198
184- * ` enabled ` ** [ boolean] [ 5 ] ** true to enable, else false.
199+ | Param | Type | Description |
200+ | --- | --- | --- |
201+ | enabled | <code >boolean</code > | true to enable, else false. |
185202
186- ## isInspectEnabled
203+ < a name = " module_NodeConnector.. isInspectEnabled" ></ a >
187204
205+ ### NodeConnector.isInspectEnabled() ⇒ <code >boolean</code >
188206Returns whether node inspector is enabled. If node is not present, always returns false.
189207
190- Returns ** [ boolean ] [ 5 ] ** True if inspect mode is enabled, false otherwise.
191-
192- ## getInspectPort
208+ ** Kind ** : inner method of [ < code >NodeConnector</ code > ] ( #module_NodeConnector )
209+ ** Returns ** : < code >boolean</ code > - True if inspect mode is enabled, false otherwise.
210+ < a name = " module_NodeConnector.. getInspectPort" ></ a >
193211
212+ ### NodeConnector.getInspectPort() ⇒ <code >number</code >
194213Retrieves the node inspector port for the Phoenix Node.js engine.
195214
196- Returns ** [ number] [ 7 ] ** The inspection port number.
197-
198- [ 1 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
199-
200- [ 2 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
201-
202- [ 3 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
203-
204- [ 4 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
205-
206- [ 5 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
207-
208- [ 6 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
209-
210- [ 7 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
215+ ** Kind** : inner method of [ <code >NodeConnector</code >] ( #module_NodeConnector )
216+ ** Returns** : <code >number</code > - The inspection port number.
0 commit comments