Skip to content

Commit 49c0f72

Browse files
committed
refactor: algorithm improve
1 parent 8f8c70f commit 49c0f72

17 files changed

Lines changed: 756 additions & 152 deletions

babel.config.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = (api) => {
2+
api.env('test');
3+
4+
return {
5+
presets: [
6+
"@babel/preset-env",
7+
[
8+
"@babel/preset-react",
9+
{
10+
runtime: "automatic",
11+
development: process.env.NODE_ENV === 'development'
12+
}
13+
],
14+
"@babel/preset-typescript"
15+
],
16+
plugins: [
17+
"@babel/plugin-proposal-class-properties",
18+
"@babel/plugin-proposal-object-rest-spread"
19+
]
20+
};
21+
};

babel.config.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

docs/api/index.md

Lines changed: 262 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,318 @@
11
# API Reference
22

3+
## Overview
4+
5+
The public API is split into four parts:
6+
7+
- Bridge creation: define the registry, payload, and per-key behavior.
8+
- Components: create scoped Boundary providers.
9+
- Hooks: register, read, and navigate APIs inside React components.
10+
- Methods: read or wait for APIs outside React components.
11+
12+
For most use cases, start with:
13+
14+
1. `createBridge()`
15+
2. `createBoundary()`
16+
3. `useRegister()`
17+
4. `useAPI()`
18+
19+
## Shared Option Types
20+
21+
### `APIOptions`
22+
23+
```ts
24+
type APIOptions = {
25+
isMulti?: boolean;
26+
}
27+
```
28+
29+
- `isMulti`: allows one API key to keep multiple active providers at the same time.
30+
31+
### `BridgeAPIOptions<APIs>`
32+
33+
```ts
34+
type BridgeAPIOptions<APIs> = Partial<Record<keyof APIs, APIOptions>>;
35+
```
36+
37+
- Configures per-key bridge behavior.
38+
- Pass it to the function returned by `createBridge(...)`.
39+
40+
### `BaseOptions<APIs, Options, Payload>`
41+
42+
```ts
43+
type BaseOptions<APIs, Options, Payload> = {
44+
contextValue?: BoundaryContextValue<APIs, Payload, Options>;
45+
}
46+
```
47+
48+
- `contextValue`: overrides the current boundary scope and explicitly targets another boundary context.
49+
50+
### `GetAPIOptions<APIs, Name, Options, Payload>`
51+
52+
```ts
53+
type GetAPIOptions<APIs, Name, Options, Payload> =
54+
BaseOptions<APIs, Options, Payload> & {
55+
onInit?: ResolveInit<APIs, Options, Name>;
56+
};
57+
```
58+
59+
- `contextValue`: read from a specific boundary scope.
60+
- `onInit`: runs when the resolved API becomes available.
61+
62+
### `UpperOptions<APIs, Options, Payload>`
63+
64+
```ts
65+
type UpperOptions<APIs, Options, Payload> =
66+
BaseOptions<APIs, Options, Payload> & {
67+
shouldForwardYield?: (boundaryDetail) => any;
68+
};
69+
```
70+
71+
- `contextValue`: sets the starting boundary for upward lookup.
72+
- `shouldForwardYield`: controls which upper boundary should be selected.
73+
It receives:
74+
- `payload`: the current candidate boundary payload.
75+
- `parent`: the next parent boundary.
76+
- `allAPI`: all APIs currently registered on that candidate boundary.
77+
78+
### `GetUpperAPIOptions<APIs, Name, Options, Payload>`
79+
80+
- Combines `GetAPIOptions` and `UpperOptions`.
81+
- Supports `contextValue`, `onInit`, and `shouldForwardYield`.
82+
83+
### `GetAPIAsyncOptions<APIs, Options, Payload>`
84+
85+
```ts
86+
type GetAPIAsyncOptions<APIs, Options, Payload> =
87+
BaseOptions<APIs, Options, Payload> & {
88+
initial?: boolean;
89+
};
90+
```
91+
92+
- `contextValue`: waits in a specific boundary scope.
93+
- `initial`: defaults to `true`. Reuses the initial waiting slot for that scope. Set it to `false` to wait for a later registration instead.
94+
395
## Bridge Creation
496

