@gamedev-sensei/react-extras 1.1.2 → 2.0.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/dist/index.d.mts +26 -16
- package/dist/index.d.ts +26 -16
- package/dist/index.js +78 -93
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +54 -65
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { DependencyList } from
|
|
1
|
+
import * as react2 from "react";
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
3
|
|
|
4
|
+
//#region src/idGenerator/generator.d.ts
|
|
4
5
|
type WithId<T extends object> = T extends object ? (T & {
|
|
5
|
-
|
|
6
|
+
id: string;
|
|
6
7
|
}) : never;
|
|
7
8
|
type WithoutId<T extends object> = T extends object ? Omit<T, "id"> : never;
|
|
8
9
|
declare const withId: <T extends object>(v: T, id: string) => WithId<T>;
|
|
9
|
-
declare const withoutId: <T extends object>({
|
|
10
|
+
declare const withoutId: <T extends object>({
|
|
11
|
+
id,
|
|
12
|
+
...v
|
|
13
|
+
}: WithId<T>) => WithoutId<WithId<T>>;
|
|
10
14
|
type ArrayWithId<T extends object> = WithId<T>[];
|
|
11
15
|
type ArrayWithoutId<T extends object> = WithoutId<WithId<T>>[];
|
|
12
16
|
declare const arrayWithIds: <T extends object>(v: T[], idGenerator: () => string) => ArrayWithId<T>;
|
|
13
17
|
declare const arrayWithoutIds: <T extends object>(v: ArrayWithId<T>) => ArrayWithoutId<T>;
|
|
14
18
|
declare function wrapIdGenerator(generator: () => string): (() => string) & {
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
withId<T extends {}>(v: T): WithId<T>;
|
|
20
|
+
arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T>;
|
|
17
21
|
};
|
|
18
22
|
type IdGenerator = ReturnType<typeof wrapIdGenerator>;
|
|
19
|
-
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/idGenerator/useIdGenerator.d.ts
|
|
20
25
|
declare function useIdGenerator(): IdGenerator;
|
|
21
|
-
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/idGenerator/useProvidedIdGenerator.d.ts
|
|
22
28
|
declare function useProvidedIdGenerator(): IdGenerator;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/idGenerator/IdGeneratorProvider.d.ts
|
|
31
|
+
declare const IdGeneratorProvider: react2.Provider<(() => string) & {
|
|
32
|
+
withId<T extends {}>(v: T): WithId<T>;
|
|
33
|
+
arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T>;
|
|
27
34
|
}>;
|
|
28
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/useDerived.d.ts
|
|
29
37
|
declare function useDerived<T>(factory: () => T, dependencies: DependencyList, compareFn?: (a: unknown, b: unknown) => boolean): T;
|
|
30
|
-
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/useMount.d.ts
|
|
31
40
|
type CleanupFunction = () => void;
|
|
32
41
|
declare function useMount(callback: () => (CleanupFunction | void)): void;
|
|
33
|
-
|
|
34
|
-
export {
|
|
42
|
+
//#endregion
|
|
43
|
+
export { ArrayWithId, ArrayWithoutId, IdGenerator, IdGeneratorProvider, WithId, WithoutId, arrayWithIds, arrayWithoutIds, useDerived, useIdGenerator, useMount, useProvidedIdGenerator, withId, withoutId, wrapIdGenerator };
|
|
44
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { DependencyList } from
|
|
1
|
+
import * as react2 from "react";
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
3
|
|
|
4
|
+
//#region src/idGenerator/generator.d.ts
|
|
4
5
|
type WithId<T extends object> = T extends object ? (T & {
|
|
5
|
-
|
|
6
|
+
id: string;
|
|
6
7
|
}) : never;
|
|
7
8
|
type WithoutId<T extends object> = T extends object ? Omit<T, "id"> : never;
|
|
8
9
|
declare const withId: <T extends object>(v: T, id: string) => WithId<T>;
|
|
9
|
-
declare const withoutId: <T extends object>({
|
|
10
|
+
declare const withoutId: <T extends object>({
|
|
11
|
+
id,
|
|
12
|
+
...v
|
|
13
|
+
}: WithId<T>) => WithoutId<WithId<T>>;
|
|
10
14
|
type ArrayWithId<T extends object> = WithId<T>[];
|
|
11
15
|
type ArrayWithoutId<T extends object> = WithoutId<WithId<T>>[];
|
|
12
16
|
declare const arrayWithIds: <T extends object>(v: T[], idGenerator: () => string) => ArrayWithId<T>;
|
|
13
17
|
declare const arrayWithoutIds: <T extends object>(v: ArrayWithId<T>) => ArrayWithoutId<T>;
|
|
14
18
|
declare function wrapIdGenerator(generator: () => string): (() => string) & {
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
withId<T extends {}>(v: T): WithId<T>;
|
|
20
|
+
arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T>;
|
|
17
21
|
};
|
|
18
22
|
type IdGenerator = ReturnType<typeof wrapIdGenerator>;
|
|
19
|
-
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/idGenerator/useIdGenerator.d.ts
|
|
20
25
|
declare function useIdGenerator(): IdGenerator;
|
|
21
|
-
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/idGenerator/useProvidedIdGenerator.d.ts
|
|
22
28
|
declare function useProvidedIdGenerator(): IdGenerator;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/idGenerator/IdGeneratorProvider.d.ts
|
|
31
|
+
declare const IdGeneratorProvider: react2.Provider<(() => string) & {
|
|
32
|
+
withId<T extends {}>(v: T): WithId<T>;
|
|
33
|
+
arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T>;
|
|
27
34
|
}>;
|
|
28
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/useDerived.d.ts
|
|
29
37
|
declare function useDerived<T>(factory: () => T, dependencies: DependencyList, compareFn?: (a: unknown, b: unknown) => boolean): T;
|
|
30
|
-
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/useMount.d.ts
|
|
31
40
|
type CleanupFunction = () => void;
|
|
32
41
|
declare function useMount(callback: () => (CleanupFunction | void)): void;
|
|
33
|
-
|
|
34
|
-
export {
|
|
42
|
+
//#endregion
|
|
43
|
+
export { ArrayWithId, ArrayWithoutId, IdGenerator, IdGeneratorProvider, WithId, WithoutId, arrayWithIds, arrayWithoutIds, useDerived, useIdGenerator, useMount, useProvidedIdGenerator, withId, withoutId, wrapIdGenerator };
|
|
44
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,122 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
8
|
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
17
|
};
|
|
18
|
-
var
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
IdGeneratorProvider: () => IdGeneratorProvider,
|
|
24
|
-
arrayWithIds: () => arrayWithIds,
|
|
25
|
-
arrayWithoutIds: () => arrayWithoutIds,
|
|
26
|
-
useDerived: () => useDerived,
|
|
27
|
-
useIdGenerator: () => useIdGenerator,
|
|
28
|
-
useMount: () => useMount,
|
|
29
|
-
useProvidedIdGenerator: () => useProvidedIdGenerator,
|
|
30
|
-
withId: () => withId,
|
|
31
|
-
withoutId: () => withoutId,
|
|
32
|
-
wrapIdGenerator: () => wrapIdGenerator
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(index_exports);
|
|
23
|
+
//#endregion
|
|
24
|
+
const react = __toESM(require("react"));
|
|
35
25
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
//#region src/idGenerator/generator.ts
|
|
27
|
+
const withId = (v, id) => ({
|
|
28
|
+
...v,
|
|
29
|
+
id
|
|
30
|
+
});
|
|
31
|
+
const withoutId = ({ id,...v }) => v;
|
|
32
|
+
const arrayWithIds = (v, idGenerator) => v.map((i) => withId(i, idGenerator()));
|
|
33
|
+
const arrayWithoutIds = (v) => v.map(withoutId);
|
|
41
34
|
function wrapIdGenerator(generator) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
35
|
+
return Object.assign(generator, {
|
|
36
|
+
withId(v) {
|
|
37
|
+
return withId(v, generator());
|
|
38
|
+
},
|
|
39
|
+
arrayWithIds(v) {
|
|
40
|
+
return arrayWithIds(v, generator);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
50
43
|
}
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
// src/useDerived.ts
|
|
56
|
-
var import_react = require("react");
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/useDerived.ts
|
|
57
47
|
function equals(x, y) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
return x === y;
|
|
48
|
+
if (typeof x === "number" && typeof y === "number") return x === y || x !== x && y !== y;
|
|
49
|
+
return x === y;
|
|
62
50
|
}
|
|
63
51
|
function useDerived(factory, dependencies, compareFn = equals) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
deps.current = dependencies;
|
|
71
|
-
return valueRef.current.value;
|
|
52
|
+
const deps = (0, react.useRef)(dependencies);
|
|
53
|
+
const valueRef = (0, react.useRef)({ value: null });
|
|
54
|
+
valueRef.current.value ??= factory();
|
|
55
|
+
if (deps.current.length !== dependencies.length || !deps.current.every((v, i) => compareFn(v, dependencies[i]))) valueRef.current.value = factory();
|
|
56
|
+
deps.current = dependencies;
|
|
57
|
+
return valueRef.current.value;
|
|
72
58
|
}
|
|
73
59
|
|
|
74
|
-
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/idGenerator/useIdGenerator.ts
|
|
75
62
|
function useIdGenerator() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
63
|
+
const nextId = (0, react.useRef)(0);
|
|
64
|
+
return useDerived(() => {
|
|
65
|
+
function generateId() {
|
|
66
|
+
return `${nextId.current++}`;
|
|
67
|
+
}
|
|
68
|
+
return wrapIdGenerator(generateId);
|
|
69
|
+
}, [nextId]);
|
|
83
70
|
}
|
|
84
71
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// src/idGenerator/IdGeneratorContext.ts
|
|
89
|
-
var import_react3 = require("react");
|
|
90
|
-
var globalNextId = 0;
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/idGenerator/IdGeneratorContext.ts
|
|
74
|
+
let globalNextId = 0;
|
|
91
75
|
function globalGenerateId() {
|
|
92
|
-
|
|
76
|
+
return `${globalNextId++}`;
|
|
93
77
|
}
|
|
94
|
-
|
|
78
|
+
const IdGeneratorContext = (0, react.createContext)(wrapIdGenerator(globalGenerateId));
|
|
95
79
|
|
|
96
|
-
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/idGenerator/useProvidedIdGenerator.ts
|
|
97
82
|
function useProvidedIdGenerator() {
|
|
98
|
-
|
|
83
|
+
return (0, react.useContext)(IdGeneratorContext);
|
|
99
84
|
}
|
|
100
85
|
|
|
101
|
-
|
|
102
|
-
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/idGenerator/IdGeneratorProvider.ts
|
|
88
|
+
const IdGeneratorProvider = IdGeneratorContext.Provider;
|
|
103
89
|
|
|
104
|
-
|
|
105
|
-
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/useMount.ts
|
|
106
92
|
function useMount(callback) {
|
|
107
|
-
|
|
93
|
+
return (0, react.useEffect)(callback, []);
|
|
108
94
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
});
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
exports.IdGeneratorProvider = IdGeneratorProvider;
|
|
98
|
+
exports.arrayWithIds = arrayWithIds;
|
|
99
|
+
exports.arrayWithoutIds = arrayWithoutIds;
|
|
100
|
+
exports.useDerived = useDerived;
|
|
101
|
+
exports.useIdGenerator = useIdGenerator;
|
|
102
|
+
exports.useMount = useMount;
|
|
103
|
+
exports.useProvidedIdGenerator = useProvidedIdGenerator;
|
|
104
|
+
exports.withId = withId;
|
|
105
|
+
exports.withoutId = withoutId;
|
|
106
|
+
exports.wrapIdGenerator = wrapIdGenerator;
|
|
122
107
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.js","names":["v: T","id: string","v: T[]","idGenerator: () => string","v: ArrayWithId<T>","generator: () => string","x: unknown","y: unknown","factory: () => T","dependencies: DependencyList","compareFn: (a: unknown, b: unknown) => boolean","globalNextId: number","callback: () => (CleanupFunction | void)"],"sources":["../src/idGenerator/generator.ts","../src/useDerived.ts","../src/idGenerator/useIdGenerator.ts","../src/idGenerator/IdGeneratorContext.ts","../src/idGenerator/useProvidedIdGenerator.ts","../src/idGenerator/IdGeneratorProvider.ts","../src/useMount.ts"],"sourcesContent":["export type WithId<T extends object> = T extends object ? (T & { id: string }) : never\nexport type WithoutId<T extends object> = T extends object ? Omit<T, \"id\"> : never\n\nexport const withId = <T extends object>(v: T, id: string) =>\n ({ ...v, id }) as WithId<T>\nexport const withoutId = <T extends object>({ id, ...v }: WithId<T>) =>\n v as WithoutId<WithId<T>>\n\nexport type ArrayWithId<T extends object> = WithId<T>[]\nexport type ArrayWithoutId<T extends object> = WithoutId<WithId<T>>[]\n\nexport const arrayWithIds = <T extends object>(v: T[], idGenerator: () => string): ArrayWithId<T> =>\n v.map(i => withId(i, idGenerator()))\nexport const arrayWithoutIds = <T extends object>(v: ArrayWithId<T>): ArrayWithoutId<T> =>\n v.map(withoutId)\n\nexport function wrapIdGenerator(generator: () => string) {\n return Object.assign(generator, {\n withId<T extends {}>(v: T): WithId<T> {\n return withId(v, generator())\n },\n arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T> {\n return arrayWithIds(v, generator)\n }\n })\n}\n\nexport type IdGenerator = ReturnType<typeof wrapIdGenerator>","import {DependencyList, useRef} from \"react\";\n\nfunction equals(x: unknown, y: unknown): boolean {\n if (typeof x === \"number\" && typeof y === \"number\") {\n return x === y || (x !== x && y !== y);\n }\n return x === y;\n}\n\nexport function useDerived<T>(\n factory: () => T,\n dependencies: DependencyList,\n compareFn: (a: unknown, b: unknown) => boolean = equals\n): T {\n const deps = useRef(dependencies)\n const valueRef = useRef<{ value: T | null }>({ value: null })\n\n valueRef.current.value ??= factory()\n\n if (deps.current.length !== dependencies.length || !deps.current.every((v, i) => compareFn(v, dependencies[i]))) {\n valueRef.current.value = factory()\n }\n\n deps.current = dependencies\n return valueRef.current.value\n}","import {useRef} from \"react\";\nimport {wrapIdGenerator, IdGenerator} from \"./generator\";\nimport {useDerived} from \"@/useDerived\";\n\nexport function useIdGenerator(): IdGenerator {\n const nextId = useRef(0)\n return useDerived(() => {\n function generateId() {\n return `${nextId.current++}`\n }\n return wrapIdGenerator(generateId)\n }, [nextId])\n}","import {createContext} from \"react\";\nimport {IdGenerator, wrapIdGenerator} from \"./generator\";\n\nlet globalNextId: number = 0\n\nfunction globalGenerateId(): string {\n return `${globalNextId++}`\n}\n\nexport const IdGeneratorContext = createContext<IdGenerator>(wrapIdGenerator(globalGenerateId))","import {useContext} from \"react\";\nimport {IdGenerator} from \"./generator\";\nimport {IdGeneratorContext} from \"./IdGeneratorContext\";\n\nexport function useProvidedIdGenerator(): IdGenerator {\n return useContext(IdGeneratorContext)\n}","import {IdGeneratorContext} from \"./IdGeneratorContext\"\n\nexport const IdGeneratorProvider = IdGeneratorContext.Provider","import {useEffect} from \"react\";\n\ntype CleanupFunction = () => void\nexport function useMount(callback: () => (CleanupFunction | void)) {\n return useEffect(callback, [])\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAa,SAAS,CAAmBA,GAAMC,QAC1C;CAAE,GAAG;CAAG;AAAI;AACjB,MAAa,YAAY,CAAmB,EAAE,GAAI,GAAG,GAAc,KAC/D;AAKJ,MAAa,eAAe,CAAmBC,GAAQC,gBACnD,EAAE,IAAI,OAAK,OAAO,GAAG,aAAa,CAAC,CAAC;AACxC,MAAa,kBAAkB,CAAmBC,MAC9C,EAAE,IAAI,UAAU;AAEpB,SAAgB,gBAAgBC,WAAyB;AACrD,QAAO,OAAO,OAAO,WAAW;EAC5B,OAAqBL,GAAiB;AAClC,UAAO,OAAO,GAAG,WAAW,CAAC;EAChC;EACD,aAA2BE,GAAwB;AAC/C,UAAO,aAAa,GAAG,UAAU;EACpC;CACJ,EAAC;AACL;;;;ACvBD,SAAS,OAAOI,GAAYC,GAAqB;AAC7C,YAAW,MAAM,mBAAmB,MAAM,SACtC,QAAO,MAAM,KAAM,MAAM,KAAK,MAAM;AAExC,QAAO,MAAM;AAChB;AAED,SAAgB,WACZC,SACAC,cACAC,YAAiD,QAChD;CACD,MAAM,OAAO,kBAAO,aAAa;CACjC,MAAM,WAAW,kBAA4B,EAAE,OAAO,KAAM,EAAC;AAE7D,UAAS,QAAQ,UAAU,SAAS;AAEpC,KAAI,KAAK,QAAQ,WAAW,aAAa,WAAW,KAAK,QAAQ,MAAM,CAAC,GAAG,MAAM,UAAU,GAAG,aAAa,GAAG,CAAC,CAC3G,UAAS,QAAQ,QAAQ,SAAS;AAGtC,MAAK,UAAU;AACf,QAAO,SAAS,QAAQ;AAC3B;;;;ACrBD,SAAgB,iBAA8B;CAC1C,MAAM,SAAS,kBAAO,EAAE;AACxB,QAAO,WAAW,MAAM;EACpB,SAAS,aAAa;AAClB,WAAQ,EAAE,OAAO,UAAU;EAC9B;AACD,SAAO,gBAAgB,WAAW;CACrC,GAAE,CAAC,MAAO,EAAC;AACf;;;;ACTD,IAAIC,eAAuB;AAE3B,SAAS,mBAA2B;AAChC,SAAQ,EAAE,eAAe;AAC5B;AAED,MAAa,qBAAqB,yBAA2B,gBAAgB,iBAAiB,CAAC;;;;ACL/F,SAAgB,yBAAsC;AAClD,QAAO,sBAAW,mBAAmB;AACxC;;;;ACJD,MAAa,sBAAsB,mBAAmB;;;;ACCtD,SAAgB,SAASC,UAA0C;AAC/D,QAAO,qBAAU,UAAU,CAAE,EAAC;AACjC"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,86 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { createContext, useContext, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/idGenerator/generator.ts
|
|
4
|
+
const withId = (v, id) => ({
|
|
5
|
+
...v,
|
|
6
|
+
id
|
|
7
|
+
});
|
|
8
|
+
const withoutId = ({ id,...v }) => v;
|
|
9
|
+
const arrayWithIds = (v, idGenerator) => v.map((i) => withId(i, idGenerator()));
|
|
10
|
+
const arrayWithoutIds = (v) => v.map(withoutId);
|
|
6
11
|
function wrapIdGenerator(generator) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
return Object.assign(generator, {
|
|
13
|
+
withId(v) {
|
|
14
|
+
return withId(v, generator());
|
|
15
|
+
},
|
|
16
|
+
arrayWithIds(v) {
|
|
17
|
+
return arrayWithIds(v, generator);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// src/useDerived.ts
|
|
21
|
-
import { useRef } from "react";
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/useDerived.ts
|
|
22
24
|
function equals(x, y) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return x === y;
|
|
25
|
+
if (typeof x === "number" && typeof y === "number") return x === y || x !== x && y !== y;
|
|
26
|
+
return x === y;
|
|
27
27
|
}
|
|
28
28
|
function useDerived(factory, dependencies, compareFn = equals) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
deps.current = dependencies;
|
|
36
|
-
return valueRef.current.value;
|
|
29
|
+
const deps = useRef(dependencies);
|
|
30
|
+
const valueRef = useRef({ value: null });
|
|
31
|
+
valueRef.current.value ??= factory();
|
|
32
|
+
if (deps.current.length !== dependencies.length || !deps.current.every((v, i) => compareFn(v, dependencies[i]))) valueRef.current.value = factory();
|
|
33
|
+
deps.current = dependencies;
|
|
34
|
+
return valueRef.current.value;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/idGenerator/useIdGenerator.ts
|
|
40
39
|
function useIdGenerator() {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
const nextId = useRef(0);
|
|
41
|
+
return useDerived(() => {
|
|
42
|
+
function generateId() {
|
|
43
|
+
return `${nextId.current++}`;
|
|
44
|
+
}
|
|
45
|
+
return wrapIdGenerator(generateId);
|
|
46
|
+
}, [nextId]);
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// src/idGenerator/IdGeneratorContext.ts
|
|
54
|
-
import { createContext } from "react";
|
|
55
|
-
var globalNextId = 0;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/idGenerator/IdGeneratorContext.ts
|
|
51
|
+
let globalNextId = 0;
|
|
56
52
|
function globalGenerateId() {
|
|
57
|
-
|
|
53
|
+
return `${globalNextId++}`;
|
|
58
54
|
}
|
|
59
|
-
|
|
55
|
+
const IdGeneratorContext = createContext(wrapIdGenerator(globalGenerateId));
|
|
60
56
|
|
|
61
|
-
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/idGenerator/useProvidedIdGenerator.ts
|
|
62
59
|
function useProvidedIdGenerator() {
|
|
63
|
-
|
|
60
|
+
return useContext(IdGeneratorContext);
|
|
64
61
|
}
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/idGenerator/IdGeneratorProvider.ts
|
|
65
|
+
const IdGeneratorProvider = IdGeneratorContext.Provider;
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/useMount.ts
|
|
71
69
|
function useMount(callback) {
|
|
72
|
-
|
|
70
|
+
return useEffect(callback, []);
|
|
73
71
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
arrayWithoutIds,
|
|
78
|
-
useDerived,
|
|
79
|
-
useIdGenerator,
|
|
80
|
-
useMount,
|
|
81
|
-
useProvidedIdGenerator,
|
|
82
|
-
withId,
|
|
83
|
-
withoutId,
|
|
84
|
-
wrapIdGenerator
|
|
85
|
-
};
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { IdGeneratorProvider, arrayWithIds, arrayWithoutIds, useDerived, useIdGenerator, useMount, useProvidedIdGenerator, withId, withoutId, wrapIdGenerator };
|
|
86
75
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/idGenerator/generator.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["v: T","id: string","v: T[]","idGenerator: () => string","v: ArrayWithId<T>","generator: () => string","x: unknown","y: unknown","factory: () => T","dependencies: DependencyList","compareFn: (a: unknown, b: unknown) => boolean","globalNextId: number","callback: () => (CleanupFunction | void)"],"sources":["../src/idGenerator/generator.ts","../src/useDerived.ts","../src/idGenerator/useIdGenerator.ts","../src/idGenerator/IdGeneratorContext.ts","../src/idGenerator/useProvidedIdGenerator.ts","../src/idGenerator/IdGeneratorProvider.ts","../src/useMount.ts"],"sourcesContent":["export type WithId<T extends object> = T extends object ? (T & { id: string }) : never\nexport type WithoutId<T extends object> = T extends object ? Omit<T, \"id\"> : never\n\nexport const withId = <T extends object>(v: T, id: string) =>\n ({ ...v, id }) as WithId<T>\nexport const withoutId = <T extends object>({ id, ...v }: WithId<T>) =>\n v as WithoutId<WithId<T>>\n\nexport type ArrayWithId<T extends object> = WithId<T>[]\nexport type ArrayWithoutId<T extends object> = WithoutId<WithId<T>>[]\n\nexport const arrayWithIds = <T extends object>(v: T[], idGenerator: () => string): ArrayWithId<T> =>\n v.map(i => withId(i, idGenerator()))\nexport const arrayWithoutIds = <T extends object>(v: ArrayWithId<T>): ArrayWithoutId<T> =>\n v.map(withoutId)\n\nexport function wrapIdGenerator(generator: () => string) {\n return Object.assign(generator, {\n withId<T extends {}>(v: T): WithId<T> {\n return withId(v, generator())\n },\n arrayWithIds<T extends {}>(v: T[]): ArrayWithId<T> {\n return arrayWithIds(v, generator)\n }\n })\n}\n\nexport type IdGenerator = ReturnType<typeof wrapIdGenerator>","import {DependencyList, useRef} from \"react\";\n\nfunction equals(x: unknown, y: unknown): boolean {\n if (typeof x === \"number\" && typeof y === \"number\") {\n return x === y || (x !== x && y !== y);\n }\n return x === y;\n}\n\nexport function useDerived<T>(\n factory: () => T,\n dependencies: DependencyList,\n compareFn: (a: unknown, b: unknown) => boolean = equals\n): T {\n const deps = useRef(dependencies)\n const valueRef = useRef<{ value: T | null }>({ value: null })\n\n valueRef.current.value ??= factory()\n\n if (deps.current.length !== dependencies.length || !deps.current.every((v, i) => compareFn(v, dependencies[i]))) {\n valueRef.current.value = factory()\n }\n\n deps.current = dependencies\n return valueRef.current.value\n}","import {useRef} from \"react\";\nimport {wrapIdGenerator, IdGenerator} from \"./generator\";\nimport {useDerived} from \"@/useDerived\";\n\nexport function useIdGenerator(): IdGenerator {\n const nextId = useRef(0)\n return useDerived(() => {\n function generateId() {\n return `${nextId.current++}`\n }\n return wrapIdGenerator(generateId)\n }, [nextId])\n}","import {createContext} from \"react\";\nimport {IdGenerator, wrapIdGenerator} from \"./generator\";\n\nlet globalNextId: number = 0\n\nfunction globalGenerateId(): string {\n return `${globalNextId++}`\n}\n\nexport const IdGeneratorContext = createContext<IdGenerator>(wrapIdGenerator(globalGenerateId))","import {useContext} from \"react\";\nimport {IdGenerator} from \"./generator\";\nimport {IdGeneratorContext} from \"./IdGeneratorContext\";\n\nexport function useProvidedIdGenerator(): IdGenerator {\n return useContext(IdGeneratorContext)\n}","import {IdGeneratorContext} from \"./IdGeneratorContext\"\n\nexport const IdGeneratorProvider = IdGeneratorContext.Provider","import {useEffect} from \"react\";\n\ntype CleanupFunction = () => void\nexport function useMount(callback: () => (CleanupFunction | void)) {\n return useEffect(callback, [])\n}"],"mappings":";;;AAGA,MAAa,SAAS,CAAmBA,GAAMC,QAC1C;CAAE,GAAG;CAAG;AAAI;AACjB,MAAa,YAAY,CAAmB,EAAE,GAAI,GAAG,GAAc,KAC/D;AAKJ,MAAa,eAAe,CAAmBC,GAAQC,gBACnD,EAAE,IAAI,OAAK,OAAO,GAAG,aAAa,CAAC,CAAC;AACxC,MAAa,kBAAkB,CAAmBC,MAC9C,EAAE,IAAI,UAAU;AAEpB,SAAgB,gBAAgBC,WAAyB;AACrD,QAAO,OAAO,OAAO,WAAW;EAC5B,OAAqBL,GAAiB;AAClC,UAAO,OAAO,GAAG,WAAW,CAAC;EAChC;EACD,aAA2BE,GAAwB;AAC/C,UAAO,aAAa,GAAG,UAAU;EACpC;CACJ,EAAC;AACL;;;;ACvBD,SAAS,OAAOI,GAAYC,GAAqB;AAC7C,YAAW,MAAM,mBAAmB,MAAM,SACtC,QAAO,MAAM,KAAM,MAAM,KAAK,MAAM;AAExC,QAAO,MAAM;AAChB;AAED,SAAgB,WACZC,SACAC,cACAC,YAAiD,QAChD;CACD,MAAM,OAAO,OAAO,aAAa;CACjC,MAAM,WAAW,OAA4B,EAAE,OAAO,KAAM,EAAC;AAE7D,UAAS,QAAQ,UAAU,SAAS;AAEpC,KAAI,KAAK,QAAQ,WAAW,aAAa,WAAW,KAAK,QAAQ,MAAM,CAAC,GAAG,MAAM,UAAU,GAAG,aAAa,GAAG,CAAC,CAC3G,UAAS,QAAQ,QAAQ,SAAS;AAGtC,MAAK,UAAU;AACf,QAAO,SAAS,QAAQ;AAC3B;;;;ACrBD,SAAgB,iBAA8B;CAC1C,MAAM,SAAS,OAAO,EAAE;AACxB,QAAO,WAAW,MAAM;EACpB,SAAS,aAAa;AAClB,WAAQ,EAAE,OAAO,UAAU;EAC9B;AACD,SAAO,gBAAgB,WAAW;CACrC,GAAE,CAAC,MAAO,EAAC;AACf;;;;ACTD,IAAIC,eAAuB;AAE3B,SAAS,mBAA2B;AAChC,SAAQ,EAAE,eAAe;AAC5B;AAED,MAAa,qBAAqB,cAA2B,gBAAgB,iBAAiB,CAAC;;;;ACL/F,SAAgB,yBAAsC;AAClD,QAAO,WAAW,mBAAmB;AACxC;;;;ACJD,MAAa,sBAAsB,mBAAmB;;;;ACCtD,SAAgB,SAASC,UAA0C;AAC/D,QAAO,UAAU,UAAU,CAAE,EAAC;AACjC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gamedev-sensei/react-extras",
|
|
3
3
|
"repository": "git@github.com:gamedev-sensei/package-extras.git",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"type-check": "tsc --noEmit",
|
|
24
|
-
"build": "
|
|
24
|
+
"build": "tsdown && attw --pack ."
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"remeda": "^2.23.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@gamedev-sensei/ts-config": "
|
|
34
|
-
"@gamedev-sensei/
|
|
33
|
+
"@gamedev-sensei/ts-config": "2.0.0",
|
|
34
|
+
"@gamedev-sensei/tsdown-config": "2.0.0",
|
|
35
35
|
"@types/react": "^19.1.8",
|
|
36
36
|
"@types/react-dom": "^19.1.6",
|
|
37
|
-
"
|
|
37
|
+
"tsdown": "^0.12.9"
|
|
38
38
|
}
|
|
39
39
|
}
|