@niyamulahsan/create-deskapp 1.2.4 → 1.2.5
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/package.json +16 -16
- package/template/src/ui/src/plugins/pulse.ts +179 -147
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@niyamulahsan/create-deskapp",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "Scaffold a new Deskapp (Electron + Vue + npm) project",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"create-deskapp": "./bin/create.js"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"bin",
|
|
11
|
-
"template",
|
|
12
|
-
"template.manifest.json",
|
|
13
|
-
"src"
|
|
14
|
-
],
|
|
15
|
-
"license": "MIT"
|
|
16
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@niyamulahsan/create-deskapp",
|
|
3
|
+
"version": "1.2.5",
|
|
4
|
+
"description": "Scaffold a new Deskapp (Electron + Vue + npm) project",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-deskapp": "./bin/create.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"template",
|
|
12
|
+
"template.manifest.json",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|
|
@@ -1,148 +1,180 @@
|
|
|
1
|
-
import { io, type Socket } from "socket.io-client";
|
|
2
|
-
import type { App } from "vue";
|
|
3
|
-
|
|
4
|
-
declare const __SOCKET_ENABLED__: boolean;
|
|
5
|
-
|
|
6
|
-
type EventCallback<T = unknown> = (payload: T) => void;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
import { io, type Socket } from "socket.io-client";
|
|
2
|
+
import type { App } from "vue";
|
|
3
|
+
|
|
4
|
+
declare const __SOCKET_ENABLED__: boolean;
|
|
5
|
+
|
|
6
|
+
type EventCallback<T = unknown> = (payload: T) => void;
|
|
7
|
+
|
|
8
|
+
// Access token (or a getter returning one) sent with the Socket.IO handshake.
|
|
9
|
+
// The desktop client cannot send the httpOnly auth cookie, so the server reads
|
|
10
|
+
// this bare JWT from `handshake.auth.token`. A getter is preferred because it
|
|
11
|
+
// is resolved on every (re)connect.
|
|
12
|
+
export type PulseToken = string | null | (() => string | null);
|
|
13
|
+
|
|
14
|
+
type ChannelBinding = {
|
|
15
|
+
event: string;
|
|
16
|
+
callback: EventCallback;
|
|
17
|
+
wrapped: EventCallback;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type PulseChannel = {
|
|
21
|
+
listen: <T = unknown>(event: string, callback: EventCallback<T>) => PulseChannel;
|
|
22
|
+
stopListening: (event: string, callback?: EventCallback) => PulseChannel;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type PulseClient = {
|
|
26
|
+
connect: () => PulseClient;
|
|
27
|
+
disconnect: () => PulseClient;
|
|
28
|
+
setToken: (token: PulseToken) => PulseClient;
|
|
29
|
+
channel: (name: string) => PulseChannel;
|
|
30
|
+
private: (room: string) => PulseChannel;
|
|
31
|
+
leave: (room: string) => PulseClient;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function createPulse(): PulseClient {
|
|
35
|
+
if (!__SOCKET_ENABLED__) {
|
|
36
|
+
const n = (): PulseChannel => ({ listen: () => n(), stopListening: () => n() });
|
|
37
|
+
const c: PulseClient = {
|
|
38
|
+
connect: () => c,
|
|
39
|
+
disconnect: () => c,
|
|
40
|
+
setToken: () => c,
|
|
41
|
+
channel: n,
|
|
42
|
+
private: n,
|
|
43
|
+
leave: () => c,
|
|
44
|
+
};
|
|
45
|
+
return c;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let socket: Socket | null = null;
|
|
49
|
+
let tokenSource: PulseToken = null;
|
|
50
|
+
const channels = new Map<string, ChannelBinding[]>();
|
|
51
|
+
const joinedRooms = new Set<string>();
|
|
52
|
+
|
|
53
|
+
// Resolve lazily so every reconnect picks up the current token.
|
|
54
|
+
const resolveToken = () => (typeof tokenSource === "function" ? tokenSource() : tokenSource);
|
|
55
|
+
|
|
56
|
+
const socketUrl = () => {
|
|
57
|
+
const explicit = import.meta.env.VITE_SOCKET_URL || import.meta.env.VITE_API_URL;
|
|
58
|
+
if (explicit) return explicit;
|
|
59
|
+
return window.location.origin;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const ensureConnected = () => {
|
|
63
|
+
if (socket) return socket;
|
|
64
|
+
|
|
65
|
+
socket = io(socketUrl(), {
|
|
66
|
+
withCredentials: true,
|
|
67
|
+
transports: ["websocket", "polling"],
|
|
68
|
+
auth: (callback) => {
|
|
69
|
+
const token = resolveToken();
|
|
70
|
+
callback(token ? { token } : {});
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
socket.on("connect", () => {
|
|
75
|
+
for (const room of joinedRooms) {
|
|
76
|
+
socket?.emit("join", room);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return socket;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const channel = (name: string): PulseChannel => {
|
|
84
|
+
const room = String(name).trim();
|
|
85
|
+
if (!room) throw new Error("Channel name is required");
|
|
86
|
+
|
|
87
|
+
const currentSocket = ensureConnected();
|
|
88
|
+
joinedRooms.add(room);
|
|
89
|
+
currentSocket.emit("join", room);
|
|
90
|
+
|
|
91
|
+
const api: PulseChannel = {
|
|
92
|
+
listen: <T = unknown>(event: string, callback: EventCallback<T>) => {
|
|
93
|
+
const wrapped = (payload: unknown) => callback(payload as T);
|
|
94
|
+
const bindings = channels.get(room) || [];
|
|
95
|
+
bindings.push({ event, callback: callback as EventCallback, wrapped });
|
|
96
|
+
channels.set(room, bindings);
|
|
97
|
+
currentSocket.on(event, wrapped);
|
|
98
|
+
return api;
|
|
99
|
+
},
|
|
100
|
+
stopListening: (event: string, callback?: EventCallback) => {
|
|
101
|
+
const bindings = channels.get(room) || [];
|
|
102
|
+
const kept: ChannelBinding[] = [];
|
|
103
|
+
|
|
104
|
+
for (const binding of bindings) {
|
|
105
|
+
const matchesEvent = binding.event === event;
|
|
106
|
+
const matchesCallback = !callback || binding.callback === callback;
|
|
107
|
+
if (matchesEvent && matchesCallback) {
|
|
108
|
+
currentSocket.off(binding.event, binding.wrapped);
|
|
109
|
+
} else {
|
|
110
|
+
kept.push(binding);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (kept.length > 0) channels.set(room, kept);
|
|
115
|
+
else channels.delete(room);
|
|
116
|
+
return api;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return api;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const pulse: PulseClient = {
|
|
124
|
+
connect: () => {
|
|
125
|
+
ensureConnected();
|
|
126
|
+
return pulse;
|
|
127
|
+
},
|
|
128
|
+
disconnect: () => {
|
|
129
|
+
socket?.disconnect();
|
|
130
|
+
socket = null;
|
|
131
|
+
return pulse;
|
|
132
|
+
},
|
|
133
|
+
setToken: (token: PulseToken) => {
|
|
134
|
+
tokenSource = token;
|
|
135
|
+
if (socket) {
|
|
136
|
+
// Force a new handshake with the updated credentials. The connect
|
|
137
|
+
// handler re-joins every room in joinedRooms.
|
|
138
|
+
socket.disconnect().connect();
|
|
139
|
+
}
|
|
140
|
+
return pulse;
|
|
141
|
+
},
|
|
142
|
+
channel,
|
|
143
|
+
private: (room: string) => channel(`private:${room}`),
|
|
144
|
+
leave: (room: string) => {
|
|
145
|
+
const name = String(room).trim();
|
|
146
|
+
if (!name) return pulse;
|
|
147
|
+
|
|
148
|
+
joinedRooms.delete(name);
|
|
149
|
+
const bindings = channels.get(name) || [];
|
|
150
|
+
channels.delete(name);
|
|
151
|
+
|
|
152
|
+
if (socket) {
|
|
153
|
+
// Tell the server too: dropping listeners alone leaves the socket
|
|
154
|
+
// subscribed and still receiving that room's events.
|
|
155
|
+
socket.emit("leave", name);
|
|
156
|
+
for (const binding of bindings) {
|
|
157
|
+
socket.off(binding.event, binding.wrapped);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return pulse;
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
return pulse;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export const pulse = createPulse();
|
|
169
|
+
|
|
170
|
+
export const PulsePlugin = {
|
|
171
|
+
install(app: App) {
|
|
172
|
+
app.config.globalProperties.$pulse = pulse;
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
declare module "vue" {
|
|
177
|
+
interface ComponentCustomProperties {
|
|
178
|
+
$pulse: PulseClient;
|
|
179
|
+
}
|
|
148
180
|
}
|