From 2d7a3d1d8af25e6e19215bc01f361e6385a9e485 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 23 Jan 2025 00:21:31 +0400 Subject: [PATCH] release --- .../{index-ChPUFYFo.js => index-BFvUFyQr.js} | 32 +++++--- ...ker-CgNZSpPE.js => web-worker-Cdzbvpxg.js} | 78 +++++++------------ docs/tg-bot-playground/index.html | 15 +++- 3 files changed, 63 insertions(+), 62 deletions(-) rename docs/tg-bot-playground/assets/{index-ChPUFYFo.js => index-BFvUFyQr.js} (99%) rename docs/tg-bot-playground/assets/{web-worker-CgNZSpPE.js => web-worker-Cdzbvpxg.js} (98%) diff --git a/docs/tg-bot-playground/assets/index-ChPUFYFo.js b/docs/tg-bot-playground/assets/index-BFvUFyQr.js similarity index 99% rename from docs/tg-bot-playground/assets/index-ChPUFYFo.js rename to docs/tg-bot-playground/assets/index-BFvUFyQr.js index eb1121f..0fd0740 100644 --- a/docs/tg-bot-playground/assets/index-ChPUFYFo.js +++ b/docs/tg-bot-playground/assets/index-BFvUFyQr.js @@ -41,6 +41,7 @@ const makeRunnableBot = (tsTextModel, worker) => (state) => tsTextModel.getJsCod console.warn("Serizalized js code not defined"); return; } + console.log("code", code.serialized); worker.postMessage({ command: "run-bot", token: state.bot.token, @@ -52,23 +53,29 @@ const makeRunnableBot = (tsTextModel, worker) => (state) => tsTextModel.getJsCod }); const checkTokenAndRun = (state, runnableBot) => { const token = state.bot.token; - if (!token) return; + if (!token) { + state.bot.isReachable = false; + return; + } fetch(`https://api.telegram.org/bot${token}/getMe`).then((_) => _.json()).then((info) => { if (info.ok) { state.bot.name = info.result.first_name; + state.bot.isReachable = true; console.log("Running bot"); runnableBot(state); } else { state.bot.name = "nameless"; + state.bot.isReachable = false; } }).catch((error2) => { console.warn("check token error", error2); + state.bot.isReachable = false; }); }; const makeBotLauncher = async (tsTextModel) => { const worker = new Worker(new URL( /* @vite-ignore */ - "" + new URL("web-worker-CgNZSpPE.js", import.meta.url).href, + "" + new URL("web-worker-Cdzbvpxg.js", import.meta.url).href, import.meta.url ), { type: "module" }); if (!worker) return; @@ -179,15 +186,12 @@ const __vitePreload = function preload(baseModule, deps, importerUrl) { const makeTsTextModel = async (monaco) => { const emptyExample = await fetchText("./example/empty.ts"); const tsModel = monaco.editor.createModel(emptyExample, "typescript"); - let cachedWorkerPromise = null; + let tsWorker = null; const getTsCode = async () => { - if (!cachedWorkerPromise) { - cachedWorkerPromise = (async () => { - const tsWorker = await monaco.languages.typescript.getTypeScriptWorker(); - return tsWorker(tsModel.uri); - })(); + if (!tsWorker) { + tsWorker = await monaco.languages.typescript.getTypeScriptWorker().then((_) => _(tsModel.uri)); } - return cachedWorkerPromise.then((_) => _.getEmitOutput(tsModel.uri.toString())); + return tsWorker.getEmitOutput(tsModel.uri.toString()); }; return { tsModel, @@ -3557,7 +3561,9 @@ const makeGlobalState = (alpine) => { bot: { name: "nameless", status: "idle", - token: "" + token: "", + isAutoReload: false, + isReachable: false }, selectedExample: "empty.ts", botUpdates: [] @@ -3574,7 +3580,11 @@ async function setup() { if (!editor) return; const botLauncher = await makeBotLauncher(editor.tsTextModel); if (!botLauncher) return; - editor.onCodeChange(() => { + editor.onCodeChange(async () => { + if (!state.bot.isReachable || !state.bot.isAutoReload) return; + botLauncher.runBot(state); + }); + document.addEventListener("reload-bot", () => { botLauncher.runBot(state); }); document.addEventListener("check-token", () => { diff --git a/docs/tg-bot-playground/assets/web-worker-CgNZSpPE.js b/docs/tg-bot-playground/assets/web-worker-Cdzbvpxg.js similarity index 98% rename from docs/tg-bot-playground/assets/web-worker-CgNZSpPE.js rename to docs/tg-bot-playground/assets/web-worker-Cdzbvpxg.js index aa36823..d56cad3 100644 --- a/docs/tg-bot-playground/assets/web-worker-CgNZSpPE.js +++ b/docs/tg-bot-playground/assets/web-worker-Cdzbvpxg.js @@ -872,7 +872,6 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, " } } const causeInterrupt = (traces = []) => new Interrupt(traces); - const causeIsFail = (self2) => self2._tag === "Fail"; const causeIsInterrupt = (self2) => self2._tag === "Interrupt"; const MicroFiberTypeId = /* @__PURE__ */ Symbol.for("effect/Micro/MicroFiber"); const fiberVariance = { @@ -1340,22 +1339,6 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, " ...options, while: (exit2) => exit2._tag === "Success" && ((options == null ? void 0 : options.while) === void 0 || options.while(exit2.value)) })); - const catchAllCause = /* @__PURE__ */ dual(2, (self2, f) => { - const onFailure = Object.create(OnFailureProto); - onFailure[args] = self2; - onFailure[failureCont] = f; - return onFailure; - }); - const OnFailureProto = /* @__PURE__ */ makePrimitiveProto({ - op: "OnFailure", - eval(fiber) { - fiber._stack.push(this); - return this[args]; - } - }); - const catchCauseIf = /* @__PURE__ */ dual(3, (self2, predicate, f) => catchAllCause(self2, (cause) => predicate(cause) ? f(cause) : failCause$1(cause))); - const tapErrorCauseIf = /* @__PURE__ */ dual(3, (self2, refinement, f) => catchCauseIf(self2, refinement, (cause) => andThen(f(cause), failCause$1(cause)))); - const tapError = /* @__PURE__ */ dual(2, (self2, f) => tapErrorCauseIf(self2, causeIsFail, (fail2) => f(fail2.error))); const matchCauseEffect = /* @__PURE__ */ dual(2, (self2, options) => { const primitive = Object.create(OnSuccessAndFailureProto); primitive[args] = self2; @@ -2971,6 +2954,8 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); Base.prototype.name = tag; return Base; }; + var BotMessageHandler = class extends Tag("BotMessageHandler")() { + }; var makeSettingsFrom = (input) => { let limit = input.batch_size ?? 10; let timeout = input.timeout ?? 10; @@ -3192,14 +3177,16 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); }) ); }; - var BotUpdatePollerService = class extends Tag("BotUpdatePollerService")() { - }; + (class extends Tag("BotUpdatePollerService")() { + }); var BotUpdatesPollerServiceDefault = gen(function* () { console.log("Initiating BotUpdatesPollerServiceDefault"); const state = { fiber: void 0 }; - const runBot = (messageHandler) => gen(function* () { + const runBot = gen(function* () { + console.log("run bot"); + const messageHandler = yield* service(BotMessageHandler); const startFiber = pollAndHandle({ settings: messageHandler }).pipe( @@ -3207,9 +3194,6 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); tap( (fiber) => fiber.addObserver((exit2) => { console.log("bot's fiber has been closed", exit2); - if (messageHandler.onExit) { - messageHandler.onExit(exit2); - } }) ) ); @@ -3219,10 +3203,10 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); } state.fiber = yield* startFiber; console.log("Fetching bot updates via long polling..."); - return state.fiber; }); return { - runBot + runBot, + getFiber: () => state.fiber }; }); var makeClientConfigFrom = (input) => gen(function* () { @@ -3246,43 +3230,33 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); } return makeTgBotClientConfig(config); }); - var makeBot = (messageHandler) => gen(function* () { - const { runBot } = yield* service(BotUpdatePollerService); - const fiber = yield* runBot(messageHandler); - const interrupt = fiberInterrupt(fiber); - return { - runBot, - interrupt - }; - }).pipe( - tapError((error) => { - console.error(error); - return void_; - }) - ); (class extends Tag("BotFactoryService")() { }); var BotFactoryServiceDefault = { - makeBot, runBot: (input) => gen(function* () { const client = make$2(TgBotClientConfig, yield* makeClientConfigFrom(input)); const poller = yield* BotUpdatesPollerServiceDefault.pipe( provideContext(client) ); - const bot = yield* makeBot(input).pipe( - provideContext(client), - provideService(BotUpdatePollerService, poller) - ); - const reload = (input2) => bot.runBot(input2).pipe( + yield* poller.runBot.pipe( + provideService(BotMessageHandler, input), provideContext(client) ); + const reload = (input2) => poller.runBot.pipe( + provideService(BotMessageHandler, input2), + provideContext(client), + runPromise + ); return { reload, - bot + fiber: poller.getFiber }; }) }; - var runTgChatBot = (input) => BotFactoryServiceDefault.runBot(input).pipe(runPromise); + var runTgChatBot = (input) => BotFactoryServiceDefault.runBot(input).pipe( + provideService(BotMessageHandler, input), + runPromise + ); var getFile = (fileId) => gen(function* () { const response = yield* execute("get_file", { file_id: fileId }); const config = yield* service(TgBotClientConfig); @@ -3345,6 +3319,7 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); messageId: messageId++ }); return async (command) => { + var _a2; if (!isRunBot(command)) { sendEvent({ error: "not a run bot command", @@ -3354,6 +3329,7 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); } if (command.command == "run-bot") { const handlers = deserialize(command.code); + console.log("worker got run-bot command", handlers); if (botInstance) { console.log("reloading..."); await botInstance.reload({ @@ -3369,14 +3345,16 @@ ${this.stack.split("\n").slice(1).join("\n")}` : this.toString(); botInstance = await runTgChatBot({ type: "config", bot_token: command.token, - ...handlers, - onExit: (exit2) => sendEvent({ + ...handlers + }); + (_a2 = botInstance.fiber()) == null ? void 0 : _a2.addObserver((exit2) => { + sendEvent({ success: "Bot's fiber has been shutdown", exit: exit2, botState: { status: "stopped" } - }) + }); }); sendEvent({ success: "Bot's fiber has been created", diff --git a/docs/tg-bot-playground/index.html b/docs/tg-bot-playground/index.html index 86169a1..400dd39 100644 --- a/docs/tg-bot-playground/index.html +++ b/docs/tg-bot-playground/index.html @@ -5,10 +5,11 @@ + - + @@ -18,6 +19,18 @@

Telegram Bot Playground 🤖

+ + + +