Skip to content

Commit

Permalink
šŸ› Make Blast.executeAfterLoadingCycle() keep did_initial_load varā€¦
Browse files Browse the repository at this point in the history
ā€¦iable alone
  • Loading branch information
skerit committed Feb 8, 2024
1 parent cf0ee27 commit 607a8d6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Add `WeakValueSetMap` class
* Make `Blast.createObjectId()` also available on the server-side
* Add `toISOString` method to `AbstractDateTime` classes
* Add `Blast.didInitialLoad()` method
* Make `Blast.executeAfterLoadingCycle()` keep `did_initial_load` variable alone

## 0.8.18 (2024-01-19)

Expand Down
29 changes: 24 additions & 5 deletions lib/function_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ Blast.defineStatic('Function', function inherits(_parent, _namespace, _newConstr
// Add the parent constitutors to the new constructor
if (newConstructor.super.constitutors != null) {
for (i = 0; i < newConstructor.super.constitutors.length; i++) {
newConstructor.constitute(newConstructor.super.constitutors[i]);
addConstructorConstitutor(newConstructor, newConstructor.super.constitutors[i], true);
}
};

Expand All @@ -674,7 +674,7 @@ Blast.defineStatic('Function', function inherits(_parent, _namespace, _newConstr
}

for (i = 0; i < oldProto.waitingConstitute.length; i++) {
newConstructor.constitute(oldProto.waitingConstitute[i]);
addConstructorConstitutor(newConstructor, oldProto.waitingConstitute[i], true);
}

doPostInheritors(newConstructor);
Expand Down Expand Up @@ -1235,7 +1235,7 @@ defClassMethod(function postInherit(constructor, task) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.1.4
* @version 0.8.18
* @version 0.9.0
*
* @param {Function} constructor
* @param {Function} task
Expand All @@ -1247,6 +1247,21 @@ defClassMethod(function constitute(constructor, task) {
return;
}

addConstructorConstitutor(constructor, task, false);
});

/**
* Actually add and maybe execute the constitutor to the given constructor
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.0
* @version 0.9.0
*
* @param {Function} constructor
* @param {Function} task
*/
function addConstructorConstitutor(constructor, task, after_wait = false) {

let tasks = constructor.constitutors;

if (tasks == null) {
Expand All @@ -1269,6 +1284,10 @@ defClassMethod(function constitute(constructor, task) {
doConstructorTask(constructor, task, finished_constitutors);
});
}
} else if (Blast.didInitialLoad()) {
Blast.loaded(() => {
doConstructorTask(constructor, task, finished_constitutors);
});
}

// If this class already has children, add them there too
Expand All @@ -1277,11 +1296,11 @@ defClassMethod(function constitute(constructor, task) {

for (child of constructor.children) {
if (!child.constitutors || child.constitutors.indexOf(task) == -1) {
constitute(child, task);
child.constitute(task);
}
}
}
});
}

/**
* Add a class instance as a static property and add static traits
Expand Down
42 changes: 33 additions & 9 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,11 +991,12 @@ function BlastInit(modifyPrototype) {
require('./server_functions.js')(Blast, extras);
//PROTOBLAST END CUT

var when_ready = [],
let when_ready = [],
when_loaded = [],
queued_meds = [],
queued_ticks = [],
did_initial_load = false;
did_initial_load = false,
temporarily_unready = false;

/**
* Execute function after Blast code has run.
Expand Down Expand Up @@ -1122,7 +1123,7 @@ function BlastInit(modifyPrototype) {
* @return {Boolean} Returns if Protoblast is already ready
*/
Blast.ready = function ready(fnc) {
if (when_ready) {
if (when_ready || temporarily_unready) {

if (fnc) {
when_ready.push(fnc);
Expand Down Expand Up @@ -1151,7 +1152,7 @@ function BlastInit(modifyPrototype) {
* @return {Boolean} Returns if Protoblast has already loaded
*/
Blast.loaded = function loaded(fnc) {
if (!did_initial_load) {
if (!did_initial_load || temporarily_unready) {

if (fnc) {
when_loaded.push(fnc);
Expand All @@ -1168,6 +1169,28 @@ function BlastInit(modifyPrototype) {
}
};

/**
* Did the initial load already happen?
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.0
* @version 0.9.0
*
* @return {boolean}
*/
Blast.didInitialLoad = () => did_initial_load;

/**
* Are we currently in a manual loading cycle?
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.0
* @version 0.9.0
*
* @return {boolean}
*/
Blast.inLoadingCycle = () => temporarily_unready;

/**
* Force Protoblast into executing the when_loaded functions
*
Expand All @@ -1177,17 +1200,18 @@ function BlastInit(modifyPrototype) {
*/
Blast.doLoaded = function doLoaded() {

if (!did_initial_load) {
if (!did_initial_load || temporarily_unready) {
Blast.loading = true;
}

doQueuedTicks();
clearAndDoTasks(queued_meds);
clearAndDoTasks(when_loaded);

if (!did_initial_load) {
if (!did_initial_load || temporarily_unready) {
Blast.loading = false;
did_initial_load = true;
temporarily_unready = false;
}
};

Expand All @@ -1202,14 +1226,14 @@ function BlastInit(modifyPrototype) {
*/
Blast.executeAfterLoadingCycle = function executeAfterLoadingCycle(task) {

if (!did_initial_load) {
if (!did_initial_load || temporarily_unready) {
Blast.loaded(task);
return;
}

did_initial_load = false;
temporarily_unready = true;
Blast.loaded(task);

Blast.setImmediate(() => {
Blast.doLoaded();
});
Expand Down
42 changes: 31 additions & 11 deletions test/function_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ describe('Inheritance', function() {
// The grandchild will only have returnTwo on the next tick
assert.strictEqual(Grandchild.returnTwo(), 2);

assert.strictEqual(order[0], 'created-parent');
assert.strictEqual(order[1], 'setting-returntwo-on-parent');
assert.strictEqual(order[2], 'constituting-grandchild');
assert.deepStrictEqual(order, [
'created-parent',
'setting-returntwo-on-parent',
'constituting-grandchild'
]);

done();
}, 20)
Expand Down Expand Up @@ -572,25 +574,25 @@ describe('Inheritance', function() {
CTTwo = Blast.Bound.Function.inherits('CTOne', function CTTwo() {});
CTTwo.constitute(function doThird() {
this.third_time = i++;
checker();
checker(this, 'CTTwo: doThird');
});

// This is the main class
setTimeout(function() {
CTOne = Blast.Bound.Function.inherits(function CTOne() {});
CTOne.constitute(function doFirst() {
this.first_time = i++;
checker();
checker(this, 'CTOne: doFirst');
});

CTOne.constitute(function doSecond() {
this.second_time = i++;
checker();
checker(this, 'CTOne: doSecond');
});
}, 10);

// This will check if everything is happening in the correct order
function checker() {
function checker(constructor, creator) {

if (i == 1) {
assert.equal(CTOne.first_time, 0);
Expand Down Expand Up @@ -641,6 +643,9 @@ describe('Inheritance', function() {
tasks = [],
i = 0;

let current_source,
current_class;

// This is the main class
DTOne = Blast.Bound.Function.inherits(function DTOne() {});
DTOne.constitute(function doFirst() {
Expand All @@ -663,7 +668,8 @@ describe('Inheritance', function() {
});

// This is another, non-related class
// This should run AFTER DTThree
// This originall ran AFTER DTThree,
// but since 2024 it will run before it.
DTOther = Blast.Bound.Function.inherits(function DTOther() {});
DTOther.constitute(function doAfterThree() {
this.fourth_time = i++;
Expand All @@ -677,9 +683,6 @@ describe('Inheritance', function() {
}
});

let current_source,
current_class;

// This will check if everything is happening in the correct order
function checker(source, _current_class) {

Expand Down Expand Up @@ -810,6 +813,23 @@ describe('Inheritance', function() {
}
}
});

it('should handle constitutors added during a loading cycle', async () => {

let Base = Function.inherits('Informer', 'Delayed', function Base() {});
Base.constitute(() => {});

let Alpha = Function.inherits('Delayed.Base', function Alpha() {});
let Beta = Function.inherits('Delayed.Base', function Beta() {});

return Function.parallel((next) => {
Alpha.constitute(() => {
Blast.executeAfterLoadingCycle(next);
});
}, (next) => {
Beta.constitute(() => next());
}, () => {});
});
});

describe('.postInherit(task)', () => {
Expand Down

0 comments on commit 607a8d6

Please sign in to comment.