Socket
Socket
Sign inDemoInstall

@dxos/async

Package Overview
Dependencies
Maintainers
13
Versions
2980
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dxos/async - npm Package Compare versions

Comparing version 1.0.0-beta.7 to 1.0.0-beta.8

38

dist/es/event.d.ts

@@ -0,1 +1,6 @@

export declare type Effect = () => (() => void) | undefined;
interface EventEmitterLike {
on(event: string, cb: (data: any) => void): void;
off(event: string, cb: (data: any) => void): void;
}
/**

@@ -32,4 +37,6 @@ * An EventEmitter variant that does not do event multiplexing and respresents a single event.

export declare class Event<T = void> implements ReadOnlyEvent<T> {
static wrap<T>(emitter: EventEmitterLike, eventName: string): Event<T>;
private readonly _listeners;
private readonly _onceListeners;
private readonly _effects;
/**

@@ -46,3 +53,2 @@ * Emit an event.

emit(data: T): void;
private _trigger;
/**

@@ -96,2 +102,27 @@ * Register an event listener.

listenerCount(): number;
/**
* Add a side effect that will be activated once the event has at least one subscriber.
* The provided callback can return a function that will be used to clean up after the last subscriber unsubscribes from the event.
* The API is similar to `useEffect` from React.
*
* ## Example:
* ```typescript
* event.addEffect(() => {
* // do stuff
* return () => {
* // clean-up
* };
* });
* ```
*
* @returns Callback that will remove this effect once called.
*/
addEffect(effect: Effect): () => void;
/**
* Turn any variant of `Event<T>` into an `Event<void>` discarding the callback parameter.
*/
discardParameter(): Event<void>;
private _trigger;
private _runEffects;
private _cleanupEffects;
}

@@ -148,3 +179,8 @@ /**

waitForCount(expectedCount: number): Promise<T>;
/**
* Turn any variant of `Event<T>` into an `Event<void>` discarding the callback parameter.
*/
discardParameter(): Event<void>;
}
export {};
//# sourceMappingURL=event.d.ts.map

@@ -38,3 +38,13 @@ "use strict";

this._onceListeners = new Set();
this._effects = new Set();
}
static wrap(emitter, eventName) {
const event = new Event();
event.addEffect(() => {
const onEvent = (data) => event.emit(data);
emitter.on(eventName, onEvent);
return () => emitter.off(eventName, onEvent);
});
return event;
}
/**

@@ -59,12 +69,2 @@ * Emit an event.

}
_trigger(listener, data) {
setImmediate(() => {
try {
listener(data);
}
catch (err) {
console.log(`Unhandled error in Event listener: ${err}`);
}
});
}
/**

@@ -83,2 +83,5 @@ * Register an event listener.

this._listeners.add(callback);
if (this.listenerCount() === 1) {
this._runEffects();
}
return () => this.off(callback);

@@ -98,2 +101,5 @@ }

this._onceListeners.delete(callback);
if (this.listenerCount() === 0) {
this._cleanupEffects();
}
}

@@ -112,3 +118,11 @@ /**

this._onceListeners.add(callback);
return () => this._onceListeners.delete(callback);
if (this.listenerCount() === 1) {
this._runEffects();
}
return () => {
this._onceListeners.delete(callback);
if (this.listenerCount() === 0) {
this._runEffects();
}
};
}

@@ -154,4 +168,63 @@ /**

}
/**
* Add a side effect that will be activated once the event has at least one subscriber.
* The provided callback can return a function that will be used to clean up after the last subscriber unsubscribes from the event.
* The API is similar to `useEffect` from React.
*
* ## Example:
* ```typescript
* event.addEffect(() => {
* // do stuff
* return () => {
* // clean-up
* };
* });
* ```
*
* @returns Callback that will remove this effect once called.
*/
addEffect(effect) {
const handle = { effect, cleanup: undefined };
if (this.listenerCount() > 0) {
handle.cleanup = handle.effect();
}
this._effects.add(handle);
return () => {
var _a;
// eslint-disable-next-line no-unused-expressions
(_a = handle.cleanup) === null || _a === void 0 ? void 0 : _a.call(handle);
this._effects.delete(handle);
};
}
/**
* Turn any variant of `Event<T>` into an `Event<void>` discarding the callback parameter.
*/
discardParameter() {
return this;
}
_trigger(listener, data) {
setImmediate(() => {
try {
listener(data);
}
catch (err) {
console.log(`Unhandled error in Event listener: ${err}`);
}
});
}
_runEffects() {
for (const handle of this._effects) {
handle.cleanup = handle.effect();
}
}
_cleanupEffects() {
var _a;
for (const handle of this._effects) {
// eslint-disable-next-line no-unused-expressions
(_a = handle.cleanup) === null || _a === void 0 ? void 0 : _a.call(handle);
handle.cleanup = undefined;
}
}
}
exports.Event = Event;
//# sourceMappingURL=event.js.map

@@ -11,2 +11,4 @@ /**

* and it's not async while still returning a promise.
*
* Java docs reference on synchronized sections: https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
*/

@@ -13,0 +15,0 @@ export declare class Lock {

@@ -14,2 +14,4 @@ "use strict";

* and it's not async while still returning a promise.
*
* Java docs reference on synchronized sections: https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
*/

@@ -16,0 +18,0 @@ class Lock {

2

package.json
{
"name": "@dxos/async",
"version": "1.0.0-beta.7",
"version": "1.0.0-beta.8",
"description": "Basic async utils",

@@ -5,0 +5,0 @@ "files": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc