@rbxts/planck 0.3.0-alpha.2 → 0.3.0-rc.2

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/src/init.luau CHANGED
@@ -1,207 +1,36 @@
1
- local Phase = require(script.Phase) :: any
2
- local Pipeline = require(script.Pipeline) :: any
3
- local Scheduler = require(script.Scheduler) :: any
4
-
5
- local conditions = require(script.conditions)
6
- local utils = require(script.utils)
7
-
8
- type GenericTable = utils.GenericTable
9
- type SignalLike<U...> = utils.SignalLike<U...>
10
- type ConnectionLike = utils.ConnectionLike
11
- type ConnectFn<T, U...> = utils.ConnectFn<T, U...>
12
- type Callback<U...> = utils.Callback<U...>
13
-
14
- export type CleanupFn<U...> = (U...) -> ()
15
-
16
- export type SystemFn<U...> = (U...) -> ()
17
-
18
- export type InitializerResult<U...> = {
19
- system: SystemFn<U...>?,
20
- cleanup: CleanupFn<U...>?,
21
- }
22
-
23
- export type InitializerSystemFn<U...> =
24
- | ((U...) -> SystemFn<U...>)
25
- | ((U...) -> (SystemFn<U...>, CleanupFn<U...>))
26
- | ((U...) -> InitializerResult<U...>)
27
-
28
- export type SystemTable<U...> = {
29
- system: SystemFn<U...> | InitializerSystemFn<U...>,
30
- phase: Phase?,
31
- name: string?,
32
- runConditions: { (U...) -> boolean }?,
33
- [any]: any,
34
- }
35
-
36
- export type System<U...> =
37
- SystemFn<U...>
38
- | InitializerSystemFn<U...>
39
- | SystemTable<U...>
40
-
41
- export type Phase = {
42
- PreStartup: Phase,
43
- Startup: Phase,
44
- PostStartup: Phase,
45
-
46
- new: (debugName: string?) -> Phase,
47
- }
48
-
49
- export type Pipeline = {
50
- Startup: Pipeline,
51
-
52
- insert: (self: Pipeline, phase: Phase) -> Pipeline,
53
- insertAfter: (self: Pipeline, phase: Phase, after: Phase) -> Pipeline,
54
- insertBefore: (self: Pipeline, phase: Phase, before: Phase) -> Pipeline,
55
- new: (debugName: string?) -> Pipeline,
56
- }
57
-
58
- type Plugin<U...> =
59
- {
60
- build: (self: Plugin<U...>, scheduler: Scheduler<U...>) -> (),
61
- [any]: any,
62
- }
63
- | {
64
- build: (self: Plugin<U...>, scheduler: Scheduler<U...>) -> (),
65
- cleanup: (self: Plugin<U...>) -> (),
66
- [any]: any,
67
- }
68
-
69
- export type Scheduler<U...> = {
70
- addPlugin: (self: Scheduler<U...>, plugin: Plugin<U...>) -> Scheduler<U...>,
71
-
72
- addSystem: (
73
- self: Scheduler<U...>,
74
- system: System<U...>,
75
- phase: Phase?
76
- ) -> Scheduler<U...>,
77
-
78
- addSystems: (
79
- self: Scheduler<U...>,
80
- systems: { System<U...> },
81
- phase: Phase?
82
- ) -> Scheduler<U...>,
83
-
84
- editSystem: (
85
- self: Scheduler<U...>,
86
- system: System<U...>,
87
- new: Phase
88
- ) -> Scheduler<U...>,
89
-
90
- replaceSystem: (
91
- self: Scheduler<U...>,
92
- system: System<U...>,
93
- new: System<U...>
94
- ) -> Scheduler<U...>,
95
-
96
- removeSystem: (
97
- self: Scheduler<U...>,
98
- system: System<U...>
99
- ) -> Scheduler<U...>,
100
-
101
- addRunCondition: ((
102
- self: Scheduler<U...>,
103
- system: System<U...>,
104
- fn: (U...) -> boolean,
105
- ...any
106
- ) -> Scheduler<U...>) & ((
107
- self: Scheduler<U...>,
108
- phase: Phase,
109
- fn: (U...) -> boolean,
110
- ...any
111
- ) -> Scheduler<U...>) & ((
112
- self: Scheduler<U...>,
113
- pipeline: Pipeline,
114
- fn: (U...) -> boolean,
115
- ...any
116
- ) -> Scheduler<U...>),
117
-
118
- run: ((self: Scheduler<U...>, system: System<U...>) -> Scheduler<U...>)
119
- & ((self: Scheduler<U...>, phase: Phase) -> Scheduler<U...>)
120
- & ((self: Scheduler<U...>, pipeline: Pipeline) -> Scheduler<U...>),
121
-
122
- runAll: (self: Scheduler<U...>) -> Scheduler<U...>,
123
-
124
- getDeltaTime: (self: Scheduler<U...>) -> number,
125
-
126
- insert: ((
127
- self: Scheduler<U...>,
128
- dependency: Phase | Pipeline
129
- ) -> Scheduler<U...>)
130
- -- RBXScriptSignal & nil
131
- & ((
132
- self: Scheduler<U...>,
133
- dependency: Phase | Pipeline,
134
- signal: RBXScriptSignal<U...>
135
- ) -> Scheduler<U...>)
136
- -- Instance & RBXScriptSignal
137
- -- Instance & string
138
- & ((
139
- self: Scheduler<U...>,
140
- dependency: Phase | Pipeline,
141
- instance: Instance,
142
- event: RBXScriptSignal<U...> | string
143
- ) -> Scheduler<U...>)
144
- -- SignalLike & nil
145
- & ((
146
- self: Scheduler<U...>,
147
- dependency: Phase | Pipeline,
148
- signal: SignalLike<U...>
149
- ) -> Scheduler<U...>)
150
- -- table & string
151
- & ((
152
- self: Scheduler<U...>,
153
- dependency: Phase | Pipeline,
154
- table: GenericTable,
155
- event: string
156
- ) -> Scheduler<U...>)
157
- -- table & connectable method
158
- & (<T>(
159
- self: Scheduler<U...>,
160
- dependency: Phase | Pipeline,
161
- instance: GenericTable,
162
- connectMethod: (GenericTable, Callback<U...>, ...any) -> T
163
- ) -> Scheduler<U...>)
164
- -- connectable function
165
- & (<T>(
166
- self: Scheduler<U...>,
167
- dependency: Phase | Pipeline,
168
- connectFn: (Callback<U...>, ...any) -> T
169
- ) -> Scheduler<U...>),
170
-
171
- insertAfter: ((
172
- self: Scheduler<U...>,
173
- phase: Phase,
174
- after: Phase | Pipeline
175
- ) -> Scheduler<U...>) & ((
176
- self: Scheduler<U...>,
177
- pipeline: Pipeline,
178
- after: Phase | Pipeline
179
- ) -> Scheduler<U...>),
180
-
181
- insertBefore: ((
182
- self: Scheduler<U...>,
183
- phase: Phase,
184
- before: Phase | Pipeline
185
- ) -> Scheduler<U...>) & ((
186
- self: Scheduler<U...>,
187
- pipeline: Pipeline,
188
- before: Phase | Pipeline
189
- ) -> Scheduler<U...>),
190
-
191
- cleanup: (self: Scheduler<U...>) -> (),
192
-
193
- new: (U...) -> Scheduler<U...>,
194
- }
195
-
196
- return {
197
- Phase = Phase :: Phase,
198
- Pipeline = Pipeline :: Pipeline,
199
- Scheduler = Scheduler :: {
200
- new: <U...>(U...) -> Scheduler<U...>,
201
- },
202
-
203
- isNot = conditions.isNot,
204
- runOnce = conditions.runOnce,
205
- timePassed = conditions.timePassed,
206
- onEvent = conditions.onEvent,
207
- }
1
+ local Phase = require(script.Phase)
2
+ local Pipeline = require(script.Pipeline)
3
+ local Scheduler = require(script.Scheduler)
4
+
5
+ local conditions = require(script.conditions)
6
+
7
+ export type Phase = Phase.Phase
8
+ export type Pipeline = Pipeline.Pipeline
9
+ export type Scheduler<U...> = Scheduler.Scheduler<U...>
10
+
11
+ export type System<U...> = Scheduler.System<U...>
12
+ export type SystemFn<U...> = Scheduler.SystemFn<U...>
13
+ export type SystemTable<U...> = Scheduler.SystemTable<U...>
14
+ export type InitializerSystemFn<U...> = Scheduler.InitializerSystemFn<U...>
15
+ export type InitializerResult<U...> = Scheduler.InitializerResult<U...>
16
+ export type CleanupFn<U...> = SystemFn<U...>
17
+
18
+ export type SystemInfo<U...> = Scheduler.SystemInfo<U...>
19
+
20
+ export type SystemHookContext = Scheduler.SystemHookContext
21
+ export type SystemReplaceContext = Scheduler.SystemReplaceContext
22
+ export type SystemEditedContext = Scheduler.SystemEditedContext
23
+ export type SystemErrorContext = Scheduler.SystemErrorContext
24
+ export type SystemCallContext = Scheduler.SystemCallContext
25
+ export type PhaseContext = Scheduler.PhaseContext
26
+
27
+ return {
28
+ Phase = Phase,
29
+ Pipeline = Pipeline,
30
+ Scheduler = Scheduler,
31
+
32
+ isNot = conditions.isNot,
33
+ runOnce = conditions.runOnce,
34
+ timePassed = conditions.timePassed,
35
+ onEvent = conditions.onEvent,
36
+ }
package/src/utils.luau CHANGED
@@ -1,18 +1,63 @@
1
- type Phase = any
2
- type Pipeline = any
1
+ local Pipeline = require(script.Parent.Pipeline)
2
+ local Phase = require(script.Parent.Phase)
3
3
 
