|
35 | 35 | * |
36 | 36 | * ### Create `NodeConnector` in Phoenix (`x.js`) |
37 | 37 | * |
| 38 | + * @example |
38 | 39 | * ```js |
39 | 40 | * const NodeConnector = require('NodeConnector'); |
40 | 41 | * const XY_NODE_CONNECTOR_ID = 'ext_x_y'; // Use a unique ID |
|
52 | 53 | * |
53 | 54 | * ### Create `NodeConnector` in Node.js (`y.js`) |
54 | 55 | * |
| 56 | + * @example |
55 | 57 | * ```js |
56 | 58 | * const XY_NODE_CONNECTOR_ID = 'ext_x_y'; // Use the same unique ID |
57 | 59 | * let nodeConnector = global.createNodeConnector(XY_NODE_CONNECTOR_ID, exports); |
|
67 | 69 | * |
68 | 70 | * To call a Node.js function from Phoenix, use the `execPeer` method. |
69 | 71 | * |
| 72 | + * @example |
70 | 73 | * ```js |
71 | 74 | * // In `x.js` (Phoenix) |
72 | 75 | * const fullPath = await nodeConnector.execPeer('getPWDRelative', 'sub/path.html'); |
73 | 76 | * ``` |
74 | 77 | * |
75 | 78 | * To execute a Phoenix function from Node.js and transfer binary data, pass an optional ArrayBuffer. |
76 | 79 | * |
| 80 | + * @example |
77 | 81 | * ```js |
78 | 82 | * // In `y.js` (Node.js) |
79 | 83 | * const { operationDone, buffer } = await nodeConnector.execPeer('modifyImage', {name:'theHills.png'}, imageAsArrayBuffer); |
|
84 | 88 | * The `NodeConnector` object implements all the APIs supported by `utils/EventDispatcher`. You can trigger and listen |
85 | 89 | * to events between Node.js and Phoenix using the `triggerPeer` and (`on`, `one` or `off`) methods. |
86 | 90 | * |
| 91 | + * @example |
87 | 92 | * ```js |
88 | 93 | * // In `y.js` (Node.js) |
89 | 94 | * nodeConnector.on('phoenixProjectOpened', (_event, projectPath) => { |
|
97 | 102 | * |
98 | 103 | * To raise an event from Phoenix to Node.js: |
99 | 104 | * |
| 105 | + * @example |
100 | 106 | * ```js |
101 | 107 | * // In `x.js` (Phoenix) |
102 | 108 | * nodeConnector.triggerPeer('phoenixProjectOpened', '/x/project/folder'); |
103 | 109 | * ``` |
104 | 110 | * |
105 | 111 | * To Switch off events |
| 112 | + * @example |
106 | 113 | * ```js |
107 | 114 | * nodeConnector.off('phoenixProjectOpened'); // will switch off all event handlers of that name. |
108 | 115 | * ``` |
|
117 | 124 | * |
118 | 125 | * Example of calling a function in Node.js with binary data transfer: |
119 | 126 | * |
| 127 | + * @example |
120 | 128 | * ```js |
121 | 129 | * // In `y.js` (Node.js) |
122 | 130 | * const { operationDone, buffer } = await nodeConnector.execPeer('modifyImage', {name:'name.png'}, imageArrayBuffer); |
|
128 | 136 | * |
129 | 137 | * Example of sending binary data in an event from Phoenix to Node.js: |
130 | 138 | * |
| 139 | + * @example |
131 | 140 | * ```js |
132 | 141 | * // In `x.js` (Phoenix) |
133 | 142 | * const imageArrayBuffer = getSomeImageArrayBuffer(); // Get the ArrayBuffer |
|
0 commit comments