Skip to content

Commit

Permalink
fix(add-sync-map-filter): action filter types and built-in filter (#148)
Browse files Browse the repository at this point in the history
* fix(add-sync-map-filter): action filter types of resent action

* fix(add-sync-map-filter): don’t resend `changed` actions that didn’t pass filter

* Update add-sync-map/index.js

---------

Co-authored-by: Andrey Sitnik <[email protected]>
  • Loading branch information
euaaaio and ai authored Aug 15, 2023
1 parent ebc9815 commit 90a0712
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions add-sync-map/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type {
LoguxSubscribeAction,
SyncMapChangeAction,
SyncMapChangedAction,
SyncMapCreateAction,
SyncMapCreatedAction,
SyncMapDeleteAction,
SyncMapDeletedAction,
SyncMapTypes,
SyncMapValues
} from '@logux/actions'
Expand Down Expand Up @@ -58,7 +61,10 @@ export function NoConflictResolution<
interface SyncMapActionFilter<Value extends SyncMapValues> {
(
ctx: Context,
action: SyncMapCreateAction<Value> | SyncMapDeleteAction,
action:
| SyncMapChangedAction<Value>
| SyncMapCreatedAction<Value>
| SyncMapDeletedAction,
meta: ServerMeta
): boolean | Promise<boolean>
}
Expand All @@ -82,7 +88,7 @@ interface SyncMapOperations<Value extends SyncMapValues> {
time: number,
action: SyncMapChangeAction<Value>,
meta: ServerMeta
): boolean | Promise<boolean | void> | void
): boolean | Promise<boolean | void> | void

create?(
ctx: Context,
Expand Down
8 changes: 8 additions & 0 deletions add-sync-map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function buildFilter(filter) {
if (action.fields[key] !== filter[key]) return false
}
}
if (action.type.endsWith('/changed')) {
for (let key in filter) {
if (
key in action.fields &&
action.fields[key] !== filter[key]
) return false
}
}
return true
}
}
Expand Down

0 comments on commit 90a0712

Please sign in to comment.