win_webview2 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -22,19 +22,7 @@ ffi.Library(webviewDllPath);
22
22
 
23
23
  module.exports = {
24
24
 
25
-
26
- /**
27
- * @typedef SimpleFFIcallback
28
- *
29
- * @param {(string : str)=>void} jscallback
30
- * @return {SimpleFFIcallback}
31
- */
32
- createFFIcallback : (jscallback)=>{
33
- return ffi.Callback('void', ['string'], (g) => {
34
- jscallback(g);
35
- })
36
- },
37
-
25
+
38
26
 
39
27
  // void OpenWebview(
40
28
  // const char* url,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "win_webview2",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/nnttoo/win_webview2"
package/winWebview2.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  // @ts-check
2
+
3
+ import net from "net"
2
4
  let isOncloseSetted = false;
3
5
  const prefixWebview = "WINWEBVIEW_";
4
6
  const prefixWebviewResult = "WIN_WEBVIEW2_RESULT";
@@ -7,15 +9,10 @@ const prefixWebviewResult = "WIN_WEBVIEW2_RESULT";
7
9
  let resultDataFromWebview2 = "";
8
10
  let resultInUse = false;
9
11
 
10
-
11
- /**
12
- *
13
- * @param {number} n
14
- */
15
- function sleep(n){
16
- return new Promise((r,x)=>{
17
- setTimeout(r,n);
18
- })
12
+ function sleep(n = 1000) {
13
+ return new Promise((r, x) => {
14
+ setTimeout(r, n);
15
+ })
19
16
  }
20
17
 
21
18
 
@@ -23,63 +20,82 @@ function sleep(n){
23
20
  *
24
21
  * @param {object } arg
25
22
  */
26
- async function waitingResult(arg){
27
- if(resultInUse ) return "";
28
- resultInUse = true;
29
-
30
- resultDataFromWebview2 = null;
31
-
32
- var objstr = JSON.stringify(arg);
33
- console.log(`${prefixWebview}JSON` + objstr);
34
-
35
- while(resultDataFromWebview2 == null){
36
- await sleep(200);
37
- }
38
-
39
- resultInUse = false;
40
- return resultDataFromWebview2;
23
+ async function waitingResult(arg) {
24
+ if (resultInUse) return "";
25
+ resultInUse = true;
41
26
 
42
- }
27
+ resultDataFromWebview2 = null;
43
28
 
29
+ var objstr = JSON.stringify(arg);
30
+ sendToPipe(`${prefixWebview}JSON` + objstr);
31
+
32
+ while (resultDataFromWebview2 == null) {
33
+ await sleep(200);
34
+ }
35
+
36
+ resultInUse = false;
37
+ return resultDataFromWebview2;
44
38
 
45
- /**
46
- *
47
- * @param {string} data
48
- */
49
- function setToResult(data){
50
- if(!data.startsWith(prefixWebviewResult)) return false;
51
- let result = data.substring(prefixWebviewResult.length);
52
- resultDataFromWebview2 = result;
53
- return true;
54
39
  }
55
40
 
56
- function setOnClosed() {
57
- process.stdin.on("data", (dataIn) => {
58
- let data = dataIn.toString();
59
- if(setToResult(data)) return;
60
-
61
- console.log(data);
62
- if (data == `${prefixWebview}MAINPAGEISCLOSED`) {
63
- console.log("server diclose");
64
- process.exit();
65
- }
66
- })
41
+
42
+
43
+ async function createPipe() {
44
+
45
+ // ini akan terhubung terus menerus
46
+
47
+ /** @type {string} */
48
+ // @ts-ignore
49
+ let PIPE_CPLUS_SENDER = process.env.PIPE_PATH + "Sender";
50
+
51
+
52
+ let client = net.createConnection(PIPE_CPLUS_SENDER, () => {
53
+ console.log('Anak terhubung ke Named Pipe!');
54
+ });
55
+
56
+ client.on('data', (buff) => {
57
+ let data = buff.toString();
58
+
59
+ if (!data.startsWith(prefixWebviewResult)) return false;
60
+ let result = data.substring(prefixWebviewResult.length);
61
+ resultDataFromWebview2 = result;
62
+ });
63
+
64
+ // while(true){
65
+ // await sleep();
66
+ // sendToPipe("ping");
67
+ // }
67
68
  }
68
69
 
70
+ /**
71
+ * @param {string} msg
72
+ */
73
+ function sendToPipe(msg) {
74
+
75
+ /** @type {any} */
76
+ var PIPE_CPLUS_Reader = process.env.PIPE_PATH;
77
+ let client = net.createConnection(PIPE_CPLUS_Reader, () => {
78
+ console.log("PIPE_CPLUS_Reader terhubung")
79
+ client.write(msg);
80
+ });
81
+ client.on("error",(e)=>{
82
+ console.log("errornya apa : ", e );
83
+ })
84
+ }
69
85
 
70
86
  /**
71
87
  *
72
88
  * @param {import("./argtype").OpenWebArg } arg
73
89
  */
74
90
  export function openWeb(arg) {
75
- if(!isOncloseSetted){
76
- isOncloseSetted = true;
77
- setOnClosed();
78
- }
79
-
80
- arg["fun"] = "OpenWeb";
81
- var objstr = JSON.stringify(arg);
82
- console.log(`${prefixWebview}JSON` + objstr);
91
+ if (!isOncloseSetted) {
92
+ isOncloseSetted = true;
93
+ createPipe();
94
+ }
95
+
96
+ arg["fun"] = "OpenWeb";
97
+ var objstr = JSON.stringify(arg);
98
+ sendToPipe(`${prefixWebview}JSON` + objstr);
83
99
  }
84
100
 
85
101
  /**
@@ -87,14 +103,14 @@ export function openWeb(arg) {
87
103
  * @param {import("./argtype").OpenDialogFileArg} arg
88
104
  * @returns {Promise<string>}
89
105
  */
90
- export function openDialogFile(arg){
91
- arg["fun"] = "OpenDialogFIle";
92
- return waitingResult(arg);
106
+ export function openDialogFile(arg) {
107
+ arg["fun"] = "OpenDialogFIle";
108
+ return waitingResult(arg);
93
109
  }
94
110
 
95
- export async function openDialogFolder(){
96
- let obj = {
97
- fun : "OpenDialogFolder"
98
- };
99
- return waitingResult(obj);
111
+ export async function openDialogFolder() {
112
+ let obj = {
113
+ fun: "OpenDialogFolder"
114
+ };
115
+ return waitingResult(obj);
100
116
  }
Binary file
Binary file