Skip to content

Commit

Permalink
Remove registerPodium(). Closes #64. Closes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Oct 16, 2021
1 parent 3bcc306 commit bb59107
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 289 deletions.
10 changes: 0 additions & 10 deletions API.md
Expand Up @@ -365,18 +365,8 @@ event activities. The `events` argument can be:
registration is ignored. **Note that if the registration config is changed between registrations,
only the first configuration is used. Defaults to `false` (a duplicate registration will throw an
error).** For detailed examples of event parameters [see here](README.md#parameters)
- a `Podium` object which is passed to [`podium.registerPodium()`](#podiumregisterpodiumpodiums).
- an array containing any of the above.

## `podium.registerPodium(podiums)`

Registers another emitter as an event source for the current emitter (any event update emitted by the
source emitter is passed to any subscriber of the current emitter) where:
- `podiums` - a `Podium` object or an array of objects, each added as a source.

Note that any events registered with a source emitter are automatically added to the current emitter.
If the events are already registered, they are left as-is.

## `podium.emit(criteria, data)`

Emits an event update to all the subscribed listeners where:
Expand Down
13 changes: 1 addition & 12 deletions lib/index.d.ts
Expand Up @@ -18,17 +18,6 @@ declare class Podium {
*/
registerEvent(events: Podium.Event | Podium.Event[]): void;

/**
* Registers another emitter as an event source for the current emitter (any event update
* emitted by the source emitter is passed to any subscriber of the current emitter).
*
* Note that any events registered with a source emitter are automatically added to the current
* emitter. If the events are already registered, they are left as-is.
*
* @param podiums - A Podium object or an array of objects, each added as a source.
*/
registerPodium(podiums: Podium | Podium[]): void;

/**
* Emits an event update to all the subscribed listeners.
Expand Down Expand Up @@ -196,7 +185,7 @@ declare namespace Podium {
readonly shared?: boolean;
}

type Event = string | EventOptions | Podium;
type Event = string | EventOptions;

type Listener<TContext extends object = any, TArgs extends any[] = any[]> =
(this: TContext, ...args: TArgs) => void | Promise<void>;
Expand Down
126 changes: 42 additions & 84 deletions lib/index.js
Expand Up @@ -43,7 +43,6 @@ exports = module.exports = internals.Podium = class {
this._eventListeners = new Map();
this._notificationsQueue = [];
this._eventsProcessing = false;
this._sourcePodiums = [];

if (events) {
this.registerEvent(events, options);
Expand Down Expand Up @@ -79,17 +78,8 @@ exports = module.exports = internals.Podium = class {

registerEvent(events, options = {}) {

events = Hoek.flatten([].concat(events));
events = [].concat(events);
for (let event of events) {
if (!event) {
continue;
}

if (event instanceof internals.Podium) {
this.registerPodium(event);
continue;
}

if (typeof event === 'string') {
event = { name: event };
}
Expand All @@ -105,29 +95,6 @@ exports = module.exports = internals.Podium = class {
}

this._eventListeners.set(name, { handlers: null, flags: event });
for (const podium of this._sourcePodiums) {
if (!podium._eventListeners.has(name)) {
podium._eventListeners.set(name, { handlers: null, flags: event });
}
}
}
}

registerPodium(podiums) {

podiums = [].concat(podiums);

for (const podium of podiums) {
if (podium._sourcePodiums.indexOf(this) !== -1) {
continue;
}

podium._sourcePodiums.push(this);
for (const name of podium._eventListeners.keys()) {
if (!this._eventListeners.has(name)) {
this._eventListeners.set(name, { handlers: null, flags: podium._eventListeners.get(name).flags });
}
}
}
}

Expand All @@ -141,9 +108,7 @@ exports = module.exports = internals.Podium = class {
const event = this._eventListeners.get(name);
Hoek.assert(event, `Unknown event ${name}`);

if (!event.handlers &&
!this._sourcePodiums.length) {

if (!event.handlers) {
return;
}

Expand All @@ -170,74 +135,67 @@ exports = module.exports = internals.Podium = class {
criteria.tags = tags;
}

if (event.handlers) {
const processing = [];
const processing = [];

const handlers = event.handlers.slice(); // Clone in case handlers are changed by listeners
for (const handler of handlers) {
if (handler.channels &&
(!criteria.channel || handler.channels.indexOf(criteria.channel) === -1)) {

const handlers = event.handlers.slice(); // Clone in case handlers are changed by listeners
for (const handler of handlers) {
if (handler.channels &&
(!criteria.channel || handler.channels.indexOf(criteria.channel) === -1)) {
continue;
}

if (handler.filter) {
if (!criteria.tags) {
continue;
}

if (handler.filter) {
if (!criteria.tags) {
continue;
}
const match = Hoek.intersect(criteria.tags, handler.filter.tags, { first: !handler.filter.all });
if (!match ||
handler.filter.all && match.length !== handler.filter.tags.length) {

const match = Hoek.intersect(criteria.tags, handler.filter.tags, { first: !handler.filter.all });
if (!match ||
handler.filter.all && match.length !== handler.filter.tags.length) {

continue;
}
continue;
}
}

if (handler.count) {
--handler.count;
if (handler.count < 1) {
internals.removeHandler(this, criteria.name, handler);
}
if (handler.count) {
--handler.count;
if (handler.count < 1) {
internals.removeHandler(this, criteria.name, handler);
}
}

if (!_generated &&
typeof data === 'function') {
if (!_generated &&
typeof data === 'function') {

data = data();
_generated = true;
}
data = data();
_generated = true;
}

const update = internals.flag('clone', handler, event) ? Hoek.clone(data) : data;
const args = internals.flag('spread', handler, event) && Array.isArray(update) ? update.slice(0) : [update];
const update = internals.flag('clone', handler, event) ? Hoek.clone(data) : data;
const args = internals.flag('spread', handler, event) && Array.isArray(update) ? update.slice(0) : [update];

if (internals.flag('tags', handler, event) &&
criteria.tags) {
if (internals.flag('tags', handler, event) &&
criteria.tags) {

args.push(criteria.tags);
}
args.push(criteria.tags);
}

try {
const result = handler.context ? handler.listener.apply(handler.context, args) : handler.listener(...args);
if (result &&
typeof result.then === 'function') {
try {
const result = handler.context ? handler.listener.apply(handler.context, args) : handler.listener(...args);
if (result &&
typeof result.then === 'function') {

processing.push(result);
}
}
catch (err) {
processing.push(Promise.reject(err));
processing.push(result);
}
}

if (processing.length) {
await Promise.all(processing);
catch (err) {
processing.push(Promise.reject(err));
}
}

if (this._sourcePodiums.length) {
const podiums = this._sourcePodiums.slice(); // Clone in case modified while processing
await Promise.all(podiums.map((podium) => podium.emit(criteria, data, _generated)));
if (processing.length) {
await Promise.all(processing);
}
}

Expand Down

0 comments on commit bb59107

Please sign in to comment.