dolphin-client 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/README.md +114 -30
- package/dist/dolphin-client.js +78 -0
- package/dist/dolphin-client.min.js +26 -0
- package/dist/index.cjs +78 -0
- package/dist/index.js +78 -0
- package/fulltutorial.md +734 -0
- package/package.json +5 -3
package/dist/index.js
CHANGED
|
@@ -83,6 +83,9 @@ var APIHandler = class {
|
|
|
83
83
|
async requestDirect(method, path, body = null, options = {}) {
|
|
84
84
|
const _isRetry = options._isRetry === true;
|
|
85
85
|
const url = `${this.client.httpUrl}${path.startsWith("/") ? path : "/" + path}`;
|
|
86
|
+
if (this.client.options.debug) {
|
|
87
|
+
console.log(`%c\u{1F680} [Dolphin API Request]:`, "color: #3b82f6; font-weight: bold;", method.toUpperCase(), path, body || "");
|
|
88
|
+
}
|
|
86
89
|
const controller = new AbortController();
|
|
87
90
|
const timeoutId = setTimeout(
|
|
88
91
|
() => controller.abort(),
|
|
@@ -115,6 +118,9 @@ var APIHandler = class {
|
|
|
115
118
|
const contentType = response.headers.get("content-type") || "";
|
|
116
119
|
const data = contentType.includes("application/json") ? await response.json() : await response.text();
|
|
117
120
|
if (!response.ok) throw { status: response.status, data };
|
|
121
|
+
if (this.client.options.debug) {
|
|
122
|
+
console.log(`%c\u2705 [Dolphin API Success]:`, "color: #10b981; font-weight: bold;", method.toUpperCase(), path, data);
|
|
123
|
+
}
|
|
118
124
|
if (data && typeof data === "object") {
|
|
119
125
|
if (data.accessToken) {
|
|
120
126
|
this.client.setToken(data.accessToken);
|
|
@@ -128,6 +134,9 @@ var APIHandler = class {
|
|
|
128
134
|
return data;
|
|
129
135
|
} catch (err) {
|
|
130
136
|
clearTimeout(timeoutId);
|
|
137
|
+
if (this.client.options.debug) {
|
|
138
|
+
console.error(`%c\u274C [Dolphin API Error]:`, "color: #ef4444; font-weight: bold;", method.toUpperCase(), path, err);
|
|
139
|
+
}
|
|
131
140
|
if (err.name === "AbortError") {
|
|
132
141
|
throw { status: 408, data: { error: "Request timed out" } };
|
|
133
142
|
}
|
|
@@ -412,6 +421,7 @@ var DolphinClient = class {
|
|
|
412
421
|
// 64 KB
|
|
413
422
|
maxReconnect: 5,
|
|
414
423
|
autoRefreshToken: true,
|
|
424
|
+
debug: false,
|
|
415
425
|
...options
|
|
416
426
|
};
|
|
417
427
|
this.socket = null;
|
|
@@ -494,6 +504,9 @@ var DolphinClient = class {
|
|
|
494
504
|
_handleMessage(data) {
|
|
495
505
|
try {
|
|
496
506
|
const msg = JSON.parse(data);
|
|
507
|
+
if (this.options.debug) {
|
|
508
|
+
console.log("%c\u{1F4E5} [Dolphin WS Incoming]:", "color: #eab308; font-weight: bold;", msg);
|
|
509
|
+
}
|
|
497
510
|
if (msg.type && msg.from && (msg.to === this.deviceId || msg.to === "all")) {
|
|
498
511
|
if (msg.msgId && msg.type !== "ACK") this._sendAck(msg.from, msg.msgId);
|
|
499
512
|
this.signalHandlers.forEach((h) => h(msg));
|
|
@@ -531,6 +544,9 @@ var DolphinClient = class {
|
|
|
531
544
|
}
|
|
532
545
|
/** @private */
|
|
533
546
|
_sendRaw(msg) {
|
|
547
|
+
if (this.options.debug) {
|
|
548
|
+
console.log("%c\u{1F4E4} [Dolphin WS Outgoing]:", "color: #8b5cf6; font-weight: bold;", msg);
|
|
549
|
+
}
|
|
534
550
|
const str = typeof msg === "string" ? msg : JSON.stringify(msg);
|
|
535
551
|
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
|
536
552
|
this.socket.send(str);
|
|
@@ -988,6 +1004,9 @@ function attachDOMBinding(clientProto) {
|
|
|
988
1004
|
}
|
|
989
1005
|
const store = this.uiStores.get(storeName);
|
|
990
1006
|
store[key] = val;
|
|
1007
|
+
if (this.options.debug) {
|
|
1008
|
+
console.log(`%c\u{1F4BE} [Dolphin Store Update]:`, "color: #ec4899; font-weight: bold;", `${storeName}.${key}`, "=", val);
|
|
1009
|
+
}
|
|
991
1010
|
if (typeof document !== "undefined") {
|
|
992
1011
|
const readElements = document.querySelectorAll(`[data-store-read="${storeName}.${key}"]`);
|
|
993
1012
|
readElements.forEach((el) => {
|
|
@@ -1067,6 +1086,42 @@ function attachDOMBinding(clientProto) {
|
|
|
1067
1086
|
}
|
|
1068
1087
|
return null;
|
|
1069
1088
|
};
|
|
1089
|
+
clientProto._executeStoreAction = function(expression, element) {
|
|
1090
|
+
this.uiStores = this.uiStores || /* @__PURE__ */ new Map();
|
|
1091
|
+
const context = new Proxy({}, {
|
|
1092
|
+
has: (target, prop) => {
|
|
1093
|
+
return true;
|
|
1094
|
+
},
|
|
1095
|
+
get: (target, prop) => {
|
|
1096
|
+
if (typeof prop === "string") {
|
|
1097
|
+
return new Proxy({}, {
|
|
1098
|
+
get: (subTarget, subProp) => {
|
|
1099
|
+
if (typeof subProp === "string") {
|
|
1100
|
+
return this.getStoreState(prop, subProp);
|
|
1101
|
+
}
|
|
1102
|
+
},
|
|
1103
|
+
set: (subTarget, subProp, val) => {
|
|
1104
|
+
if (typeof subProp === "string") {
|
|
1105
|
+
this.setStoreState(prop, subProp, val);
|
|
1106
|
+
return true;
|
|
1107
|
+
}
|
|
1108
|
+
return false;
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
try {
|
|
1115
|
+
const fn = new Function("ctx", `with(ctx) { ${expression} }`);
|
|
1116
|
+
fn(context);
|
|
1117
|
+
} catch (err) {
|
|
1118
|
+
console.error("%c[Dolphin Store Action Error]:", "color: #ef4444; font-weight: bold;", err);
|
|
1119
|
+
if (element) {
|
|
1120
|
+
console.error("%cFailed Element:", "color: #f97316; font-weight: bold;", element);
|
|
1121
|
+
}
|
|
1122
|
+
console.error("%cFailed Expression:", "color: #3b82f6; font-style: italic;", expression);
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1070
1125
|
clientProto._initDOMBinding = function() {
|
|
1071
1126
|
if (this._domInitialized) return;
|
|
1072
1127
|
this._domInitialized = true;
|
|
@@ -1240,6 +1295,14 @@ function attachDOMBinding(clientProto) {
|
|
|
1240
1295
|
console.error(`[Dolphin] API ${evtName} Error:`, err);
|
|
1241
1296
|
}
|
|
1242
1297
|
}
|
|
1298
|
+
const storeActionBtn = e.target.closest(`[data-store-${evtName}]`);
|
|
1299
|
+
if (storeActionBtn) {
|
|
1300
|
+
if (evtName === "submit") e.preventDefault();
|
|
1301
|
+
const expr = storeActionBtn.getAttribute(`data-store-${evtName}`);
|
|
1302
|
+
if (expr) {
|
|
1303
|
+
this._executeStoreAction(expr, storeActionBtn);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1243
1306
|
});
|
|
1244
1307
|
});
|
|
1245
1308
|
this.subscribe("#", (payload, topic) => {
|
|
@@ -2094,6 +2157,21 @@ attachPwa(DolphinClient.prototype);
|
|
|
2094
2157
|
attachTesting(DolphinClient.prototype);
|
|
2095
2158
|
if (typeof window !== "undefined") {
|
|
2096
2159
|
window.DolphinClient = DolphinClient;
|
|
2160
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
2161
|
+
if (!window.dolphin) {
|
|
2162
|
+
const scriptEl = document.querySelector('script[src*="dolphin-client"]');
|
|
2163
|
+
const debugMode = scriptEl ? scriptEl.getAttribute("data-debug") === "true" : false;
|
|
2164
|
+
const dolphin = new DolphinClient(void 0, void 0, { debug: debugMode });
|
|
2165
|
+
window.dolphin = dolphin;
|
|
2166
|
+
if (debugMode) {
|
|
2167
|
+
console.log("%c\u{1F42C} [Dolphin Client] Auto-initialized local reactive engine!", "color: #06b6d4; font-weight: bold; font-size: 14px;");
|
|
2168
|
+
console.log('%c\u{1F449} Tip: You can access the client instance via "window.dolphin" in console.', "color: #94a3b8; font-style: italic;");
|
|
2169
|
+
}
|
|
2170
|
+
if (document.querySelector('[data-store-write="app.username"]')) {
|
|
2171
|
+
dolphin.setStoreState("app", "username", "\u0928\u092E\u0938\u094D\u0924\u0947 \u0938\u093E\u0925\u0940!");
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
});
|
|
2097
2175
|
}
|
|
2098
2176
|
export {
|
|
2099
2177
|
DolphinClient
|