Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Miscellaneous programming patterns.
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).
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.
});
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.
var handleWrapper = require('ypatterns').handleWrapper;
//...
function factory(...) {
function initialize(callback) {
//...
}
function cleanup(callback) {
//...
}
return handleWrapper({ initialize: initialize, cleanup: cleanup });
}
var handle = factory(...);
...
handle.close(function() {
// Object is closed.
});
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.
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.
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);
}
MIT
FAQs
useful programming patterns
The npm package ypatterns receives a total of 36 weekly downloads. As such, ypatterns popularity was classified as not popular.
We found that ypatterns demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.