Skip to content

Follow JavaScript design pattern(observer pattern) and implementing this pattern.

Notifications You must be signed in to change notification settings

tedshd/observer_pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

observer_pattern

Follow JavaScript design pattern(observer pattern) and implementing this pattern.

Alt text

DevNote

Intro

This is min function observer pattern implementing

It can handle cross module communication

hub is a array tmp actions

publish include a action and something want to do and publish to hub provide subscribe

unpublish can cancel published action or something want to do

fire is function trigger action, it is like subscribe in observer pattern

Usage

publish

OB.publish(action, callback);

action[string]: action for fire, it is not unique

callback[function]: Something to do

function redAlert() {
    alert('RED ALERT');
}

B.publish('alarm', redAlert);

unPublish

OB.unPublish(action, callback name);

action[string]: cancel publish action

callback name[var]: it is a option if want cancel something want to do but in many actions, can point function name to cancel it

OB.unPublish('alarm');
// or
function redAlert() {
    alert('alert');
}
OB.unPublish('', redAlert);
// or
OB.unPublish('alarm', redAlert);

fire

OB.fire(action, callback);

action[string]: fire action(s) to do something

callback[function]: Something to do, it can handle if publish is async behavior

OB.fire('alarm');
// or
function funcA() {
    console.log('fin');
}
OB.fire('alarm', funcA);

About

Follow JavaScript design pattern(observer pattern) and implementing this pattern.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published