Handler for the window resize event of the observer pattern.
npm
$ npm i window-resize-subject
yarn
$ yarn add window-resize-subject
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();
- 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
});
Start watching the resize event.
Stop watching the resize event.
note: observer is not deleted.
- params:
- size:
- type:
Object
- width:
number
- height:
number
- width:
- type:
- size:
Force update.
subject.notifyObservers({ width: 800, height: 600 });
- params:
- name:
- type:
string
|Symbol
- type:
- name:
Deletes the specified Observer.
subject.deleteObserver('name');
Deletes all Observers.
- params:
- type:
number
- type:
Changes the delay time for a resize event.
subject.setDelay(100);
returns true if it has an observer.
Dispatches the current window size.
- type:
number
- default:
33
Changes the delay time for a resize event.
import { WindowResizeSubject } from 'window-resize-subject';
const subject = new WindowResizeSubject({ delay: 100 });
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.