5-
### `createBridge<APIs, PayloadType>(globalPayload?, options?)`
97+
### `createBridge<APIs, PayloadType>(globalPayload?)(options?)`
698

799
Creates a bridge instance.
8100

9-
Use it to define:
101+
Call pattern:
102+
103+
- `createBridge<APIs, PayloadType>(globalPayload?)`: creates the bridge and optionally defines the global payload.
104+
- `(...)(options?)`: optionally configures per-key bridge behavior such as `isMulti: true`.
105+
106+
First call parameters:
107+
108+
- `globalPayload`: optional payload stored on the global scope when no Boundary is used.
109+
110+
Second call parameters:
111+
112+
- `options`: optional `BridgeAPIOptions<APIs>` for per-key behavior such as `isMulti: true`.
113+
114+
Example:
115+
116+
```ts
117+
const bridge = createBridge<{
118+
notification: {
119+
id: string;
120+
show: (message: string) => void;
121+
};
122+
}>()({
123+
notification: { isMulti: true }
124+
});
125+
```
126+
127+
Returns:
128+
129+
- A bridge object used by all hooks, methods, and Boundaries created from it.
130+
131+
## Components
132+
133+
### `createBoundary(bridge)`
134+
135+
Creates a boundary component factory for scoped API access.
136+
137+
Parameters:
138+
139+
- `bridge`: the bridge created by `createBridge(...)`.
140+
141+
Boundary props:
142+
143+
- `payload`: the payload attached to this boundary scope.
144+
- `contextValue`: an existing boundary context value created by `useBoundaryContext(...)`.
10145

11-
- API names
12-
- API shapes
13-
- optional global payload
14-
- optional API options such as multi-instance behavior
146+
Boundary ref value:
147+
148+
- `payload`: the payload on this boundary.
149+
- `parent`: the parent boundary context, if any.
150+
- `getAPI(name)`: gets an API from this boundary scope.
15151

16152
## Hooks
17153

18-
### `useRegister(bridge, name, factory, deps, options?)`
154+
### `useRegister(bridge, name, factory, deps?, options?)`
19155

20156
Registers a component API under `name`.
21157

158+
Options type:
159+
160+
- `BaseOptions`
161+
162+
Parameters:
163+
164+
- `bridge`: the target bridge.
165+
- `name`: the API key to register.
166+
- `factory`: returns the imperative API object or function exposed by this component.
167+
- `deps`: dependency list passed to `useImperativeHandle`. Rebuilds the API when these values change.
168+
- `options`: optional `BaseOptions`.
169+
- `contextValue`: registers into a specific boundary scope.
170+
22171
### `useAPI(bridge, name, options?)`
23172

24173
Reads the API in the current boundary scope.
25174

175+
Options type:
176+
177+
- `GetAPIOptions`
178+
179+
Parameters:
180+
181+
- `bridge`: the target bridge.
182+
- `name`: the API key to read.
183+
- `options`: optional `GetAPIOptions`.
184+
- `contextValue`: reads from a specific boundary scope.
185+
- `onInit`: runs once the API becomes available.
186+
26187
### `useUpperAPI(bridge, name, options?)`
27188

28189
Reads a matching API from an upper boundary.
29190

191+
Options type:
192+
193+
- `GetUpperAPIOptions`
194+
195+
Parameters:
196+
197+
- `bridge`: the target bridge.
198+
- `name`: the API key to read.
199+
- `options`: optional `GetUpperAPIOptions`.
200+
- `contextValue`: chooses the starting boundary.
201+
- `onInit`: runs once the resolved upper API becomes available.
202+
- `shouldForwardYield`: controls how far the upward lookup continues.
203+
30204
### `useBoundaryPayload(bridge, options?)`
31205

32206
Reads the payload attached to the current boundary.
33207