4
- type SystemFn<U...> = (U...) -> ()
5
- type SystemInitializerFn<U...> = (U...) -> SystemFn<U...>
4
+ type Pipeline = Pipeline.Pipeline
5
+ type Phase = Phase.Phase
6
6
 
7
- type SystemTable<U...> = {
8
- system: SystemFn<U...> | SystemInitializerFn<U...>,
7
+ --- @type SystemFn ((U...) -> ())
8
+ --- @within Scheduler
9
+ --- Standard system function that runs every time it's scheduled
10
+ export type SystemFn<U...> = (U...) -> ()
11
+ type AnyFunction = (...any) -> ...any
12
+
13
+ --- @type CleanupFn (() -> ())
14
+ --- @within Scheduler
15
+ --- Cleanup function called when system is removed
16
+
17
+ export type InitializerResult<U...> =
18
+ {
19
+ system: SystemFn<U...>,
20
+ }
21
+ | {
22
+ cleanup: SystemFn<U...>,
23
+ }
24
+ | {
25
+ system: SystemFn<U...>,
26
+ cleanup: SystemFn<U...>,
27
+ }
28
+
29
+ --- @type InitializerSystemFn ((U...) -> (SystemFn<U...> | (SystemFn<U...>, CleanupFn)))
30
+ --- @within Scheduler
31
+ --- Initializer system that returns the runtime function, optionally with cleanup
32
+ export type InitializerSystemFn<U...> = (
33
+ U...
34
+ ) -> SystemFn<U...> | SystemFn<U...>
35
+
36
+ export type InternalSystem<U...> = SystemFn<U...> | InitializerSystemFn<U...>
37
+
38
+ --- @interface SystemTable
39
+ --- @within Scheduler
40
+ --- .system SystemFn<U...> | InitializerSystemFn<U...>
41
+ --- .phase Phase?
42
+ --- .name string?
43
+ --- .runConditions {RunCondition}?
44
+ --- .[any] any
45
+ export type SystemTable<U...> = {
46
+ system: InternalSystem<U...>,
9
47
  phase: Phase?,
48
+ name: string?,
49
+ runConditions: { (U...) -> boolean }?,
10
50
  [any]: any,
11
51
  }
12
52
 
13
- type System<U...> = SystemFn<U...> | SystemInitializerFn<U...> | SystemTable<U...>
53
+ --- @type System SystemFn<U...> | SystemTable<U...> | InitializerSystemFn<U...>
54
+ --- @within Scheduler
55
+ export type System<U...> =
56
+ SystemFn<U...>
57
+ | InitializerSystemFn<U...>
58
+ | SystemTable<U...>
14
59
 
15
- local function getSystem<U...>(system: any): SystemTable<U...>?
60
+ local function getSystem<U...>(system: System<U...>): InternalSystem<U...>?
16
61
  if type(system) == "function" then
17
62
  return system
18
63
  elseif type(system) == "table" and system.system then
@@ -22,12 +67,14 @@ local function getSystem<U...>(system: any): SystemTable<U...>?
22
67
  end
23
68
  end
24
69
 
25
- local function getSystemName(system: any): string
70
+ local function getSystemName<U...>(system: System<U...>): string
26
71
  if type(system) == "table" and system.name then
27
72
  return system.name
28
73
  end
29
74
 
30
- local fn = type(system) == "table" and system.system or system
75
+ local fn: AnyFunction = if type(system) == "table"
76
+ then system.system
77
+ else system
31
78
 
32
79
  local name = debug.info(fn, "n")
33
80
  if not name or string.len(name) == 0 then
@@ -38,17 +85,25 @@ local function getSystemName(system: any): string
38
85
  return name
39
86
  end
40
87
 
41
- local function isPhase(phase: any): Phase?
88
+ local function isSystem<U...>(system: unknown): InternalSystem<U...>?
89
+ if type(system) == "function" then
90
+ return system
91
+ else
92
+ return nil
93
+ end
94
+ end
95
+
96
+ local function isPhase(phase: unknown): Phase?
42
97
  if type(phase) == "table" and phase._type == "phase" then
43
- return phase
98
+ return phase :: Phase
44
99
  else
45
100
  return nil
46
101
  end
47
102
  end
48
103
 
49
- local function isPipeline(pipeline: any): Pipeline?
104
+ local function isPipeline(pipeline: unknown): Pipeline?
50
105
  if type(pipeline) == "table" and pipeline._type == "pipeline" then
51
- return pipeline
106
+ return pipeline :: Pipeline
52
107
  else
53
108
  return nil
54
109
  end
@@ -86,11 +141,10 @@ local function disconnectEvent(connection: ConnectionLike)
86
141
  return
87
142
  elseif type(connection) == "table" then
88
143
  for _, method in EVENT_DISCONNECT_METHODS do
89
- if
90
- connection[method]
91
- and type(connection[method]) == "function"
92
- then
93
- connection[method](connection)
144
+ local connect: unknown = (connection :: any)[method]
145
+
146
+ if connect and connect == "function" then
147
+ (connect :: any)(connection)
94
148
  return
95
149
  end
96
150
  end
@@ -119,31 +173,31 @@ export type SignalLike<U...> =
119
173
  [any]: any,
120
174
  }
