tc39 / proposal-function-once

A TC39 proposal for an Function.prototype.once method in the JavaScript language.
BSD 3-Clause "New" or "Revised" License
46 stars 3 forks source link

Web compatibility #4

Open zloirock opened 2 years ago

zloirock commented 2 years ago

As I already wrote, Function.prototype.once is available in SugarJS (and IIRC in some other libraries). At the current meeting, it was decided to rename Array.prototype.groupBy because of a similar conflict with SugarJS. Because of this, I see 2 options for avoid this possible conflict:

js-choi commented 2 years ago

Thanks for raising this issue.

From what I recall, SugarJS stopped conditionally monkey-patching Function.prototype around 2016 (see also Bugzilla 1750812). So this partially depends on whether there are any codebases still in use today that use that old version of SugarJS. (As long as the monkey-patching is not conditional, then it’s okay, but the old version of SugarJS did conditionally monkey-patch, which is why Array.prototype.groupBy broke code that used the old version. 😔)

We will have to determine whether that old version of SugarJS also monkey-patched in Function.prototype.once.

We will also have to closely analyze Sugar.js’s Function.prototype.once’s behavior and determine which of the options in #2 it matches. It seems like it does cache results, but there are other subtleties that we will have to confirm…

And, yes, we could always use another method name; see #1.

I will be presenting this tomorrow, and I have added this unfortunate issue to my slides. I will also fix the explainer’s section when I have time.

hax commented 2 years ago

I assume static Function.once do not have webcompt issue?

keithamus commented 2 years ago

Some other implementations in the wild. I don't think any of these will cause problems with webcompat.

zloirock commented 2 years ago

@hax SugarJS - only in case that no one used it as a shortcut for Function.prototype.once.

A simple search on GitHub shows many cases usage of static Function.once like this.

js-choi commented 2 years ago

What matters is if the monkey patching of Function (or Function.prototype) is conditional. Unconditional monkey patching won’t be affected by extending the built-in. At least V4Core’s monkey patching does not seem to conditional, so that’s good.

hax commented 2 years ago

One possible solution of webcomp is using meta method syntax, function.once or do.once ( do.once may be not good for this proposal, but do.pipe seems ok for Function.pipe proposal.)

ljharb commented 2 years ago

that would be pretty annoying to be forced to wrap it in a function in order to pass it elsewhere; it’d also make it impossible to polyfill.

hax commented 2 years ago

Yeah, so I think Function.xxx should be the first choice for helpers proposals.

But if we are considering some syntax proposal, for example, extensions or call-this proposal, we could consider something like value->do.pipe(f) (or value->let(f) if use Kotlin-style name).

js-choi commented 2 years ago

But if we are considering some syntax proposal, for example, extensions or call-this proposal, we could consider something like value->do.pipe(f) (or value->let(f) if use Kotlin-style name).

Although I applaud the creativity; this proposal is for a tiny convenience function. I think it doesn’t deserve the huge complexity burden of new syntax. 🙂

There are always alternative names if the pre-2016-SugarJS problem ends up being insurmountable, even if they are worse than once. Like oneTime and onlyOnce and singleTime and limitOne.