@filter not working as expected

25 Apr 2019, 13:32

My GameState has a map (MapSchema) of players (Player). Inside Player, I have a field which should be private (visible to the player and not the entire room). I am trying to use @filter, but without success.

export class SecretSchema extends Schema {
    @type("number")
    xyz: number;

    @type("number")
    abc: number;
}
export class Player extends Schema {
    @filter(function(this: Player, client: any, value: GameMap, root: GameState) {
            console.log("filter");
            return true;
    })
    @type(SecretSchema)
    secretVar: SecretSchema;
}
export class GameState extends Schema {
    @type({ map: Player })
    players = new MapSchema<Player>();
}

First, I have typescript errors on @filter. It says that Player and Schema are incompatible. But apart from that, "filter" is not printed to the console and I get the whole state on all Players in the room.

25 Apr 2019, 13:33

@emarkk filters are not working yet, they're exposed but they don't really work, I'm going to remove it in the next version until we can actually use them

25 Apr 2019, 14:23

Ok, I get it. The same goes for @nosync?

25 Apr 2019, 14:24

@nosync is for FossilDelta only, it has no effect using the Schema serializer

1 Apr 2020, 08:57

Has this been fixed ? I am trying to use Filters to hide other players Hand in a card game but I face an issue

Firstly, I get typescript error saying Schema of Value does not match
Argument of type '(this: PlayerTurn, value: ArraySchema<Card>, client: Client, root?: GameState) => boolean' is not assignable to parameter of type 'FilterCallback<PlayerTurn, Schema, GameState>'.
Types of parameters 'client' and 'value' are incompatible.
Type 'Schema' is not assignable to type 'Client'.
Type 'Schema' is missing the following properties from type 'WebSocket': binaryType, bufferedAmount, extensions, protocol, and 32 more.ts(2345)

If i change the value to the Type itself, the filter is never invoked.

1 Apr 2020, 09:28

Hi @meetsreekanth, filters are experimental right now, but it should work for the use-case you've just described.

I think the order of your parameters is not correct, the filter callback type looks like this:

export type FilterCallback<
    T extends Schema = any,
    V = any,
    R extends Schema = any
> = (this: T, client: Client, value: V, root?: R) => boolean;

It's also important not to use arrow functions on filters, so this should work:

@filter(function (this: PlayerTurn, client: Client, value: ArraySchema<Card>, root?: GameState) {
  return // ??
})

Hope this helps, cheers!