@rbxts/netweave 0.0.0-stage → 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.
@@ -0,0 +1,229 @@
1
+ --!strict
2
+ --!optimize 2
3
+
4
+ --[=[
5
+ @class Store
6
+
7
+ Where the game keeps the state netweave replicates.
8
+
9
+ netweave does not own the truth and does not want to. A game already has Charm, or Replica, or a
10
+ table it wrote itself, and a library that demands those be torn out to get delta compression
11
+ will not be adopted (`RESEARCH §4`). This is the narrowest thing netweave needs from whichever
12
+ one it is.
13
+
14
+ Internal. `nw.replicate` takes one.
15
+ ]=]
16
+
17
+ --[[
18
+ `PLAN-M4` D-3, refined by building phases 3 and 4.
19
+
20
+ Phase 1 read both neighbours and concluded "netweave is the transport a replication library
21
+ plugs into", because Charm Sync already diffs and wants a `connect(onSync)` callback. That was
22
+ one step too far, and the step is worth naming: **netweave adapts the store, not the store's
23
+ replication layer.**
24
+
25
+ `charm-sync` is Charm's own answer to the same problem, and carrying its `SyncPayload` would
26
+ mean moving an arbitrarily shaped patch table through a schema-driven codec — which is to say,
27
+ as an opaque blob, which is exactly what phase 1 warned throws the byte case away. `charm`
28
+ underneath it is an `atom` and a `subscribe`, which is a value and a signal, and that is all the
29
+ baseline in `Baseline.luau` and the diff in `Delta.luau` need. So netweave **replaces**
30
+ `charm-sync` rather than riding it, and the same is true of ReplicaService's six RemoteEvents.
31
+
32
+ ~~What that leaves is two functions, and one of them is optional: `subjects` and `read`, plus a
33
+ `changed` for the store that can say when something moved. A seam that required `changed` would
34
+ have supported one of the two libraries it was designed for.~~
35
+
36
+ **Two functions, and neither is optional.** `changed` is gone as of M4 phase 8, and it went on a
37
+ measurement rather than an opinion.
38
+
39
+ It was declared here, documented as an optimisation, and checked at declaration by
40
+ `nw.replicate` — for four phases, without one line ever reading it. `Store.charm`'s caution
41
+ promised that passing Charm's `subscribe` made a tick where nothing moved cost one branch; the
42
+ tick walked every subject regardless, so a game that passed it got exactly the walk it was told
43
+ it had avoided. Both ways out were open, so the tick was built both ways and timed
44
+ (`bench/tick.luau`, fifty players and five hundred subjects, nothing moving):
45
+
46
+ | idle tick | ms |
47
+ |---|---|
48
+ | before the phase 8 gate | 36.26 |
49
+ | comparing each subject against the snapshot its settled clients hold | 1.63 |
50
+ | trusting a perfect change signal instead of comparing | 1.40 |
51
+
52
+ 0.23 ms, and only while **nothing at all** is moving. A whole-store signal is set by any subject
53
+ moving, so in a world where anything happens it reads dirty every tick and buys nothing — and a
54
+ tick's cost only matters in a world where something happens.
55
+
56
+ Against that: a signal that misses a mutation is a subject that stops replicating, silently and
57
+ for ever, on every client. A game keeping a plain table inside a Charm atom and writing to it in
58
+ place would do exactly that, and neither side would report it. `CLAUDE.md` §9's rule is that a
59
+ claim which cannot be probed is a claim to drop — and there is no probe for "the store told the
60
+ truth", because when it does not, the freeze is the behaviour.
61
+
62
+ So the seam is `subjects` and `read`, which every store can answer, and netweave does the
63
+ comparing. ~~Charm's immutable atoms still make it cheap: a subject whose table was replaced fails
64
+ the comparison on the first key.~~ They do not: the tick compares against its own snapshot, over the
65
+ schema's keys, so an atom costs what a plain table costs and a replaced-but-equal table is found
66
+ equal after the walk (M4 report finding 59; M4-1). The seam is still the right one — it is just not
67
+ cheaper for Charm than for anyone else.
68
+ ]]
69
+
70
+ local Store = {}
71
+
72
+ --[=[
73
+ @interface Store
74
+ @within Store
75
+ .subjects () -> { any } -- every subject that exists right now
76
+ .read (subject: any) -> any -- the current value, or nil if it is gone
77
+
78
+ `subjects` is called once a tick and its result is not retained, so a store may return the same
79
+ table every time.
80
+
81
+ :::note
82
+ There is no change signal, and the note above says why one was removed rather than added.
83
+ netweave compares each subject once a tick against the snapshot its up-to-date clients hold, so
84
+ a store only has to say what it has — never when it moved.
85
+ :::
86
+ ]=]
87
+ export type Store = {
88
+ subjects: () -> { any },
89
+ read: (subject: any) -> any,
90
+ }
91
+
92
+ --[=[
93
+ @within Store
94
+ @function of
95
+ @param source { [any]: any } -- subject to value
96
+ @return Store
97
+
98
+ A plain table. The store for a game that has no state library, and the one every test uses.
99
+
100
+ ```lua
101
+ local world = { [1] = { hp = 10 }, [2] = { hp = 4 } }
102
+ local store = Store.of(world)
103
+ ```
104
+
105
+ :::note
106
+ The table is read live rather than copied, so mutating a value in place is visible to the next
107
+ tick. That is deliberate. ~~A game that replaces a subject's table pays a pointer comparison and
108
+ one that mutates in place pays the walk.~~ Both pay the same walk over the schema's keys: the tick
109
+ compares against its own snapshot, never against the game's table (M4 report finding 59).
110
+ :::
111
+ ]=]
112
+ function Store.of(source: { [any]: any }): Store
113
+ --[[ Rebuilt per call rather than cached, because a cached list would go stale exactly when a
114
+ subject appeared — which is the tick that matters most. It is one table a tick against a
115
+ diff of every subject in it. ]]
116
+ local function subjects(): { any }
117
+ local out = {}
118
+
119
+ for subject in source do
120
+ table.insert(out, subject)
121
+ end
122
+
123
+ return out
124
+ end
125
+
126
+ return {
127
+ subjects = subjects,
128
+
129
+ read = function(subject: any): any
130
+ return source[subject]
131
+ end,
132
+ }
133
+ end
134
+
135
+ --[=[
136
+ @within Store
137
+ @function charm
138
+ @param getter () -> { [any]: any } -- an atom, or any Charm getter, holding subject to value
139
+ @return Store
140
+
141
+ Charm, adapted at the **atom** rather than at `charm-sync`.
142
+
143
+ ```lua
144
+ local world = Charm.atom({})
145
+ local store = Store.charm(world)
146
+ ```
147
+
148
+ netweave never requires Charm — there is no package path for it (`CLAUDE.md` §8) and it should
149
+ not need one. The whole adapter is one call of the getter, so it is a shape rather than a
150
+ dependency, and anything that returns a subject-to-value table works without being Charm at all.
151
+
152
+ :::note
153
+ ~~Pass `Charm.subscribe` if you have it: without it netweave diffs every subject every tick, and
154
+ with it a tick where nothing moved costs one branch.~~ **There is no second parameter.** The
155
+ signal was never read, and when it was finally measured it was worth 0.23 ms of an idle tick
156
+ against a silent freeze if it ever missed one — see the note at the top of this file.
157
+
158
+ ~~Charm still gets the cheap path: an atom's tables are replaced rather than written to, so a
159
+ subject that moved differs on its first key.~~ There is no cheap path to get — see `of`. What the
160
+ atom's immutability buys is correctness: the tick's snapshot can never alias a table the game
161
+ then writes into.
162
+ :::
163
+ ]=]
164
+ function Store.charm(getter: () -> { [any]: any }): Store
165
+ local function subjects(): { any }
166
+ local out = {}
167
+
168
+ for subject in getter() do
169
+ table.insert(out, subject)
170
+ end
171
+
172
+ return out
173
+ end
174
+
175
+ return {
176
+ subjects = subjects,
177
+
178
+ read = function(subject: any): any
179
+ return getter()[subject]
180
+ end,
181
+ }
182
+ end
183
+
184
+ --[=[
185
+ @within Store
186
+ @function replica
187
+ @param replicas { [any]: { Data: any } } -- subject to Replica
188
+ @return Store
189
+
190
+ ReplicaService, adapted at `Replica.Data`.
191
+
192
+ ```lua
193
+ local store = Store.replica(byPlayer)
194
+ ```
195
+
196
+ There is no change signal, and that is ReplicaService's design rather than an omission: the
197
+ game names the mutation — `SetValue`, `ArrayInsert`, `Write` — and each fires its own
198
+ RemoteEvent per player at the moment it is called
199
+ (`ReplicaService/src/ServerScriptService/ReplicaService.lua:403-541`). Nothing observes `Data`
200
+ on the server, so netweave reads it once a tick.
201
+
202
+ :::note
203
+ That polling is also the win. Six mutations to one replica in one frame are six RemoteEvent
204
+ calls per player under ReplicaService and one batched patch under netweave, because the tick
205
+ sees the state after all six rather than each of them in turn.
206
+ :::
207
+ ]=]
208
+ function Store.replica(replicas: { [any]: { Data: any } }): Store
209
+ local function subjects(): { any }
210
+ local out = {}
211
+
212
+ for subject in replicas do
213
+ table.insert(out, subject)
214
+ end
215
+
216
+ return out
217
+ end
218
+
219
+ return {
220
+ subjects = subjects,
221
+
222
+ read = function(subject: any): any
223
+ local replica = replicas[subject]
224
+ return if replica then replica.Data else nil
225
+ end,
226
+ }
227
+ end
228
+
229
+ return Store