@ts-ioc-container/react 0.5.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/cjm/OutOfScopeError.js +11 -0
- package/cjm/ScopeContext.js +35 -0
- package/cjm/index.js +11 -0
- package/esm/OutOfScopeError.js +7 -0
- package/esm/ScopeContext.js +28 -0
- package/esm/index.js +2 -0
- package/esm/package.json +3 -0
- package/package.json +84 -0
- package/typings/OutOfScopeError.d.ts +4 -0
- package/typings/ScopeContext.d.ts +12 -0
- package/typings/index.d.ts +2 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OutOfScopeError = void 0;
|
|
4
|
+
class OutOfScopeError extends Error {
|
|
5
|
+
name = 'OutOfScopeError';
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
Object.setPrototypeOf(this, OutOfScopeError.prototype);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.OutOfScopeError = OutOfScopeError;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScopeContext = void 0;
|
|
4
|
+
exports.useScope = useScope;
|
|
5
|
+
exports.useScopeOrFail = useScopeOrFail;
|
|
6
|
+
exports.useResolveOrFail = useResolveOrFail;
|
|
7
|
+
exports.Scope = Scope;
|
|
8
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const ts_ioc_container_1 = require("ts-ioc-container");
|
|
11
|
+
const OutOfScopeError_1 = require("./OutOfScopeError");
|
|
12
|
+
exports.ScopeContext = (0, react_1.createContext)(null);
|
|
13
|
+
function useScope() {
|
|
14
|
+
return (0, react_1.useContext)(exports.ScopeContext);
|
|
15
|
+
}
|
|
16
|
+
function useScopeOrFail() {
|
|
17
|
+
const container = useScope();
|
|
18
|
+
if (!container) {
|
|
19
|
+
throw new OutOfScopeError_1.OutOfScopeError('useScopeOrFail must be called inside ScopeContext.Provider');
|
|
20
|
+
}
|
|
21
|
+
return container;
|
|
22
|
+
}
|
|
23
|
+
function useResolveOrFail(key) {
|
|
24
|
+
const container = useScopeOrFail();
|
|
25
|
+
if (key instanceof ts_ioc_container_1.InjectionToken) {
|
|
26
|
+
return key.resolve(container);
|
|
27
|
+
}
|
|
28
|
+
return container.resolve(key);
|
|
29
|
+
}
|
|
30
|
+
function Scope(props) {
|
|
31
|
+
const parent = useScopeOrFail();
|
|
32
|
+
const [child] = (0, react_1.useState)(() => parent.createScope({ tags: props.tags }));
|
|
33
|
+
(0, react_1.useEffect)(() => () => child.dispose(), [child]);
|
|
34
|
+
return (0, jsx_runtime_1.jsx)(exports.ScopeContext.Provider, { value: child, children: props.children });
|
|
35
|
+
}
|
package/cjm/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OutOfScopeError = exports.useScopeOrFail = exports.useScope = exports.useResolveOrFail = exports.ScopeContext = exports.Scope = void 0;
|
|
4
|
+
var ScopeContext_1 = require("./ScopeContext");
|
|
5
|
+
Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return ScopeContext_1.Scope; } });
|
|
6
|
+
Object.defineProperty(exports, "ScopeContext", { enumerable: true, get: function () { return ScopeContext_1.ScopeContext; } });
|
|
7
|
+
Object.defineProperty(exports, "useResolveOrFail", { enumerable: true, get: function () { return ScopeContext_1.useResolveOrFail; } });
|
|
8
|
+
Object.defineProperty(exports, "useScope", { enumerable: true, get: function () { return ScopeContext_1.useScope; } });
|
|
9
|
+
Object.defineProperty(exports, "useScopeOrFail", { enumerable: true, get: function () { return ScopeContext_1.useScopeOrFail; } });
|
|
10
|
+
var OutOfScopeError_1 = require("./OutOfScopeError");
|
|
11
|
+
Object.defineProperty(exports, "OutOfScopeError", { enumerable: true, get: function () { return OutOfScopeError_1.OutOfScopeError; } });
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useState } from 'react';
|
|
3
|
+
import { InjectionToken } from 'ts-ioc-container';
|
|
4
|
+
import { OutOfScopeError } from './OutOfScopeError.js';
|
|
5
|
+
export const ScopeContext = createContext(null);
|
|
6
|
+
export function useScope() {
|
|
7
|
+
return useContext(ScopeContext);
|
|
8
|
+
}
|
|
9
|
+
export function useScopeOrFail() {
|
|
10
|
+
const container = useScope();
|
|
11
|
+
if (!container) {
|
|
12
|
+
throw new OutOfScopeError('useScopeOrFail must be called inside ScopeContext.Provider');
|
|
13
|
+
}
|
|
14
|
+
return container;
|
|
15
|
+
}
|
|
16
|
+
export function useResolveOrFail(key) {
|
|
17
|
+
const container = useScopeOrFail();
|
|
18
|
+
if (key instanceof InjectionToken) {
|
|
19
|
+
return key.resolve(container);
|
|
20
|
+
}
|
|
21
|
+
return container.resolve(key);
|
|
22
|
+
}
|
|
23
|
+
export function Scope(props) {
|
|
24
|
+
const parent = useScopeOrFail();
|
|
25
|
+
const [child] = useState(() => parent.createScope({ tags: props.tags }));
|
|
26
|
+
useEffect(() => () => child.dispose(), [child]);
|
|
27
|
+
return _jsx(ScopeContext.Provider, { value: child, children: props.children });
|
|
28
|
+
}
|
package/esm/index.js
ADDED
package/esm/package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ts-ioc-container/react",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "React bindings for ts-ioc-container: a Scope component that maps container scopes onto the component tree, plus hooks to resolve from the surrounding scope.",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"registry": "https://registry.npmjs.org/"
|
|
8
|
+
},
|
|
9
|
+
"author": "ibabkin <igba14@gmail.com>",
|
|
10
|
+
"homepage": "https://igorbabkin.github.io/ts-ioc-container",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "cjm/index.js",
|
|
14
|
+
"types": "typings/index.d.ts",
|
|
15
|
+
"module": "esm/index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./typings/index.d.ts",
|
|
20
|
+
"default": "./esm/index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./typings/index.d.ts",
|
|
24
|
+
"default": "./cjm/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"react",
|
|
31
|
+
"dependency-injection",
|
|
32
|
+
"inversion-of-control",
|
|
33
|
+
"container",
|
|
34
|
+
"ioc",
|
|
35
|
+
"di",
|
|
36
|
+
"scope",
|
|
37
|
+
"scoped-dependencies",
|
|
38
|
+
"hooks",
|
|
39
|
+
"ts-ioc-container"
|
|
40
|
+
],
|
|
41
|
+
"files": [
|
|
42
|
+
"cjm/**/*",
|
|
43
|
+
"esm/**/*",
|
|
44
|
+
"typings/**/*"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/IgorBabkin/ts-ioc-container",
|
|
49
|
+
"directory": "packages/react"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build:cjm": "node ../scripts/build.mjs cjm",
|
|
53
|
+
"build:esm": "node ../scripts/build.mjs esm",
|
|
54
|
+
"build:types": "node ../scripts/build.mjs types",
|
|
55
|
+
"build": "run-p build:*",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=lcov",
|
|
58
|
+
"check:format": "prettier --check \"lib/**/*.ts\" \"lib/**/*.tsx\" \"__tests__/**/*.tsx\"",
|
|
59
|
+
"check:type": "tsc --noEmit",
|
|
60
|
+
"check:lint": "eslint lib/**/*.tsx lib/**/*.ts __tests__/**/*.tsx",
|
|
61
|
+
"check": "run-s check:*"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"react": ">=18",
|
|
65
|
+
"ts-ioc-container": ">=56"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@testing-library/react": "^16.3.0",
|
|
69
|
+
"@types/react": "^19.2.2",
|
|
70
|
+
"@types/react-dom": "^19.2.2",
|
|
71
|
+
"jsdom": "^27.0.0",
|
|
72
|
+
"npm-run-all":"^4.1.5",
|
|
73
|
+
"react": "^19.2.0",
|
|
74
|
+
"react-dom": "^19.2.0",
|
|
75
|
+
"reflect-metadata": "^0.2.2",
|
|
76
|
+
"ts-ioc-container": "56.0.0",
|
|
77
|
+
"typescript": "5.8.3",
|
|
78
|
+
"unplugin-swc": "^1.5.9",
|
|
79
|
+
"vitest": "^4.1.2"
|
|
80
|
+
},
|
|
81
|
+
"engines": {
|
|
82
|
+
"node": ">= 18"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type constructor, type IContainer, InjectionToken } from 'ts-ioc-container';
|
|
3
|
+
export declare const ScopeContext: import("react").Context<IContainer | null>;
|
|
4
|
+
export declare function useScope(): IContainer | null;
|
|
5
|
+
export declare function useScopeOrFail(): IContainer;
|
|
6
|
+
export declare function useResolveOrFail<T>(key: InjectionToken<T> | constructor<T>): T;
|
|
7
|
+
interface ScopeProps {
|
|
8
|
+
tags: string[];
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
export declare function Scope(props: ScopeProps): import("react").JSX.Element;
|
|
12
|
+
export {};
|