🧊 feat: fix createEventEmitter dispatch, add once and reset - #506
Open
ashenoooone wants to merge 1 commit into
Open
🧊 feat: fix createEventEmitter dispatch, add once and reset#506ashenoooone wants to merge 1 commit into
ashenoooone wants to merge 1 commit into
Conversation
…пшоту, once и reset Падающий листенер обрывал forEach, подписчики после него не получали событие, а исключение всплывало в месте вызова push. Set.forEach ещё и обходит записи, добавленные во время итерации, поэтому подписка внутри листенера получала текущее событие. - push идёт по копии, ошибка листенера уходит в queueMicrotask - once с отпиской, снимается и по исходной ссылке, как в Node - reset для одного события и для всех - useSubscribeEffect для подписчиков без ререндера - мапа по keyof Events вместо string, убраны четыре as string - объявлен EventEmitterApi, на который уже ссылался JSDoc Листенер, снятый во время рассылки, теперь получает событие в полёте, как в Node и DOM. Это изменение поведения.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #505
pushdispatched withSet.forEachover the live listener set. A listener that threw killed the loop, so every listener registered after it never fired and the exception surfaced at thepushcall site instead of staying with the broken subscriber.Set.forEachalso visits entries added during iteration, so callingsubscribeinside a listener delivered the event being dispatched to the new listener. Dispatch now runs over a snapshot with each call isolated, and a listener error is rethrown throughqueueMicrotaskso it still reaches the global handler without breaking delivery.Added
once, which unsubscribes before invoking the listener so that a throwing one does not stay subscribed forever, and which can be cancelled either by the returned function or byunsubscribewith the original reference, matching Node'sEventEmitterandaddEventListenerwith{ once: true }. Addedreset(event)andreset(). Split offuseSubscribeEffectfor subscribers that only want a side effect, sinceuseSubscribecallssetDataon every push and re-renders either way. Keyed the listener map bykeyof Eventsinstead ofstring, which drops the fouras stringcasts, and declared theEventEmitterApi<Events>type that the JSDoc already pointed at but that did not exist, the waycreateStoredoes withStoreApi.One behavior change worth calling out: a listener removed mid dispatch now receives the in flight event, where before it did not. That matches Node and the DOM and falls out of the snapshot fix. 14 tests cover the new behavior. The demo is left alone here, since it would have to use API that is not published yet and the docs site builds demos against the released package rather than the workspace one.