wrapFunction fixes and "importance" parameter #35

Merged
e016 merged 1 commit from important-flag into main 2026-05-20 16:04:15 -05:00
e016 commented 2026-05-10 17:03:43 -05:00 (Migrated from github.com)

I'll also add a split-blocks addon to the sparkleaddons repository soon

I'll also add a split-blocks addon to the sparkleaddons repository soon
codingisfun2831t (Migrated from github.com) reviewed 2026-05-10 17:03:43 -05:00
Bubgamer07 (Migrated from github.com) reviewed 2026-05-10 17:03:43 -05:00
PPPDUD commented 2026-05-11 08:26:54 -05:00 (Migrated from github.com)

Will try to review soon, thanks!

Will try to review soon, thanks!
PPPDUD commented 2026-05-19 18:07:04 -05:00 (Migrated from github.com)

@e016 I don't think that I'll have the time to review this soon. @codingisfun2831t @Bubgamer07 Would one of you all please review the changes and merge if it looks good to you?

@e016 I don't think that I'll have the time to review this soon. @codingisfun2831t @Bubgamer07 Would one of you all please review the changes and merge if it looks good to you?
codingisfun2831t commented 2026-05-19 18:09:21 -05:00 (Migrated from github.com)

I've wrote this little new wrapFunction implementation, that should allow mods to do more. Maybe the important parameter should still be implemented in some way?

static OBJ_FUNCS_HOOKS = Symbol("Function Hooks")

    wrapFunction(object, name, callback, type = "after") {
        if (typeof object[name] !== 'function') {
            throw new Error("Not a function or doesnt exist.");
        }

        if (!object[OBJ_FUNCS_HOOKS]) {
            object[OBJ_FUNCS_HOOKS] = new Map();
        }

        let objHooks = object[OBJ_FUNCS_HOOKS]
        let isNew = false;
        if (!objHooks.has(name)) {
            objHooks.set(name, { before: [], instead: null, after: [], original: object[name] })
            isNew = true;
        }

        let hooks = objHooks.get(name)

        if (type == "instead") {
            hooks.instead = callback;
        } else {
            hooks[type].push(callback);
        }

        if (isNew) {
            object[name] = function (...args) {
                const context = {
                    args: args,
                    ret: undefined,
                    cancel: false,
                    target: this
                }

                // BEFORE
                for (const hook of hooks.before) {
                    hook(context);
                    if (context.cancel) return context.ret;
                }

                // INSTEAD / ORIGINAL
                let result;
                if (hooks.instead) {
                    result = hooks.instead(context, hooks.original.bind(context.target));
                } else {
                    result = hooks.original.apply(context.target, context.args);
                }

                if (context.ret === undefined) context.ret = result;

                // AFTER
                for (const hook of hooks.after) {
                    hook(context);
                }

                return context.ret;
            }
        }
    }
I've wrote this little new wrapFunction implementation, that should allow mods to do more. Maybe the important parameter should still be implemented in some way? ```javascript static OBJ_FUNCS_HOOKS = Symbol("Function Hooks") wrapFunction(object, name, callback, type = "after") { if (typeof object[name] !== 'function') { throw new Error("Not a function or doesnt exist."); } if (!object[OBJ_FUNCS_HOOKS]) { object[OBJ_FUNCS_HOOKS] = new Map(); } let objHooks = object[OBJ_FUNCS_HOOKS] let isNew = false; if (!objHooks.has(name)) { objHooks.set(name, { before: [], instead: null, after: [], original: object[name] }) isNew = true; } let hooks = objHooks.get(name) if (type == "instead") { hooks.instead = callback; } else { hooks[type].push(callback); } if (isNew) { object[name] = function (...args) { const context = { args: args, ret: undefined, cancel: false, target: this } // BEFORE for (const hook of hooks.before) { hook(context); if (context.cancel) return context.ret; } // INSTEAD / ORIGINAL let result; if (hooks.instead) { result = hooks.instead(context, hooks.original.bind(context.target)); } else { result = hooks.original.apply(context.target, context.args); } if (context.ret === undefined) context.ret = result; // AFTER for (const hook of hooks.after) { hook(context); } return context.ret; } } } ```
PPPDUD commented 2026-05-19 18:10:47 -05:00 (Migrated from github.com)

