Skip to content

Commit b1100d0

Browse files
committed
Move ProtonStart lifecycle
1 parent d40e123 commit b1100d0

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rbxts/proton",
3-
"version": "0.5.8",
3+
"version": "0.5.9",
44
"description": "Framework for Roblox game development",
55
"main": "out/init.lua",
66
"scripts": {

src/core.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { LifecycleBehavior, ProtonLifecycle } from "./lifecycle";
2-
31
const providerClasses = new Map<new () => unknown, unknown>();
42

53
let started = false;
64
const awaitStartThreads: thread[] = [];
7-
8-
export const ProtonStart = new ProtonLifecycle<() => void>(LifecycleBehavior.Concurrent);
5+
const awaitCallbacks: (() => void)[] = [];
96

107
/**
118
* Provider decorator.
@@ -36,8 +33,10 @@ export namespace Proton {
3633
*/
3734
export function start() {
3835
if (started) return;
39-
ProtonStart.fire();
4036
started = true;
37+
for (const callback of awaitCallbacks) {
38+
task.spawn(callback);
39+
}
4140
for (const awaitThread of awaitStartThreads) {
4241
task.spawn(awaitThread);
4342
}
@@ -62,6 +61,20 @@ export namespace Proton {
6261
coroutine.yield();
6362
}
6463

64+
/**
65+
* Calls the callback once Proton has fully started.
66+
* If Proton is already started, the callback will
67+
* be spawned immediately.
68+
* @param callback Callback
69+
*/
70+
export function onStart(callback: () => void) {
71+
if (started) {
72+
task.spawn(callback);
73+
return;
74+
}
75+
awaitCallbacks.push(callback);
76+
}
77+
6578
/**
6679
* Gets a provider within Proton.
6780
*

src/lifecycle.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Proton, ProtonStart, Provider } from "./core";
1+
import { Proton } from "./core";
22

33
type LifecycleCallback<T> = (...args: Parameters<T>) => void;
44

@@ -160,3 +160,6 @@ export function Lifecycle<T extends LifecycleCallback<T>>(lifecycle: ProtonLifec
160160
);
161161
};
162162
}
163+
164+
export const ProtonStart = new ProtonLifecycle<() => void>(LifecycleBehavior.Concurrent);
165+
Proton.onStart(() => ProtonStart.fire());

0 commit comments

Comments
 (0)