mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-17 18:11:20 -08:00
refactor(core): CORE-02 type emitter maps and fix nested once dispatch
This commit is contained in:
1 parent
d3c695024a
commit
362c616998
21 files changed
+943
-72
No files matched your search
@@ -1,6 +1,51 @@
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
export const emitterContracts = {
|
||||
'EVENT.lazy-registry': function (Emitter) {
|
||||
const emitter = new Emitter()
|
||||
assert.deepEqual(Object.keys(emitter), [])
|
||||
emitter.emit('empty').off('empty')
|
||||
assert.equal(Object.getOwnPropertyDescriptor(emitter, 'e').enumerable, true)
|
||||
assert.deepEqual(Object.keys(emitter.e), [])
|
||||
assert.equal(typeof emitter.e.hasOwnProperty, 'function')
|
||||
},
|
||||
'EVENT.symbol-number-channels': function (Emitter) {
|
||||
const emitter = new Emitter()
|
||||
const symbol = Symbol('channel')
|
||||
const seen = []
|
||||
emitter.on(symbol, value => seen.push(value)).on(1, value => seen.push(value))
|
||||
emitter.emit(symbol, 'symbol').emit('1', 'numeric')
|
||||
emitter.off(symbol).off(1).emit(symbol).emit('1')
|
||||
assert.deepEqual(seen, ['symbol', 'numeric'])
|
||||
},
|
||||
'EVENT.context-removal': function (Emitter) {
|
||||
const emitter = new Emitter()
|
||||
const seen = []
|
||||
function fn() {
|
||||
seen.push(this)
|
||||
}
|
||||
const a = {}
|
||||
const b = {}
|
||||
emitter.on('value', fn, a).once('value', fn, b)
|
||||
const wrapper = emitter.e.value[1].fn
|
||||
assert.equal(wrapper._, fn)
|
||||
emitter.off('value', wrapper).emit('value')
|
||||
assert.deepEqual(seen, [a])
|
||||
emitter.on('value', fn, b).off('value', fn).emit('value')
|
||||
assert.deepEqual(seen, [a])
|
||||
},
|
||||
'EVENT.once-throw-consumed': function (Emitter) {
|
||||
const emitter = new Emitter()
|
||||
const marker = new Error('once failed')
|
||||
let calls = 0
|
||||
emitter.once('value', () => {
|
||||
calls++
|
||||
throw marker
|
||||
})
|
||||
assert.throws(() => emitter.emit('value'), error => error === marker)
|
||||
emitter.emit('value')
|
||||
assert.equal(calls, 1)
|
||||
},
|
||||
'EVENT.chain-context-arguments': function (Emitter) {
|
||||
const emitter = new Emitter()
|
||||
const context = {}
|
||||
|
||||
Reference in new issue
Block a user