Socket
Socket
Sign inDemoInstall

y-event

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 0.2.0

5

lib/index.d.ts

@@ -5,3 +5,3 @@ export declare type callback = (...args: any[]) => any;

args: any[];
callback: callback;
callbacks: callback[];
}

@@ -16,6 +16,7 @@ export declare class YEvent {

$emit(type: string, ...args: any[]): void;
$off(type: string): void;
$off(type: string, cb?: callback): void;
$always(type: string, ...args: any[]): void;
$once(type: string, cb: callback): void;
}
declare const e: YEvent;
export default e;

26

lib/index.js

@@ -5,3 +5,3 @@ function generateDefaultState() {

args: [],
callback() { }
callbacks: []
};

@@ -24,3 +24,3 @@ }

const state = this.getState(type);
state.callback = cb;
state.callbacks.push(cb);
if (state.stalled) {

@@ -35,6 +35,12 @@ state.stalled = false;

return;
state.callback(...args);
state.callbacks.forEach(cb => cb(...args));
}
$off(type) {
delete this.list[type];
$off(type, cb) {
if (!cb) {
delete this.list[type];
}
else {
const cbs = this.list[type].callbacks;
this.list[type].callbacks = cbs.filter(callback => callback !== cb);
}
}

@@ -52,6 +58,14 @@ $always(type, ...args) {

return;
state.callback(...args);
state.callbacks.forEach(cb => cb(...args));
}
$once(type, cb) {
let called = false;
let fn = function (...args) {
!called && cb(...args);
called = true;
};
this.$on(type, fn);
}
}
const e = new YEvent();
export default e;
{
"name": "y-event",
"version": "0.1.1",
"version": "0.2.0",
"description": "Event system",

@@ -15,4 +15,4 @@ "main": "lib/index.js",

"scripts": {
"start": "tsc -w"
"start": "tsc"
}
}

@@ -27,5 +27,5 @@ ## Intro

event.$on('event2', (...args) => {
console.log('event2', ...args) // event2, 1, 2, 3
console.log('event2', ...args)
})
event.$emit('event2', 1, 2, 3)
event.$emit('event2', 1, 2, 3) // event2, 1, 2, 3

@@ -35,2 +35,9 @@ // off

event.$emit('event2') // nothing happens
// invoke once
event.$once('event3', () => {
console.log('event3')
})
event.$emit('event3') // 'event 3'
event.$emit('event3')
```

@@ -37,0 +44,0 @@

@@ -6,3 +6,3 @@ export type callback = (...args: any[]) => any

args: any[]
callback: callback
callbacks: callback[]
}

@@ -14,3 +14,3 @@

args: [],
callback() {}
callbacks: []
}

@@ -37,3 +37,3 @@ }

const state: State = this.getState(type)
state.callback = cb
state.callbacks.push(cb)
if (state.stalled) {

@@ -48,7 +48,12 @@ state.stalled = false

if (!state) return
state.callback(...args)
state.callbacks.forEach(cb => cb(...args))
}
$off(type: string) {
delete this.list[type]
$off(type: string, cb?: callback) {
if (!cb) {
delete this.list[type]
} else {
const cbs = this.list[type].callbacks
this.list[type].callbacks = cbs.filter(callback => callback !== cb)
}
}

@@ -66,4 +71,13 @@

if (state.stalled) return
state.callback(...args)
state.callbacks.forEach(cb => cb(...args))
}
$once(type: string, cb: callback) {
let called: boolean = false
let fn = function(...args: any[]) {
!called && cb(...args)
called = true
}
this.$on(type, fn)
}
}

@@ -70,0 +84,0 @@

@@ -27,1 +27,18 @@ import e from '../lib/index.js'

e.$emit('test3')
// multi-on
e.$on('test4', (arg) => {
console.log('test4 1', arg)
})
e.$on('test4', (arg) => {
console.log('test4 2', arg)
})
e.$emit('test4', 'ok')
// once
e.$once('test5', () => {
console.log('test 5, should only print once')
})
e.$emit('test5')
e.$emit('test5')

@@ -8,3 +8,4 @@ {

"outDir": "lib",
"declaration": true
"declaration": true,
"watch": true
},

@@ -11,0 +12,0 @@ "include": [

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc