-
Notifications
You must be signed in to change notification settings - Fork 153
new: async package for Solid 2.0 #909
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
base: next
Are you sure you want to change the base?
Changes from all commits
6ebc020
ddb14a3
3daf04d
5964304
1d9d362
de77f7a
a479a54
e34cd0b
4e9040b
0642aff
8f0756d
109a73a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2021 Solid Primitives Working Group | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <p> | ||
| <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=async" alt="Solid Primitives async"> | ||
| </p> | ||
|
|
||
| # @solid-primitives/async | ||
|
|
||
| [](https://bundlephobia.com/package/@solid-primitives/async) | ||
| [](https://www.npmjs.com/package/@solid-primitives/async) | ||
| [](https://github.com/solidjs-community/solid-primitives#contribution-process) | ||
|
|
||
| A collection of primitves for handling of asynchronous memos, optimistic signals, stores and actions: | ||
|
|
||
| - [`fromStream`](#fromStream) - wraps a fetch request to support web streams in memos or optimistic signals | ||
| - [`fromJSONStream`](#fromJSONStream) - wraps a fetch request returning a web stream containing (incomplete) JSON for the use in memos or optimistic signals | ||
| - [`makeAbortable`](#makeabortable) - sets up an AbortSignal with auto-abort on re-fetch or timeout | ||
| - [`createAbortable`](#createabortable) - like `makeAbortable`, but with automatic abort on cleanup | ||
| - [`makeRetrying`](#makeretrying) - wraps the fetcher to retry requests after a delay | ||
| - [`createAggregated`](#createAggregated) - aggregates the values of an accessor | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @solid-primitives/async | ||
| # or | ||
| yarn add @solid-primitives/async | ||
| # or | ||
| pnpm add @solid-primitives/async | ||
| ``` | ||
|
|
||
| ## `fromStream` | ||
|
|
||
| Turns a function returning a [Web Stream API ReadableStream](https://streams.spec.whatwg.org/#rs-class) or a streaming response directly or in a promise into an async iterator function that buffers the stream and updates with each data package. Node.js Web Streams are also supported, but will only work on streaming SSR. | ||
|
|
||
|
|
||
| ```ts | ||
| // definition | ||
| fromStream<Args extends any[]>( | ||
| webStreamOrResponse: (...args: Args) => ReadableStream | Response | ||
| ): (...args: Args) => AsyncGenerator<string, void, unknown>; | ||
|
|
||
| // on the client | ||
| const plainText = createMemo(fromStream(() => fetch(url()))); | ||
|
|
||
| // on the server | ||
| const readme = createMemo(fromStream(Readable.toWeb(createReadStream('README.md')))); | ||
| ``` | ||
|
|
||
| If the packages were very small and contained only a few words from lorem ipsum, the result would be (one line per update): | ||
|
|
||
| ``` | ||
| Lorem ipsum | ||
| Lorem ipsum dolor sit amet, | ||
| Lorem ipsum dolor sit amet, consetetur sadipscing | ||
| ``` | ||
|
|
||
| and so on. Usual HTTP packets can transmit ~1.4kb including headers, so expect mutliple updates for larger data. | ||
|
|
||
| ## `fromJSONStream` | ||
|
|
||
| The same as `fromStream`, but it auto-closes a partial JSON string to allow for successful parsing. | ||
|
|
||
| ```ts | ||
| // definition | ||
| fromStream<Args extends any[], JSON extends any>( | ||
| webStreamOrResponse: (...args: Args) => ReadableStream | Response | ||
| ): (...args: Args) => AsyncGenerator<JSON, void, unknown>; | ||
|
Comment on lines
+64
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. README API docs drift from implementation ( The Also applies to: 108-109, 118-118 🤖 Prompt for AI Agents |
||
|
|
||
| // usage | ||
| const answer = createMemo(fromJSONStream(() => fetch(url()))); | ||
| ``` | ||
|
|
||
| The result looks like this: | ||
|
|
||
| ```js | ||
| // current data | ||
| // parsed JSON | ||
|
|
||
| '[{"id":8429,"name":"fromStrea' | ||
| [{ id: 8429, name: "fromStrea" }] | ||
|
|
||
| '[{"id":8429,"name":"fromStream","description":"tu' | ||
| [{ id: 8429, name: "fromStream", description: "tu" }] | ||
|
|
||
| '[{"id":8429,"name":"fromStream","description":"turns web streams into' | ||
| [{ id: 8429, name: "fromStream", description: "turns web streams into" }] | ||
|
|
||
| '[{"id":8429,"name":"fromStream","description":"turns web streams into async iterator"},{"id":294' | ||
| [{ id: 8429, name: "fromStream", description: "turns web streams into async iterator" }, { id: 294 }] | ||
|
|
||
| '[{"id":8429,"name":"fromStream","description":"turns web streams into async iterator"},{"id":2947,"name":"fromJSONStream",' | ||
| [{ id: 8429, name: "fromStream", description: "turns web streams into async iterator" }, { id: 2947, name: "fromJSONStream }] | ||
|
|
||
| // and so on | ||
| ``` | ||
|
|
||
| ## `makeAbortable` | ||
|
|
||
| Orchestrates AbortController creation and aborting of abortable fetchers, either on refetch or after a timeout, depending on configuration: | ||
|
|
||
| ```ts | ||
| // definition | ||
| const [ | ||
| signal: AbortSignal, | ||
| abort: () => void, | ||
| filterErrors: <E>(err: E) => E instanceof AbortError ? void : E | ||
| ] = makeAbortable({ | ||
| timeout?: 10000, | ||
| noAutoAbort?: true, | ||
| }); | ||
|
|
||
| // usage | ||
| const [signal, abort, filterErrors] = makeAbortable(); | ||
| const data = createMemo(fromStream(() => fetch(url(), { signal: signal() }).catch(filterErrors)); | ||
| // use `createAbortable` if you do not want manual cleanup: | ||
| onCleanup(abort); | ||
| ``` | ||
|
|
||
| * The signal function always returns a signal that is not yet aborted; if noAutoAbort is not set to true, calling it will also abort a previous signal, if present | ||
| * The abort callback will always abort the current signal | ||
| * If timeout is set, the signal will be aborted after that many Milliseconds | ||
| * The filterErrors function can be used to filter out abort errors | ||
|
|
||
| ## `createAbortable` | ||
|
|
||
| This function does exactly the same as makeAbortable, but also automatically aborts on cleanup. Only use within a reactive scope. | ||
|
|
||
| ## `makeRetrying` | ||
|
|
||
| Wraps a fetcher and can catch errors and retry after a delay: | ||
|
|
||
| ```ts | ||
| // definition | ||
| const fetcher: () => AsyncGenerator<any, void, unknown> = makeRetrying( | ||
| () => fetch(url()).then(r => r.body), | ||
| { | ||
| delay: 1000, // number of Milliseconds to wait before retrying; default is 5s | ||
| retries: 1, // number of times a rest should be repeated before throwing the last error; default is 3 times | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| If you want to retry for an infinite number of times, you can set `options.retries` to `Infinity`. | ||
|
|
||
| ## `createAggregated` | ||
|
|
||
| Aggregates the output of any accessor/memo: | ||
|
|
||
| ```ts | ||
| const aggregated: Accessor<T> = createAggregated( | ||
| accessor: Accessor<T>, initialValue?: T | U | ||
| ); | ||
| const pages = createAggregated(currentPage, []); | ||
| ``` | ||
|
|
||
| * `null` will not overwrite `undefined` | ||
| * If the previous value is an Array, incoming values will be appended | ||
| * If any of the values are Objects, the current one will be shallow-merged into the previous one | ||
| * If the previous value is a string, more string data will be appended | ||
| * Otherwise the incoming data will be put into an array | ||
| * Objects and Arrays are re-created on each operation, but the values will be left untouched, so `<For>` should work fine | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| { | ||
| "name": "@solid-primitives/async", | ||
| "version": "0.0.100", | ||
| "description": "A template primitive example.", | ||
| "author": "Your Name <you@youremail.com>", | ||
|
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace template metadata before publish.
🤖 Prompt for AI Agents |
||
| "contributors": [], | ||
| "license": "MIT", | ||
| "homepage": "https://primitives.solidjs.community/package/async", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/solidjs-community/solid-primitives.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/solidjs-community/solid-primitives/issues" | ||
| }, | ||
| "primitive": { | ||
| "name": "async", | ||
| "stage": 0, | ||
| "list": [ | ||
| "fromStream", | ||
| "fromJSONStream", | ||
| "makeAbortable", | ||
| "createAbortable", | ||
| "makeRetrying", | ||
| "createAggregated" | ||
| ], | ||
| "category": "Reactivity" | ||
| }, | ||
| "keywords": [ | ||
| "solid", | ||
| "primitives" | ||
| ], | ||
| "private": false, | ||
| "sideEffects": false, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "type": "module", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "browser": {}, | ||
| "exports": { | ||
| "import": { | ||
| "@solid-primitives/source": "./src/index.ts", | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
|
Comment on lines
+35
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Checking package manifests for `@solid-primitives/source` export vs published files..."
fd -p package.json packages | while read -r manifest; do
has_source_cond=$(jq -r '(.exports | tostring | contains("`@solid-primitives/source`")) // false' "$manifest")
if [ "$has_source_cond" = "true" ]; then
name=$(jq -r '.name // "<unknown>"' "$manifest")
files=$(jq -c '.files // []' "$manifest")
source_target=$(jq -r '
.exports as $e
| if ($e|type) == "object" then
($e.import["`@solid-primitives/source`"] // $e["."]?.import?["`@solid-primitives/source`"] // "<not-found>")
else "<not-found>" end
' "$manifest")
echo "$name | $manifest | source_target=$source_target | files=$files"
fi
done
echo
echo "Expected: either src is published (e.g., files includes \"src\") or source condition is removed/repointed for published artifact."Repository: solidjs-community/solid-primitives Length of output: 10409 This pattern is systematic across the entire monorepo: the
Either publish the 🤖 Prompt for AI Agents |
||
| }, | ||
| "typesVersions": {}, | ||
| "scripts": { | ||
| "dev": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/dev.ts", | ||
| "build": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/build.ts", | ||
| "vitest": "vitest -c ../../configs/vitest.config.ts", | ||
| "vitest2": "vitest -c ../../configs/vitest.config.solid2.ts", | ||
| "test": "pnpm run vitest", | ||
| "test:ssr": "pnpm run vitest --mode ssr" | ||
| }, | ||
| "peerDependencies": { | ||
| "solid-js": "2.0.0-beta.14" | ||
| }, | ||
| "devDependencies": { | ||
| "solid-js": "2.0.0-beta.14" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix README anchor fragments in the primitive index.
These fragments won’t resolve with standard slugging (
#fromstream,#fromjsonstream,#createaggregated), so the table links break.🧰 Tools
🪛 markdownlint-cli2 (0.22.1)
[warning] 13-13: Link fragments should be valid
Expected:
#fromstream; Actual:#fromStream(MD051, link-fragments)
[warning] 14-14: Link fragments should be valid
Expected:
#fromjsonstream; Actual:#fromJSONStream(MD051, link-fragments)
[warning] 18-18: Link fragments should be valid
Expected:
#createaggregated; Actual:#createAggregated(MD051, link-fragments)
🤖 Prompt for AI Agents
Source: Linters/SAST tools