Skip to content

Commit

Permalink
feat(properties): add withGlobalEffect decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbonnet committed Jun 2, 2019
1 parent 2cb26eb commit 699fecc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ The `realue` module exposes the following functions:
- [Lifecycle](#lifecycle)
- [`withEffect()`](#witheffect)
- [`withImmediateEffect()`](#withimmediateeffect)
- [`withGlobalEffect()`](#withglobaleffect)
- [`withImmediateGlobalEffect()`](#withimmediateglobaleffect)
- [`onPropsChange()`](#onpropschange)
- [Scoped-based decorators](#scoped-based-decorators)
- [`scoped()`](#scoped)
Expand Down Expand Up @@ -378,6 +380,20 @@ const withListener = withEffect(
Similar to `withEffect`, except that it runs the `handler` at component construction and before each render if `shouldHandleOrKeys` returns `true`.

#### `withGlobalEffect()`

> ➡️ `(handler)`
Runs `handler()` when the first element of this component is mounted.
If the handler returns a callback, it is called when the last element of this component is unmounted.

#### `withImmediateGlobalEffect()`

> ➡️ `(handler)`
Runs `handler()` when the first element of this component is constructed (that is, before it mounts).
If the handler returns a callback, it is called when the last element of this component is unmounted.

#### `onPropsChange()`

> ➡️ `(shouldHandleOrKeys, handler, callOnMount = true)`
Expand Down
57 changes: 57 additions & 0 deletions src/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,63 @@ export function withImmediateEffect(shouldHandleOrKeys, handler) {
)
}

export function withGlobalEffect(handler) {
/*
Runs `handler()` when the first element of this component is mounted.
If the handler returns a callback, it is called when the last element of this component is unmounted.
*/
let elementsCount = 0
let cleanup = null
return (Component) =>
setWrapperName(
Component,
class withGlobalEffect extends BaseComponent {
componentDidMount() {
if (elementsCount === 0) {
cleanup = handler()
}
elementsCount += 1
}
componentWillUnmount() {
elementsCount -= 1
if (elementsCount === 0 && typeof cleanup === 'function') {
cleanup()
cleanup = null
}
}
},
)
}

export function withImmediateGlobalEffect(handler) {
/*
Runs `handler()` when the first element of this component is constructed (that is, before it mounts).
If the handler returns a callback, it is called when the last element of this component is unmounted.
*/
let elementsCount = 0
let cleanup = null
return (Component) =>
setWrapperName(
Component,
class withImmediateGlobalEffect extends BaseComponent {
constructor(props) {
super(props)
if (elementsCount === 0) {
cleanup = handler()
}
elementsCount += 1
}
componentWillUnmount() {
elementsCount -= 1
if (elementsCount === 0 && typeof cleanup === 'function') {
cleanup()
cleanup = null
}
}
},
)
}

export function onPropsChange(
shouldHandleOrKeys,
handler,
Expand Down

0 comments on commit 699fecc

Please sign in to comment.