Skip to content

Commit

Permalink
feat(utils): add function filterEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Jul 29, 2023
1 parent 7462c4d commit d7c162f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .yarn/versions/7bf34662.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@codemirror-toolkit/extensions": patch
"@codemirror-toolkit/utils": patch
14 changes: 5 additions & 9 deletions packages/extensions/src/updateListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Extension } from '@codemirror/state'
import { StateEffect, StateField } from '@codemirror/state'
import type { ViewUpdate } from '@codemirror/view'
import { EditorView } from '@codemirror/view'
import { isEffectOfType, mapEffectValue } from '@codemirror-toolkit/utils'
import { filterEffects, mapEffectValue } from '@codemirror-toolkit/utils'

export type UpdateListener = (update: ViewUpdate) => void

Expand All @@ -20,7 +20,7 @@ const updateListenerField = /*#__PURE__*/ StateField.define<UpdateListenerSet>({
return new Set()
},
update(listeners, transaction) {
transaction.effects.filter(isEffectOfType(UpdateListenerEffect)).forEach(
filterEffects(transaction.effects, UpdateListenerEffect).forEach(
mapEffectValue((action) => {
action.add?.forEach((listener) => listeners.add(listener))
action.remove?.forEach((listener) => listeners.delete(listener))
Expand All @@ -29,13 +29,9 @@ const updateListenerField = /*#__PURE__*/ StateField.define<UpdateListenerSet>({
return listeners
},
provide(thisField) {
return EditorView.updateListener.from(
thisField,
(listeners) =>
function aggregatedListener(update) {
listeners.forEach((listener) => listener(update))
},
)
return EditorView.updateListener.from(thisField, (listeners) => (update) => {
listeners.forEach((listener) => listener(update))
})
},
})

Expand Down
7 changes: 7 additions & 0 deletions packages/utils/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export function mapEffectValue<T, R>(
? (effect: StateEffect<T>) => arg1(effect.value)
: arg2!(arg1.value)
}

export function filterEffects<T>(
effects: readonly StateEffect<unknown>[],
type: StateEffectType<T>,
): StateEffect<T>[] {
return effects.filter(isEffectOfType(type))
}

0 comments on commit d7c162f

Please sign in to comment.