addFilter('my_filter', function(myVar,context,context2)-- myVar = 'maybe' (set by the other filter with priortiy: 5)-- context = 'init'-- context2 = 'something'-- update myVar to 'no'return'no'end, 10)
my_script_2.lua
addFilter('my_filter', function(myVar,context,context2)-- myVar = 'yes' (set by initial applyFilters())-- context = 'init'-- context2 = 'something'-- update myVar to 'maybe' (note the priority: 5)return'maybe'end, 5)
local thisthis = { storage = { actions = {}, filters = {} },addAction=function(action,callback,priority,context)iftype(action) =='string' andtype(callback) =='function' then priority =tonumber(priority or10) this._addHook('actions', action, callback, priority, context)endreturn thisend,doAction=function(...)local args = { ... }local action =table.remove(args, 1)iftype(action) =='string' then this._runHook('actions', action, args)endreturn thisend,removeAction=function(action,callback)iftype(action) =='string' then this._removeHook('actions', action, callback)endreturn thisend,addFilter=function(filter,callback,priority,context)iftype(filter) =='string' andtype(callback) =='function' then priority =tonumber(priority or10) this._addHook('filters', filter, callback, priority, context)endreturn thisend,applyFilters=function(...)local args = { ... }local filter =table.remove(args, 1)iftype(filter) =='string' thenreturn this._runHook('filters', filter, args)endreturn thisend,removeFilter=function(filter,callback)iftype(filter) =='string' then this._removeHook('filters', filter, callback)endreturn thisend,_removeHook=function(type,hook,callback,context)ifnot this.storage[type][hook] thenreturnendifnot callback then this.storage[type][hook] = {}elselocal handlers = this.storage[type][hook]--local iifnot context thenfor i =#handlers, 1, -1doif handlers[i].callback == callback thentable.remove(handlers, i)endendelsefor i =#handlers, 1, -1dolocal handler = handlers[i]if handler.callback == callback and handler.context == context thentable.remove(handlers, i)endendendendend,_addHook=function(type,hook,callback,priority,context)local hookObject = { callback = callback, priority = priority, context = context }local hooks = this.storage[type][hook]if hooks thentable.insert(hooks, hookObject) hooks = this._hookInsertSort(hooks)else hooks = { hookObject }end this.storage[type][hook] = hooksend,_hookInsertSort=function(hooks)local tmpHook, j, prevHookfor i =2, #hooks do tmpHook = hooks[i] j = i prevHook = hooks[j -1]while prevHook and prevHook.priority > tmpHook.priority do hooks[j] = hooks[j -1] j = j -1 prevHook = hooks[j -1]end hooks[j] = tmpHookendreturn hooksend,_runHook=function(type,hook,args)local handlers = this.storage[type][hook]ifnot handlers thenreturntype=='filters' and args[1] orfalseendlocal i =1local len =#handlersiftype=='filters' thenfor i =1, len do args[1] = handlers[i].callback(unpack(args))endelsefor i =1, len do handlers[i].callback(unpack(args))endendreturntype=='filters' and args[1] ortrueend}-- history storagelocal actionHistory = {}addAction=function(...) this.addAction(...)return thisendremoveAction=function(...) this.removeAction(...)return thisenddoAction=function(...)local args = {...}local action = args[1] actionHistory[action] =1 this.doAction(...) actionHistory[action] =0return thisenddoingAction=function(action)return actionHistory[action] ==1enddidAction=function(action)return actionHistory[action] ~=nilendcurrentAction=function()for k inpairs(actionHistory) doif actionHistory[k] thenreturn kendendreturnfalseendaddFilter=function(...) this.addFilter(...)return thisendremoveFilter=function(...) this.removeFilter(...)return thisendapplyFilters=function(...)return this.applyFilters(...)end