Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@unic/composite-observer
Advanced tools
Small and simple observer pattern as a composite for your factories
$ npm install @unic/composite-observer
// ES6 Module
import observer from '@unic/composite-observer';
// CommomJS
const observer = require('@unic/composite-observer').default;
A composite is a function or an object which can be used as is or to merged with another object. These composites are normally used in the factory/composition pattern.
Helpful Ressources:
Important: In further examples and the API will just infer that you've already generated your new object with the composite applied on it and will not give any more examples on how to do that.
Examples
// Applying the composite to a new object literal
const obj = Object.assign({}, observer());
// Equivalent with lodash.merge
const obj = _.merge({}, observer());
// Just use it as a
const obj = observer();
Subscribe to an event
Returns: Integer
- Returns an identifyer to unsubscribe
Param | Type | Default | Description |
---|---|---|---|
event | String | Eventname to subscribe to | |
callback | function | Callback function to execute when the event is triggered | |
[once] | boolean | false | When true, will unsubscribe automatically after first execution |
Example
// Subscribe to the 'eventName' event
obj.on('eventName', () => {
console.log('eventName was called');
});
// Subscribe to the 'eventName' event but unsubscribe automatically after first call
obj.on('eventName', () => {
console.log('eventName was called');
console.log('This handler unsubscribes automatically');
}, true);
Unsubscribe a single handler by the identifier returned by .on() or unsubscribe a whole event group by providing an eventname you want to unsubscribe all listeners from
Returns: String/Number
- For now... lets jsut say the return doesn't matter
Param | Type | Description |
---|---|---|
identifier | String/Number | Either the eventname or the identifiers returned by .on() |
Example
// Subscribe to the 'eventName' event
const uid = obj.on('eventName', () => {
console.log('eventName was called');
});
// Unsubscribe by uid
obj.off(uid);
// Unsubscribe by eventname, this unsubscribes all listeners for this event
obj.off('eventName');
Trigger all listeners by eventname
Returns: undefined
Param | Type | Description |
---|---|---|
event | String | Eventname to trigger |
[params...] | Any | Pass any number of arguments you want to receive in the listener |
Example
// Subscribe to the 'eventName' event
obj.on('eventName', () => {
console.log('eventName was called');
});
// Trigger the event 'eventName'
obj.trigger('eventName');
// Subscribe and output all the params you get in the callback
obj.on('eventName', (param1, param2, ...rest) => {
console.log(param1, param2, rest);
});
// Trigger the event 'eventName' and add custom parameters for this trigger
obj.trigger('eventName', 'Hello', 'World', '!!!');
MIT © Christian Sany
FAQs
Simple observer composite to use for your factories
The npm package @unic/composite-observer receives a total of 5 weekly downloads. As such, @unic/composite-observer popularity was classified as not popular.
We found that @unic/composite-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.