208+
Options type:
209+
210+
- `BaseOptions`
211+
212+
Parameters:
213+
214+
- `bridge`: the target bridge.
215+
- `options`: optional `BaseOptions`.
216+
- `contextValue`: reads payload from a specific boundary context.
217+
34218
### `useUpperBoundaryPayload(bridge, options?)`
35219

36220
Reads payload from an upper boundary.
37221

222+
Options type:
223+
224+
- `UpperOptions`
225+
226+
Parameters:
227+
228+
- `bridge`: the target bridge.
229+
- `options`: optional `UpperOptions`.
230+
- `contextValue`: chooses the starting boundary.
231+
- `shouldForwardYield`: controls which upper boundary is selected.
232+
38233
### `useBoundaryContext(bridge, payload?)`
39234

40235
Creates a reusable boundary context value.
41236

237+
Parameters:
238+
239+
- `bridge`: the target bridge.
240+
- `payload`: the payload for the new boundary context value.
241+
242+
Returns:
243+
244+
- A `BoundaryContextValue` object that can be passed into `<Boundary contextValue={...} />` or any API that accepts `contextValue`.
245+
246+
### `useBoundaryRef(bridge)`
247+
248+
Creates a ref for a Boundary component.
249+
250+
Parameters:
251+
252+
- `bridge`: the target bridge.
253+
254+
Returns:
255+
256+
- A React ref whose `.current` exposes `payload`, `parent`, and `getAPI(name)`.
257+
42258
### `useTools(bridge, options?)`
43259

44260
Returns helper methods for programmatic access inside components.
45261

262+
Options type:
263+
264+
- `BaseOptions`
265+
266+
Parameters:
267+
268+
- `bridge`: the target bridge.
269+
- `options`: optional `BaseOptions`.
270+
- `contextValue`: sets the default boundary scope for all returned helpers.
271+
272+
Returned helpers:
273+
274+
- `getAPI(name, options?)`: reads an API like `getBridgeAPI`, but defaults to the current component scope.
275+
- `getBoundaryPayload(options?)`: reads boundary payload.
276+
- `getUpperAPI(name, options?)`: reads an API from an upper boundary.
277+
- `getUpperBoundaryPayload(options?)`: reads payload from an upper boundary.
278+
- `getAPIAsync(name, options?)`: waits for an API like `getBridgeAPIAsync`, but defaults to the current component scope.
279+
46280
## Methods
47281

48282
### `getBridgeAPI(bridge, name, options?)`
49283

50284
Gets an API outside components. By default, it reads from the global scope.
51285

286+
Options type:
287+
288+
- `BaseOptions`
289+
290+
Parameters:
291+
292+
- `bridge`: the target bridge.
293+
- `name`: the API key to read.
294+
- `options`: optional `BaseOptions`.
295+
- `contextValue`: reads from a specific boundary scope instead of the global scope.
296+
52297
### `getBridgeAPIAsync(bridge, name, options?)`
53298

54299
Waits for an API to be registered and returns it as a promise.
55300

56-
## Components
301+
Options type:
57302

58-
### `createBoundary(bridge)`
303+
- `GetAPIAsyncOptions`
59304

60-
Creates a boundary component factory for scoped API access.
305+
Parameters:
306+
307+
- `bridge`: the target bridge.
308+
- `name`: the API key to wait for.
309+
- `options`: optional `GetAPIAsyncOptions`.
310+
- `contextValue`: waits in a specific boundary scope.
311+
- `initial`: reuses the initial waiter by default, or waits for a later registration when set to `false`.
61312

62313
## Notes
63314

64315
- Most consumers should start with `createBridge`, `createBoundary`, `useRegister`, and `useAPI`
65316
- Reach for `useUpperAPI` and `getBridgeAPIAsync` when you need more advanced coordination
66317
- Use `isMulti: true` when one API key needs many active providers
318+
- Add Boundary only when you need local scope isolation; otherwise APIs resolve from the shared global scope

0 commit comments

Comments
 (0)