node-callable-instance icon indicating copy to clipboard operation
node-callable-instance copied to clipboard

ES6 classes and Advanced Typings

Open pabadm opened this issue 2 years ago • 6 comments

Hello. I decided to remake implementation of your callable-instance. (Switched to ES6 class syntax and ES6 Symbols).

  1. Now Callable also accepts its Symbol for CALL method. (old propertyName in constructor also works)
  2. Does not copy length because CALL method can be overwritten. (has length: 0 like all ((...args) => any) functions
  3. Does not forward function name. Instead forwards constructor.name
  4. Made advanced typings to easily forward property and extend CreatedClass
  5. Built index.js using ts-compiler

If you don`t mind Accepting my pull request, let me know first so I can create Readme.

pabadm avatar Sep 21 '23 21:09 pabadm

Thanks for your interest in contributing! Wow, this completely rewrites the package. I'm not opposed to accepting, but there is a laundry list of changes I'd like to see before doing so 🙂

Overall questions/comments:

  1. Where does your interest in this package come from? Are you using this package in production and find it limiting?
  2. Let's please keep the dist directory in .gitignore.
  3. What is OverrideCall?
  4. Can you update .github/workflows/node.js.yml to test [16.x, 18.x, 20.x, 22.x, 24.x]? That should fix the CI as well.
  5. Please run Prettier on all the files (default config).

Then there are some inline comments.

CGamesPlay avatar Sep 26 '23 23:09 CGamesPlay

Interest came from idea of callable objects in JS. I decided to change implementation because of setPrototypeOf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf. My variant is inspired by this article (the bind way) https://medium.com/@adrien.za/creating-callable-objects-in-javascript-fbf88db9904c. I decided to make something like OverrideCall because of typescript limitations. With OverrideCall its easy to change type of Call signature if you extend Callable class. For Example MyCallable extends Callable. MyNewClass extends MyCallable. So you can write MyNewClass extends (MyCallable as OverrideCall) Also i thought of keeping this package for legacy pre ES5 implementation with updated types and posting other package es6-callable with ES6 syntax.

pabadm avatar Sep 28 '23 15:09 pabadm

Everything in dist is still in the repo. You ignored only future files. You'll need to remove the files and commit the removals as well.

I decided to make something like OverrideCall because of typescript limitations. With OverrideCall its easy to change type of Call signature if you extend Callable class. For Example MyCallable extends Callable. MyNewClass extends MyCallable. So you can write MyNewClass extends (MyCallable as OverrideCall)

Got it. An example would be very useful in the README.

Also i thought of keeping this package for legacy pre ES5 implementation with updated types and posting other package es6-callable with ES6 syntax.

This change will be a new major version, so we can have a note that says that ES5-compatible implementation lives in ~2.0.0.

Checks look good, please run Prettier on the source files and update the README as you wanted to. I'll proofread the README and then we can publish this as version 3.0.0.

CGamesPlay avatar Oct 03 '23 23:10 CGamesPlay

apparently adding my top-level comment left all these in pending, apologies.

Will be finished. just dont have time now. I will comment when module is ready

pabadm avatar Oct 04 '23 13:10 pabadm

Now it's possible to make regular JS objects callable. Also bind, apply, call now works the same as function's bind, apply, call. Also removed build and made module.exports for Callable class.

pabadm avatar Oct 27 '23 16:10 pabadm

Hello, also apologies for the delay. I recently found out that CSP blocks Function constructor. So I decided to stick up with your implementation and optimised it a lot using WeakMap (setPrototypeOf called only once per class creation). Module works mostly as earlier except the bind method. I decided to make an alias to the regular Function's bind because binding of CallableObject returns CallableObject without properties but with its prototype (may be confusing for a lot of people).

UPD: Simplified code with new.target.prototype. Basically I combined the bind and prototype way from medium article. So now it must be both performant and CSP friendly. (Prototype is set only once and bind is used for prototyped function cloning)

Readme is not that necessary for me but you can add me to contributors/collaborators if you want.

pabadm avatar Dec 03 '23 16:12 pabadm