mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-18 19:01: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,3 +1,4 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import process from 'node:process'
|
||||
// eslint-disable-next-line test/no-import-node-test -- Run the same public contract against each actual implementation.
|
||||
import { test } from 'node:test'
|
||||
@@ -12,3 +13,37 @@ for (const { name, Artplayer } of targets) {
|
||||
for (const [id, verify] of Object.entries(emitterContracts))
|
||||
test(`${id}: ${name}`, () => verify(Artplayer.Emitter))
|
||||
}
|
||||
|
||||
test('EVENT.prototype-event-names: candidate supports names that collide with Object.prototype', () => {
|
||||
for (const name of ['__proto__', 'constructor', 'toString', 'hasOwnProperty']) {
|
||||
assert.throws(() => new targets[0].Artplayer.Emitter().on(name, () => {}), error => error.name === 'TypeError')
|
||||
for (const { Artplayer } of targets.slice(1)) {
|
||||
const emitter = new Artplayer.Emitter()
|
||||
let calls = 0
|
||||
const fn = () => calls++
|
||||
emitter.emit(name).off(name)
|
||||
const prototype = Object.getPrototypeOf(emitter.e)
|
||||
emitter.on(name, fn).once(name, fn)
|
||||
emitter.emit(name).emit(name).off(name, fn).emit(name)
|
||||
assert.equal(calls, 3)
|
||||
assert.equal(Object.prototype.hasOwnProperty.call(emitter.e, name), false)
|
||||
assert.equal(Object.getPrototypeOf(emitter.e), prototype)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('EVENT.once-nested-snapshot: candidate consumes a once registration across nested dispatch snapshots', () => {
|
||||
for (const [index, { Artplayer }] of targets.entries()) {
|
||||
const emitter = new Artplayer.Emitter()
|
||||
let nested = false
|
||||
let calls = 0
|
||||
emitter.on('value', () => {
|
||||
if (!nested) {
|
||||
nested = true
|
||||
emitter.emit('value')
|
||||
}
|
||||
}).once('value', () => calls++)
|
||||
emitter.emit('value')
|
||||
assert.equal(calls, index === 0 ? 2 : 1)
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user