Skip to content

Commit

Permalink
extend/filter
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Mar 11, 2023
1 parent 3ad21b9 commit b2b73de
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/extend/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,59 @@ const typeAlias = {
'after_render:html': '_after_html_render'
};

interface FilterOptions {
context?: any;
args?: any;
}

interface StoreFunction {
(...args: any[]): any;
priority?: number;
}

interface Store {
[key: string]: StoreFunction[]
}

class Filter {
public store: any;
public store: Store;

constructor() {
this.store = {};
}

list(type) {
list(): Store;
list(type: string): StoreFunction[];
list(type?: string) {
if (!type) return this.store;
return this.store[type] || [];
}

register(type, fn, priority) {
register(fn: StoreFunction, priority: number);
register(type?: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
if (!priority) {
if (typeof type === 'function') {
priority = fn;
priority = fn as number;
fn = type;
type = 'after_post_render';
}
}

if (typeof fn !== 'function') throw new TypeError('fn must be a function');

type = typeAlias[type] || type;
type = typeAlias[type as string] || type;
priority = priority == null ? 10 : priority;

const store = this.store[type] || [];
this.store[type] = store;
const store = this.store[type as string] || [];
this.store[type as string] = store;

fn.priority = priority;
store.push(fn);

store.sort((a, b) => a.priority - b.priority);
}

unregister(type, fn) {
unregister(type: string, fn: StoreFunction) {
if (!type) throw new TypeError('type is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

Expand All @@ -55,7 +72,7 @@ class Filter {
if (index !== -1) list.splice(index, 1);
}

exec(type, data, options = {}) {
exec(type: string, data, options: FilterOptions = {}) {
const filters = this.list(type);
if (filters.length === 0) return Promise.resolve(data);

Expand All @@ -70,7 +87,7 @@ class Filter {
})).then(() => args[0]);
}

execSync(type, data, options = {}) {
execSync(type: string, data, options: FilterOptions = {}) {
const filters = this.list(type);
const filtersLen = filters.length;
if (filtersLen === 0) return data;
Expand Down

0 comments on commit b2b73de

Please sign in to comment.