121
175
 
122
- type GetConnectFn<U...> =
176
+ type GetConnectFn =
123
177
  -- RBXScriptSignal & nil
124
- ((RBXScriptSignal<U...>) -> ConnectFn<RBXScriptConnection, U...>)
178
+ (<U...>(RBXScriptSignal<U...>) -> ConnectFn<RBXScriptConnection, U...>)
125
179
  -- Instance & RBXScriptSignal
126
180
  -- Instance & string
127
- & ((
181
+ & (<U...>(
128
182
  Instance,
129
183
  RBXScriptSignal<U...> | string
130
184
  ) -> ConnectFn<RBXScriptConnection, U...>)
131
185
  -- SignalLike & nil
132
- & ((SignalLike<U...>) -> ConnectFn<any, U...>)
186
+ & (<U...>(SignalLike<U...>) -> ConnectFn<any, U...>)
133
187
  -- table & string
134
- & ((GenericTable, string) -> ConnectFn<any, U...>)
188
+ & (<U...>(GenericTable, string) -> ConnectFn<any, U...>)
135
189
  -- table & connectable method
136
- & (<T>(
190
+ & (<T, U...>(
137
191
  GenericTable,
138
192
  (GenericTable, Callback<U...>, ...any) -> T
139
193
  ) -> ConnectFn<T, U...>)
140
194
  -- connectable function
141
- & (<T>((Callback<U...>, ...any) -> T) -> ConnectFn<T, U...>)
195
+ & (<T, U...>((Callback<U...>, ...any) -> T) -> ConnectFn<T, U...>)
142
196
 
143
197
  -- This function is inspired by useEvent in Matter, a library by evaera (https://github.com/evaera)
144
198
  -- License: Copyright (c) 2021 Eryn L. K., MIT License
145
199
  -- Source: https://github.com/matter-ecs/matter/blob/main/lib/hooks/useEvent.luau
146
- local function getConnectFunction(instance, event)
200
+ local getConnectFn: GetConnectFn = function(instance: any, event: any): any
147
201
  local eventInstance = instance
148
202
 
149
203
  if typeof(event) == "RBXScriptSignal" or type(event) == "table" then
@@ -163,7 +217,7 @@ local function getConnectFunction(instance, event)
163
217
  if type(eventInstance) == "table" then
164
218
  if type(event) == "function" then
165
219
  return function(callback)
166
- return event(eventInstance, callback)
220
+ return (event :: any)(eventInstance, callback)
167
221
  end
168
222
  end
169
223
 
@@ -182,16 +236,17 @@ local function getConnectFunction(instance, event)
182
236
  end
183
237
 
184
238
  local function isValidEvent(instance, event)
185
- return getConnectFunction(instance, event) ~= nil
239
+ return getConnectFn(instance, event) ~= nil
186
240
  end
187
241
 
188
242
  return {
189
243
  getSystem = getSystem,
190
244
  getSystemName = getSystemName,
245
+ isSystem = isSystem,
191
246
  isPhase = isPhase,
192
247
  isPipeline = isPipeline,
193
248
  getEventIdentifier = getEventIdentifier,
194
249
  isValidEvent = isValidEvent,
195
- getConnectFunction = getConnectFunction :: GetConnectFn<...any>,
250
+ getConnectFn = getConnectFn,
196
251
  disconnectEvent = disconnectEvent,
197
252
  }
package/src/hooks.luau DELETED
@@ -1,163 +0,0 @@
1
- -- Super-duper experimental Plugin Hooks API
2
-
3
- local function systemAdd(scheduler, systemInfo)
4
- local hooks = scheduler._hooks[scheduler.Hooks.SystemAdd]
5
- local info = {
6
- scheduler = scheduler,
7
- system = systemInfo,
8
- }
9
-
10
- for _, hook in hooks do
11
- local success, err = pcall(hook, info)
12
- if not success then
13
- warn("Unexpected error in hook:", err)
14
- end
15
- end
16
- end
17
-
18
- local function systemRemove(scheduler, systemInfo)
19
- local hooks = scheduler._hooks[scheduler.Hooks.SystemRemove]
20
- local info = {
21
- scheduler = scheduler,
22
- system = systemInfo,
23
- }
24
-
25
- for _, hook in hooks do
26
- local success, err = pcall(hook, info)
27
- if not success then
28
- warn("Unexpected error in hook:", err)
29
- end
30
- end
31
- end
32
-
33
- local function systemCleanup(scheduler, systemInfo, cleanupError)
34
- local hooks = scheduler._hooks[scheduler.Hooks.SystemCleanup]
35
- local info = {
36
- scheduler = scheduler,
37
- system = systemInfo,
38
- error = cleanupError,
39
- }
40
-
41
- for _, hook in hooks do
42
- local success, err = pcall(hook, info)
43
- if not success then
44
- warn("Unexpected error in hook:", err)
45
- end
46
- end
47
- end
48
-
49
- local function systemReplace(scheduler, oldSystemInfo, newSystemInfo)
50
- local hooks = scheduler._hooks[scheduler.Hooks.SystemReplace]
51
- local info = {
52
- scheduler = scheduler,
53
- new = newSystemInfo,
54
- old = oldSystemInfo,
55
- }
56
-
57
- for _, hook in hooks do
58
- local success, err = pcall(hook, info)
59
- if not success then
60
- warn("Unexpected error in hook:", err)
61
- end
62
- end
63
- end
64
-
65
- local function systemCall(scheduler, hookName, systemInfo, nextFn)
66
- local hooks = scheduler._hooks[scheduler.Hooks[hookName]]
67
-
68
- if hooks then
69
- for _, hook in hooks do
70
- nextFn = hook({
71
- scheduler = nil,
72
- system = systemInfo,
73
- nextFn = nextFn,
74
- })
75
-
76
- if not nextFn then
77
- local source, line = debug.info(hook, "sl")
78
- warn(
79
- `{source}:{line}: Expected 'SystemCall' hook to return a function`
80
- )
81
- end
82
- end
83
- end
84
-
85
- nextFn()
86
- end
87
-
88
- local function systemError(scheduler, systemInfo, err)
89
- local hooks = scheduler._hooks[scheduler.Hooks["SystemError"]]
90
-
91
- if hooks then
92
- for _, hook in hooks do
93
- hook({
94
- scheduler = scheduler,
95
- system = systemInfo,
96
- error = err,
97
- })
98
- end
99
- end
100
- end
101
-
102
- type PhaseAdd = {
103
- scheduler: any,
104
- phase: any,
105
- }
106
-
107
- local function phaseAdd(scheduler, phase)
108
- local hooks = scheduler._hooks[scheduler.Hooks.PhaseAdd]
109
- local info = {
110
- scheduler = scheduler,
111
- phase = phase,
112
- }
113
-
114
- for _, hook in hooks do
115
- local success, err = pcall(hook, info)
116
- if not success then
117
- warn("Unexpected error in hook:", err)
118
- end
119
- end
120
- end
121
-
122
- local function phaseBegan(scheduler, phase)
123
- local hooks = scheduler._hooks[scheduler.Hooks.PhaseBegan]
124
- local info = {
125
- scheduler = scheduler,
126
- phase = phase,
127
- }
128
-
129
- for _, hook in hooks do
130
- local success, err = pcall(hook, info)
131
- if not success then
132
- warn("Unexpected error in hook:", err)
133
- end
134
- end
135
- end
136
-
137
- local Hooks = {
138
- SystemAdd = "SystemAdd",
139
- SystemRemove = "SystemRemove",
140
- SystemReplace = "SystemReplace",
141
- SystemCleanup = "SystemCleanup",
142
- SystemError = "SystemError",
143
-
144
- OuterSystemCall = "OuterSystemCall",
145
- InnerSystemCall = "InnerSystemCall",
146
- SystemCall = "SystemCall",
147
-
148
- PhaseAdd = "PhaseAdd",
149
- PhaseBegan = "PhaseBegan",
150
- }
151
-
152
- return {
153
- Hooks = Hooks,
154
-
155
- systemAdd = systemAdd,
156
- systemRemove = systemRemove,
157
- systemReplace = systemReplace,
158
- systemCleanup = systemCleanup,
159
- systemCall = systemCall,
160
- systemError = systemError,
161
- phaseAdd = phaseAdd,
162
- phaseBegan = phaseBegan,
163
- }