From 6a110e3394be427cb734c674535e5203fd07fadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Olguncu?= <21091016+ogzhanolguncu@users.noreply.github.com> Date: Tue, 17 Oct 2023 15:01:14 +0300 Subject: [PATCH] Add length to pipeline (#656) * Add length to pipeline * Run fmt on pipeline.ts --- .../README.md | 7 ++++--- examples/google-cloud-functions/README.md | 7 ++++--- pkg/pipeline.test.ts | 21 +++++++++++++++++++ pkg/pipeline.ts | 9 +++++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/examples/google-cloud-functions-with-typescript/README.md b/examples/google-cloud-functions-with-typescript/README.md index 3dbabff7..8ae761bc 100644 --- a/examples/google-cloud-functions-with-typescript/README.md +++ b/examples/google-cloud-functions-with-typescript/README.md @@ -12,9 +12,10 @@ npm install 2. Create a free Database on [upstash.com](https://console.upstash.com/redis) 3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to - `Runtime, build, connections and security settings > Runtime environment variables` in GCP website when creating a new function or pass those keys when you are deploying from the CLI. - + `Runtime, build, connections and security settings > Runtime environment variables` + in GCP website when creating a new function or pass those keys when you are + deploying from the CLI. ## Work locally -Simply run `npm run start` \ No newline at end of file +Simply run `npm run start` diff --git a/examples/google-cloud-functions/README.md b/examples/google-cloud-functions/README.md index 9981304f..74c6d259 100644 --- a/examples/google-cloud-functions/README.md +++ b/examples/google-cloud-functions/README.md @@ -12,9 +12,10 @@ npm install 2. Create a free Database on [upstash.com](https://console.upstash.com/redis) 3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to - `Runtime, build, connections and security settings > Runtime environment variables` in GCP when creating a new function or pass those keys when you are deploying from the CLI. - + `Runtime, build, connections and security settings > Runtime environment variables` + in GCP when creating a new function or pass those keys when you are deploying + from the CLI. ## Work locally -Simply run `npm run start` \ No newline at end of file +Simply run `npm run start` diff --git a/pkg/pipeline.test.ts b/pkg/pipeline.test.ts index 8f6ad3b2..c471eb35 100644 --- a/pkg/pipeline.test.ts +++ b/pkg/pipeline.test.ts @@ -67,6 +67,27 @@ Deno.test("when no commands were added", async (t) => { }); }); +Deno.test("when length called", async (t) => { + await t.step("before exec()", () => { + const key = newKey(); + const p = new Pipeline({ client }); + for (let i = 0; i < 10; i++) { + p.set(key, randomID()); + } + assertEquals(p.length(), 10); + }); + + await t.step("after exec()", async () => { + const key = newKey(); + const p = new Pipeline({ client }); + for (let i = 0; i < 10; i++) { + p.set(key, randomID()); + } + await p.exec(); + assertEquals(p.length(), 10); + }); +}); + Deno.test("when one command throws an error", async (t) => { await t.step("throws", async () => { const p = new Pipeline({ client }).set("key", "value").hget("key", "field"); diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index 7f675d26..3d78e900 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -207,7 +207,7 @@ export class Pipeline[] = []> { }) { this.client = opts.client; - this.commands = ([] as unknown) as TCommands; // the TCommands generic in the class definition is only used for carrying through chained command types and should never be explicitly set when instantiating the class + this.commands = [] as unknown as TCommands; // the TCommands generic in the class definition is only used for carrying through chained command types and should never be explicitly set when instantiating the class this.commandOptions = opts.commandOptions; this.multiExec = opts.multiExec ?? false; } @@ -250,6 +250,13 @@ export class Pipeline[] = []> { }) as TCommandResults; }; + /** + * Returns the length of pipeline before the execution + */ + length(): number { + return this.commands.length; + } + /** * Pushes a command into the pipeline and returns a chainable instance of the * pipeline