UNPKG

@litert/redis

Version:

A redis protocol implement for Node.js.

119 lines (113 loc) 3.56 kB
"use strict"; /** * Copyright 2025 Angus.Fenying <fenying@litert.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MultiClient = void 0; /* eslint-disable @typescript-eslint/unbound-method */ const C = require("./Common"); const E = require("./Errors"); const CMD = require("./Commands"); const ProtocolClient_1 = require("./ProtocolClient"); class MultiClient extends ProtocolClient_1.ProtocolClient { _queue; _multi = false; constructor(opts) { super({ mode: C.EClientMode.SIMPLE, ...opts }); this._queue = []; this.on('close', () => { this._multi = false; this._queue = []; }); } async watch(keys) { await this._command('WATCH', keys); } async unwatch() { await this._command('UNWATCH', []); } async multi() { if (this._multi) { return; } await this._command('multi', []); this._multi = true; } async discard() { if (this._multi) { await this._command('discard', []); } this._queue = []; this._multi = false; } async exec() { if (!this._multi) { throw new E.E_NOT_MULTI_MODE(); } if (!this._queue.length) { await this.discard(); return []; } const queue = this._queue; this._queue = []; const ret = new Array(queue.length); const data = await this.command('EXEC', []); this._multi = false; for (let i = 0; i < queue.length; i++) { const qi = queue[i]; if (qi.process === undefined) { ret[i] = data[i][1]; } else if (qi.process === null) { ret[i] = null; } else { ret[i] = qi.process(data[i][1], qi.args, qi.ctx); } } return ret; } command(cmd, args, cb) { if (!this._multi) { throw new E.E_NOT_MULTI_MODE(); } const ret = this._command(cmd, args, cb); this._queue.push({ args, process: undefined, cmd }); return ret; } } exports.MultiClient = MultiClient; (function (c) { let name; for (name in CMD.COMMANDS) { if (name === 'auth' || name === 'select') { continue; } const cmd = CMD.COMMANDS[name]; c.prototype[name] = (new Function('process', 'command', 'E', `return function(...args) { if (!this._multi) { throw new E.E_NOT_MULTI_MODE(); } const req = command(...args); const ret = this._command(req.cmd, req.args); this._queue.push({ args: req.args, process: process, cmd: req.cmd, ctx: req.ctx }); return ret; };`))(cmd.process, cmd.prepare, E); } })(MultiClient); //# sourceMappingURL=WatchClient.js.map