Skip to content

Commit

Permalink
Prommy simplification!
Browse files Browse the repository at this point in the history
  • Loading branch information
hubol committed Sep 5, 2024
1 parent 06b0401 commit 4617d57
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 255 deletions.
30 changes: 26 additions & 4 deletions esbuild.prommy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,34 @@ const Ts = {
* @param {import("typescript").Node} node
*/
function isFunctionDeclarationReturningPromise(node) {
if (isFromNodeModules(node.parent?.expression))
return false;

if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node))
return doesNodeReturnPromise(node);

return false;
}

/**
*
* @param {import("typescript").Node} node
*/
function isFromNodeModules(node) {
if (!node)
return false;
const declarations = Ts.checker.getSymbolAtLocation(node)?.getDeclarations();

if (!declarations)
return false;

for (const declaration of declarations) {
if (declaration.getSourceFile().fileName.includes('node_modules'))
return true;
}
return false;
}

/**
*
* @param {import("typescript").Node} node
Expand Down Expand Up @@ -187,9 +209,9 @@ const promiseMethods = new Set([ 'then', 'catch', 'finally' ]);
*
* @param {import("typescript").Node} node
*/
function isInvocationOfFunctionThatReturnsPromise(node) {
if (ts.isCallExpression(node)) {
const expressionSymbol = Ts.checker.getTypeAtLocation(node.expression).symbol;
function isInvocationOfHubolMadeFunctionThatReturnsPromise(node) {
if (ts.isCallExpression(node) && !isFromNodeModules(node.expression)) {
const expressionSymbol = Ts.checker.getSymbolAtLocation(node.expression);

if (promiseMethods.has(expressionSymbol?.name))
return false;
Expand Down Expand Up @@ -276,7 +298,7 @@ const transformSourceFile = (context) => (sourceFile) => {
* @param {import("typescript").Node} node
*/
function visitor(node) {
if (isInvocationOfFunctionThatReturnsPromise(node)) {
if (isInvocationOfHubolMadeFunctionThatReturnsPromise(node)) {
return ts.visitEachChild(createCallExpressionWithContextParameter(factory, node), visitor, context);
}

Expand Down
3 changes: 1 addition & 2 deletions src/igua/core/scene/obj-solid-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BLEND_MODES, Container, Graphics } from "pixi.js";
import { Undefined } from "../../../lib/types/undefined";
import { lerp } from "../../../lib/game-engine/promise/lerp";
import { renderer } from "../../globals";
import { Prommy } from "../../../lib/zone/prommy";

export function objSolidOverlay() {
let dirty = true;
Expand All @@ -17,7 +16,7 @@ export function objSolidOverlay() {
console.log('c.destroy()');
})
.show(g);
return new Prommy<void>(r => container!.once('destroyed', r));
return new Promise<void>(r => container!.once('destroyed', r));
}

const g = new Graphics()
Expand Down
12 changes: 7 additions & 5 deletions src/igua/objects/obj-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ async function myAsyncFunction(value?) {
}

export function objTest() {
const b = myAsyncFunction;
const data = {} as ImageData;
const b = createImageBitmap;

return container()
.async(async () => {
await wait(() => true);
await myAsyncFunction().then(myAsyncFunction); // Tricky case!
await myAsyncFunction().then(b); // Also tricky case
await Promise.all([myAsyncFunction()]);
// await createImageBitmap(data);
// await wait(() => true);
// await myAsyncFunction().then(myAsyncFunction); // Tricky case!
await myAsyncFunction().then(() => b(data)); // Also tricky case
// await Promise.all([myAsyncFunction()]);
})
}
2 changes: 0 additions & 2 deletions src/igua/objects/obj-water-drip-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Rng } from "../../lib/math/rng";
import { RpgAttack } from "../rpg/rpg-attack";
import { RpgFaction } from "../rpg/rpg-faction";
import { mxnRpgAttack } from "../mixins/mxn-rpg-attack";
import { PrommyContext } from "../../lib/zone/prommy";

interface ObjWaterDripSourceArgs {
delayMin: number;
Expand All @@ -20,7 +19,6 @@ export function objWaterDripSource({ delayMin, delayMax }: ObjWaterDripSourceArg
.merge({ poison: false })
.async(async self => {
while (true) {
console.log('asyncWaterDripSource', PrommyContext.current().Name);
await sleep(Rng.intc(delayMin, delayMax));
objWaterDrip(self.poison ? atkPoisonDrip : atkDrip).at(self).show(self.parent);
}
Expand Down
4 changes: 0 additions & 4 deletions src/igua/ui/iguana-designer/obj-ui-iguana-designer-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { Toast } from "../../../lib/game-engine/toast";
import { merge } from "../../../lib/object/merge";
import { ClipboardPojo } from "../../../lib/browser/clipboard-pojo";
import { clone } from "../../../lib/object/clone";
import { PrommyContext } from "../../../lib/zone/prommy";

function context() {
let looks = getDefaultLooks();
Expand Down Expand Up @@ -184,12 +183,9 @@ function objUiSavePage() {
await layers.overlay.solid.fadeIn(500);
const looks = UiIguanaDesignerContext.value.looks;
RpgProgress.character.looks = looks;
console.log('after fadein', PrommyContext.current());
sceneStack.replace(scnPlayerTest, { useGameplay: false });
page.destroy();
await layers.overlay.solid.fadeOut(500);
console.log('before fadeout', PrommyContext.current());
console.log('fadeOut done');
});
}, width).jiggle().center().at(gap, 160);

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import "./lib/zone/prommy";
import { PrommyRuntime } from "./lib/zone/prommy";

PrommyRuntime.install();

import { loadLaunchAssets } from "./igua/launch/load-launch-assets";
import { showLoadingScreen } from "./igua/launch/show-loading-screen";
import { integralUpscaleCanvas } from "./lib/browser/integral-upscale-canvas";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/extensions/pixi-displayobject-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface DisplayObjectPrivate {

Object.defineProperties(DisplayObject.prototype, {
async: {
value: function (this: DisplayObject & DisplayObjectPrivate, asyncFn: (self?: any) => Promise<unknown>) {
value: function (this: DisplayObject & DisplayObjectPrivate, asyncFn: (self?: any, c?: any) => unknown) {
if (asyncFn.length)
asyncFn = asyncFn.bind(null, this);
asyncFn = asyncFn.bind(null, this, this);

this.cancellationToken;

Expand Down
32 changes: 7 additions & 25 deletions src/lib/game-engine/asshat-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { CancellationError, CancellationToken } from "../promise/cancellation-to
import { AsshatMicrotaskFactory } from "./promise/asshat-microtasks";
import { IAsshatTicker } from "./asshat-ticker";
import { ErrorReporter } from "./error-reporter";
import { EngineConfig } from "./engine-config";
import { Prommy, PrommyContext } from "../zone/prommy";

interface AsshatZoneContext {
cancellationToken: CancellationToken;
Expand All @@ -18,29 +16,13 @@ class AsshatZoneImpl {
console.log(...Logging.componentArgs(this));
}

get context() {
const context = PrommyContext.current();
if (!context) {
ErrorReporter.reportDevOnlyState(new Error('AsshatZone.context was falsy, using EngineConfig.showDefaultStage'));
return EngineConfig.showDefaultStage as any;
}

return context;
}

run(fn: () => unknown, context: AsshatZoneContext) {
Prommy.createRoot<void>(async () => {
try {
await new Prommy<void>((resolve, reject) => {
const microtask = AsshatMicrotaskFactory.create(alwaysPredicate, context, resolve as any, reject);
context.ticker.addMicrotask(microtask);
});
await fn();
}
catch (e) {
handleAsshatZoneError(e);
}
}, context)
run(fn: () => unknown, $c: AsshatZoneContext) {
new Promise<void>((resolve, reject) => {
const microtask = AsshatMicrotaskFactory.create(alwaysPredicate, $c, resolve as any, reject);
$c.ticker.addMicrotask(microtask);
})
.then(fn)
.catch(handleAsshatZoneError)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/game-engine/promise/sleep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { wait } from "./wait";

export function sleep(ms: number) {
if (ms <= 0)
return;
return Promise.resolve();

// TODO Fixed FPS
// 60frames / 1000ms
Expand Down
8 changes: 3 additions & 5 deletions src/lib/game-engine/promise/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { AsshatZone } from "../asshat-zone";

type Predicate = () => boolean;

export function wait(predicate: Predicate) {
export function wait(predicate: Predicate, $c?) {
if (predicate())
return Promise.resolve();

const context = AsshatZone.context;

return new Promise<void>((resolve, reject) => {
const microtask = AsshatMicrotaskFactory.create(predicate, context, resolve, reject);
context.ticker.addMicrotask(microtask);
const microtask = AsshatMicrotaskFactory.create(predicate, $c, resolve, reject);
$c.ticker.addMicrotask(microtask);
});
}
Loading

0 comments on commit 4617d57

Please sign in to comment.