window-resize-subject
v1.5.0
Published
[![npm version](https://badge.fury.io/js/window-resize-subject.svg)](https://badge.fury.io/js/window-resize-subject) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) ![CI](https://github.com/mya-ak
Downloads
12,643
Maintainers
Readme
window-resize-subject
Handler for the window resize event of the observer pattern.
Install
npm
$ npm i window-resize-subject
yarn
$ yarn add window-resize-subject
Usage
import { WindowResizeSubject } from 'window-resize-subject';
// create subject
const subject = new WindowResizeSubject();
// add observer
const observer = ({ width, height }) => {
// do something
};
subejct.addObserver('name', observer);
// start watching the resize event
subject.subscribe();
note: You can also write in a method chain
subejct.addObserver('name', observer).subscribe();
Methods
addObserver
- params:
- name:
- type:
string
|Symbol
- type:
- observer:
- type:
WindowResizeObserver
- details:
(event: { width: number, height: number }) => void
- details:
- type:
- name:
Add observer.
And called with the current window size.
subejct.addObserver('name', ({ width, height }) => {
// do something
});
subscribe
Start watching the resize event.
unsubscribe
Stop watching the resize event.
note: observer is not deleted.
notifyObservers
- params:
- size:
- type:
Object
- width:
number
- height:
number
- width:
- type:
- size:
Force update.
subject.notifyObservers({ width: 800, height: 600 });
deleteObserver
- params:
- name:
- type:
string
|Symbol
- type:
- name:
Deletes the specified Observer.
subject.deleteObserver('name');
deleteObservers
Deletes all Observers.
setDelay
- params:
- type:
number
- type:
Changes the delay time for a resize event.
subject.setDelay(100);
hasObserver
returns true if it has an observer.
dispatch
Dispatches the current window size.
Options
delay
(option)
- type:
number
- default:
33
Changes the delay time for a resize event.
import { WindowResizeSubject } from 'window-resize-subject';
const subject = new WindowResizeSubject({ delay: 100 });
Contribution
If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.