Skip to content

Commit

Permalink
fix: avoid memory leak after installing a plugin too many times
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Nov 23, 2020
1 parent a92eb6c commit b8d2e32
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,10 @@ dayjs.prototype = proto;
})

dayjs.extend = (plugin, option) => {
if (plugin.installed) {
return dayjs
if (!plugin.$i) { // install plugin only once
plugin(option, Dayjs, dayjs)
plugin.$i = true
}

plugin(option, Dayjs, dayjs)
plugin.installed = true
return dayjs
}

Expand Down
1 change: 1 addition & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ it('Time from now with UTC', () => {

it('Custom thresholds and rounding support', () => {
expect(dayjs().subtract(45, 'm').fromNow()).toBe('an hour ago')
delete relativeTime.$i // this allow plugin to be installed again
dayjs.extend(relativeTime, {
rounding: Math.floor,
thresholds: [
Expand Down

0 comments on commit b8d2e32

Please sign in to comment.