@rune-hub/react 1.0.0 → 1.0.1
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/README.md +40 -11
- package/hooks/index.d.ts +1 -0
- package/hooks/index.es6.js +1 -0
- package/hooks/index.js +1 -0
- package/hooks/useGetSlot/index.d.ts +1 -0
- package/hooks/useGetSlot/index.es6.js +1 -0
- package/hooks/useGetSlot/index.js +9 -0
- package/hooks/useGetSlot/useGetSlot.d.ts +34 -0
- package/hooks/useGetSlot/useGetSlot.es6.js +43 -0
- package/hooks/useGetSlot/useGetSlot.js +47 -0
- package/hooks/useOn/useOn.es6.js +3 -3
- package/hooks/useOn/useOn.js +3 -3
- package/hooks/useRune/useRune.es6.js +3 -8
- package/hooks/useRune/useRune.js +3 -8
- package/hooks/useSlot/useSlot.d.ts +11 -21
- package/hooks/useSlot/useSlot.es6.js +17 -26
- package/hooks/useSlot/useSlot.js +16 -25
- package/index.es6.js +1 -0
- package/index.js +2 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ With full TypeScript support and straightforward integration, the library enable
|
|
|
51
51
|
|
|
52
52
|
<sup>**[ [Install](#install) ]**</sup>
|
|
53
53
|
<sup>**[ [Examples](#examples) ]** [Basic Counter](#basic-counter) • [Todo List](#todo-list)</sup>
|
|
54
|
-
<sup>**[ [API](#api) ]** [HubProvider](#hubprovider) • [useRune](#userune) • [useOn](#useon) • [useAction](#useaction) • [useHub](#usehub) • [
|
|
54
|
+
<sup>**[ [API](#api) ]** [HubProvider](#hubprovider) • [useRune](#userune) • [useSlot](#useslot) • [useOn](#useon) • [useAction](#useaction) • [useHub](#usehub) • [useGetSlot](#usegetslot)</sup>
|
|
55
55
|
<sup>**[ [Links](#links) ]**</sup>
|
|
56
56
|
|
|
57
57
|
## Install
|
|
@@ -130,7 +130,7 @@ Component:
|
|
|
130
130
|
```tsx
|
|
131
131
|
import { useState } from 'react'
|
|
132
132
|
import { useRune } from '@rune-hub/react'
|
|
133
|
-
import { todos, addTodo, toggleTodo } from './store
|
|
133
|
+
import { todos, addTodo, toggleTodo } from './store'
|
|
134
134
|
|
|
135
135
|
function TodoList () {
|
|
136
136
|
const todoList = useRune(todos)
|
|
@@ -174,7 +174,7 @@ function TodoList () {
|
|
|
174
174
|
## API
|
|
175
175
|
###### [🏠︎](#index) / API [↑](#examples) [↓](#links)
|
|
176
176
|
|
|
177
|
-
<sup>[HubProvider](#hubprovider) • [useRune](#userune) • [useOn](#useon) • [useAction](#useaction) • [useHub](#usehub) • [
|
|
177
|
+
<sup>[HubProvider](#hubprovider) • [useRune](#userune) • [useSlot](#useslot) • [useOn](#useon) • [useAction](#useaction) • [useHub](#usehub) • [useGetSlot](#usegetslot)</sup>
|
|
178
178
|
|
|
179
179
|
### HubProvider
|
|
180
180
|
###### [🏠︎](#index) / [API](#api) / HubProvider [↓](#userune)
|
|
@@ -199,7 +199,9 @@ function App () {
|
|
|
199
199
|
If you don't provide a Hub, the default `Hub.root` will be used.
|
|
200
200
|
|
|
201
201
|
### useRune
|
|
202
|
-
###### [🏠︎](#index) / [API](#api) / useRune [↑](#hubprovider) [↓](#
|
|
202
|
+
###### [🏠︎](#index) / [API](#api) / useRune [↑](#hubprovider) [↓](#useslot)
|
|
203
|
+
|
|
204
|
+
Subscribes to a Rune and returns its current value.
|
|
203
205
|
|
|
204
206
|
Uses `useSyncExternalStore` for proper synchronization with React's rendering cycle.
|
|
205
207
|
Automatically subscribes to the Rune's slot and unsubscribes on unmount.
|
|
@@ -222,8 +224,34 @@ function Counter () {
|
|
|
222
224
|
}
|
|
223
225
|
```
|
|
224
226
|
|
|
227
|
+
### useSlot
|
|
228
|
+
###### [🏠︎](#index) / [API](#api) / useSlot [↑](#userune) [↓](#useon)
|
|
229
|
+
|
|
230
|
+
Subscribes to a Slot and returns its current value.
|
|
231
|
+
|
|
232
|
+
Uses `useSyncExternalStore` for proper synchronization with React's rendering cycle.
|
|
233
|
+
Automatically subscribes to the Slot and unsubscribes on unmount.
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
import { Slot } from 'rune-hub'
|
|
237
|
+
import { useSlot } from '@rune-hub/react'
|
|
238
|
+
|
|
239
|
+
const count = new Slot(() => 0)
|
|
240
|
+
const log = new Slot(() => console.log(count.value))
|
|
241
|
+
|
|
242
|
+
function Counter () {
|
|
243
|
+
const value = useSlot(count)
|
|
244
|
+
// Subscribe to count changes
|
|
245
|
+
|
|
246
|
+
useSlot(log)
|
|
247
|
+
// Activate log effect
|
|
248
|
+
|
|
249
|
+
return <div>Count: {value}</div>
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
225
253
|
### useOn
|
|
226
|
-
###### [🏠︎](#index) / [API](#api) / useOn [↑](#
|
|
254
|
+
###### [🏠︎](#index) / [API](#api) / useOn [↑](#useslot) [↓](#useaction)
|
|
227
255
|
|
|
228
256
|
Use `useOn` when you need to run side effects (like logging, analytics, or synchronization) that depend on other Runes, but don't need the return value in your component.
|
|
229
257
|
|
|
@@ -274,7 +302,7 @@ function Counter () {
|
|
|
274
302
|
```
|
|
275
303
|
|
|
276
304
|
### useHub
|
|
277
|
-
###### [🏠︎](#index) / [API](#api) / useHub [↑](#useaction) [↓](#
|
|
305
|
+
###### [🏠︎](#index) / [API](#api) / useHub [↑](#useaction) [↓](#usegetslot)
|
|
278
306
|
|
|
279
307
|
Returns the current Hub instance from context.
|
|
280
308
|
|
|
@@ -290,21 +318,22 @@ function MyComponent () {
|
|
|
290
318
|
}
|
|
291
319
|
```
|
|
292
320
|
|
|
293
|
-
###
|
|
294
|
-
###### [🏠︎](#index) / [API](#api) /
|
|
321
|
+
### useGetSlot
|
|
322
|
+
###### [🏠︎](#index) / [API](#api) / useGetSlot [↑](#usehub)
|
|
295
323
|
|
|
296
324
|
**Returns a Slot instance for a given Rune within the current Hub context.**
|
|
297
325
|
|
|
298
|
-
A Slot is a Hub-scoped reactive container that tracks changes to a Rune.
|
|
326
|
+
A Slot is a Hub-scoped reactive container that tracks changes to a Rune.
|
|
327
|
+
Use this when you need direct access to the [Slot API](https://github.com/d8corp/rune-hub#slot-api) rather than just the value.
|
|
299
328
|
|
|
300
329
|
```tsx
|
|
301
330
|
import { useEffect } from 'react'
|
|
302
|
-
import {
|
|
331
|
+
import { useGetSlot } from '@rune-hub/react'
|
|
303
332
|
|
|
304
333
|
const count = () => 0
|
|
305
334
|
|
|
306
335
|
function Counter () {
|
|
307
|
-
const slot =
|
|
336
|
+
const slot = useGetSlot(count)
|
|
308
337
|
|
|
309
338
|
useEffect(() => {
|
|
310
339
|
// Access raw value without subscribing
|
package/hooks/index.d.ts
CHANGED
package/hooks/index.es6.js
CHANGED
package/hooks/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useGetSlot';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useGetSlot } from './useGetSlot.es6.js';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Rune, Slot } from 'rune-hub';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a Slot instance for a given Rune within the current Hub context.
|
|
4
|
+
*
|
|
5
|
+
* A Slot is a Hub-scoped reactive container that tracks changes to a Rune.
|
|
6
|
+
* The Slot instance is memoized and stable for the Rune and Hub combination.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The Rune value type
|
|
9
|
+
* @param rune - The Rune to get a Slot for
|
|
10
|
+
* @returns A memoized Slot instance for the Rune
|
|
11
|
+
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const count = () => 0
|
|
16
|
+
*
|
|
17
|
+
* function Counter () {
|
|
18
|
+
* const slot = useGetSlot(count)
|
|
19
|
+
*
|
|
20
|
+
* useEffect(() => {
|
|
21
|
+
* // Access raw value without subscribing
|
|
22
|
+
* console.log(slot.raw)
|
|
23
|
+
*
|
|
24
|
+
* // Manually listen to changes
|
|
25
|
+
* return slot.on('change', () => {
|
|
26
|
+
* console.log('Count changed:', slot.raw)
|
|
27
|
+
* })
|
|
28
|
+
* }, [slot])
|
|
29
|
+
*
|
|
30
|
+
* return <div>Count: {slot.raw}</div>
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useGetSlot<T>(rune: Rune<T>): Slot<T>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { slot } from 'rune-hub';
|
|
3
|
+
import '../useHub/index.es6.js';
|
|
4
|
+
import { useHub } from '../useHub/useHub.es6.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns a Slot instance for a given Rune within the current Hub context.
|
|
8
|
+
*
|
|
9
|
+
* A Slot is a Hub-scoped reactive container that tracks changes to a Rune.
|
|
10
|
+
* The Slot instance is memoized and stable for the Rune and Hub combination.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The Rune value type
|
|
13
|
+
* @param rune - The Rune to get a Slot for
|
|
14
|
+
* @returns A memoized Slot instance for the Rune
|
|
15
|
+
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const count = () => 0
|
|
20
|
+
*
|
|
21
|
+
* function Counter () {
|
|
22
|
+
* const slot = useGetSlot(count)
|
|
23
|
+
*
|
|
24
|
+
* useEffect(() => {
|
|
25
|
+
* // Access raw value without subscribing
|
|
26
|
+
* console.log(slot.raw)
|
|
27
|
+
*
|
|
28
|
+
* // Manually listen to changes
|
|
29
|
+
* return slot.on('change', () => {
|
|
30
|
+
* console.log('Count changed:', slot.raw)
|
|
31
|
+
* })
|
|
32
|
+
* }, [slot])
|
|
33
|
+
*
|
|
34
|
+
* return <div>Count: {slot.raw}</div>
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function useGetSlot(rune) {
|
|
39
|
+
var hub = useHub();
|
|
40
|
+
return useMemo(function () { return hub.use(function () { return slot(rune); }); }, [rune, hub]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { useGetSlot };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var runeHub = require('rune-hub');
|
|
7
|
+
require('../useHub/index.js');
|
|
8
|
+
var useHub = require('../useHub/useHub.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns a Slot instance for a given Rune within the current Hub context.
|
|
12
|
+
*
|
|
13
|
+
* A Slot is a Hub-scoped reactive container that tracks changes to a Rune.
|
|
14
|
+
* The Slot instance is memoized and stable for the Rune and Hub combination.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The Rune value type
|
|
17
|
+
* @param rune - The Rune to get a Slot for
|
|
18
|
+
* @returns A memoized Slot instance for the Rune
|
|
19
|
+
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* const count = () => 0
|
|
24
|
+
*
|
|
25
|
+
* function Counter () {
|
|
26
|
+
* const slot = useGetSlot(count)
|
|
27
|
+
*
|
|
28
|
+
* useEffect(() => {
|
|
29
|
+
* // Access raw value without subscribing
|
|
30
|
+
* console.log(slot.raw)
|
|
31
|
+
*
|
|
32
|
+
* // Manually listen to changes
|
|
33
|
+
* return slot.on('change', () => {
|
|
34
|
+
* console.log('Count changed:', slot.raw)
|
|
35
|
+
* })
|
|
36
|
+
* }, [slot])
|
|
37
|
+
*
|
|
38
|
+
* return <div>Count: {slot.raw}</div>
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function useGetSlot(rune) {
|
|
43
|
+
var hub = useHub.useHub();
|
|
44
|
+
return react.useMemo(function () { return hub.use(function () { return runeHub.slot(rune); }); }, [rune, hub]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.useGetSlot = useGetSlot;
|
package/hooks/useOn/useOn.es6.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import '../
|
|
3
|
-
import {
|
|
2
|
+
import '../useGetSlot/index.es6.js';
|
|
3
|
+
import { useGetSlot } from '../useGetSlot/useGetSlot.es6.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Activates a Rune effect without subscribing to its value.
|
|
@@ -24,7 +24,7 @@ import { useSlot } from '../useSlot/useSlot.es6.js';
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
function useOn(rune) {
|
|
27
|
-
var slot =
|
|
27
|
+
var slot = useGetSlot(rune);
|
|
28
28
|
useEffect(function () { return slot.on(); }, [slot]);
|
|
29
29
|
}
|
|
30
30
|
|
package/hooks/useOn/useOn.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var react = require('react');
|
|
6
|
-
require('../
|
|
7
|
-
var
|
|
6
|
+
require('../useGetSlot/index.js');
|
|
7
|
+
var useGetSlot = require('../useGetSlot/useGetSlot.js');
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Activates a Rune effect without subscribing to its value.
|
|
@@ -28,7 +28,7 @@ var useSlot = require('../useSlot/useSlot.js');
|
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
30
|
function useOn(rune) {
|
|
31
|
-
var slot =
|
|
31
|
+
var slot = useGetSlot.useGetSlot(rune);
|
|
32
32
|
react.useEffect(function () { return slot.on(); }, [slot]);
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import '../useGetSlot/index.es6.js';
|
|
2
2
|
import '../useSlot/index.es6.js';
|
|
3
3
|
import { useSlot } from '../useSlot/useSlot.es6.js';
|
|
4
|
+
import { useGetSlot } from '../useGetSlot/useGetSlot.es6.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Subscribes to a Rune and returns its current value.
|
|
@@ -25,13 +26,7 @@ import { useSlot } from '../useSlot/useSlot.es6.js';
|
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
28
|
function useRune(rune) {
|
|
28
|
-
|
|
29
|
-
var stop = useMemo(function () { return slot.on(); }, [slot]);
|
|
30
|
-
useEffect(function () { return stop; }, [stop]);
|
|
31
|
-
var subscribe = useCallback(function (callback) {
|
|
32
|
-
return slot.on('change', callback);
|
|
33
|
-
}, [slot]);
|
|
34
|
-
return useSyncExternalStore(subscribe, function () { return slot.raw; });
|
|
29
|
+
return useSlot(useGetSlot(rune));
|
|
35
30
|
}
|
|
36
31
|
|
|
37
32
|
export { useRune };
|
package/hooks/useRune/useRune.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
require('../useGetSlot/index.js');
|
|
6
6
|
require('../useSlot/index.js');
|
|
7
7
|
var useSlot = require('../useSlot/useSlot.js');
|
|
8
|
+
var useGetSlot = require('../useGetSlot/useGetSlot.js');
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Subscribes to a Rune and returns its current value.
|
|
@@ -29,13 +30,7 @@ var useSlot = require('../useSlot/useSlot.js');
|
|
|
29
30
|
* ```
|
|
30
31
|
*/
|
|
31
32
|
function useRune(rune) {
|
|
32
|
-
|
|
33
|
-
var stop = react.useMemo(function () { return slot.on(); }, [slot]);
|
|
34
|
-
react.useEffect(function () { return stop; }, [stop]);
|
|
35
|
-
var subscribe = react.useCallback(function (callback) {
|
|
36
|
-
return slot.on('change', callback);
|
|
37
|
-
}, [slot]);
|
|
38
|
-
return react.useSyncExternalStore(subscribe, function () { return slot.raw; });
|
|
33
|
+
return useSlot.useSlot(useGetSlot.useGetSlot(rune));
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
exports.useRune = useRune;
|
|
@@ -1,34 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Slot } from 'rune-hub';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Subscribes to a Slot and returns its current value.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Uses `useSyncExternalStore` for proper synchronization with React's rendering cycle.
|
|
6
|
+
* Automatically subscribes to the Rune's slot and unsubscribes on unmount.
|
|
7
7
|
*
|
|
8
|
-
* @template T -
|
|
9
|
-
* @param
|
|
10
|
-
* @returns
|
|
8
|
+
* @template T - Slot value type
|
|
9
|
+
* @param slot - The Slot to subscribe to
|
|
10
|
+
* @returns The current value of the Slot
|
|
11
11
|
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```tsx
|
|
15
|
-
* const count = () => 0
|
|
15
|
+
* const count = new Slot(() => 0)
|
|
16
16
|
*
|
|
17
17
|
* function Counter () {
|
|
18
|
-
* const
|
|
18
|
+
* const value = useSlot(count)
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
* // Access raw value without subscribing
|
|
22
|
-
* console.log(slot.raw)
|
|
23
|
-
*
|
|
24
|
-
* // Manually listen to changes
|
|
25
|
-
* return slot.on('change', () => {
|
|
26
|
-
* console.log('Count changed:', slot.raw)
|
|
27
|
-
* })
|
|
28
|
-
* }, [slot])
|
|
29
|
-
*
|
|
30
|
-
* return <div>Count: {slot.raw}</div>
|
|
20
|
+
* return <div>Count: {value}</div>
|
|
31
21
|
* }
|
|
32
22
|
* ```
|
|
33
23
|
*/
|
|
34
|
-
export declare function useSlot<T>(
|
|
24
|
+
export declare function useSlot<T>(slot: Slot<T>): T;
|
|
@@ -1,43 +1,34 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
import { slot } from 'rune-hub';
|
|
3
|
-
import '../useHub/index.es6.js';
|
|
4
|
-
import { useHub } from '../useHub/useHub.es6.js';
|
|
1
|
+
import { useMemo, useEffect, useCallback, useSyncExternalStore } from 'react';
|
|
5
2
|
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
4
|
+
* Subscribes to a Slot and returns its current value.
|
|
8
5
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Uses `useSyncExternalStore` for proper synchronization with React's rendering cycle.
|
|
7
|
+
* Automatically subscribes to the Rune's slot and unsubscribes on unmount.
|
|
11
8
|
*
|
|
12
|
-
* @template T -
|
|
13
|
-
* @param
|
|
14
|
-
* @returns
|
|
9
|
+
* @template T - Slot value type
|
|
10
|
+
* @param slot - The Slot to subscribe to
|
|
11
|
+
* @returns The current value of the Slot
|
|
15
12
|
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
16
13
|
*
|
|
17
14
|
* @example
|
|
18
15
|
* ```tsx
|
|
19
|
-
* const count = () => 0
|
|
16
|
+
* const count = new Slot(() => 0)
|
|
20
17
|
*
|
|
21
18
|
* function Counter () {
|
|
22
|
-
* const
|
|
19
|
+
* const value = useSlot(count)
|
|
23
20
|
*
|
|
24
|
-
*
|
|
25
|
-
* // Access raw value without subscribing
|
|
26
|
-
* console.log(slot.raw)
|
|
27
|
-
*
|
|
28
|
-
* // Manually listen to changes
|
|
29
|
-
* return slot.on('change', () => {
|
|
30
|
-
* console.log('Count changed:', slot.raw)
|
|
31
|
-
* })
|
|
32
|
-
* }, [slot])
|
|
33
|
-
*
|
|
34
|
-
* return <div>Count: {slot.raw}</div>
|
|
21
|
+
* return <div>Count: {value}</div>
|
|
35
22
|
* }
|
|
36
23
|
* ```
|
|
37
24
|
*/
|
|
38
|
-
function useSlot(
|
|
39
|
-
var
|
|
40
|
-
|
|
25
|
+
function useSlot(slot) {
|
|
26
|
+
var stop = useMemo(function () { return slot.on(); }, [slot]);
|
|
27
|
+
useEffect(function () { return stop; }, [stop]);
|
|
28
|
+
var subscribe = useCallback(function (callback) {
|
|
29
|
+
return slot.on('change', callback);
|
|
30
|
+
}, [slot]);
|
|
31
|
+
return useSyncExternalStore(subscribe, function () { return slot.raw; });
|
|
41
32
|
}
|
|
42
33
|
|
|
43
34
|
export { useSlot };
|
package/hooks/useSlot/useSlot.js
CHANGED
|
@@ -3,45 +3,36 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var react = require('react');
|
|
6
|
-
var runeHub = require('rune-hub');
|
|
7
|
-
require('../useHub/index.js');
|
|
8
|
-
var useHub = require('../useHub/useHub.js');
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
|
-
*
|
|
8
|
+
* Subscribes to a Slot and returns its current value.
|
|
12
9
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
10
|
+
* Uses `useSyncExternalStore` for proper synchronization with React's rendering cycle.
|
|
11
|
+
* Automatically subscribes to the Rune's slot and unsubscribes on unmount.
|
|
15
12
|
*
|
|
16
|
-
* @template T -
|
|
17
|
-
* @param
|
|
18
|
-
* @returns
|
|
13
|
+
* @template T - Slot value type
|
|
14
|
+
* @param slot - The Slot to subscribe to
|
|
15
|
+
* @returns The current value of the Slot
|
|
19
16
|
* @see {@link https://github.com/d8corp/rune-hub-react#useslot}
|
|
20
17
|
*
|
|
21
18
|
* @example
|
|
22
19
|
* ```tsx
|
|
23
|
-
* const count = () => 0
|
|
20
|
+
* const count = new Slot(() => 0)
|
|
24
21
|
*
|
|
25
22
|
* function Counter () {
|
|
26
|
-
* const
|
|
23
|
+
* const value = useSlot(count)
|
|
27
24
|
*
|
|
28
|
-
*
|
|
29
|
-
* // Access raw value without subscribing
|
|
30
|
-
* console.log(slot.raw)
|
|
31
|
-
*
|
|
32
|
-
* // Manually listen to changes
|
|
33
|
-
* return slot.on('change', () => {
|
|
34
|
-
* console.log('Count changed:', slot.raw)
|
|
35
|
-
* })
|
|
36
|
-
* }, [slot])
|
|
37
|
-
*
|
|
38
|
-
* return <div>Count: {slot.raw}</div>
|
|
25
|
+
* return <div>Count: {value}</div>
|
|
39
26
|
* }
|
|
40
27
|
* ```
|
|
41
28
|
*/
|
|
42
|
-
function useSlot(
|
|
43
|
-
var
|
|
44
|
-
|
|
29
|
+
function useSlot(slot) {
|
|
30
|
+
var stop = react.useMemo(function () { return slot.on(); }, [slot]);
|
|
31
|
+
react.useEffect(function () { return stop; }, [stop]);
|
|
32
|
+
var subscribe = react.useCallback(function (callback) {
|
|
33
|
+
return slot.on('change', callback);
|
|
34
|
+
}, [slot]);
|
|
35
|
+
return react.useSyncExternalStore(subscribe, function () { return slot.raw; });
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
exports.useSlot = useSlot;
|
package/index.es6.js
CHANGED
|
@@ -2,6 +2,7 @@ import './context/index.es6.js';
|
|
|
2
2
|
import './hooks/index.es6.js';
|
|
3
3
|
export { HubContext, HubProvider } from './context/HubContext/HubContext.es6.js';
|
|
4
4
|
export { useAction } from './hooks/useAction/useAction.es6.js';
|
|
5
|
+
export { useGetSlot } from './hooks/useGetSlot/useGetSlot.es6.js';
|
|
5
6
|
export { useHub } from './hooks/useHub/useHub.es6.js';
|
|
6
7
|
export { useOn } from './hooks/useOn/useOn.es6.js';
|
|
7
8
|
export { useRune } from './hooks/useRune/useRune.es6.js';
|
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@ require('./context/index.js');
|
|
|
6
6
|
require('./hooks/index.js');
|
|
7
7
|
var HubContext = require('./context/HubContext/HubContext.js');
|
|
8
8
|
var useAction = require('./hooks/useAction/useAction.js');
|
|
9
|
+
var useGetSlot = require('./hooks/useGetSlot/useGetSlot.js');
|
|
9
10
|
var useHub = require('./hooks/useHub/useHub.js');
|
|
10
11
|
var useOn = require('./hooks/useOn/useOn.js');
|
|
11
12
|
var useRune = require('./hooks/useRune/useRune.js');
|
|
@@ -16,6 +17,7 @@ var useSlot = require('./hooks/useSlot/useSlot.js');
|
|
|
16
17
|
exports.HubContext = HubContext.HubContext;
|
|
17
18
|
exports.HubProvider = HubContext.HubProvider;
|
|
18
19
|
exports.useAction = useAction.useAction;
|
|
20
|
+
exports.useGetSlot = useGetSlot.useGetSlot;
|
|
19
21
|
exports.useHub = useHub.useHub;
|
|
20
22
|
exports.useOn = useOn.useOn;
|
|
21
23
|
exports.useRune = useRune.useRune;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rune-hub/react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Integrating RuneHub with React",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.es6.js",
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
"homepage": "https://github.com/d8corp/rune-hub-react",
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"react": ">=18",
|
|
45
|
-
"rune-hub": "^1.0.
|
|
45
|
+
"rune-hub": "^1.0.1"
|
|
46
46
|
}
|
|
47
47
|
}
|