@rbxts/planck 0.2.5 → 0.3.0-alpha.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.
@@ -1,161 +1,197 @@
1
- type Phase = any
2
- type Pipeline = any
3
-
4
- type SystemFn<U...> = ((U...) -> any) | ((U...) -> ())
5
-
6
- type SystemTable<U...> = {
7
- system: SystemFn<U...>,
8
- phase: Phase?,
9
- [any]: any,
10
- }
11
-
12
- type System<U...> = SystemFn<U...> | SystemTable<U...>
13
-
14
- local function getSystem<U...>(system: any): SystemTable<U...>?
15
- if type(system) == "function" then
16
- return system
17
- elseif type(system) == "table" and system.system then
18
- return system.system
19
- else
20
- return nil
21
- end
22
- end
23
-
24
- local function getSystemName(system: any): string
25
- local name = debug.info(system, "n")
26
- if not name or string.len(name) == 0 then
27
- local source, line = debug.info(system, "sl")
28
- name = `{source}:{line}`
29
- end
30
-
31
- return name
32
- end
33
-
34
- local function isPhase(phase: any): Phase?
35
- if type(phase) == "table" and phase._type == "phase" then
36
- return phase
37
- else
38
- return nil
39
- end
40
- end
41
-
42
- local function isPipeline(pipeline: any): Pipeline?
43
- if type(pipeline) == "table" and pipeline._type == "pipeline" then
44
- return pipeline
45
- else
46
- return nil
47
- end
48
- end
49
-
50
- local function getEventIdentifier(instance, event)
51
- return `{instance}{event and `@{event}` or ""}`
52
- end
53
-
54
- local EVENT_CONNECT_METHODS = { "Connect", "On", "on", "connect" }
55
-
56
- export type EventLike = RBXScriptSignal | {
57
- connect: (self: EventLike, ...any) -> any,
58
- [any]: any,
59
- } | {
60
- Connect: (self: EventLike, ...any) -> any,
61
- [any]: any,
62
- } | {
63
- on: (self: EventLike, ...any) -> any,
64
- [any]: any,
65
- }
66
-
67
- export type EventInstance = Instance | {
68
- [any]: EventLike,
69
- }
70
-
71
- export type ConnectionLike = RBXScriptConnection | {
72
- disconnect: (self: ConnectionLike, ...any) -> any,
73
- [any]: any,
74
- } | {
75
- Disconnect: (self: ConnectionLike, ...any) -> any,
76
- [any]: any,
77
- } | {
78
- destroy: (self: ConnectionLike, ...any) -> any,
79
- [any]: any,
80
- } | {
81
- Destroy: (self: ConnectionLike, ...any) -> any,
82
- [any]: any,
83
- } | (...any) -> any
84
-
85
- export type ConnectFn<U...> = (callback: (U...) -> ()) -> ConnectionLike
86
-
87
- local EVENT_DISCONNECT_METHODS =
88
- { "disconnect", "Disconnect", "destroy", "Destroy" }
89
-
90
- local function disconnectEvent(connection: ConnectionLike)
91
- if type(connection) == "function" then
92
- connection()
93
- return
94
- elseif typeof(connection) == "RBXScriptConnection" then
95
- connection:Disconnect()
96
- return
97
- elseif type(connection) == "table" then
98
- for _, method in EVENT_DISCONNECT_METHODS do
99
- if
100
- connection[method]
101
- and type(connection[method]) == "function"
102
- then
103
- connection[method](connection)
104
- return
105
- end
106
- end
107
- end
108
- end
109
-
110
- -- This function is inspired by useEvent in Matter, a library by evaera (https://github.com/evaera)
111
- -- License: Copyright (c) 2021 Eryn L. K., MIT License
112
- -- Source: https://github.com/matter-ecs/matter/blob/main/lib/hooks/useEvent.luau
113
- local function getConnectFunction<U...>(
114
- instance: EventInstance | EventLike,
115
- event: string | EventLike
116
- ): ConnectFn<U...>?
117
- local eventInstance = instance
118
-
119
- if typeof(event) == "RBXScriptSignal" or type(event) == "table" then
120
- eventInstance = event
121
- elseif type(event) == "string" then
122
- eventInstance = (instance :: any)[event]
123
- end
124
-
125
- if type(eventInstance) == "function" then
126
- return eventInstance
127
- elseif typeof(eventInstance) == "RBXScriptSignal" then
128
- return function(callback)
129
- return eventInstance:Connect(callback)
130
- end
131
- end
132
-
133
- if type(eventInstance) == "table" then
134
- for _, method in EVENT_CONNECT_METHODS do
135
- if type(eventInstance[method]) ~= "function" then
136
- continue
137
- end
138
-
139
- return function(callback)
140
- return eventInstance[method](eventInstance, callback)
141
- end
142
- end
143
- end
144
-
145
- return nil
146
- end
147
-
148
- local function isValidEvent(instance, event)
149
- return getConnectFunction(instance, event) ~= nil
150
- end
151
-
152
- return {
153
- getSystem = getSystem,
154
- getSystemName = getSystemName,
155
- isPhase = isPhase,
156
- isPipeline = isPipeline,
157
- getEventIdentifier = getEventIdentifier,
158
- isValidEvent = isValidEvent,
159
- getConnectFunction = getConnectFunction,
160
- disconnectEvent = disconnectEvent,
161
- }
1
+ type Phase = any
2
+ type Pipeline = any
3
+
4
+ type SystemFn<U...> = (U...) -> ()
5
+ type SystemInitializerFn<U...> = (U...) -> SystemFn<U...>
6
+
7
+ type SystemTable<U...> = {
8
+ system: SystemFn<U...> | SystemInitializerFn<U...>,
9
+ phase: Phase?,
10
+ [any]: any,
11
+ }
12
+
13
+ type System<U...> = SystemFn<U...> | SystemInitializerFn<U...> | SystemTable<U...>
14
+
15
+ local function getSystem<U...>(system: any): SystemTable<U...>?
16
+ if type(system) == "function" then
17
+ return system
18
+ elseif type(system) == "table" and system.system then
19
+ return system.system
20
+ else
21
+ return nil
22
+ end
23
+ end
24
+
25
+ local function getSystemName(system: any): string
26
+ if type(system) == "table" and system.name then
27
+ return system.name
28
+ end
29
+
30
+ local fn = type(system) == "table" and system.system or system
31
+
32
+ local name = debug.info(fn, "n")
33
+ if not name or string.len(name) == 0 then
34
+ local source, line = debug.info(fn, "sl")
35
+ name = `{source}:{line}`
36
+ end
37
+
38
+ return name
39
+ end
40
+
41
+ local function isPhase(phase: any): Phase?
42
+ if type(phase) == "table" and phase._type == "phase" then
43
+ return phase
44
+ else
45
+ return nil
46
+ end
47
+ end
48
+
49
+ local function isPipeline(pipeline: any): Pipeline?
50
+ if type(pipeline) == "table" and pipeline._type == "pipeline" then
51
+ return pipeline
52
+ else
53
+ return nil
54
+ end
55
+ end
56
+
57
+ local function getEventIdentifier(instance, event)
58
+ return `{instance}{event and `@{event}` or ""}`
59
+ end
60
+
61
+ local EVENT_CONNECT_METHODS = { "Connect", "On", "on", "connect" }
62
+
63
+ export type ConnectionLike = RBXScriptConnection | {
64
+ disconnect: (self: ConnectionLike, ...any) -> any,
65
+ [any]: any,
66
+ } | {
67
+ Disconnect: (self: ConnectionLike, ...any) -> any,
68
+ [any]: any,
69
+ } | {
70
+ destroy: (self: ConnectionLike, ...any) -> any,
71
+ [any]: any,
72
+ } | {
73
+ Destroy: (self: ConnectionLike, ...any) -> any,
74
+ [any]: any,
75
+ } | (...any) -> any
76
+
77
+ local EVENT_DISCONNECT_METHODS =
78
+ { "disconnect", "Disconnect", "destroy", "Destroy" }
79
+
80
+ local function disconnectEvent(connection: ConnectionLike)
81
+ if type(connection) == "function" then
82
+ connection()
83
+ return
84
+ elseif typeof(connection) == "RBXScriptConnection" then
85
+ connection:Disconnect()
86
+ return
87
+ elseif type(connection) == "table" then
88
+ for _, method in EVENT_DISCONNECT_METHODS do
89
+ if
90
+ connection[method]
91
+ and type(connection[method]) == "function"
92
+ then
93
+ connection[method](connection)
94
+ return
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ export type Callback<U...> = (U...) -> ...any
101
+ export type ConnectFn<T, U...> = (callback: (U...) -> ...any) -> T
102
+ export type GenericTable = { [any]: any }
103
+
104
+ export type SignalLike<U...> =
105
+ {
106
+ connect: (self: SignalLike<U...>, Callback<U...>, ...any?) -> any,
107
+ [any]: any,
108
+ }
109
+ | {
110
+ Connect: (self: SignalLike<U...>, Callback<U...>, ...any?) -> any,
111
+ [any]: any,
112
+ }
113
+ | {
114
+ on: (self: SignalLike<U...>, Callback<U...>, ...any?) -> any,
115
+ [any]: any,
116
+ }
117
+ | {
118
+ On: (self: SignalLike<U...>, Callback<U...>, ...any?) -> any,
119
+ [any]: any,
120
+ }
121
+
122
+ type GetConnectFn<U...> =
123
+ -- RBXScriptSignal & nil
124
+ ((RBXScriptSignal<U...>) -> ConnectFn<RBXScriptConnection, U...>)
125
+ -- Instance & RBXScriptSignal
126
+ -- Instance & string
127
+ & ((
128
+ Instance,
129
+ RBXScriptSignal<U...> | string
130
+ ) -> ConnectFn<RBXScriptConnection, U...>)
131
+ -- SignalLike & nil
132
+ & ((SignalLike<U...>) -> ConnectFn<any, U...>)
133
+ -- table & string
134
+ & ((GenericTable, string) -> ConnectFn<any, U...>)
135
+ -- table & connectable method
136
+ & (<T>(
137
+ GenericTable,
138
+ (GenericTable, Callback<U...>, ...any) -> T
139
+ ) -> ConnectFn<T, U...>)
140
+ -- connectable function
141
+ & (<T>((Callback<U...>, ...any) -> T) -> ConnectFn<T, U...>)
142
+
143
+ -- This function is inspired by useEvent in Matter, a library by evaera (https://github.com/evaera)
144
+ -- License: Copyright (c) 2021 Eryn L. K., MIT License
145
+ -- Source: https://github.com/matter-ecs/matter/blob/main/lib/hooks/useEvent.luau
146
+ local function getConnectFunction(instance, event)
147
+ local eventInstance = instance
148
+
149
+ if typeof(event) == "RBXScriptSignal" or type(event) == "table" then
150
+ eventInstance = event
151
+ elseif type(event) == "string" then
152
+ eventInstance = (instance :: any)[event]
153
+ end
154
+
155
+ if type(eventInstance) == "function" then
156
+ return eventInstance
157
+ elseif typeof(eventInstance) == "RBXScriptSignal" then
158
+ return function(callback)
159
+ return eventInstance:Connect(callback)
160
+ end
161
+ end
162
+
163
+ if type(eventInstance) == "table" then
164
+ if type(event) == "function" then
165
+ return function(callback)
166
+ return event(eventInstance, callback)
167
+ end
168
+ end
169
+
170
+ for _, method in EVENT_CONNECT_METHODS do
171
+ if type(eventInstance[method]) ~= "function" then
172
+ continue
173
+ end
174
+
175
+ return function(callback)
176
+ return eventInstance[method](eventInstance, callback)
177
+ end
178
+ end
179
+ end
180
+
181
+ return nil
182
+ end
183
+
184
+ local function isValidEvent(instance, event)
185
+ return getConnectFunction(instance, event) ~= nil
186
+ end
187
+
188
+ return {
189
+ getSystem = getSystem,
190
+ getSystemName = getSystemName,
191
+ isPhase = isPhase,
192
+ isPipeline = isPipeline,
193
+ getEventIdentifier = getEventIdentifier,
194
+ isValidEvent = isValidEvent,
195
+ getConnectFunction = getConnectFunction :: GetConnectFn<...any>,
196
+ disconnectEvent = disconnectEvent,
197
+ }
package/out/Phase.d.ts DELETED
@@ -1,8 +0,0 @@
1
- declare class Phase {
2
- constructor(debugName?: string)
3
- static readonly PreStartup: Phase
4
- static readonly Startup: Phase
5
- static readonly PostStartup: Phase
6
- }
7
-
8
- export = Phase;
package/out/Pipeline.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import Phase from "./Phase";
2
-
3
- declare class Pipeline {
4
- constructor(debugName?: string)
5
- insert(phase: Phase) : Pipeline
6
- insertAfter(phase: Phase, after: Phase): Pipeline
7
- insertBefore(phase: Phase, before: Phase): Pipeline
8
- static readonly Startup: Pipeline
9
- }
10
-
11
- export = Pipeline;
@@ -1,31 +0,0 @@
1
- import Phase from "./Phase";
2
- import Pipeline from "./Pipeline";
3
- import { Hooks, EventInstance, EventLike, System, Plugin, HookFunctionMap, HookFunctionArgs } from "./types";
4
-
5
- declare class Scheduler<T extends unknown[]> {
6
- /** @hidden */
7
- _orderedPhases: Phase[]
8
- Hooks: Hooks["Hooks"]
9
- cleanup(): void
10
- addRunCondition(dependent: System<T>, fn: (...args: T) => boolean): Scheduler<T>
11
- replaceSystem(oldSystem: System<T>, newSystem: System<T>): Scheduler<T>
12
- removeSystem(system: System<T>): Scheduler<T>
13
- editSystem(system: System<T>, phase: Phase): Scheduler<T>
14
- addSystem(system: System<T>, phase?: Phase): Scheduler<T>
15
- addSystems(systems: System<T>[], phase?: Phase): Scheduler<T>
16
- insertBefore(dependent: Phase, before: Phase): Scheduler<T>
17
- insertBefore(dependent: Pipeline, before: Phase): Scheduler<T>
18
- insertAfter(phase: Phase, after: Phase | Pipeline): Scheduler<T>
19
- insertAfter(pipeline: Pipeline, after: Phase | Pipeline): Scheduler<T>
20
- insert(dependent: Phase | Pipeline, instance: EventInstance, event?: string): Scheduler<T>
21
- insert(dependent: Phase | Pipeline): Scheduler<T>
22
- runAll(): Scheduler<T>
23
- run(dependent: System<T> | Phase | Pipeline): Scheduler<T>
24
- getDeltaTime(): number
25
- addPlugin(plugin: Plugin): Scheduler<T>
26
- /** @hidden */
27
- _addHook<K extends keyof HookFunctionMap>(hook: K, fn: (info: HookFunctionArgs<K, T>) => void): void
28
- constructor(...args: T)
29
- }
30
-
31
- export = Scheduler;
@@ -1,14 +0,0 @@
1
- import { EventInstance, EventLike } from "./types";
2
-
3
- type Condition = () => boolean;
4
-
5
- declare const timePassed: (time: number) => Condition;
6
-
7
- declare const exported: {
8
- timePassed: (time: number) => Condition;
9
- runOnce: () => Condition;
10
- onEvent: <T extends unknown[]>(instance: EventInstance<T>, event?: string) => LuaTuple<[Condition, () => IterableFunction<LuaTuple<[number, ...T]>>, () => void]>;
11
- isNot: (condition: Condition) => Condition;
12
- cleanupCondition: (condition: Condition) => void
13
- }
14
- export = exported
package/out/hooks.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { Hooks } from "./types";
2
-
3
- declare const hooks : Hooks;
4
- export = hooks;
package/out/index.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import phase from "./Phase";
2
- import pipeline from "./Pipeline";
3
- import schedular from "./Scheduler";
4
- import condition from "./conditions"
5
-
6
- interface Planck {
7
- Phase: typeof phase;
8
- Pipeline: typeof pipeline;
9
- Scheduler: typeof schedular;
10
-
11
- isNot: typeof condition.isNot;
12
- timePassed: typeof condition.timePassed;
13
- runOnce: typeof condition.runOnce;
14
- onEvent: typeof condition.onEvent;
15
- }
16
-
17
- declare const planck: Planck;
18
- export = planck;
package/out/init.luau DELETED
@@ -1,143 +0,0 @@
1
- local Phase = require(script:FindFirstChild("Phase")) :: any
2
- local Pipeline = require(script:FindFirstChild("Pipeline")) :: any
3
- local Scheduler = require(script:FindFirstChild("Scheduler")) :: any
4
-
5
- local conditions = require(script:FindFirstChild("conditions"))
6
- local utils = require(script:FindFirstChild("utils"))
7
-
8
- type EventLike = utils.EventLike
9
- type EventInstance = utils.EventInstance
10
-
11
- export type SystemFn<U...> = ((U...) -> any) | ((U...) -> ())
12
-
13
- export type SystemTable<U...> = {
14
- system: SystemFn<U...>,
15
- phase: Phase?,
16
- name: string?,
17
- runConditions: { (U...) -> boolean }?,
18
- [any]: any,
19
- }
20
-
21
- export type System<U...> = SystemFn<U...> | SystemTable<U...>
22
-
23
- export type Phase = {
24
- PreStartup: Phase,
25
- Startup: Phase,
26
- PostStartup: Phase,
27
-
28
- new: (debugName: string?) -> Phase,
29
- }
30
-
31
- export type Pipeline = {
32
- Startup: Pipeline,
33
-
34
- insert: (self: Pipeline, phase: Phase) -> Pipeline,
35
- insertAfter: (self: Pipeline, phase: Phase, after: Phase) -> Pipeline,
36
- new: (debugName: string?) -> Pipeline,
37
- }
38
-
39
- type Plugin<U...> = {
40
- build: (self: Plugin<U...>, scheduler: Scheduler<U...>) -> (),
41
- cleanup: ((self: Plugin<U...>) -> ())?,
42
- [any]: any,
43
- }
44
-
45
- export type Scheduler<U...> = {
46
- addPlugin: (self: Scheduler<U...>, plugin: Plugin<U...>) -> Scheduler<U...>,
47
-
48
- addSystem: (
49
- self: Scheduler<U...>,
50
- system: System<U...>,
51
- phase: Phase?
52
- ) -> Scheduler<U...>,
53
-
54
- addSystems: (
55
- self: Scheduler<U...>,
56
- systems: { System<U...> },
57
- phase: Phase?
58
- ) -> Scheduler<U...>,
59
-
60
- editSystem: (
61
- self: Scheduler<U...>,
62
- system: System<U...>,
63
- new: Phase
64
- ) -> Scheduler<U...>,
65
-
66
- replaceSystem: (
67
- self: Scheduler<U...>,
68
- system: System<U...>,
69
- new: System<U...>
70
- ) -> Scheduler<U...>,
71
-
72
- removeSystem: (
73
- self: Scheduler<U...>,
74
- system: System<U...>
75
- ) -> Scheduler<U...>,
76
-
77
- addRunCondition: ((
78
- self: Scheduler<U...>,
79
- system: System<U...>,
80
- fn: (U...) -> boolean,
81
- ...any
82
- ) -> Scheduler<U...>) & ((
83
- self: Scheduler<U...>,
84
- phase: Phase,
85
- fn: (U...) -> boolean,
86
- ...any
87
- ) -> Scheduler<U...>) & ((
88
- self: Scheduler<U...>,
89
- pipeline: Pipeline,
90
- fn: (U...) -> boolean,
91
- ...any
92
- ) -> Scheduler<U...>),
93
-
94
- run: ((self: Scheduler<U...>, system: System<U...>) -> Scheduler<U...>)
95
- & ((self: Scheduler<U...>, phase: Phase) -> Scheduler<U...>)
96
- & ((self: Scheduler<U...>, pipeline: Pipeline) -> Scheduler<U...>),
97
-
98
- runAll: (self: Scheduler<U...>) -> Scheduler<U...>,
99
-
100
- getDeltaTime: (self: Scheduler<U...>) -> number,
101
-
102
- insert: ((self: Scheduler<U...>, phase: Phase) -> Scheduler<U...>) & ((
103
- self: Scheduler<U...>,
104
- pipeline: Pipeline
105
- ) -> Scheduler<U...>) & ((
106
- self: Scheduler<U...>,
107
- phase: Phase,
108
- instance: EventInstance | EventLike,
109
- event: string | EventLike
110
- ) -> Scheduler<U...>) & ((
111
- self: Scheduler<U...>,
112
- Pipeline: Pipeline,
113
- instance: EventInstance | EventLike,
114
- event: string | EventLike
115
- ) -> Scheduler<U...>),
116
-
117
- insertAfter: ((
118
- self: Scheduler<U...>,
119
- phase: Phase,
120
- after: Phase | Pipeline
121
- ) -> Scheduler<U...>) & ((
122
- self: Scheduler<U...>,
123
- pipeline: Pipeline,
124
- after: Phase | Pipeline
125
- ) -> Scheduler<U...>),
126
-
127
- cleanup: (self: Scheduler<U...>) -> (),
128
-
129
- new: (U...) -> Scheduler<U...>,
130
- }
131
-
132
- return {
133
- Phase = Phase :: Phase,
134
- Pipeline = Pipeline :: Pipeline,
135
- Scheduler = Scheduler :: {
136
- new: <U...>(U...) -> Scheduler<U...>,
137
- },
138
-
139
- isNot = conditions.isNot,
140
- runOnce = conditions.runOnce,
141
- timePassed = conditions.timePassed,
142
- onEvent = conditions.onEvent,
143
- }