@rbxts/scribe-react 2.6.0 → 2.6.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.
@@ -1 +1 @@
1
- {"version":"2.6.0","format":1,"description":"Segments are [packedLine, sourceLine]. Use the last segment at or before the packed line, then add its line offset.","files":{"addons/ui/ScribeReact.luau":{"sourceSha256":"6e85c1c51649d96cd3b8b3106766f782f1373103ffad31a5e1cbfea632127929","packedSha256":"6e85c1c51649d96cd3b8b3106766f782f1373103ffad31a5e1cbfea632127929","segments":[[1,1]],"packagePath":"init.luau"}}}
1
+ {"version":"2.6.1","format":1,"description":"Segments are [packedLine, sourceLine]. Use the last segment at or before the packed line, then add its line offset.","files":{"addons/ui/ScribeReact.luau":{"sourceSha256":"f41d3f612836b11a19a9b01da12d276e10bd1887a57c086b4e985369825260c1","packedSha256":"f41d3f612836b11a19a9b01da12d276e10bd1887a57c086b4e985369825260c1","segments":[[1,1]],"packagePath":"init.luau"}}}
package/init.luau CHANGED
@@ -1,33 +1,74 @@
1
1
  --!strict
2
2
  -- React adapter. Copy into your game; not part of the Scribe package.
3
3
  -- local Scribe = require(path.to.ScribeReact)(React)
4
- -- local coins = Scribe.useScribe(Data.Coins) -- re-renders
5
- -- local coins = Scribe.useScribeBinding(Data.Coins) -- property only
6
- --
7
- -- useState + useEffect, not useSyncExternalStore: that is React 18 and jsdotlua
8
- -- React 17.2.1 does not export it.
9
-
10
- return function(React: any)
11
- local Adapter = {}
12
-
13
- --- State tracking one accessor. Re-renders on every change.
14
- function Adapter.useScribe(accessor: any): any
15
- local value, setValue = React.useState(accessor.Get)
4
+ -- local coins = Scribe.useScribe(Data.Coins) -- number; re-renders
5
+ -- local coins = Scribe.useScribeBinding(Data.Coins) -- Binding<number>; property only
6
+ -- Uses jsdotlua React 17.2.1 hooks.
7
+
8
+ export type Disconnect = () -> ()
9
+
10
+ export type Readable<T> = {
11
+ --[[
12
+ Returns the current value.
13
+ ]]
14
+ read Get: () -> T,
15
+ --[[
16
+ Runs on future changes and returns a disconnect function.
17
+ ]]
18
+ read Changed: (callback: (new: T, old: T) -> ()) -> Disconnect,
19
+ }
20
+
21
+ -- Matches react-lua's binding type.
22
+ type CoreBinding<T> = {
23
+ --[[
24
+ Returns the current binding value.
25
+ ]]
26
+ getValue: (self: CoreBinding<T>) -> T,
27
+ _source: string?,
28
+ }
29
+ type BindingMap = {
30
+ --[[
31
+ Creates a binding that transforms this binding's value.
32
+ ]]
33
+ map: <T, U>(self: CoreBinding<T> & BindingMap, (T) -> U) -> BindingMap & CoreBinding<U>,
34
+ }
35
+ export type Binding<T> = CoreBinding<T> & BindingMap
36
+
37
+ export type React = {
38
+ read useState: (initialState: any) -> (any, any),
39
+ read useEffect: (create: any, deps: any) -> (),
40
+ read useBinding: (initialValue: any) -> (any, any),
41
+ }
42
+
43
+ export type Adapter = {
44
+ --[[
45
+ Tracks an accessor and re-renders when it changes.
46
+ ]]
47
+ useScribe: <T>(accessor: Readable<T>) -> T,
48
+ --[[
49
+ Updates a bound property without re-rendering.
50
+ ]]
51
+ useScribeBinding: <T>(accessor: Readable<T>) -> Binding<T>,
52
+ }
53
+
54
+ --[[
55
+ Creates React hooks for Scribe accessors.
56
+ ]]
57
+ return function(React: React): Adapter
58
+ local function useScribe<T>(accessor: Readable<T>): T
59
+ local state: T, setValue: (T) -> () = React.useState(accessor.Get)
16
60
  React.useEffect(function()
17
- -- Re-read before subscribing: a write landing between render and effect is
18
- -- otherwise missed, and the UI sits on the stale first value forever.
61
+ -- Catch writes between render and the effect.
19
62
  setValue(accessor.Get())
20
63
  return accessor.Changed(function(new)
21
64
  setValue(new)
22
65
  end)
23
66
  end, { accessor })
24
- return value
67
+ return state
25
68
  end
26
69
 
27
- --- A binding instead of state: updates the property without re-rendering. Prefer it
28
- --- for a value that only feeds Text or Size.
29
- function Adapter.useScribeBinding(accessor: any): any
30
- local binding, setBinding = React.useBinding(accessor.Get())
70
+ local function useScribeBinding<T>(accessor: Readable<T>): Binding<T>
71
+ local binding: Binding<T>, setBinding: (T) -> () = React.useBinding(accessor.Get())
31
72
  React.useEffect(function()
32
73
  setBinding(accessor.Get())
33
74
  return accessor.Changed(function(new)
@@ -37,5 +78,8 @@ return function(React: any)
37
78
  return binding
38
79
  end
39
80
 
40
- return Adapter
81
+ return {
82
+ useScribe = useScribe,
83
+ useScribeBinding = useScribeBinding,
84
+ }
41
85
  end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbxts/scribe-react",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "description": "Optional react addon for Scribe",
5
5
  "main": "init.luau",
6
6
  "types": "index.d.ts",
@@ -12,7 +12,7 @@
12
12
  "homepage": "https://scribe.ericplane.dev/",
13
13
  "author": "ericplane",
14
14
  "peerDependencies": {
15
- "@rbxts/scribe": "^2.6.0",
15
+ "@rbxts/scribe": "^2.6.1",
16
16
  "@rbxts/react": "^17.0.0 || ^17.3.7-ts.2"
17
17
  },
18
18
  "publishConfig": {