@akashic/trigger
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -17,7 +17,8 @@ function isPromise(target) { | ||
if (typeof paramsOrFunc === "function") { | ||
this._handlers.push({ | ||
this._addHandler({ | ||
func: paramsOrFunc, | ||
owner, | ||
once: false, | ||
name: undefined | ||
name: undefined, | ||
filter: undefined, | ||
}); | ||
@@ -27,18 +28,10 @@ } | ||
const params = paramsOrFunc; | ||
if (typeof params.index === "number") { | ||
this._handlers.splice(params.index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name | ||
}); | ||
} | ||
else { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name | ||
}); | ||
} | ||
const index = typeof params.index === "number" ? params.index : undefined; | ||
this._addHandler({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name, | ||
filter: params.filter, | ||
}, index); | ||
} | ||
@@ -49,7 +42,8 @@ this.length = this._handlers.length; | ||
if (typeof paramsOrFunc === "function") { | ||
this._handlers.push({ | ||
this._addHandler({ | ||
func: paramsOrFunc, | ||
owner, | ||
once: true, | ||
name: undefined | ||
name: undefined, | ||
filter: undefined, | ||
}); | ||
@@ -59,18 +53,10 @@ } | ||
const params = paramsOrFunc; | ||
if (typeof params.index === "number") { | ||
this._handlers.splice(params.index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name | ||
}); | ||
} | ||
else { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name | ||
}); | ||
} | ||
const index = typeof params.index === "number" ? params.index : undefined; | ||
this._addHandler({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name, | ||
filter: params.filter | ||
}, index); | ||
} | ||
@@ -81,3 +67,3 @@ this.length = this._handlers.length; | ||
* このTriggerにハンドラを追加する。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。実装の変化のため、 `func` が `boolean` を返した時の動作はサポートされていない。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。 | ||
*/ | ||
@@ -104,2 +90,4 @@ handle(owner, func, name) { | ||
const handler = handlers[i]; | ||
if (handler.filter && !handler.filter(handler)) | ||
continue; | ||
const ret = handler.func.call(handler.owner, arg); | ||
@@ -131,3 +119,6 @@ const returnedTruthy = !isPromise(ret) && !!ret; | ||
const handler = this._handlers[i]; | ||
if (condition.func === handler.func && condition.owner === handler.owner && condition.name === handler.name) { | ||
if (condition.func === handler.func && | ||
condition.owner === handler.owner && | ||
condition.name === handler.name && | ||
condition.filter === handler.filter) { | ||
this._handlers.splice(i, 1); | ||
@@ -178,2 +169,25 @@ --this.length; | ||
*/ | ||
_addHandler(params, index) { | ||
if (index == null) { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: params.once, | ||
name: params.name, | ||
filter: params.filter, | ||
}); | ||
} | ||
else { | ||
this._handlers.splice(index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: params.once, | ||
name: params.name, | ||
filter: params.filter, | ||
}); | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
_comparePartial(target, compare) { | ||
@@ -186,2 +200,4 @@ if (target.func !== undefined && target.func !== compare.func) | ||
return false; | ||
if (target.filter !== undefined && target.filter !== compare.filter) | ||
return false; | ||
return true; | ||
@@ -188,0 +204,0 @@ } |
@@ -1,1 +0,1 @@ | ||
class e{constructor(){this._handlers=[],this.length=0}add(e,n){if("function"==typeof e)this._handlers.push({func:e,owner:n,once:!1,name:void 0});else{const n=e;"number"==typeof n.index?this._handlers.splice(n.index,0,{func:n.func,owner:n.owner,once:!1,name:n.name}):this._handlers.push({func:n.func,owner:n.owner,once:!1,name:n.name})}this.length=this._handlers.length}addOnce(e,n){if("function"==typeof e)this._handlers.push({func:e,owner:n,once:!0,name:void 0});else{const n=e;"number"==typeof n.index?this._handlers.splice(n.index,0,{func:n.func,owner:n.owner,once:!0,name:n.name}):this._handlers.push({func:n.func,owner:n.owner,once:!0,name:n.name})}this.length=this._handlers.length}handle(e,n,t){this.add(n?{owner:e,func:n,name:t}:{func:e})}fire(e){if(!this._handlers||!this._handlers.length)return;const n=this._handlers.concat();for(let i=0;i<n.length;i++){const s=n[i],h=s.func.call(s.owner,e);if(!(t=h,null!=t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then||!h)||s.once){if(!this._handlers)continue;const e=this._handlers.indexOf(s);-1!==e&&this._handlers.splice(e,1)}}var t;null!=this._handlers&&(this.length=this._handlers.length)}contains(e,n){const t="function"==typeof e?{func:e,owner:n}:e;for(let e=0;e<this._handlers.length;e++)if(this._comparePartial(t,this._handlers[e]))return!0;return!1}remove(e,n){const t="function"==typeof e?{func:e,owner:n}:e;for(let e=0;e<this._handlers.length;e++){const n=this._handlers[e];if(t.func===n.func&&t.owner===n.owner&&t.name===n.name)return this._handlers.splice(e,1),void--this.length}}removeAll(e){const n=[];if(e)for(let t=0;t<this._handlers.length;t++){const i=this._handlers[t];this._comparePartial(e,i)||n.push(i)}this._handlers=n,this.length=this._handlers.length}destroy(){this._handlers=null,this.length=null}destroyed(){return null===this._handlers}_comparePartial(e,n){return(void 0===e.func||e.func===n.func)&&((void 0===e.owner||e.owner===n.owner)&&(void 0===e.name||e.name===n.name))}}class n extends e{constructor(e,n,t){super(),this.chain=e,this.filter=null!=n?n:null,this.filterOwner=t,this._isActivated=!1}add(e,n){super.add(e,n),this._isActivated||(this.chain.add(this._onChainTriggerFired,this),this._isActivated=!0)}addOnce(e,n){super.addOnce(e,n),this._isActivated||(this.chain.add(this._onChainTriggerFired,this),this._isActivated=!0)}remove(e,n){super.remove(e,n),0===this.length&&this._isActivated&&(this.chain.remove(this._onChainTriggerFired,this),this._isActivated=!1)}removeAll(e){super.removeAll(e),0===this.length&&this._isActivated&&(this.chain.remove(this._onChainTriggerFired,this),this._isActivated=!1)}destroy(){super.destroy(),this.chain.remove(this._onChainTriggerFired,this),this.filter=null,this.filterOwner=null,this._isActivated=!1}_onChainTriggerFired(e){this.filter&&!this.filter.call(this.filterOwner,e)||this.fire(e)}}export{n as ChainTrigger,e as Trigger}; | ||
class e{constructor(){this._handlers=[],this.length=0}add(e,n){if("function"==typeof e)this._addHandler({func:e,owner:n,once:!1,name:void 0,filter:void 0});else{const n=e,t="number"==typeof n.index?n.index:void 0;this._addHandler({func:n.func,owner:n.owner,once:!1,name:n.name,filter:n.filter},t)}this.length=this._handlers.length}addOnce(e,n){if("function"==typeof e)this._addHandler({func:e,owner:n,once:!0,name:void 0,filter:void 0});else{const n=e,t="number"==typeof n.index?n.index:void 0;this._addHandler({func:n.func,owner:n.owner,once:!0,name:n.name,filter:n.filter},t)}this.length=this._handlers.length}handle(e,n,t){this.add(n?{owner:e,func:n,name:t}:{func:e})}fire(e){if(!this._handlers||!this._handlers.length)return;const n=this._handlers.concat();for(let i=0;i<n.length;i++){const r=n[i];if(r.filter&&!r.filter(r))continue;const s=r.func.call(r.owner,e);if(!(t=s,null!=t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then||!s)||r.once){if(!this._handlers)continue;const e=this._handlers.indexOf(r);-1!==e&&this._handlers.splice(e,1)}}var t;null!=this._handlers&&(this.length=this._handlers.length)}contains(e,n){const t="function"==typeof e?{func:e,owner:n}:e;for(let e=0;e<this._handlers.length;e++)if(this._comparePartial(t,this._handlers[e]))return!0;return!1}remove(e,n){const t="function"==typeof e?{func:e,owner:n}:e;for(let e=0;e<this._handlers.length;e++){const n=this._handlers[e];if(t.func===n.func&&t.owner===n.owner&&t.name===n.name&&t.filter===n.filter)return this._handlers.splice(e,1),void--this.length}}removeAll(e){const n=[];if(e)for(let t=0;t<this._handlers.length;t++){const i=this._handlers[t];this._comparePartial(e,i)||n.push(i)}this._handlers=n,this.length=this._handlers.length}destroy(){this._handlers=null,this.length=null}destroyed(){return null===this._handlers}_addHandler(e,n){null==n?this._handlers.push({func:e.func,owner:e.owner,once:e.once,name:e.name,filter:e.filter}):this._handlers.splice(n,0,{func:e.func,owner:e.owner,once:e.once,name:e.name,filter:e.filter})}_comparePartial(e,n){return(void 0===e.func||e.func===n.func)&&((void 0===e.owner||e.owner===n.owner)&&((void 0===e.name||e.name===n.name)&&(void 0===e.filter||e.filter===n.filter)))}}class n extends e{constructor(e,n,t){super(),this.chain=e,this.filter=null!=n?n:null,this.filterOwner=t,this._isActivated=!1}add(e,n){super.add(e,n),this._isActivated||(this.chain.add(this._onChainTriggerFired,this),this._isActivated=!0)}addOnce(e,n){super.addOnce(e,n),this._isActivated||(this.chain.add(this._onChainTriggerFired,this),this._isActivated=!0)}remove(e,n){super.remove(e,n),0===this.length&&this._isActivated&&(this.chain.remove(this._onChainTriggerFired,this),this._isActivated=!1)}removeAll(e){super.removeAll(e),0===this.length&&this._isActivated&&(this.chain.remove(this._onChainTriggerFired,this),this._isActivated=!1)}destroy(){super.destroy(),this.chain.remove(this._onChainTriggerFired,this),this.filter=null,this.filterOwner=null,this._isActivated=!1}_onChainTriggerFired(e){this.filter&&!this.filter.call(this.filterOwner,e)||this.fire(e)}}export{n as ChainTrigger,e as Trigger}; |
import type { ChainTriggerLike, ChainTriggerFilterFunction } from "./ChainTriggerLike"; | ||
import { Trigger } from "./Trigger"; | ||
import type { TriggerLike, TriggerRemoveConditions, HandlerFunction } from "./TriggerLike"; | ||
import type { TriggerLike, TriggerRemoveConditions, HandlerFunction, TriggerAddParameters } from "./TriggerLike"; | ||
/** | ||
@@ -37,4 +37,6 @@ * 他のTriggerLikeに反応して発火するイベント通知機構。 | ||
constructor(chain: TriggerLike<T>, filter?: ChainTriggerFilterFunction<T>, filterOwner?: unknown); | ||
add(paramsOrHandler: any, owner?: unknown): void; | ||
addOnce(paramsOrHandler: any, owner?: unknown): void; | ||
add(func: HandlerFunction<T>, owner?: unknown): void; | ||
add(params: TriggerAddParameters<T>): void; | ||
addOnce(func: HandlerFunction<T>, owner?: unknown): void; | ||
addOnce(params: TriggerAddParameters<T>): void; | ||
remove(func: HandlerFunction<T>, owner?: unknown): void; | ||
@@ -41,0 +43,0 @@ remove(params: TriggerRemoveConditions<T>): void; |
@@ -42,3 +42,3 @@ import type { HandlerFunction, TriggerHandler, TriggerAddParameters, TriggerRemoveConditions, TriggerSearchConditions, TriggerLike } from "./TriggerLike"; | ||
* このTriggerにハンドラを追加する。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。実装の変化のため、 `func` が `boolean` を返した時の動作はサポートされていない。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。 | ||
*/ | ||
@@ -75,3 +75,3 @@ handle(owner: any, func?: HandlerFunction<T>, name?: string): void; | ||
* 関数が `func` であり、かつオーナーが `owner` であるハンドラを削除する。 | ||
* 同じ組み合わせで複数登録されている場合、一つだけ削除する。 | ||
* 同じ組み合わせで複数登録されている場合、index が最も小さいハンドラのみ削除する。 | ||
* | ||
@@ -84,3 +84,3 @@ * @param func 削除条件として用いるハンドラの関数 | ||
* 指定した条件に完全一致するハンドラを削除する。 | ||
* 同じ組み合わせで複数登録されている場合、一つだけ削除する。 | ||
* 同じ組み合わせで複数登録されている場合、index が最も小さいハンドラのみ削除する。 | ||
* | ||
@@ -112,3 +112,7 @@ * @param params 削除するハンドラの条件 | ||
*/ | ||
_addHandler(params: TriggerHandler<T>, index?: number): void; | ||
/** | ||
* @private | ||
*/ | ||
_comparePartial(target: TriggerSearchConditions<T>, compare: TriggerSearchConditions<T>): boolean; | ||
} |
@@ -12,7 +12,8 @@ import { isPromise } from "./isPromise"; | ||
if (typeof paramsOrFunc === "function") { | ||
this._handlers.push({ | ||
this._addHandler({ | ||
func: paramsOrFunc, | ||
owner, | ||
once: false, | ||
name: undefined | ||
name: undefined, | ||
filter: undefined, | ||
}); | ||
@@ -22,18 +23,10 @@ } | ||
const params = paramsOrFunc; | ||
if (typeof params.index === "number") { | ||
this._handlers.splice(params.index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name | ||
}); | ||
} | ||
else { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name | ||
}); | ||
} | ||
const index = typeof params.index === "number" ? params.index : undefined; | ||
this._addHandler({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: false, | ||
name: params.name, | ||
filter: params.filter, | ||
}, index); | ||
} | ||
@@ -44,7 +37,8 @@ this.length = this._handlers.length; | ||
if (typeof paramsOrFunc === "function") { | ||
this._handlers.push({ | ||
this._addHandler({ | ||
func: paramsOrFunc, | ||
owner, | ||
once: true, | ||
name: undefined | ||
name: undefined, | ||
filter: undefined, | ||
}); | ||
@@ -54,18 +48,10 @@ } | ||
const params = paramsOrFunc; | ||
if (typeof params.index === "number") { | ||
this._handlers.splice(params.index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name | ||
}); | ||
} | ||
else { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name | ||
}); | ||
} | ||
const index = typeof params.index === "number" ? params.index : undefined; | ||
this._addHandler({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: true, | ||
name: params.name, | ||
filter: params.filter | ||
}, index); | ||
} | ||
@@ -76,3 +62,3 @@ this.length = this._handlers.length; | ||
* このTriggerにハンドラを追加する。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。実装の変化のため、 `func` が `boolean` を返した時の動作はサポートされていない。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。 | ||
*/ | ||
@@ -99,2 +85,4 @@ handle(owner, func, name) { | ||
const handler = handlers[i]; | ||
if (handler.filter && !handler.filter(handler)) | ||
continue; | ||
const ret = handler.func.call(handler.owner, arg); | ||
@@ -126,3 +114,6 @@ const returnedTruthy = !isPromise(ret) && !!ret; | ||
const handler = this._handlers[i]; | ||
if (condition.func === handler.func && condition.owner === handler.owner && condition.name === handler.name) { | ||
if (condition.func === handler.func && | ||
condition.owner === handler.owner && | ||
condition.name === handler.name && | ||
condition.filter === handler.filter) { | ||
this._handlers.splice(i, 1); | ||
@@ -173,2 +164,25 @@ --this.length; | ||
*/ | ||
_addHandler(params, index) { | ||
if (index == null) { | ||
this._handlers.push({ | ||
func: params.func, | ||
owner: params.owner, | ||
once: params.once, | ||
name: params.name, | ||
filter: params.filter, | ||
}); | ||
} | ||
else { | ||
this._handlers.splice(index, 0, { | ||
func: params.func, | ||
owner: params.owner, | ||
once: params.once, | ||
name: params.name, | ||
filter: params.filter, | ||
}); | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
_comparePartial(target, compare) { | ||
@@ -181,4 +195,6 @@ if (target.func !== undefined && target.func !== compare.func) | ||
return false; | ||
if (target.filter !== undefined && target.filter !== compare.filter) | ||
return false; | ||
return true; | ||
} | ||
} |
@@ -9,2 +9,3 @@ /** | ||
export type HandlerAsyncFunction<T> = (arg: T) => Promise<unknown>; | ||
export type HandlerFilterFunction<T> = (handler: TriggerHandler<T>) => boolean; | ||
/** | ||
@@ -31,2 +32,6 @@ * Triggerのハンドラ。 | ||
name: string | null | undefined; | ||
/** | ||
* ハンドラを発火する条件。 | ||
*/ | ||
filter: HandlerFilterFunction<T> | undefined; | ||
} | ||
@@ -55,2 +60,6 @@ /** | ||
index?: number; | ||
/** | ||
* ハンドラを発火する条件。 | ||
*/ | ||
filter?: HandlerFilterFunction<T>; | ||
} | ||
@@ -85,2 +94,10 @@ /** | ||
name?: string; | ||
/** | ||
* ハンドラを発火する条件。 | ||
* | ||
* 登録時 `filter` に指定された値がこの値と同値でないハンドラは削除されない。 | ||
* 省略された場合、 `remove()` では `undefined` とみなされる。 | ||
* 省略された場合、 `removeAll()` ではこの値に関係なく他の条件にマッチする限り削除される。 | ||
*/ | ||
filter?: HandlerFilterFunction<T>; | ||
} | ||
@@ -94,2 +111,3 @@ /** | ||
name?: string | null; | ||
filter?: HandlerFilterFunction<T>; | ||
} | ||
@@ -130,3 +148,3 @@ /** | ||
* このTriggerにハンドラを追加する。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。実装の変化のため、 `func` が `boolean` を返した時の動作はサポートされていない。 | ||
* @deprecated 互換性のために残されている。代わりに `add()` を利用すべきである。 | ||
*/ | ||
@@ -133,0 +151,0 @@ handle(owner: any, func?: HandlerFunction<T>, name?: string): void; |
{ | ||
"name": "@akashic/trigger", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "An event emitting module for TypeScript", | ||
@@ -43,19 +43,19 @@ "type": "module", | ||
"devDependencies": { | ||
"@akashic/eslint-config": "^1.1.1", | ||
"@rollup/plugin-commonjs": "^25.0.4", | ||
"@rollup/plugin-node-resolve": "^15.2.0", | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"@rollup/plugin-typescript": "^11.1.2", | ||
"@types/jest": "^29.5.0", | ||
"@typescript-eslint/eslint-plugin": "^5.43.0", | ||
"eslint": "^8.27.0", | ||
"jest": "^29.5.0", | ||
"@akashic/eslint-config": "^2.0.0", | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.5", | ||
"@types/jest": "^29.5.6", | ||
"@typescript-eslint/eslint-plugin": "^6.8.0", | ||
"eslint": "^8.52.0", | ||
"jest": "^29.7.0", | ||
"npm-run-all": "^4.1.5", | ||
"remark-cli": "^11.0.0", | ||
"remark-lint": "^9.1.1", | ||
"rimraf": "^4.0.0", | ||
"rollup": "^3.28.0", | ||
"ts-jest": "^29.1.0", | ||
"typescript": "^5.0.0" | ||
"remark-cli": "^12.0.0", | ||
"remark-lint": "^9.1.2", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.1.4", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
56981
1200