GithubHelp home page GithubHelp logo

node-ypatterns's Introduction

ypatterns

Miscellaneous programming patterns.

Build Status

Gate

This is the pattern of usage counter. Anybody can enter the gate is the gate is not closed. When gate is closed, nobody allowed entering. However, all those that are already inside, can exit the gate. The completion of gate closure will be called when everybody left (exited the gate).

Usage

var gate = require('ypatterns').gate;
//...
if (gate.enter()) {
  //... entrance granted.
  //...
  gate.exit();
}
else {
  //... entrance is not allowed anymore, the gate is closed.
}
...
gate.close(function() {
  //... nobody is inside.
});

Handle Wrapper

This pattern is for objects that exhibit handle-like facade. Upon instantiation, such objects return handle, which has only method close. Such facade is useful for object that are listeners and call for notifications. To expose handle facade, one has to implement factory, which has initialize and cleanup methods. The wrapper takes care to call cleanup only after initialization is completed.

Definition

var handleWrapper = require('ypatterns').handleWrapper;
//...
function factory(...) {
  function initialize(callback) {
    //...
  }
  function cleanup(callback) {
    //...
  }
  return handleWrapper({ initialize: initialize, cleanup: cleanup });
}

Usage

var handle = factory(...);
...
handle.close(function() {
  // Object is closed.
});

Sync-Async Facade

This pattern is used to create objects which expose asynchronous instantiation, but can be used synchronously. Better behaving usage will use object upon instantiation completion, which ensures asynchronous initialization was completed. However, if object is used before completion, this will still work. The facade wrapper invoke synchronous initializer if object is used before asynchronous initializer was completed. All this done in a way opaque to usage.

Usage

var obj = factory(..., function() {
  // Object initialization completed asynchronously. The object can be used.
  obj.foo();
});
// At this point initialization might not be completed yet, however, the object can be used 
// here as well.
obj.foo();
// Function foo will trigger synchronous initializer, which will be invoked before calling foo, 
// if object's initialziation was not completed.

Definition

var syncasyncFacade = require('ypatterns').syncasyncFacade;
function factory(..., callback) {
  
  function createInstance() {
    // create object
    // ...
    return instance;
  }
  
  function initializeAsync(instance, callback) {
    // asynchronous initializer
  }
  
  function initializeSync(instance) {
    // synchronous initializer is invoked only if needed.
    // initializers should be idempotent, at least, it should be anticipated that synchronous 
    // initializer will be invoked after asynchronous started and asynchronous one should 
    // anticipate synchronous was called in the middle of its execution.
  }

  return syncasyncFacade({ 
      create: createInstance, 
      initializeAsync: initializeAsync, 
      initializeSync: initializeSync 
      }, 
    callback);
}

License

MIT

node-ypatterns's People

Contributors

yosefd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dineshkummarc

node-ypatterns's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.