mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
refactor: improve structure and alien-signals integration
This commit is contained in:
parent
a9c60c701f
commit
95476e93ec
107 changed files with 3660 additions and 815 deletions
6
packages/charm-sync/Charm.luau
Normal file
6
packages/charm-sync/Charm.luau
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
local Charm = require(script.Parent.Parent.charm.src)
|
||||
|
||||
export type Atom<T = any> = Charm.Atom<T>
|
||||
export type Selector<T = any> = Charm.Selector<T>
|
||||
|
||||
return Charm
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "CharmSync",
|
||||
"name": "charm-sync",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
}
|
||||
|
|
|
|||
27
packages/charm-sync/package.json
Normal file
27
packages/charm-sync/package.json
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "@rbxts/charm-sync",
|
||||
"version": "0.3.0",
|
||||
"description": "Simple server-to-client sync for Charm",
|
||||
"license": "MIT",
|
||||
"main": "src/init.luau",
|
||||
"types": "src/index.d.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"*.luau",
|
||||
"!wally.toml",
|
||||
"!wally.lock",
|
||||
"!default.project.json",
|
||||
"!**/__tests__"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/littensy/charm.git"
|
||||
},
|
||||
"scripts": {
|
||||
"prepare": "cp ../../LICENSE.md . && cp ../../README.md .",
|
||||
"publish:wally": "wally publish"
|
||||
},
|
||||
"dependencies": {
|
||||
"@rbxts/charm": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local Charm = require(ReplicatedStorage.Packages.Charm)
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local Charm = require("../../Charm")
|
||||
local CharmSync = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
local atom = Charm.atom
|
||||
local subscribe = Charm.subscribe
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local Charm = require(ReplicatedStorage.Packages.Charm)
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local Charm = require("../../Charm")
|
||||
local CharmSync = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
local atom = Charm.atom
|
||||
local flatten = CharmSync.flatten
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
local HttpService = game:GetService("HttpService")
|
||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local CharmSync = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
local patch = CharmSync.patch
|
||||
local test = suite.test
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local Charm = require(ReplicatedStorage.Packages.Charm)
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local Charm = require("../../Charm")
|
||||
local CharmSync = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
type SyncPayload = CharmSync.SyncPayload
|
||||
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
suite.describe("charm-sync", function()
|
||||
if not game then
|
||||
return suite.skip()
|
||||
end
|
||||
suite.describe("client", function()
|
||||
require("./client.spec")
|
||||
end)
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
local ServerScriptService = game:GetService("ServerScriptService")
|
||||
|
||||
local Charm = require(ReplicatedStorage.Packages.Charm)
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
local suite = require(ServerScriptService.Tests.suite)
|
||||
local Charm = require("../../Charm")
|
||||
local CharmSync = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
local atom = Charm.atom
|
||||
local validate = CharmSync.validate
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
local Charm = require(script.Parent.Parent.Charm)
|
||||
local patch = require(script.Parent.patch)
|
||||
local types = require(script.Parent.types)
|
||||
local Charm = require("../Charm")
|
||||
local patch = require("./patch")
|
||||
local types = require("./types")
|
||||
|
||||
type AtomMap = types.AtomMap
|
||||
type SyncPayload = types.SyncPayload
|
||||
|
|
@ -36,7 +36,7 @@ local function client(options: ClientOptions): ClientSyncer
|
|||
local function hydrate(state: { [string | number]: any })
|
||||
hydrated = true
|
||||
|
||||
for key, value in next, state do
|
||||
for key, value in state do
|
||||
local atom = atoms[key]
|
||||
atom(value)
|
||||
end
|
||||
|
|
@ -45,13 +45,13 @@ local function client(options: ClientOptions): ClientSyncer
|
|||
local function apply(data: { [string | number]: any })
|
||||
local target = {}
|
||||
|
||||
for key, atom in next, atoms do
|
||||
for key, atom in atoms do
|
||||
target[key] = atom()
|
||||
end
|
||||
|
||||
target = patch.apply(target, data)
|
||||
|
||||
for key, atom in next, atoms do
|
||||
for key, atom in atoms do
|
||||
atom(target[key])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
local types = require(script.Parent.types)
|
||||
local types = require("./types")
|
||||
|
||||
type AtomMap = types.AtomMap
|
||||
|
||||
|
|
|
|||
1
packages/charm-sync/src/index.d.ts
vendored
Normal file
1
packages/charm-sync/src/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export {};
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
local client = require(script.client)
|
||||
local flatten = require(script.flatten)
|
||||
local patch = require(script.patch)
|
||||
local server = require(script.server)
|
||||
local types = require(script.types)
|
||||
local validate = require(script.validate)
|
||||
local client = require("@self/client")
|
||||
local flatten = require("@self/flatten")
|
||||
local patch = require("@self/patch")
|
||||
local server = require("@self/server")
|
||||
local types = require("@self/types")
|
||||
local validate = require("@self/validate")
|
||||
|
||||
export type SyncPayload = types.SyncPayload
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
local RunService = game:GetService("RunService")
|
||||
|
||||
--- Schedules a function to be called at a fixed interval of `seconds`.
|
||||
---
|
||||
--- @param callback The function to call.
|
||||
|
|
@ -12,7 +10,7 @@ local function interval(callback: () -> (), seconds: number): () -> ()
|
|||
|
||||
local clock = 0
|
||||
|
||||
local connection = RunService.Heartbeat:Connect(function(delta)
|
||||
local connection = game:GetService("RunService").Heartbeat:Connect(function(delta)
|
||||
clock += delta
|
||||
if clock >= seconds then
|
||||
clock = 0
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
local validate = require(script.Parent.validate)
|
||||
|
||||
local __DEV__ = _G.__DEV__
|
||||
local Charm = require("../Charm")
|
||||
local validate = require("./validate")
|
||||
|
||||
--- A special symbol that denotes the absence of a value. Used to represent
|
||||
--- deleted values in patches.
|
||||
|
|
@ -64,7 +63,7 @@ local function diff(prevState: { [any]: any }, nextState: { [any]: any }, serial
|
|||
patches = stringifySparseArray(patches)
|
||||
end
|
||||
|
||||
if serialize and __DEV__ then
|
||||
if serialize and Charm.strict.enabled then
|
||||
for key, value in next, patches do
|
||||
validate(value, key)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
local Players = game:GetService("Players")
|
||||
|
||||
local Charm = require(script.Parent.Parent.Charm)
|
||||
local interval = require(script.Parent.interval)
|
||||
local patch = require(script.Parent.patch)
|
||||
local types = require(script.Parent.types)
|
||||
local Charm = require("../Charm")
|
||||
local interval = require("./interval")
|
||||
local patch = require("./patch")
|
||||
local types = require("./types")
|
||||
|
||||
type AtomMap = types.AtomMap
|
||||
type SyncPayload = types.SyncPayload
|
||||
|
|
@ -64,7 +62,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
local function createSnapshot()
|
||||
local snapshot = {}
|
||||
|
||||
for key, atom in next, atoms do
|
||||
for key, atom in atoms do
|
||||
snapshot[key] = atom()
|
||||
end
|
||||
|
||||
|
|
@ -110,7 +108,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
|
||||
-- Populate the snapshot with the initial state of each atom.
|
||||
-- Subscribe to each atom and update the state when it changes.
|
||||
for key, atom in next, atoms do
|
||||
for key, atom in atoms do
|
||||
subscriptions[key] = Charm.subscribe(atom, function(current, previous)
|
||||
pushSnapshot(key, current, previous)
|
||||
changed = true
|
||||
|
|
@ -125,7 +123,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
local payloads: { SyncPayload } = {}
|
||||
local lastSnapshot
|
||||
|
||||
for index, snapshot in next, snapshots do
|
||||
for index, snapshot in snapshots do
|
||||
lastSnapshot = snapshot
|
||||
|
||||
if index == 1 then
|
||||
|
|
@ -141,7 +139,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
snapshots = { lastSnapshot }
|
||||
changed = false
|
||||
|
||||
for _, player in next, Players:GetPlayers() do
|
||||
for _, player in game:GetService("Players"):GetPlayers() do
|
||||
callback(player, unpack(payloads))
|
||||
end
|
||||
end, syncRate)
|
||||
|
|
@ -149,7 +147,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
return function()
|
||||
disconnect()
|
||||
|
||||
for _, unsubscribe in next, subscriptions do
|
||||
for _, unsubscribe in subscriptions do
|
||||
unsubscribe()
|
||||
end
|
||||
end
|
||||
|
|
@ -164,7 +162,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
|
||||
local payloads: { SyncPayload } = {}
|
||||
|
||||
for index, snapshot in next, snapshots do
|
||||
for index, snapshot in snapshots do
|
||||
if index == 1 then
|
||||
continue
|
||||
end
|
||||
|
|
@ -194,7 +192,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
sync = callback
|
||||
|
||||
-- Subscribe to each atom and update the state when one changes.
|
||||
for key, atom in next, atoms do
|
||||
for key, atom in atoms do
|
||||
subscriptions[key] = Charm.subscribe(atom, function(state)
|
||||
currentSnapshot[key] = state
|
||||
changed = true
|
||||
|
|
@ -214,7 +212,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
previousSnapshot = table.clone(currentSnapshot)
|
||||
changed = false
|
||||
|
||||
for _, player in next, Players:GetPlayers() do
|
||||
for _, player in game:GetService("Players"):GetPlayers() do
|
||||
callback(player, payload)
|
||||
end
|
||||
end, syncRate)
|
||||
|
|
@ -222,7 +220,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
return function()
|
||||
disconnect()
|
||||
|
||||
for _, unsubscribe in next, subscriptions do
|
||||
for _, unsubscribe in subscriptions do
|
||||
unsubscribe()
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
local Charm = require(script.Parent.Parent.Charm)
|
||||
local Charm = require("../Charm")
|
||||
|
||||
type Atom<T> = Charm.Atom<T>
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,13 @@ name = "littensy/charm-sync"
|
|||
version = "0.3.0"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
exclude = ["tests"]
|
||||
exclude = [
|
||||
"node_modules",
|
||||
"src/__tests__",
|
||||
"src/index.d.ts",
|
||||
"package.json",
|
||||
"Charm.luau",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
Charm = "littensy/charm@^0.10"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue