Skip to content

Commit a0a42f6

Browse files
committed
cache method getters
I recall testing the `rinse()` fn (specifically the removal of event listeners), and thinking "sweet, now this is working"... But as I spotted here, `bind(context, fn)` returns a new function, so I'm not sure how `removeEventListener` was ever working... This deserves some more testing, but for now, gut says cache those method getters.
1 parent a8a6ff7 commit a0a42f6

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/instance/constructor.js

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ import $ from 'tealight'
1818

1919
import { version } from '../../package.json'
2020

21-
let _config
22-
let _debug
23-
let _instance
21+
let boundDelegate
22+
let boundDestroy
23+
let boundReveal
24+
let boundClean
25+
let boundSync
26+
let config
27+
let debug
28+
let instance
2429

2530
export default function ScrollReveal(options = {}) {
2631
const invokedWithoutNew =
@@ -41,37 +46,30 @@ export default function ScrollReveal(options = {}) {
4146
* assigning the contents to the private variable `_config`.
4247
*/
4348
let buffer
44-
{
45-
try {
46-
buffer = _config
47-
? deepAssign({}, _config, options)
48-
: deepAssign({}, defaults, options)
49-
} catch (e) {
50-
logger.call(
51-
this,
52-
'Instantiation failed.',
53-
'Invalid configuration.',
54-
e.message
55-
)
56-
return noop
57-
}
49+
try {
50+
buffer = config
51+
? deepAssign({}, config, options)
52+
: deepAssign({}, defaults, options)
53+
} catch (e) {
54+
logger.call(this, 'Instantiation failed.', 'Invalid configuration.', e.message)
55+
return noop
56+
}
5857

59-
try {
60-
const container = $(buffer.container)[0]
61-
if (!container) {
62-
throw new Error('Invalid container.')
63-
}
64-
if ((!buffer.mobile && isMobile()) || (!buffer.desktop && !isMobile())) {
65-
throw new Error('This device is disabled.')
66-
}
67-
} catch (e) {
68-
logger.call(this, 'Instantiation failed.', e.message)
69-
return noop
58+
try {
59+
const container = $(buffer.container)[0]
60+
if (!container) {
61+
throw new Error('Invalid container.')
7062
}
71-
72-
_config = buffer
63+
if ((!buffer.mobile && isMobile()) || (!buffer.desktop && !isMobile())) {
64+
throw new Error('This device is disabled.')
65+
}
66+
} catch (e) {
67+
logger.call(this, 'Instantiation failed.', e.message)
68+
return noop
7369
}
7470

71+
config = buffer
72+
7573
/**
7674
* Modify the DOM to reflect successful instantiation.
7775
*/
@@ -93,17 +91,23 @@ export default function ScrollReveal(options = {}) {
9391

9492
this.pristine = true
9593

96-
Object.defineProperty(this, 'delegate', { get: () => delegate.bind(this) })
97-
Object.defineProperty(this, 'destroy', { get: () => destroy.bind(this) })
98-
Object.defineProperty(this, 'reveal', { get: () => reveal.bind(this) })
99-
Object.defineProperty(this, 'clean', { get: () => clean.bind(this) })
100-
Object.defineProperty(this, 'sync', { get: () => sync.bind(this) })
94+
boundDelegate = boundDelegate || delegate.bind(this)
95+
boundDestroy = boundDestroy || destroy.bind(this)
96+
boundReveal = boundReveal || reveal.bind(this)
97+
boundClean = boundClean || clean.bind(this)
98+
boundSync = boundSync || sync.bind(this)
99+
100+
Object.defineProperty(this, 'delegate', { get: () => boundDelegate })
101+
Object.defineProperty(this, 'destroy', { get: () => boundDestroy })
102+
Object.defineProperty(this, 'reveal', { get: () => boundReveal })
103+
Object.defineProperty(this, 'clean', { get: () => boundClean })
104+
Object.defineProperty(this, 'sync', { get: () => boundSync })
101105

102-
Object.defineProperty(this, 'defaults', { get: () => _config })
106+
Object.defineProperty(this, 'defaults', { get: () => config })
103107
Object.defineProperty(this, 'version', { get: () => version })
104108
Object.defineProperty(this, 'noop', { get: () => false })
105109

106-
return _instance ? _instance : (_instance = this)
110+
return instance ? instance : (instance = this)
107111
}
108112

109113
/**
@@ -113,6 +117,6 @@ export default function ScrollReveal(options = {}) {
113117
ScrollReveal.isSupported = () => transformSupported() && transitionSupported()
114118

115119
Object.defineProperty(ScrollReveal, 'debug', {
116-
get: () => _debug || false,
117-
set: value => (_debug = typeof value === 'boolean' ? value : _debug)
120+
get: () => debug || false,
121+
set: value => (debug = typeof value === 'boolean' ? value : debug)
118122
})

0 commit comments

Comments
 (0)