@typewirets/react 0.1.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 +205 -0
- package/dist/cjs/index.d.ts +16 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +136 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +96 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +96 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +84 -0
- package/src/index.tsx +111 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TypeWireTS
|
|
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,205 @@
|
|
|
1
|
+
# TypeWire React
|
|
2
|
+
|
|
3
|
+
React integration for TypeWireTS - seamlessly integrate TypeWire dependency injection with React components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @typewirets/react @typewirets/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
The most effective pattern for using TypeWire with React is with an async main function:
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { TypeWireContainer, typeWireGroupOf } from "@typewirets/core";
|
|
17
|
+
import { ResolutionContextProvider } from "@typewirets/react";
|
|
18
|
+
import ReactDOM from "react-dom/client";
|
|
19
|
+
import { ApiWire } from "./wires/api.wire";
|
|
20
|
+
import { UserStoreWire } from "./wires/user-store.wire";
|
|
21
|
+
import { App } from "./App";
|
|
22
|
+
|
|
23
|
+
async function main() {
|
|
24
|
+
// Create the container
|
|
25
|
+
const container = new TypeWireContainer();
|
|
26
|
+
|
|
27
|
+
// Group all your TypeWires
|
|
28
|
+
const wireGroup = typeWireGroupOf([
|
|
29
|
+
ApiWire,
|
|
30
|
+
UserStoreWire,
|
|
31
|
+
// Add all your application wires here
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
// Apply all wires to the container at once
|
|
35
|
+
await wireGroup.apply(container);
|
|
36
|
+
|
|
37
|
+
// Render your React app with the container
|
|
38
|
+
const rootEl = document.getElementById("root");
|
|
39
|
+
const root = ReactDOM.createRoot(rootEl);
|
|
40
|
+
root.render(<App container={container} />);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Start your application
|
|
44
|
+
main().catch(console.error);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Then in your App component:
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { ResolutionContextProvider } from "@typewirets/react";
|
|
51
|
+
import type { ResolutionContext } from "@typewirets/core";
|
|
52
|
+
import { Suspense } from "react";
|
|
53
|
+
|
|
54
|
+
export function App({ container }: { container: ResolutionContext }) {
|
|
55
|
+
return (
|
|
56
|
+
<ResolutionContextProvider resolutionContext={container}>
|
|
57
|
+
<Suspense fallback={<div>Loading application...</div>}>
|
|
58
|
+
<YourComponents />
|
|
59
|
+
</Suspense>
|
|
60
|
+
</ResolutionContextProvider>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Defining TypeWires for React
|
|
66
|
+
|
|
67
|
+
When defining TypeWires for React, use the `createWith` pattern for better dependency management:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
// api.wire.ts
|
|
71
|
+
import { typeWireOf } from "@typewirets/core";
|
|
72
|
+
|
|
73
|
+
export const ApiWire = typeWireOf({
|
|
74
|
+
token: "Api",
|
|
75
|
+
createWith() {
|
|
76
|
+
return {
|
|
77
|
+
async fetchUsers() {
|
|
78
|
+
const response = await fetch("/api/users");
|
|
79
|
+
return response.json();
|
|
80
|
+
},
|
|
81
|
+
async updateUser(id: string, data: any) {
|
|
82
|
+
// Implementation
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// user-store.wire.ts
|
|
89
|
+
import { typeWireOf } from "@typewirets/core";
|
|
90
|
+
import { create } from "zustand";
|
|
91
|
+
import { ApiWire } from "./api.wire";
|
|
92
|
+
|
|
93
|
+
export const UserStoreWire = typeWireOf({
|
|
94
|
+
token: "UserStore",
|
|
95
|
+
imports: {
|
|
96
|
+
api: ApiWire,
|
|
97
|
+
},
|
|
98
|
+
createWith({ api }) {
|
|
99
|
+
return create((set) => ({
|
|
100
|
+
users: [],
|
|
101
|
+
loading: false,
|
|
102
|
+
error: null,
|
|
103
|
+
|
|
104
|
+
fetchUsers: async () => {
|
|
105
|
+
set({ loading: true });
|
|
106
|
+
try {
|
|
107
|
+
const users = await api.fetchUsers();
|
|
108
|
+
set({ users, loading: false });
|
|
109
|
+
} catch (error) {
|
|
110
|
+
set({ error, loading: false });
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
updateUser: async (id: string, data: any) => {
|
|
115
|
+
// Implementation
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Using TypeWires in Components
|
|
123
|
+
|
|
124
|
+
Access your TypeWires in React components with the `useTypeWire` hook:
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { useTypeWire } from "@typewirets/react";
|
|
128
|
+
import { UserStoreWire } from "./wires/user-store.wire";
|
|
129
|
+
import { useEffect } from "react";
|
|
130
|
+
|
|
131
|
+
export function UserList() {
|
|
132
|
+
const userStore = useTypeWire(UserStoreWire);
|
|
133
|
+
const { users, loading, error, fetchUsers } = userStore;
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
fetchUsers();
|
|
137
|
+
}, [fetchUsers]);
|
|
138
|
+
|
|
139
|
+
if (loading) return <div>Loading users...</div>;
|
|
140
|
+
if (error) return <div>Error: {error.message}</div>;
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<ul>
|
|
144
|
+
{users.map(user => (
|
|
145
|
+
<li key={user.id}>{user.name}</li>
|
|
146
|
+
))}
|
|
147
|
+
</ul>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Handling Async TypeWires
|
|
153
|
+
|
|
154
|
+
When using TypeWires with async initialization, React Suspense handles the loading state:
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
// Component that depends on an async TypeWire
|
|
158
|
+
function UserDashboard() {
|
|
159
|
+
const userStore = useTypeWire(UserStoreWire);
|
|
160
|
+
|
|
161
|
+
// If UserStoreWire has async initialization,
|
|
162
|
+
// React Suspense will catch the promise and show the fallback
|
|
163
|
+
|
|
164
|
+
return <UserList />;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// In your parent component
|
|
168
|
+
function App({ container }) {
|
|
169
|
+
return (
|
|
170
|
+
<ResolutionContextProvider resolutionContext={container}>
|
|
171
|
+
<Suspense fallback={<div>Loading dashboard...</div>}>
|
|
172
|
+
<UserDashboard />
|
|
173
|
+
</Suspense>
|
|
174
|
+
</ResolutionContextProvider>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Core Components
|
|
180
|
+
|
|
181
|
+
- **ResolutionContextProvider**: Provides TypeWire container to React components
|
|
182
|
+
- **useTypeWire**: Hook to access TypeWire instances in components
|
|
183
|
+
- **useContainer**: Direct access to the ResolutionContext (advanced use cases)
|
|
184
|
+
|
|
185
|
+
## Best Practices
|
|
186
|
+
|
|
187
|
+
1. **Initialize TypeWires in a main function**:
|
|
188
|
+
- Group your TypeWires with `typeWireGroupOf`
|
|
189
|
+
- Apply them all at once before rendering
|
|
190
|
+
|
|
191
|
+
2. **Use state management libraries with TypeWire**:
|
|
192
|
+
- Zustand, Redux, or MobX work well with TypeWire
|
|
193
|
+
- Inject TypeWires as dependencies using `imports` and `createWith`
|
|
194
|
+
|
|
195
|
+
3. **Structure your application by features**:
|
|
196
|
+
- Group related TypeWires by feature
|
|
197
|
+
- Co-locate TypeWires with their React components
|
|
198
|
+
|
|
199
|
+
4. **Ensure proper cleanup**:
|
|
200
|
+
- If your TypeWires need cleanup, register cleanup handlers
|
|
201
|
+
- Use React's useEffect for component-level cleanup
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type ResolutionContext, type TypeSymbol, type TypeWire } from "@typewirets/core";
|
|
3
|
+
export declare class ReactResolutionContext {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(resolutionContext: ResolutionContext);
|
|
6
|
+
getSync<T>(typeSymbol: TypeSymbol<T>): T;
|
|
7
|
+
get<T>(typeSymbol: TypeSymbol<T>): Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
export declare const ContainerContext: React.Context<ResolutionContext>;
|
|
10
|
+
export declare function useContainer(): ResolutionContext;
|
|
11
|
+
export declare function useTypeWire<T>(typeWire: TypeWire<T>): T;
|
|
12
|
+
export declare function ResolutionContextProvider(props: {
|
|
13
|
+
resolutionContext: ResolutionContext;
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,QAAQ,EAGd,MAAM,kBAAkB,CAAC;AAE1B,qBAAa,sBAAsB;;gBAKrB,iBAAiB,EAAE,iBAAiB;IAIhD,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC;IAOlC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAqBpD;AAED,eAAO,MAAM,gBAAgB,kCAE5B,CAAC;AAEF,wBAAgB,YAAY,sBAG3B;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAsCvD;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE;IAC/C,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,2CAUA"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
36
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
37
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
38
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
39
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
40
|
+
};
|
|
41
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
42
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
43
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
44
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
45
|
+
};
|
|
46
|
+
var _ReactResolutionContext_resolutionContext, _ReactResolutionContext_resolutions, _ReactResolutionContext_resolutionErrors;
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.ContainerContext = exports.ReactResolutionContext = void 0;
|
|
49
|
+
exports.useContainer = useContainer;
|
|
50
|
+
exports.useTypeWire = useTypeWire;
|
|
51
|
+
exports.ResolutionContextProvider = ResolutionContextProvider;
|
|
52
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
53
|
+
const React = __importStar(require("react"));
|
|
54
|
+
const core_1 = require("@typewirets/core");
|
|
55
|
+
class ReactResolutionContext {
|
|
56
|
+
constructor(resolutionContext) {
|
|
57
|
+
_ReactResolutionContext_resolutionContext.set(this, void 0);
|
|
58
|
+
_ReactResolutionContext_resolutions.set(this, new Map());
|
|
59
|
+
_ReactResolutionContext_resolutionErrors.set(this, new Map());
|
|
60
|
+
__classPrivateFieldSet(this, _ReactResolutionContext_resolutionContext, resolutionContext, "f");
|
|
61
|
+
}
|
|
62
|
+
getSync(typeSymbol) {
|
|
63
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
64
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
65
|
+
}
|
|
66
|
+
return __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").getSync(typeSymbol);
|
|
67
|
+
}
|
|
68
|
+
async get(typeSymbol) {
|
|
69
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
70
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
71
|
+
}
|
|
72
|
+
const promise = __classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").get(typeSymbol.symbol);
|
|
73
|
+
if (promise) {
|
|
74
|
+
return promise;
|
|
75
|
+
}
|
|
76
|
+
const newResolution = __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").get(typeSymbol);
|
|
77
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").set(typeSymbol.symbol, newResolution);
|
|
78
|
+
return newResolution
|
|
79
|
+
.catch((err) => {
|
|
80
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").set(typeSymbol.symbol, err);
|
|
81
|
+
throw err;
|
|
82
|
+
})
|
|
83
|
+
.finally(() => {
|
|
84
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").delete(typeSymbol.symbol);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ReactResolutionContext = ReactResolutionContext;
|
|
89
|
+
_ReactResolutionContext_resolutionContext = new WeakMap(), _ReactResolutionContext_resolutions = new WeakMap(), _ReactResolutionContext_resolutionErrors = new WeakMap();
|
|
90
|
+
exports.ContainerContext = React.createContext(new ReactResolutionContext(new core_1.TypeWireContainer()));
|
|
91
|
+
function useContainer() {
|
|
92
|
+
const container = React.useContext(exports.ContainerContext);
|
|
93
|
+
return container;
|
|
94
|
+
}
|
|
95
|
+
function useTypeWire(typeWire) {
|
|
96
|
+
const container = useContainer();
|
|
97
|
+
const [value, setValue] = React.useState(() => {
|
|
98
|
+
try {
|
|
99
|
+
return typeWire.getInstanceSync(container);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
if (err instanceof core_1.TypeWireError && err.retriable) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const promise = typeWire.getInstance(container);
|
|
109
|
+
React.useEffect(() => {
|
|
110
|
+
let mounted = false;
|
|
111
|
+
promise
|
|
112
|
+
.then((value) => {
|
|
113
|
+
if (mounted) {
|
|
114
|
+
console.log("we got the value", value);
|
|
115
|
+
setValue(() => value);
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
.catch((err) => {
|
|
119
|
+
console.error(err);
|
|
120
|
+
});
|
|
121
|
+
return () => {
|
|
122
|
+
mounted = false;
|
|
123
|
+
};
|
|
124
|
+
}, [promise]);
|
|
125
|
+
if (value) {
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
throw promise;
|
|
129
|
+
}
|
|
130
|
+
function ResolutionContextProvider(props) {
|
|
131
|
+
const resolutionContext = props.resolutionContext instanceof ReactResolutionContext
|
|
132
|
+
? props.resolutionContext
|
|
133
|
+
: new ReactResolutionContext(props.resolutionContext);
|
|
134
|
+
return ((0, jsx_runtime_1.jsx)(exports.ContainerContext, { value: resolutionContext, children: props.children }));
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,oCAGC;AAED,kCAsCC;AAED,8DA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;aACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;QACL,OAAO,GAAG,EAAE;YACV,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,OAAO,CAAC;AAChB,CAAC;AAED,SAAgB,yBAAyB,CAAC,KAGzC;IACC,MAAM,iBAAiB,GACrB,KAAK,CAAC,iBAAiB,YAAY,sBAAsB;QACvD,CAAC,CAAC,KAAK,CAAC,iBAAiB;QACzB,CAAC,CAAC,IAAI,sBAAsB,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAC1D,OAAO,CACL,uBAAC,wBAAgB,IAAC,KAAK,EAAE,iBAAiB,YACvC,KAAK,CAAC,QAAQ,GACE,CACpB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type ResolutionContext, type TypeSymbol, type TypeWire } from "@typewirets/core";
|
|
3
|
+
export declare class ReactResolutionContext {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(resolutionContext: ResolutionContext);
|
|
6
|
+
getSync<T>(typeSymbol: TypeSymbol<T>): T;
|
|
7
|
+
get<T>(typeSymbol: TypeSymbol<T>): Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
export declare const ContainerContext: React.Context<ResolutionContext>;
|
|
10
|
+
export declare function useContainer(): ResolutionContext;
|
|
11
|
+
export declare function useTypeWire<T>(typeWire: TypeWire<T>): T;
|
|
12
|
+
export declare function ResolutionContextProvider(props: {
|
|
13
|
+
resolutionContext: ResolutionContext;
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,QAAQ,EAGd,MAAM,kBAAkB,CAAC;AAE1B,qBAAa,sBAAsB;;gBAKrB,iBAAiB,EAAE,iBAAiB;IAIhD,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC;IAOlC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAqBpD;AAED,eAAO,MAAM,gBAAgB,kCAE5B,CAAC;AAEF,wBAAgB,YAAY,sBAG3B;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAsCvD;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE;IAC/C,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,2CAUA"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _ReactResolutionContext_resolutionContext, _ReactResolutionContext_resolutions, _ReactResolutionContext_resolutionErrors;
|
|
13
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
import * as React from "react";
|
|
15
|
+
import { TypeWireError, TypeWireContainer, } from "@typewirets/core";
|
|
16
|
+
export class ReactResolutionContext {
|
|
17
|
+
constructor(resolutionContext) {
|
|
18
|
+
_ReactResolutionContext_resolutionContext.set(this, void 0);
|
|
19
|
+
_ReactResolutionContext_resolutions.set(this, new Map());
|
|
20
|
+
_ReactResolutionContext_resolutionErrors.set(this, new Map());
|
|
21
|
+
__classPrivateFieldSet(this, _ReactResolutionContext_resolutionContext, resolutionContext, "f");
|
|
22
|
+
}
|
|
23
|
+
getSync(typeSymbol) {
|
|
24
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
25
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
26
|
+
}
|
|
27
|
+
return __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").getSync(typeSymbol);
|
|
28
|
+
}
|
|
29
|
+
async get(typeSymbol) {
|
|
30
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
31
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
32
|
+
}
|
|
33
|
+
const promise = __classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").get(typeSymbol.symbol);
|
|
34
|
+
if (promise) {
|
|
35
|
+
return promise;
|
|
36
|
+
}
|
|
37
|
+
const newResolution = __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").get(typeSymbol);
|
|
38
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").set(typeSymbol.symbol, newResolution);
|
|
39
|
+
return newResolution
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").set(typeSymbol.symbol, err);
|
|
42
|
+
throw err;
|
|
43
|
+
})
|
|
44
|
+
.finally(() => {
|
|
45
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").delete(typeSymbol.symbol);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
_ReactResolutionContext_resolutionContext = new WeakMap(), _ReactResolutionContext_resolutions = new WeakMap(), _ReactResolutionContext_resolutionErrors = new WeakMap();
|
|
50
|
+
export const ContainerContext = React.createContext(new ReactResolutionContext(new TypeWireContainer()));
|
|
51
|
+
export function useContainer() {
|
|
52
|
+
const container = React.useContext(ContainerContext);
|
|
53
|
+
return container;
|
|
54
|
+
}
|
|
55
|
+
export function useTypeWire(typeWire) {
|
|
56
|
+
const container = useContainer();
|
|
57
|
+
const [value, setValue] = React.useState(() => {
|
|
58
|
+
try {
|
|
59
|
+
return typeWire.getInstanceSync(container);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (err instanceof TypeWireError && err.retriable) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const promise = typeWire.getInstance(container);
|
|
69
|
+
React.useEffect(() => {
|
|
70
|
+
let mounted = false;
|
|
71
|
+
promise
|
|
72
|
+
.then((value) => {
|
|
73
|
+
if (mounted) {
|
|
74
|
+
console.log("we got the value", value);
|
|
75
|
+
setValue(() => value);
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
console.error(err);
|
|
80
|
+
});
|
|
81
|
+
return () => {
|
|
82
|
+
mounted = false;
|
|
83
|
+
};
|
|
84
|
+
}, [promise]);
|
|
85
|
+
if (value) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
throw promise;
|
|
89
|
+
}
|
|
90
|
+
export function ResolutionContextProvider(props) {
|
|
91
|
+
const resolutionContext = props.resolutionContext instanceof ReactResolutionContext
|
|
92
|
+
? props.resolutionContext
|
|
93
|
+
: new ReactResolutionContext(props.resolutionContext);
|
|
94
|
+
return (_jsx(ContainerContext, { value: resolutionContext, children: props.children }));
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAIL,aAAa,EACb,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,OAAO,sBAAsB;IAKjC,YAAY,iBAAoC;QAJvC,4DAAsC;QACtC,8CAA8C,IAAI,GAAG,EAAE,EAAC;QACxD,mDAA0C,IAAI,GAAG,EAAE,EAAC;QAG3D,uBAAA,IAAI,6CAAsB,iBAAiB,MAAA,CAAC;IAC9C,CAAC;IAED,OAAO,CAAI,UAAyB;QAClC,IAAI,uBAAA,IAAI,gDAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,MAAM,uBAAA,IAAI,gDAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACtD,CAAC;QACD,OAAO,uBAAA,IAAI,iDAAmB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,GAAG,CAAI,UAAyB;QACpC,IAAI,uBAAA,IAAI,gDAAkB,CAAC,GAAG,CAAC,UAA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,CAAC,QAAQ,GACE,CACpB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type ResolutionContext, type TypeSymbol, type TypeWire } from "@typewirets/core";
|
|
3
|
+
export declare class ReactResolutionContext {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(resolutionContext: ResolutionContext);
|
|
6
|
+
getSync<T>(typeSymbol: TypeSymbol<T>): T;
|
|
7
|
+
get<T>(typeSymbol: TypeSymbol<T>): Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
export declare const ContainerContext: React.Context<ResolutionContext>;
|
|
10
|
+
export declare function useContainer(): ResolutionContext;
|
|
11
|
+
export declare function useTypeWire<T>(typeWire: TypeWire<T>): T;
|
|
12
|
+
export declare function ResolutionContextProvider(props: {
|
|
13
|
+
resolutionContext: ResolutionContext;
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,QAAQ,EAGd,MAAM,kBAAkB,CAAC;AAE1B,qBAAa,sBAAsB;;gBAKrB,iBAAiB,EAAE,iBAAiB;IAIhD,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC;IAOlC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAqBpD;AAED,eAAO,MAAM,gBAAgB,kCAE5B,CAAC;AAEF,wBAAgB,YAAY,sBAG3B;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAsCvD;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE;IAC/C,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,2CAUA"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _ReactResolutionContext_resolutionContext, _ReactResolutionContext_resolutions, _ReactResolutionContext_resolutionErrors;
|
|
13
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
import * as React from "react";
|
|
15
|
+
import { TypeWireError, TypeWireContainer, } from "@typewirets/core";
|
|
16
|
+
export class ReactResolutionContext {
|
|
17
|
+
constructor(resolutionContext) {
|
|
18
|
+
_ReactResolutionContext_resolutionContext.set(this, void 0);
|
|
19
|
+
_ReactResolutionContext_resolutions.set(this, new Map());
|
|
20
|
+
_ReactResolutionContext_resolutionErrors.set(this, new Map());
|
|
21
|
+
__classPrivateFieldSet(this, _ReactResolutionContext_resolutionContext, resolutionContext, "f");
|
|
22
|
+
}
|
|
23
|
+
getSync(typeSymbol) {
|
|
24
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
25
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
26
|
+
}
|
|
27
|
+
return __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").getSync(typeSymbol);
|
|
28
|
+
}
|
|
29
|
+
async get(typeSymbol) {
|
|
30
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
31
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
32
|
+
}
|
|
33
|
+
const promise = __classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").get(typeSymbol.symbol);
|
|
34
|
+
if (promise) {
|
|
35
|
+
return promise;
|
|
36
|
+
}
|
|
37
|
+
const newResolution = __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").get(typeSymbol);
|
|
38
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").set(typeSymbol.symbol, newResolution);
|
|
39
|
+
return newResolution
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").set(typeSymbol.symbol, err);
|
|
42
|
+
throw err;
|
|
43
|
+
})
|
|
44
|
+
.finally(() => {
|
|
45
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutions, "f").delete(typeSymbol.symbol);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
_ReactResolutionContext_resolutionContext = new WeakMap(), _ReactResolutionContext_resolutions = new WeakMap(), _ReactResolutionContext_resolutionErrors = new WeakMap();
|
|
50
|
+
export const ContainerContext = React.createContext(new ReactResolutionContext(new TypeWireContainer()));
|
|
51
|
+
export function useContainer() {
|
|
52
|
+
const container = React.useContext(ContainerContext);
|
|
53
|
+
return container;
|
|
54
|
+
}
|
|
55
|
+
export function useTypeWire(typeWire) {
|
|
56
|
+
const container = useContainer();
|
|
57
|
+
const [value, setValue] = React.useState(() => {
|
|
58
|
+
try {
|
|
59
|
+
return typeWire.getInstanceSync(container);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (err instanceof TypeWireError && err.retriable) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const promise = typeWire.getInstance(container);
|
|
69
|
+
React.useEffect(() => {
|
|
70
|
+
let mounted = false;
|
|
71
|
+
promise
|
|
72
|
+
.then((value) => {
|
|
73
|
+
if (mounted) {
|
|
74
|
+
console.log("we got the value", value);
|
|
75
|
+
setValue(() => value);
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
console.error(err);
|
|
80
|
+
});
|
|
81
|
+
return () => {
|
|
82
|
+
mounted = false;
|
|
83
|
+
};
|
|
84
|
+
}, [promise]);
|
|
85
|
+
if (value) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
throw promise;
|
|
89
|
+
}
|
|
90
|
+
export function ResolutionContextProvider(props) {
|
|
91
|
+
const resolutionContext = props.resolutionContext instanceof ReactResolutionContext
|
|
92
|
+
? props.resolutionContext
|
|
93
|
+
: new ReactResolutionContext(props.resolutionContext);
|
|
94
|
+
return (_jsx(ContainerContext, { value: resolutionContext, children: props.children }));
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAIL,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AAC;QACH,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;QACL,OAAO,GAAG,EAAE;YACV,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,OAAO,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,KAGzC;IACC,MAAM,iBAAiB,GACrB,KAAK,CAAC,iBAAiB,YAAY,sBAAsB;QACvD,CAAC,CAAC,KAAK,CAAC,iBAAiB;QACzB,CAAC,CAAC,IAAI,sBAAsB,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAC1D,OAAO,CACL,KAAC,gBAAgB,IAAC,KAAK,EAAE,iBAAiB,YACvC,KAAK,CAAC,QAAQ,GACE,CACpB,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typewirets/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React adapter for TypeWire dependency injection",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/esm/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src/*.ts",
|
|
19
|
+
"src/*.tsx",
|
|
20
|
+
"!src/**/*.test.ts",
|
|
21
|
+
"!src/**/*.test.tsx",
|
|
22
|
+
"!src/**/__tests__/**",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"typescript",
|
|
28
|
+
"dependency-injection",
|
|
29
|
+
"di",
|
|
30
|
+
"react",
|
|
31
|
+
"adapter",
|
|
32
|
+
"typewire"
|
|
33
|
+
],
|
|
34
|
+
"author": "Rex Kim",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/typewirets/typewirets.git",
|
|
39
|
+
"directory": "packages/react"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/typewirets/typewirets/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/typewirets/typewirets#readme",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@testing-library/react": "^16.3.0",
|
|
47
|
+
"@types/node": "^22.13.10",
|
|
48
|
+
"@types/react": "^19.1.0",
|
|
49
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
50
|
+
"jsdom": "^26.1.0",
|
|
51
|
+
"react": "^19.1.0",
|
|
52
|
+
"rimraf": "^6.0.1",
|
|
53
|
+
"typescript": "5.8.2",
|
|
54
|
+
"vitest": "^3.0.8",
|
|
55
|
+
"zustand": "^5.0.3",
|
|
56
|
+
"@typewirets/typescript-config": "0.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"@types/react": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@typewirets/core": "0.1.0"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "pnpm clean && pnpm --filter '@typewirets/react' --parallel '/^build:.*/'",
|
|
71
|
+
"build:cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module Commonjs --moduleResolution Node",
|
|
72
|
+
"build:esm": "tsc --project tsconfig.build.json --outDir dist/esm --module ES2020 --moduleResolution Node",
|
|
73
|
+
"build:types": "tsc --project tsconfig.build.json --outDir dist/types --declaration --declarationMap",
|
|
74
|
+
"dev": "pnpm clean && pnpm --filter '@typewirets/react' --parallel '/^dev:.*/'",
|
|
75
|
+
"dev:cjs": "tsc --watch --project tsconfig.build.json --outDir dist/cjs --module Commonjs --moduleResolution Node",
|
|
76
|
+
"dev:esm": "tsc --watch --project tsconfig.build.json --outDir dist/esm --module ES2020 --moduleResolution Node",
|
|
77
|
+
"dev:types": "tsc --watch --project tsconfig.build.json --outDir dist/types --declaration --declarationMap",
|
|
78
|
+
"clean": "rimraf dist",
|
|
79
|
+
"check-types": "tsc --noEmit",
|
|
80
|
+
"test": "vitest run",
|
|
81
|
+
"test:watch": "vitest",
|
|
82
|
+
"test:coverage": "vitest run --coverage"
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
type ResolutionContext,
|
|
4
|
+
type TypeSymbol,
|
|
5
|
+
type TypeWire,
|
|
6
|
+
TypeWireError,
|
|
7
|
+
TypeWireContainer,
|
|
8
|
+
} from "@typewirets/core";
|
|
9
|
+
|
|
10
|
+
export class ReactResolutionContext {
|
|
11
|
+
readonly #resolutionContext: ResolutionContext;
|
|
12
|
+
readonly #resolutions: Map<symbol, Promise<unknown>> = new Map();
|
|
13
|
+
readonly #resolutionErrors: Map<symbol, unknown> = new Map();
|
|
14
|
+
|
|
15
|
+
constructor(resolutionContext: ResolutionContext) {
|
|
16
|
+
this.#resolutionContext = resolutionContext;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getSync<T>(typeSymbol: TypeSymbol<T>): T {
|
|
20
|
+
if (this.#resolutionErrors.has(typeSymbol.symbol)) {
|
|
21
|
+
throw this.#resolutionErrors.get(typeSymbol.symbol);
|
|
22
|
+
}
|
|
23
|
+
return this.#resolutionContext.getSync(typeSymbol);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async get<T>(typeSymbol: TypeSymbol<T>): Promise<T> {
|
|
27
|
+
if (this.#resolutionErrors.has(typeSymbol.symbol)) {
|
|
28
|
+
throw this.#resolutionErrors.get(typeSymbol.symbol);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const promise = this.#resolutions.get(typeSymbol.symbol) as Promise<T>;
|
|
32
|
+
if (promise) {
|
|
33
|
+
return promise;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const newResolution = this.#resolutionContext.get(typeSymbol);
|
|
37
|
+
this.#resolutions.set(typeSymbol.symbol, newResolution);
|
|
38
|
+
return newResolution
|
|
39
|
+
.catch((err) => {
|
|
40
|
+
this.#resolutionErrors.set(typeSymbol.symbol, err);
|
|
41
|
+
throw err;
|
|
42
|
+
})
|
|
43
|
+
.finally(() => {
|
|
44
|
+
this.#resolutions.delete(typeSymbol.symbol);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const ContainerContext = React.createContext<ResolutionContext>(
|
|
50
|
+
new ReactResolutionContext(new TypeWireContainer()),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
export function useContainer() {
|
|
54
|
+
const container = React.useContext(ContainerContext);
|
|
55
|
+
return container;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useTypeWire<T>(typeWire: TypeWire<T>): T {
|
|
59
|
+
const container = useContainer();
|
|
60
|
+
const [value, setValue] = React.useState(() => {
|
|
61
|
+
try {
|
|
62
|
+
return typeWire.getInstanceSync(container);
|
|
63
|
+
} catch (err: unknown) {
|
|
64
|
+
if (err instanceof TypeWireError && err.retriable) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const promise = typeWire.getInstance(container);
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
let mounted = false;
|
|
75
|
+
|
|
76
|
+
promise
|
|
77
|
+
.then((value) => {
|
|
78
|
+
if (mounted) {
|
|
79
|
+
console.log("we got the value", value);
|
|
80
|
+
setValue(() => value);
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.catch((err) => {
|
|
84
|
+
console.error(err);
|
|
85
|
+
});
|
|
86
|
+
return () => {
|
|
87
|
+
mounted = false;
|
|
88
|
+
};
|
|
89
|
+
}, [promise]);
|
|
90
|
+
|
|
91
|
+
if (value) {
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw promise;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function ResolutionContextProvider(props: {
|
|
99
|
+
resolutionContext: ResolutionContext;
|
|
100
|
+
children?: React.ReactNode;
|
|
101
|
+
}) {
|
|
102
|
+
const resolutionContext =
|
|
103
|
+
props.resolutionContext instanceof ReactResolutionContext
|
|
104
|
+
? props.resolutionContext
|
|
105
|
+
: new ReactResolutionContext(props.resolutionContext);
|
|
106
|
+
return (
|
|
107
|
+
<ContainerContext value={resolutionContext}>
|
|
108
|
+
{props.children}
|
|
109
|
+
</ContainerContext>
|
|
110
|
+
);
|
|
111
|
+
}
|