Pocket-sized minimalist framework of common design patterns for JavaScript.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
build
samples
src/main
.gitignore
LICENSE
MIT-LICENSE
README.md
gulpfile.js
jsdoc.json
karma.conf.js
package.json

README.md

Pattern JS

Pattern JS is a light weight pocket-sized minimalist framework of common design patterns written in JavaScript.

Features

  • Publisher-Subscriber: Pub/Sub and custom events.
  • Observer-Observable: Classic Observer pattern.
  • Event-Signal: Stack of listeners for easy transport of events and data.
  • Notifier-Receiver: behavioral design pattern with event notifications.

You can also:

  • Use the built-in FIFO Queue.
  • View and run Usage Samples in your browser.

Installation

Pattern JS requires Node.js, NPM, and Gulp. Clone the repo:

git clone https://github.com/rgr-myrg/pattern-js.git
cd pattern-js

Install dependencies and run gulp to build.

$ npm install -d
$ gulp

Running Tests

Executing gulp from the command line runs test by default. You can also run the task directly:

$ gulp start-tests

Development

Want to contribute? Great!

Per GitFlow, create your own feature branch.

$ git checkout develop
$ git pull origin develop
$ git branch feature/your-branch-name
$ git checkout feature/your-branch-name

Run tests continuously and make your changes.

$ gulp watch

Submit a pull request when finished!

Usage Samples

Publisher-Subscriber

// Create a Subscriber
var subscriber = {
    onMsgReceived: function(msg) {
        console.log("received: ", msg);
    }
};

// Create an instance of Publisher and register the Subscriber
var publisher = Pattern.Publisher().registerSubscriber(subscriber);

// Dispatch the notification
publisher.notify("onMsgReceived", {name: "test", text: "my message"});

Event-Signal

// Create a signal and add listeners.
var signal = Pattern.EventSignal()
            .addListener(
                function(msg) {
                    console.log("received: ", msg);
                }
            );

// Dispatch the notification
signal.dispatch({name: "test", text: "my message"});

Notifier-Receiver

// Create a Receiver with a handler method.
var receiver = Pattern.Receiver()
            .on("msgReceived", function(msg) {
                console.log("received: ", msg);
            });
            
// Create the Notifier and register the Receiver.
var notifier = Pattern.Notifier().addReceiver(receiver);

// Send the notification
notifier.notify("msgReceived", {name: "test", text: "my message"});

Observable-Observer

// Create the Observer
var observer = Pattern.Observer({
        onMsgReceived: function(msg) {
            console.log("received: ", msg);
        }
    });
    
// Create the Observable and register the Observer.
var observable = Pattern.Observable().addObserver(observer);

// Send the notification
observable.notifyObservers("onMsgReceived", {name: "test", text: "my message"});

Working Samples

Run the samples in your browser Usage Samples

To-do

  • Code for additional design patterns
  • Write more Tests
  • Code comments and JsDoc

License

MIT License