Skip to content

Commit 94f5249

Browse files
committed
Add wally toml and move to src folder
1 parent 699d708 commit 94f5249

7 files changed

Lines changed: 32 additions & 15 deletions

File tree

default.project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "roblox-lua-parallel",
2+
"name": "parallel",
33
"tree": {
4-
"$path": "lib"
4+
"$path": "src"
55
}
66
}

lib/init.luau renamed to src/init.luau

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ export type PreparedParallel = {
1616
destroy: () -> (),
1717
}
1818

19+
type ActorStateMap = {
20+
[Actor]: ActorState,
21+
}
22+
1923
type ActorState = {
2024
count: number,
21-
running: {Promise},
25+
running: { [Promise]: boolean },
2226
}
2327

2428
-- Set correct worker folder depending on
@@ -37,16 +41,17 @@ local Parallel = {}
3741
Parallel.__index = Parallel
3842

3943
function Parallel.of(runnable: (...any?) -> any?): PreparedParallel
44+
assert(type(runnable) == "function", "Runnable must be a function")
4045
local self = setmetatable({
4146
_runnable = runnable,
4247
_name = "ParallelWorker",
4348
_actorCount = 1,
4449
_bindableEvent = Instance.new("BindableEvent"),
4550
_folder = Instance.new("Folder"),
4651

47-
_actors = {},
48-
_actorState = {} :: ActorState,
49-
_results = {},
52+
_actors = {} :: { Actor },
53+
_actorState = {} :: ActorStateMap,
54+
_results = {} :: { [string]: table },
5055

5156
_connection = nil,
5257
_destroyed = false,
@@ -63,13 +68,17 @@ end
6368

6469
function Parallel:withName(name: string): PreparedParallel
6570
assert(not self._destroyed, "Parallel destroyed")
71+
assert(type(name) == "string", "Name must be a string")
72+
assert(#name > 0, "Name must be non-empty")
6673
self._name = name
6774
self._folder.Name = name
6875
return self
6976
end
7077

7178
function Parallel:withActors(actorCount: number): PreparedParallel
7279
assert(not self._destroyed, "Parallel destroyed")
80+
assert(type(actorCount) == "number", "Actor count must be a number")
81+
assert(actorCount > 0, "Actor count must be greater than 0")
7382
self._actorCount = actorCount
7483
self:_createActors()
7584
return self
@@ -121,15 +130,13 @@ end
121130
function Parallel:destroy()
122131
assert(not self._destroyed, "Parallel already destroyed")
123132
self._destroyed = true
124-
125133
self._connection:Disconnect()
126134
self._connection = nil
127135

128136
for _, actor in self._actors do
129137
for runningPromise, _ in self._actorState[actor].running do
130138
runningPromise:cancel()
131139
end
132-
133140
actor:SendMessage("Parallel:Destroy")
134141
end
135142

@@ -139,18 +146,17 @@ function Parallel:destroy()
139146

140147
self._bindableEvent:Destroy()
141148
self._bindableEvent = nil
142-
143149
self._folder:Destroy()
144150
self._folder = nil
145151
end
146152

147153
function Parallel:_findAvailableActor(): Actor
148154
assert(#self._actors > 0, "No available actors")
149-
150-
-- Find actor with least running promises
151-
local min = 999_999
152-
local minActor = nil
153-
for _, actor in self._actors do
155+
local min = self._actors[1]
156+
local minActor = self._actorState[min].count
157+
158+
for i = 2, #self._actors do
159+
local actor = self._actors[i]
154160
local count = self._actorState[actor].count
155161
if count < min then
156162
min = count
@@ -187,7 +193,7 @@ function Parallel._createTemplatedActor(): Actor
187193
end
188194

189195
function Parallel:__tostring()
190-
return string.format("Parallel(%s)", self._name)
196+
return string.format("Parallel<%s>", self._name)
191197
end
192198

193199
return Parallel

wally.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "dig/parallel"
3+
description = "Parallel execution library for Roblox lua"
4+
version = "1.0.0"
5+
license = "MIT"
6+
registry = "https://github.com/UpliftGames/wally-index"
7+
realm = "shared"
8+
include = ["src", "default.project.json"]
9+
10+
[dependencies]
11+
Promise = "evaera/promise@4.0.0"

0 commit comments

Comments
 (0)