Skip to content

Commit

Permalink
added tools for library creators to make use of
Browse files Browse the repository at this point in the history
  • Loading branch information
tinkerer-red committed Oct 13, 2024
1 parent b0722f4 commit 194ffb6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
43 changes: 40 additions & 3 deletions scripts/GMLPromise/GMLPromise.gml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ function Promise(_executor) constructor {

//note providing no start time will force the promise to execute all sub processes
static Execute = function(_time_to_live=infinity) {
static __global = __PromiseNamespace__.__global;

if (state == PROMISE_STATE.PAUSED) {
return;
}
Expand All @@ -285,7 +287,14 @@ function Promise(_executor) constructor {
var _j=0; repeat(array_length(executors)) {
var _executor = executors[0];
var _val = _executor(value);
array_delete(executors, 0, 1);

if (__global.postpone_task_removal) {
__global.postpone_task_removal = false;
}
else {
array_delete(executors, 0, 1);
}

value = (is_undefined(_val)) ? value : _val;

if (array_length(executors) == 0) {
Expand Down Expand Up @@ -344,6 +353,34 @@ function Promise(_executor) constructor {
return _promise;
}

//init statics and start timers

/////////////////////////////////
/// For Library and Tool creators
/////////////////////////////////

#region jsDoc
/// @func PromiseExceededFrameBudget()
/// @desc A helper function to assist library creators in knowing if the frame budget has been exceeded so they can exit their task.
/// @returns {Bool}
#endregion
function PromiseExceededFrameBudget() {
static __global = __PromiseNamespace__.__global
return (get_timer() >= __global.time_to_live);
}

#region jsDoc
/// @func PromisePostponeTaskRemoval()
/// @desc A helper function to assist library creators to have better control of when to remove a currently active task/executor. This is commonly used when you have a single function which can execute across multiple frames, but it's not finished and you wish to keep the currently running task in the queue to be executred next frame.
/// @returns {Bool}
#endregion
function PromisePostponeTaskRemoval() {
static __global = __PromiseNamespace__.__global
__global.postpone_task_removal = true;
}



#region init statics and start timers
new Promise();
time_source_start(__PromiseNamespace__.__global.async_obj_spawn_time_source);
time_source_start(__PromiseNamespace__.__global.async_obj_spawn_time_source);
#endregion
11 changes: 7 additions & 4 deletions scripts/__privPromise/__privPromise.gml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ function __PromiseNamespace__() {
promises_time_source : time_source_create(time_source_game, 1, time_source_units_frames, function() {
static __global = __PromiseNamespace__()
//handle all of the currently active processes
var _time_to_live = get_timer() + PROMISE_MAX_TIME;
__global.postpone_task_removal = false; //reset just incase someone is calling this outside of a promise function.
__global.time_to_live = get_timer() + PROMISE_MAX_TIME;

var _i=0; repeat(array_length(__global.active_promises)) {
var _promise = __global.active_promises[_i];

_promise.Execute(_time_to_live);
_promise.Execute(__global.time_to_live);

if (_promise.state == PROMISE_STATE.RESOLVED)
|| (_promise.state == PROMISE_STATE.REJECTED) {
Expand All @@ -19,7 +20,7 @@ function __PromiseNamespace__() {
}

//early out
if (get_timer() >= _time_to_live) {
if (get_timer() >= __global.time_to_live) {
break;
}

Expand All @@ -38,6 +39,8 @@ function __PromiseNamespace__() {
}, [], 1),
active_promises : [],
async_handlers: {},
time_to_live: get_timer() + PROMISE_MAX_TIME,
postpone_task_removal: false,
};

return __global;
Expand Down Expand Up @@ -78,7 +81,7 @@ function __registerAsyncHandler(_async_type, _async_id, _resolve_callback, _reje

if (_resolve_callback == undefined) _resolve_callback = function(){};

var _promise = Promise(_resolve_callback).Catch(_reject_callback);
var _promise = Promise.Finally(_resolve_callback).Catch(_reject_callback);
_promise.state = PROMISE_STATE.PAUSED;

var _resolve = method(_promise, function(_async_load){
Expand Down

0 comments on commit 194ffb6

Please sign in to comment.