8000 Fix monostable filter scope forward reference by Pablete1234 · Pull Request #1078 · PGMDev/PGM · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix monostable filter scope forward reference #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ default <T extends MatchModule> Optional<T> moduleOptional(Class<T> cls) {
default <T extends ReactorFactory.Reactor> T reactor(ReactorFactory<T> factory) {
return getMatch().needModule(FilterMatchModule.class).getReactor(factory);
}

default <F extends Filterable<?>> F filterable(Class<F> type) {
return extractFilterable().getFilterableAncestor(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import tc.oc.pgm.filters.operator.SingleFilterFunction;

public class MonostableFilter extends SingleFilterFunction
implements TypedFilter<MatchQuery>, ReactorFactory<MonostableFilter.Reactor> {
implements TypedFilter<MatchQuery>, ReactorFactory<MonostableFilter.Reactor<?>> {

private final Duration duration;
private final Class<? extends Filterable<?>> scope;

public static Filter afterMatchStart(Duration duration) {
return after(MatchPhaseFilter.RUNNING, duration);
Expand All @@ -42,7 +41,6 @@ public static Filter after(Filter filter, Duration duration) {
public MonostableFilter(Filter filter, Duration duration) {
super(filter);
this.duration = duration;
this.scope = Filterables.scope(filter);
}

@Override
Expand All @@ -52,32 +50,37 @@ public Class<MatchQuery> queryType() {

@Override
public boolean matches(MatchQuery query) {
boolean response = this.filter.response(query);
final Filterable<?> filterable = query.extractFilterable().getFilterableAncestor(this.scope);
if (filterable == null)
throw new IllegalArgumentException(
"The scope of this filter does not match the query it received");

return query.reactor(this).matches(filterable, response);
return query.reactor(this).matches(query);
}

@Override
public MonostableFilter.Reactor createReactor(Match match, FilterMatchModule fmm) {
return new Reactor(match, fmm);
public Reactor<?> createReactor(Match match, FilterMatchModule fmm) {
return new Reactor<>(match, fmm, Filterables.scope(filter));
}

protected final class Reactor extends ReactorFactory.Reactor implements Tickable {
protected final class Reactor<F extends Filterable<?>> extends ReactorFactory.Reactor
implements Tickable {

private final Class<F> scope;

// Filterables that currently pass the inner filter, mapped to the instants that they expire.
// They are not actually removed until the inner filter goes false.
final Map<Filterable<?>, Instant> endTimes = new HashMap<>();

public Reactor(Match match, FilterMatchModule fmm) {
public Reactor(Match match, FilterMatchModule fmm, Class<F> scope) {
super(match, fmm);
this.scope = scope;
match.addTickable(this, MatchScope.LOADED);
fmm.onChange(scope, filter, this::matches);
}

boolean matches(MatchQuery query) {
final Filterable<?> filterable = query.filterable(this.scope);
if (filterable == null) return false;

return matches(filterable, filter.response(query));
}

boolean matches(Filterable<?> filterable, boolean response) {
if (response) { // If inner filter still matches, check if the time has expired
final Instant now = this.match.getTick().instant;
Expand Down
0