question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable and promote code reuse the fp way

See original GitHub issue

Related to #815

The wrapper pattern

To reuse swal configuration, the promoted way is currently to use swal.setDefaults, but that method

  1. stores configuration in global state, often making it harder to determine (by reading code) what defaults will be used
  2. only supports having one configuration at a time
  3. cannot merge configurations intelligently/dynamically. For example, if your reusable configuration uses onOpen and your instance configuration does too, the instance’s onOpen will take precedence and the reusable configuration will be effectively broken.

A better way is to use mixins, e.g. swal({ ...myPromptOptions, title: 'whats your name?' }) and swal({ ...myToastOptions, title: 'this is a toast' }). This does not have the 1st or 2nd issues from above, but it doesn’t help with the 3rd. Also this method is a bit verbose and not ergonomic in environments without object spread syntax (which is not yet implemented in Edge or Safari) where it looks more like swal(Object.assign({}, myPromptOptions, title: 'whats your name?' }).

Finally, if you want or need real extensibility, it is possible (almost! see Enable section). Behold:

// withCopyrightInFooter.js
export default (swal) => {
  const fn = (params) => {
    ...params,
    footer: (params.footer ? params.footer + '<br/>' : '') + 'Copyright Rick Sanchez'
  }
  Object.assign(fn, swal}
  return fn
}

// app.js
import swal from 'sweetalert2'
import withCopyrightInFooter from './withCopyrightInFooter'
const mySwal = withCopyrightInFooter(swal)
mySwal({title: 'Look! ', footer: 'This doesnt erase the copyright text'})

(edit: in the above code I changed swal(withCopyrightInFooter) to withCopyrightInFooter(swal)

As you can see, with this simple pattern, we gain a very robust extensibility. Not only can we add configuration pre-processing logic (i.e. fixes the 3rd issue above; we can merge configurations intelligently/dynamically), we can also extend the static methods (e.g. swal.enableButtons(), etc.), and transform the result value

Enable it

For the wrapper pattern to work, the swal function/object must have a consistent API/shape. Currently a bunch of methods are added after the first swal opens, but they don’t exist beforehand. I would assume this is not compatible with our .d.ts file. It’s a problem because these methods don’t exist at initialization, and thus they don’t get added to the child swal in the above example of the pattern. See PR #969.

Also it would be really nice if we could expose the currently internal logic for mapping shorthand arguments (i.e. swal(title, html, type) to a params object. This way we can use the shorthand with our composed mySwal objects and don’t need to duplicate the logic ourselves. Without this, the shorthand feature is likely to be discarded in many scenarios.

Promote it

We should deprecate swal.setDefaults and swal.clearDefaults and promote using mixins, or when robust extensibility is needed, the wrapper pattern.

We could and should also make the wrapper pattern easier to discover and use by adding an unintrusive convenience method!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
zenflowcommented, Feb 28, 2018

For one awesome example of what you can do with this pattern (though there are endless awesome examples possible), check out my WIP https://github.com/zenflow/sweetalert2-react-content ! Please open an issue! I’d like to get lots of review before transferring it to the sweetalert2 org and releasing 1.0.0!

2reactions
zenflowcommented, Mar 1, 2018

I was just going to demonstrate what I meant by “drop-in replacement”:

// before 
import swal from 'sweetalert2'
swal('using shorthand')
swal.clickConfirm() // using swal method

// after adding extension
import baseSwal from 'sweetalert2'
import withCopyrightInFooter from './withCopyrightInFooter'
const swal = withCopyrightInFooter(baseSwal)
swal('using shorthand')
swal.clickConfirm() // using swal method

It has the same API as the original, possibly extended with more methods/options/etc…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code Reuse - Devopedia
There are two approaches to code reuse: design and implement code with the expectation of future reuse; identify and refactor when we see ......
Read more >
Does OOP fulfill the promise of code reuse? What alternatives ...
The question is about the method of code reuse offered by OOP. Was it a good thing? Are there better methods to achieved...
Read more >
How does FP achieve reuse? - Eric Normand
Functional programming gets its reuse by creating small components that are very well-defined. Small means they're probably generally useful ...
Read more >
4 Ways to Make Your Code More Reusable | by Lance Harvie
Keep your code reusable by maintaining high cohesion. This ensures that your code can work and adapt to different environments making it useful ......
Read more >
How can we use inheritance for code reusability? - Quora
There is no such thing. Writing maintainable and reusable code is the job of the programmer not the language. You could write great...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found