-
Notifications
You must be signed in to change notification settings - Fork 0
Added delay operator #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
51ed6e9
Added delay operator
alexanderharding 95da2cd
Applied formatting
alexanderharding 148c412
Merge branch 'main' into delay
alexanderharding 2491c52
Refactor delay operator so it's early return logic is in alignment wi…
alexanderharding b677c6b
Corrected delay readme
alexanderharding 872cec6
Merge branch 'main' into delay
alexanderharding 7cc08d0
Added modern logic to the delay operator
alexanderharding a099269
Add AI prompt and usage guidelines to delay README
alexanderharding 3e954a7
Added better test coverage for the Infinity argument
alexanderharding 2d9bf05
Added better test coverage for various logic that has a condition whe…
alexanderharding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| # [@observable/delay](https://jsr.io/@observable/delay) | ||
|
|
||
| Delays values by the given number of `milliseconds`. | ||
|
|
||
| ## Build | ||
|
|
||
| Automated by [JSR](https://jsr.io/) | ||
|
|
||
| ## Publishing | ||
|
|
||
| Automated by `.github\workflows\publish.yml`. | ||
|
|
||
| ## Running unit tests | ||
|
|
||
| Run `deno task test` or `deno task test:ci` to execute the unit tests via | ||
| [Deno](https://deno.land/). | ||
|
|
||
| ## Examples | ||
|
|
||
| 1000 milliseconds | ||
|
|
||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
| pipe(of([1, 2, 3, 4, 5]), delay(1_000)).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log("next", value), | ||
| return: () => console.log("return"), | ||
| throw: (value) => console.log("throw", value), | ||
| }); | ||
|
|
||
| // Console output (after 1 second): | ||
| // "next" 1 | ||
| // "next" 2 | ||
| // "next" 3 | ||
| // "next" 4 | ||
| // "next" 5 | ||
| // "return" | ||
| ``` | ||
|
|
||
| 0 milliseconds | ||
|
|
||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
| pipe(of([1, 2, 3, 4, 5]), delay(0)).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log("next", value), | ||
| return: () => console.log("return"), | ||
| throw: (value) => console.log("throw", value), | ||
| }); | ||
|
|
||
| // Console output (synchronously): | ||
| // "next" 1 | ||
| // "next" 2 | ||
| // "next" 3 | ||
| // "next" 4 | ||
| // "next" 5 | ||
| // "return" | ||
| ``` | ||
|
|
||
| Infinite milliseconds | ||
|
|
||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
| pipe(of([1, 2, 3, 4, 5]), delay(Infinity)).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log("next", value), | ||
| return: () => console.log("return"), | ||
| throw: (value) => console.log("throw", value), | ||
| }); | ||
|
|
||
| // Console output (synchronously): | ||
| // "return" | ||
| ``` | ||
|
|
||
| Negative milliseconds | ||
|
|
||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
| pipe(of([1, 2, 3, 4, 5]), delay(-1)).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log("next", value), | ||
| return: () => console.log("return"), | ||
| throw: (value) => console.log("throw", value), | ||
| }); | ||
|
|
||
| // Console output (synchronously): | ||
| // "return" | ||
| ``` | ||
|
|
||
| NaN milliseconds | ||
|
|
||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
| pipe(of([1, 2, 3, 4, 5]), delay(NaN))).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log("next", value), | ||
| return: () => console.log("return"), | ||
| throw: (value) => console.log("throw", value), | ||
| }); | ||
|
|
||
| // Console output (synchronously): | ||
| // "return" | ||
| ``` | ||
|
|
||
| ## AI Prompt | ||
|
|
||
| Use the following prompt with AI assistants to help them understand this library: | ||
|
|
||
| ```` | ||
| You are helping me with code that uses @observable/delay from the @observable library ecosystem. | ||
|
|
||
| WHAT IT DOES: | ||
| `delay(milliseconds)` shifts each emitted value forward in time by scheduling it after `milliseconds` have elapsed. Values stay in the same order as the source. | ||
|
|
||
| CRITICAL: This library is NOT RxJS. Key differences: | ||
| - `delay` is a standalone function used with `pipe()` — NOT a method on Observable | ||
| - Observer uses `return`/`throw` — NOT `complete`/`error` | ||
| - Unsubscription via `AbortController.abort()` — NOT `subscription.unsubscribe()` | ||
|
|
||
| USAGE PATTERN: | ||
| ```ts | ||
| import { delay } from "@observable/delay"; | ||
| import { of } from "@observable/of"; | ||
| import { pipe } from "@observable/pipe"; | ||
|
|
||
| const controller = new AbortController(); | ||
|
|
||
| pipe(of([1, 2, 3]), delay(1_000)).subscribe({ | ||
| signal: controller.signal, | ||
| next: (value) => console.log(value), | ||
| return: () => console.log("done"), | ||
| throw: (error) => console.error(error), | ||
| }); | ||
| ``` | ||
|
|
||
| WRONG USAGE: | ||
| ```ts | ||
| // ✗ WRONG: delay is NOT a method on Observable | ||
| of([1, 2, 3]).delay(1000) // This does NOT work! | ||
| ``` | ||
|
|
||
| MILLISECONDS SEMANTICS: | ||
| - **Positive number:** each `next` is delayed by that many milliseconds; source order is preserved. | ||
| - **0:** values pass through without using timers (effectively immediate). | ||
| - **Negative or `NaN`:** the result is an empty Observable (no `next`; behaves like `@observable/empty`). | ||
| - **`Infinity`:** all values are dropped; when the source returns, the output returns (no delayed values). | ||
|
|
||
| ABORT / UNSUBSCRIBE: | ||
| Aborting the observer clears pending timers. After abort, fired timer callbacks must not deliver values to the consumer. | ||
|
|
||
| ARGUMENT VALIDATION: | ||
| `delay` requires a `number` for milliseconds. The returned operator function requires an Observable source; both throw `TypeError` if missing or wrong type. | ||
| ```` | ||
|
|
||
| ## Glossary And Semantics | ||
|
|
||
| [@observable/core](https://jsr.io/@observable/core#glossary-and-semantics) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "@observable/delay", | ||
| "version": "0.1.0", | ||
| "license": "MIT", | ||
| "exports": "./mod.ts", | ||
| "publish": { "exclude": ["*.test.ts"] } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.