Skip to content

Commit

Permalink
Merge branch 'release/release/0.13.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Oct 11, 2023
2 parents ae33266 + f40ffb6 commit f1ae3cc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.13.18",
"version": "0.13.19",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -73,11 +73,11 @@
"c8": "^8.0.1",
"cross-fetch": "^4.0.0",
"esbuild": "^0.19.4",
"eslint": "^8.50.0",
"eslint": "^8.51.0",
"madge": "^6.1.0",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vite": "^4.4.11",
"vitest": "^0.34.6"
}
}
17 changes: 17 additions & 0 deletions src/common/msg/emitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,21 @@ describe('Emitter', () => {
}
`)
})

it('should respect priorities', async () => {
const e = new Emitter()

let l: any = []

e.on('p', () => l.push(2), { priority: 100 })
e.on('p', () => l.push(5), { priority: -1 })
e.on('p', () => l.push(6), { priority: -2 })
e.on('p', () => l.push(1), { priority: 110 })
e.on('p', () => l.push(3))
e.on('p', () => l.push(4)) // this one has same prio as previous one, but was registered later!

await e.emit('p')

expect(l).toEqual([1,2,3,4,5,6])
})
})
52 changes: 36 additions & 16 deletions src/common/msg/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ export declare interface DefaultListener {
[k: string]: (...args: any[]) => any
}

interface EmitterSubscriber {
fn: EmitterHandler // (...args: any[]) => any
priority: number
}

export interface EmitterSubscriberOptions {
priority?: number
}

export class Emitter<
RemoteListener extends ListenerSignature<RemoteListener> = DefaultListener,
LocalListener extends ListenerSignature<LocalListener> = RemoteListener,
> {
subscribers: any = {}
subscribersOnAny: any[] = []
private subscribers: Record<any, EmitterSubscriber[]> = {}
private subscribersOnAny: any[] = []

_logEmitter = DefaultLogger('zeed:emitter', 'warn')

Expand All @@ -44,20 +53,17 @@ export class Emitter<
await this.emit(name, ...args),
})

public async emit<U extends keyof RemoteListener>(
event: U,
...args: Parameters<RemoteListener[U]>
): Promise<boolean> {
public async emit<U extends keyof RemoteListener>(event: U, ...args: Parameters<RemoteListener[U]>): Promise<boolean> {
let ok = false

try {
const subscribers = (this.subscribers[event] || []) as EmitterHandler[]
const subscribers = (this.subscribers[event] || []) as EmitterSubscriber[]
this._logEmitter.debug('emit', this?.constructor?.name, event, ...args, subscribers)

this.subscribersOnAny.forEach(fn => fn(event, ...args))

if (subscribers.length > 0) {
const all = subscribers.map((fn) => {
const all = subscribers.map(({ fn }) => {
try {
return promisify(fn(...args))
}
Expand All @@ -82,13 +88,29 @@ export class Emitter<

public on<U extends keyof LocalListener>(
event: U,
listener: LocalListener[U],
fn: LocalListener[U],
opt: EmitterSubscriberOptions = {},
): DisposerFunction {
const subscribers = (this.subscribers[event] || []) as EmitterHandler[]
subscribers.push(listener)
this.subscribers[event] = subscribers
const { priority = 0 } = opt
const subscribers = (this.subscribers[event] || [])
const slen = subscribers.length
const sobj = { fn, priority }
if (slen <= 0) {
this.subscribers[event] = [sobj]
}
else {
let pos = slen
for (let i = subscribers.length - 1; i >= 0; i--) {
const s = subscribers[i]
// Insert after last entry of same priority
if (priority <= s.priority)
break
pos -= 1
}
subscribers.splice(pos, 0, sobj) // in place
}
return () => {
this.off(event, listener)
this.off(event, fn)
}
}

Expand Down Expand Up @@ -116,9 +138,7 @@ export class Emitter<
listener: LocalListener[U],
): this {
// log("off", key)
this.subscribers[event] = (this.subscribers[event] || []).filter(
(f: any) => listener && f !== listener,
)
this.subscribers[event] = (this.subscribers[event] || []).filter(f => listener && f.fn !== listener)
return this
}

Expand Down
5 changes: 5 additions & 0 deletions src/node/files-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ export async function filesAsync(opt: {

return paths
}

// export function glob(patterns: string[], opt: {
// cwd?: string,
// dot?: boolean
// })

0 comments on commit f1ae3cc

Please sign in to comment.