@weasel-js/react 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 orochi235
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @weasel-js/react
|
|
2
|
+
|
|
3
|
+
Generic React hooks the weasel packages share: `useLatest`, a ref holding the
|
|
4
|
+
value of the last committed render, and `useStableByContent`, which keeps one
|
|
5
|
+
identity for a value rebuilt equal every render. No weasel domain in it.
|
|
6
|
+
|
|
7
|
+
Part of [weasel](https://github.com/orochi235/weasel), a domain-agnostic 2D
|
|
8
|
+
scene-graph canvas kit for React. See the
|
|
9
|
+
[API reference](https://orochi235.github.io/weasel/api/).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @weasel-js/react
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { useLatest } from '@weasel-js/react';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A ref holding the `value` of the last **committed** render, for event
|
|
3
|
+
* handlers, frame loops and other code that runs outside render and must not
|
|
4
|
+
* re-subscribe every time `value` changes.
|
|
5
|
+
*
|
|
6
|
+
* Writing `ref.current = value` in a render body is not the same thing: React
|
|
7
|
+
* may start a render and throw it away (a transition that suspends, a render
|
|
8
|
+
* interrupted by a more urgent update), and the value that render computed
|
|
9
|
+
* would stay behind for the next event to read. This writes from
|
|
10
|
+
* `useInsertionEffect`, so only a render that commits ever lands, and it lands
|
|
11
|
+
* before any layout effect in that commit runs — a sibling's or a child's
|
|
12
|
+
* `useLayoutEffect`, or a synchronous paint one of them triggers, already sees
|
|
13
|
+
* it.
|
|
14
|
+
*
|
|
15
|
+
* Don't read `.current` during render expecting this render's value: until the
|
|
16
|
+
* commit it holds the previous committed one (on mount, this first `value`).
|
|
17
|
+
* Render code should use `value` itself.
|
|
18
|
+
*/
|
|
19
|
+
declare function useLatest<T>(value: T): {
|
|
20
|
+
readonly current: T;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `value`, or the one the last committed render returned when `same` says the
|
|
25
|
+
* two are equal — so a caller rebuilding an equivalent object every render
|
|
26
|
+
* hands its readers one identity. Compared against the committed value only:
|
|
27
|
+
* a render React throws away never becomes the one the next render keeps.
|
|
28
|
+
*/
|
|
29
|
+
declare function useStableByContent<T>(value: T, same: (a: T, b: T) => boolean): T;
|
|
30
|
+
/** Same length, and the same element at every index. */
|
|
31
|
+
declare function sameList<T>(a: readonly T[], b: readonly T[]): boolean;
|
|
32
|
+
|
|
33
|
+
export { sameList, useLatest, useStableByContent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useRef, useInsertionEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/useLatest.ts
|
|
4
|
+
function useLatest(value) {
|
|
5
|
+
const ref = useRef(value);
|
|
6
|
+
useInsertionEffect(() => {
|
|
7
|
+
ref.current = value;
|
|
8
|
+
});
|
|
9
|
+
return ref;
|
|
10
|
+
}
|
|
11
|
+
function useStableByContent(value, same) {
|
|
12
|
+
const held = useRef(value);
|
|
13
|
+
const out = held.current === value || same(held.current, value) ? held.current : value;
|
|
14
|
+
useInsertionEffect(() => {
|
|
15
|
+
held.current = out;
|
|
16
|
+
});
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
function sameList(a, b) {
|
|
20
|
+
return a.length === b.length && a.every((t, i) => t === b[i]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { sameList, useLatest, useStableByContent };
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useLatest.ts","../src/useStableByContent.ts"],"names":["useRef","useInsertionEffect"],"mappings":";;;AAoBO,SAAS,UAAa,KAAA,EAAmC;AAC9D,EAAA,MAAM,GAAA,GAAM,OAAO,KAAK,CAAA;AACxB,EAAA,kBAAA,CAAmB,MAAM;AACvB,IAAA,GAAA,CAAI,OAAA,GAAU,KAAA;AAAA,EAChB,CAAC,CAAA;AACD,EAAA,OAAO,GAAA;AACT;AClBO,SAAS,kBAAA,CAAsB,OAAU,IAAA,EAAkC;AAChF,EAAA,MAAM,IAAA,GAAOA,OAAO,KAAK,CAAA;AACzB,EAAA,MAAM,GAAA,GAAM,IAAA,CAAK,OAAA,KAAY,KAAA,IAAS,IAAA,CAAK,KAAK,OAAA,EAAS,KAAK,CAAA,GAAI,IAAA,CAAK,OAAA,GAAU,KAAA;AACjF,EAAAC,mBAAmB,MAAM;AACvB,IAAA,IAAA,CAAK,OAAA,GAAU,GAAA;AAAA,EACjB,CAAC,CAAA;AACD,EAAA,OAAO,GAAA;AACT;AAGO,SAAS,QAAA,CAAY,GAAiB,CAAA,EAA0B;AACrE,EAAA,OAAO,CAAA,CAAE,MAAA,KAAW,CAAA,CAAE,MAAA,IAAU,CAAA,CAAE,KAAA,CAAM,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,KAAM,CAAA,CAAE,CAAC,CAAC,CAAA;AAC9D","file":"index.js","sourcesContent":["import { useInsertionEffect, useRef } from 'react';\n\n/**\n * A ref holding the `value` of the last **committed** render, for event\n * handlers, frame loops and other code that runs outside render and must not\n * re-subscribe every time `value` changes.\n *\n * Writing `ref.current = value` in a render body is not the same thing: React\n * may start a render and throw it away (a transition that suspends, a render\n * interrupted by a more urgent update), and the value that render computed\n * would stay behind for the next event to read. This writes from\n * `useInsertionEffect`, so only a render that commits ever lands, and it lands\n * before any layout effect in that commit runs — a sibling's or a child's\n * `useLayoutEffect`, or a synchronous paint one of them triggers, already sees\n * it.\n *\n * Don't read `.current` during render expecting this render's value: until the\n * commit it holds the previous committed one (on mount, this first `value`).\n * Render code should use `value` itself.\n */\nexport function useLatest<T>(value: T): { readonly current: T } {\n const ref = useRef(value);\n useInsertionEffect(() => {\n ref.current = value;\n });\n return ref;\n}\n","import { useInsertionEffect, useRef } from 'react';\n\n/**\n * `value`, or the one the last committed render returned when `same` says the\n * two are equal — so a caller rebuilding an equivalent object every render\n * hands its readers one identity. Compared against the committed value only:\n * a render React throws away never becomes the one the next render keeps.\n */\nexport function useStableByContent<T>(value: T, same: (a: T, b: T) => boolean): T {\n const held = useRef(value);\n const out = held.current === value || same(held.current, value) ? held.current : value;\n useInsertionEffect(() => {\n held.current = out;\n });\n return out;\n}\n\n/** Same length, and the same element at every index. */\nexport function sameList<T>(a: readonly T[], b: readonly T[]): boolean {\n return a.length === b.length && a.every((t, i) => t === b[i]);\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@weasel-js/react",
|
|
3
|
+
"version": "1.7.0",
|
|
4
|
+
"description": "Generic React hooks the weasel packages share: a committed-value ref and content-stable identity. No weasel domain in it.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"react": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"peerDependenciesMeta": {
|
|
22
|
+
"react": { "optional": true }
|
|
23
|
+
},
|
|
24
|
+
"author": "orochi235",
|
|
25
|
+
"homepage": "https://orochi235.github.io/weasel/",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/orochi235/weasel.git",
|
|
29
|
+
"directory": "packages/react"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/orochi235/weasel/issues"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"provenance": true
|
|
48
|
+
}
|
|
49
|
+
}
|