I've wrote this little new wrapFunction implementation, that should allow mods to do more. Maybe the important parameter should still be implemented in some way?

static OBJ_FUNCS_HOOKS = Symbol("Function Hooks")

    wrapFunction(object, name, callback, type = "after") {
        if (typeof object[name] !== 'function') {
            throw new Error("Not a function or doesnt exist.");
        }

        if (!object[OBJ_FUNCS_HOOKS]) {
            object[OBJ_FUNCS_HOOKS] = new Map();
        }

        let objHooks = object[OBJ_FUNCS_HOOKS]
        let isNew = false;
        if (!objHooks.has(name)) {
            objHooks.set(name, { before: [], instead: null, after: [], original: object[name] })
            isNew = true;
        }

        let hooks = objHooks.get(name)

        if (type == "instead") {
            hooks.instead = callback;
        } else {
            hooks[type].push(callback);
        }

        if (isNew) {
            object[name] = function (...args) {
                const context = {
                    args: args,
                    ret: undefined,
                    cancel: false,
                    target: this
                }

                // BEFORE
                for (const hook of hooks.before) {
                    hook(context);
                    if (context.cancel) return context.ret;
                }

                // INSTEAD / ORIGINAL
                let result;
                if (hooks.instead) {
                    result = hooks.instead(context, hooks.original.bind(context.target));
                } else {
                    result = hooks.original.apply(context.target, context.args);
                }

                if (context.ret === undefined) context.ret = result;

                // AFTER
                for (const hook of hooks.after) {
                    hook(context);
                }

                return context.ret;
            }
        }
    }

Nice! FYI, you forgot an apostrophe in throw new Error("Not a function or doesnt exist.");.

> I've wrote this little new wrapFunction implementation, that should allow mods to do more. Maybe the important parameter should still be implemented in some way? > > ```js > static OBJ_FUNCS_HOOKS = Symbol("Function Hooks") > > wrapFunction(object, name, callback, type = "after") { > if (typeof object[name] !== 'function') { > throw new Error("Not a function or doesnt exist."); > } > > if (!object[OBJ_FUNCS_HOOKS]) { > object[OBJ_FUNCS_HOOKS] = new Map(); > } > > let objHooks = object[OBJ_FUNCS_HOOKS] > let isNew = false; > if (!objHooks.has(name)) { > objHooks.set(name, { before: [], instead: null, after: [], original: object[name] }) > isNew = true; > } > > let hooks = objHooks.get(name) > > if (type == "instead") { > hooks.instead = callback; > } else { > hooks[type].push(callback); > } > > if (isNew) { > object[name] = function (...args) { > const context = { > args: args, > ret: undefined, > cancel: false, > target: this > } > > // BEFORE > for (const hook of hooks.before) { > hook(context); > if (context.cancel) return context.ret; > } > > // INSTEAD / ORIGINAL > let result; > if (hooks.instead) { > result = hooks.instead(context, hooks.original.bind(context.target)); > } else { > result = hooks.original.apply(context.target, context.args); > } > > if (context.ret === undefined) context.ret = result; > > // AFTER > for (const hook of hooks.after) { > hook(context); > } > > return context.ret; > } > } > } > ``` Nice! FYI, you forgot an apostrophe in `throw new Error("Not a function or doesnt exist.");`.
codingisfun2831t commented 2026-05-20 08:31:41 -05:00 (Migrated from github.com)

Ill fix that. May I add that to my sparkle class PR or somewhere else?

Ill fix that. May I add that to my sparkle class PR or somewhere else?
PPPDUD commented 2026-05-20 09:13:08 -05:00 (Migrated from github.com)

Ill fix that. May I add that to my sparkle class PR or somewhere else?

You can put it in the class PR, go ahead.

> Ill fix that. May I add that to my sparkle class PR or somewhere else? You can put it in the class PR, go ahead.
PPPDUD commented 2026-05-20 16:04:07 -05:00 (Migrated from github.com)

Merging to reduce development delays. If there are any bugs, please open an issue as I haven't reviewed these changes properly yet.

Merging to reduce development delays. If there are any bugs, please open an issue as I haven't reviewed these changes properly yet.
Commenting is not possible because the repository is archived.
No description provided.