@latticexyz/react 1.33.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/CHANGELOG.md +10 -0
- package/README.md +20 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/useComponentValue.d.ts +3 -0
- package/dist/useDeprecatedComputedValue.d.ts +3 -0
- package/dist/useEntityQuery.d.ts +4 -0
- package/dist/useObservableValue.d.ts +3 -0
- package/package.json +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [1.33.0](https://github.com/latticexyz/mud/compare/v1.32.0...v1.33.0) (2023-01-12)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **react:** add react package ([#294](https://github.com/latticexyz/mud/issues/294)) ([f5ee290](https://github.com/latticexyz/mud/commit/f5ee290e776276b2b0dd273705694df04a85f400))
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# MUD React
|
|
2
|
+
|
|
3
|
+
React hooks (and more) for building MUD clients.
|
|
4
|
+
|
|
5
|
+
### useComponentValue
|
|
6
|
+
|
|
7
|
+
Returns the value of the component for the entity, and triggers a re-render as the component is added/removed/updated.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
const position = useComponentValue(entity, Position);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### useEntityQuery
|
|
14
|
+
|
|
15
|
+
Returns all matching `EntityIndex`es for a given entity query, and triggers a re-render as new query results come in.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
const entities = useEntityQuery([Has(Position)]);
|
|
19
|
+
const playersAtPosition = useEntityQuery([Has(Player), HasValue(Position, { x: 0, y: 0 })]);
|
|
20
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { getComponentValue, defineQuery, Has, isComponentUpdate } from '@latticexyz/recs';
|
|
2
|
+
import { useState, useEffect, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
function useComponentValue(component, entityIndex, defaultValue) {
|
|
5
|
+
const [value, setValue] = useState(entityIndex != null ? getComponentValue(component, entityIndex) : undefined);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
// component or entityIndex changed, update state to latest value
|
|
8
|
+
setValue(entityIndex != null ? getComponentValue(component, entityIndex) : undefined);
|
|
9
|
+
if (entityIndex == null)
|
|
10
|
+
return;
|
|
11
|
+
const queryResult = defineQuery([Has(component)], { runOnInit: false });
|
|
12
|
+
const subscription = queryResult.update$.subscribe((update) => {
|
|
13
|
+
if (isComponentUpdate(update, component) && update.entity === entityIndex) {
|
|
14
|
+
const [nextValue] = update.value;
|
|
15
|
+
setValue(nextValue);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return () => subscription.unsubscribe();
|
|
19
|
+
}, [component, entityIndex]);
|
|
20
|
+
return value ?? defaultValue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @deprecated See https://github.com/latticexyz/mud/issues/339 */
|
|
24
|
+
const useDeprecatedComputedValue = (computedValue) => {
|
|
25
|
+
const [value, setValue] = useState(computedValue.get());
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const unsubscribe = computedValue.observe_(() => setValue(computedValue.get()));
|
|
28
|
+
return () => unsubscribe();
|
|
29
|
+
}, [computedValue]);
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const defaultOptions = { runOnInit: true };
|
|
34
|
+
function useEntityQuery(...args) {
|
|
35
|
+
const [fragments, options = defaultOptions] = args;
|
|
36
|
+
const queryResult = useMemo(() => defineQuery(fragments, options), [fragments, options]);
|
|
37
|
+
const [value, setValue] = useState([...queryResult.matching]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
// If query changes, we need to update state (initialState is only set once)
|
|
40
|
+
if (options.runOnInit) {
|
|
41
|
+
setValue([...queryResult.matching]);
|
|
42
|
+
}
|
|
43
|
+
const subscription = queryResult.update$.subscribe(() => setValue([...queryResult.matching]));
|
|
44
|
+
return () => subscription.unsubscribe();
|
|
45
|
+
}, [queryResult, options.runOnInit]);
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function useObservableValue(observable, defaultValue) {
|
|
50
|
+
const [value, setValue] = useState(defaultValue);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
const subscription = observable.subscribe(setValue);
|
|
53
|
+
return () => subscription.unsubscribe();
|
|
54
|
+
}, [observable]);
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { useComponentValue, useDeprecatedComputedValue, useEntityQuery, useObservableValue };
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/useComponentValue.ts","../src/useDeprecatedComputedValue.ts","../src/useEntityQuery.ts","../src/useObservableValue.ts"],"sourcesContent":[null,null,null,null],"names":[],"mappings":";;;SAwBgB,iBAAiB,CAC/B,SAA4C,EAC5C,WAAoC,EACpC,YAAgC,EAAA;IAEhC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,WAAW,IAAI,IAAI,GAAG,iBAAiB,CAAC,SAAS,EAAE,WAAW,CAAC,GAAG,SAAS,CAAC,CAAC;IAEhH,SAAS,CAAC,MAAK;;AAEb,QAAA,QAAQ,CAAC,WAAW,IAAI,IAAI,GAAG,iBAAiB,CAAC,SAAS,EAAE,WAAW,CAAC,GAAG,SAAS,CAAC,CAAC;QACtF,IAAI,WAAW,IAAI,IAAI;YAAE,OAAO;AAEhC,QAAA,MAAM,WAAW,GAAG,WAAW,CAAC,CAA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AAO,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;AACzF,IAAA,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;IAE9D,SAAS,CAAC,MAAK;;QAEb,IAAI,OAAO,CAAC,SAAS,EAAE;YACrB,QAAQ,CAAC,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;AACrC,SAAA;QACD,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,QAAQ,CAAC,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC9F,QAAA,OAAO,MAAM,YAAY,CAAC,WAAW,EAAE,CAAC;KACzC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;AAErC,IAAA,OAAO,KAAK,CAAC;AACf;;ACfgB,SAAA,kBAAkB,CAAI,UAAyB,EAAE,YAAgB,EAAA;IAC/E,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;IAEjD,SAAS,CAAC,MAAK;QACb,MAAM,YAAY,GAAG,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AACpD,QAAA,OAAO,MAAM,YAAY,CAAC,WAAW,EAAE,CAAC;AAC1C,KAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;AAEjB,IAAA,OAAO,KAAK,CAAC;AACf;;;;"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { Component, ComponentValue, EntityIndex, Metadata, Schema } from "@latticexyz/recs";
|
|
2
|
+
export declare function useComponentValue<S extends Schema>(component: Component<S, Metadata, undefined>, entityIndex: EntityIndex | undefined, defaultValue: ComponentValue<S>): ComponentValue<S>;
|
|
3
|
+
export declare function useComponentValue<S extends Schema>(component: Component<S, Metadata, undefined>, entityIndex: EntityIndex | undefined): ComponentValue<S> | undefined;
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@latticexyz/react",
|
|
3
|
+
"version": "1.33.0",
|
|
4
|
+
"description": "React tools for MUD client.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"source": "src/index.ts",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/latticexyz/mud.git",
|
|
11
|
+
"directory": "packages/react"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepare": "yarn build",
|
|
15
|
+
"link": "yarn link",
|
|
16
|
+
"docs": "rimraf API && typedoc src && find API -type f -name '*.md' -exec sed -E -i \"\" \"s/(#.*)(<.*>)/\\1/\" {} \\; && echo 'label: API' > API/index.yml",
|
|
17
|
+
"test": "tsc && jest",
|
|
18
|
+
"prepack": "mv package.json package.json.bak && jq \".main = \\\"dist/index.js\\\"\" package.json.bak > package.json ",
|
|
19
|
+
"postpack": "mv package.json.bak package.json || echo 'no package.json.bak'",
|
|
20
|
+
"build": "rimraf dist && rollup -c rollup.config.js",
|
|
21
|
+
"release": "npm publish || echo 'version already published'"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@latticexyz/recs": "^1.33.0",
|
|
25
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
26
|
+
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
27
|
+
"@rollup/plugin-typescript": "^8.3.1",
|
|
28
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
29
|
+
"@types/react": "^18.0.12",
|
|
30
|
+
"eslint-plugin-react": "^7.31.11",
|
|
31
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
32
|
+
"jest": "^29.3.1",
|
|
33
|
+
"mobx": "^6.4.2",
|
|
34
|
+
"react": "^18.2.0",
|
|
35
|
+
"react-test-renderer": "^18.2.0",
|
|
36
|
+
"rimraf": "^3.0.2",
|
|
37
|
+
"rollup": "^2.69.0",
|
|
38
|
+
"rollup-plugin-commonjs": "^10.1.0",
|
|
39
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
40
|
+
"rxjs": "^7.5.5",
|
|
41
|
+
"ts-jest": "^29.0.3",
|
|
42
|
+
"tslib": "^2.3.1",
|
|
43
|
+
"typedoc": "0.23.21",
|
|
44
|
+
"typedoc-plugin-markdown": "^3.13.6",
|
|
45
|
+
"typescript": "^4.5.5"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@latticexyz/recs": "^1.32.0",
|
|
49
|
+
"mobx": "^6.4.2",
|
|
50
|
+
"react": "^18.2.0",
|
|
51
|
+
"rxjs": "^7.5.5"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "2008fa7e8e5d6bd1009cafcf9cfae2ae68e2d28e"
|
|
54
|
+
}
|