Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

emmi

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emmi - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

dist/helpers/output-spread.d.ts

27

dist/helpers.js

@@ -0,1 +1,2 @@

import { i, m } from "./output-spread-ba56d5bc.js";
function iterate(arr, callback) {

@@ -5,5 +6,5 @@ return new Promise((resolve, reject) => {

const length = arr.length;
let i = -1;
while (length > ++i) {
const maybePromise = arr[i];
let i2 = -1;
while (length > ++i2) {
const maybePromise = arr[i2];
if (isPromise(maybePromise)) {

@@ -31,2 +32,10 @@ maybePromise.then((result) => {

}
function filter(arr, predicate) {
return new Promise((resolve, reject) => {
const allResults = [];
iterate(arr, (item) => {
predicate(item) && allResults.push(item);
}).then(() => resolve(allResults)).catch(reject);
});
}
function find(arr, predicate) {

@@ -45,14 +54,8 @@ let done = false;

}
function filter(arr, predicate) {
return new Promise((resolve, reject) => {
const allResults = [];
iterate(arr, (item) => {
predicate(item) && allResults.push(item);
}).then(() => resolve(allResults)).catch(reject);
});
}
export {
filter,
find,
iterate
i as isMarkedForSpread,
iterate,
m as markForSpread
};

@@ -0,3 +1,4 @@

export * from "./filter.js";
export * from "./find.js";
export * from "./filter.js";
export * from "./iterate.js";
export * from "./output-spread.js";
export type MaybePromise<T> = T | Promise<T>;
/**
* Iterate over an array containing `MaybePromise`s
*
*/
export declare function iterate<T>(arr: MaybePromise<T>[], callback: (value: T, itemsLeft: number) => void | Promise<void>): Promise<void>;

@@ -0,1 +1,3 @@

import { MarkedForSpread } from "./helpers";
export declare const meta: unique symbol;
export type Event<Input, Output> = {

@@ -7,8 +9,2 @@ input: Input;

type NonUndefined<T> = T extends undefined | void ? never : T;
interface ListenerOptions {
spreadReturn: boolean;
}
type IsSpreadOptions = ListenerOptions & {
spreadReturn: true;
};
/**

@@ -20,11 +16,8 @@ * emmi - a small event emitter that enables many workflows

export declare function emmi<EMap extends EventMap>(): {
on: {
<Key extends keyof EMap>(key: Key, listener: (args: EMap[Key]["input"]) => EMap[Key]["output"][], options: IsSpreadOptions): void;
<Key_1 extends keyof EMap>(key: Key_1, listener: (args: EMap[Key_1]["input"]) => EMap[Key_1]["output"], options?: ListenerOptions): void;
};
emit: <Key_2 extends keyof EMap>(key: Key_2, data: EMap[Key_2]["input"]) => NonUndefined<EMap[Key_2]["output"]>[];
onReply: <Key_3 extends keyof EMap>(key: Key_3, listener: (input: EMap[Key_3]["input"], output: NonUndefined<EMap[Key_3]["output"]>[]) => void) => void;
off: <Key_4 extends keyof EMap>(key: Key_4, listener?: ((args: EMap[Key_4]["input"]) => EMap[Key_4]["output"]) | undefined) => void;
offReply: <Key_5 extends keyof EMap>(key: Key_5, listener?: ((input: EMap[Key_5]["input"], output: NonUndefined<EMap[Key_5]["output"]>[]) => void) | undefined) => void;
on: <Key extends keyof EMap>(key: Key, listener: (args: EMap[Key]["input"]) => EMap[Key]["output"] | MarkedForSpread<EMap[Key]["output"][]>) => void;
emit: <Key_1 extends keyof EMap>(key: Key_1, data: EMap[Key_1]["input"]) => NonUndefined<EMap[Key_1]["output"]>[];
onReply: <Key_2 extends keyof EMap>(key: Key_2, listener: (input: EMap[Key_2]["input"], output: NonUndefined<EMap[Key_2]["output"]>[]) => void) => void;
off: <Key_3 extends keyof EMap>(key: Key_3, listener?: ((args: EMap[Key_3]["input"]) => EMap[Key_3]["output"]) | undefined) => void;
offReply: <Key_4 extends keyof EMap>(key: Key_4, listener?: ((input: EMap[Key_4]["input"], output: NonUndefined<EMap[Key_4]["output"]>[]) => void) | undefined) => void;
};
export {};

@@ -0,7 +1,7 @@

import { i as isMarkedForSpread } from "./output-spread-ba56d5bc.js";
const meta = Symbol("emmmetadata");
function emmi() {
const listeners = /* @__PURE__ */ new Map();
const optionsMap = /* @__PURE__ */ new Map();
const replyListeners = /* @__PURE__ */ new Map();
function on(key, listener, options) {
options && optionsMap.set(listener, options);
function on(key, listener) {
const handlers = listeners.get(key);

@@ -28,6 +28,6 @@ if (handlers) {

const fn = l[i];
const { spreadReturn = false } = optionsMap.get(fn) || {};
const res = fn(data);
if (shouldSpreadResult(spreadReturn)) {
replies.push(...getDefinedArr(res));
const isArr = isArray(res);
if (isArr && isMarkedForSpread(res)) {
replies.push(...res.filter(isDefined));
} else if (isDefined(res)) {

@@ -78,10 +78,8 @@ replies.push(res);

}
function getDefinedArr(t) {
return t.filter(isDefined);
function isArray(t) {
return Array.isArray(t);
}
function shouldSpreadResult(shouldSpreadResult2, t) {
return shouldSpreadResult2;
}
export {
emmi
emmi,
meta
};
{
"name": "emmi",
"version": "0.8.0",
"version": "0.9.0",
"type": "module",

@@ -26,3 +26,3 @@ "main": "./dist/index.cjs",

"test": "vitest",
"preversion": "bun test && npm run build",
"preversion": "dprint check && bun test && npm run build",
"prepublishOnly": "vitest run && npm run build"

@@ -33,3 +33,3 @@ },

"@vitest/coverage-v8": "^0.34.1",
"prettier": "^3.0.1",
"dprint": "^0.40.2",
"typescript": "^5.0.2",

@@ -39,6 +39,3 @@ "vite": "^4.4.5",

"vitest": "^0.34.1"
},
"prettier": {
"printWidth": 80
}
}

@@ -5,11 +5,9 @@ # emmi

Inspired by [mitt](https://github.com/developit/mitt/tree/main), but with a few modifications. Main difference is that events are typed with an `input` and `output` field.
Inspired by [mitt](https://github.com/developit/mitt/tree/main), but with a large modification: listeners return a response to the `emit` caller.
This enables:
When defining an event i `emmi`, you need to type it with an `input` and `output` field. Calling `emit` will return an `Output[]` value. `Output` can be anything you want.
- calling `emit` returns an `Output[]` value.
- listeners can directly respond to your events
- You can listen to event replies with `onReply`
Also added an `onReply` listener that receives the `Input` and `Output[]` result of an `emit`.
Other that that, should be similar to other event emitter libraries.
This enables some interesting patterns not possible with most event emitters.

@@ -16,0 +14,0 @@ ## Listeners

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