Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 1x 1x 1x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 2x 2x 113x 27x 27x 113x 262x 262x 283x 254x 254x 283x 283x 262x 113x 262x 113x 262x 262x 262x 113x 265x 1x 1x 265x 2x 2x 262x 262x 262x 262x 262x 236x 265x 113x 64x 3x 3x 3x 3x 3x 61x 64x 60x 64x 211x 211x 211x 38x 64x 113x 14x 14x 17x 17x 17x 17x 11x 11x 17x 14x 113x 14x 113x 14x 14x 14x 113x 17x 3x 3x 14x 14x 14x 17x 113x 12x 3x 3x 3x 3x 3x 9x 12x 8x 12x 13x 13x 13x 8x 12x 113x 79x 79x 79x 75x 75x 73x 71x 71x 73x 4x 4x 4x 4x 3x 2x 2x 3x 2x 6x 79x 113x 26x 26x 26x 26x 26x 26x 26x 26x 26x 113x 373x 373x 373x 373x 373x 373x 373x 373x 248x 248x 298x 298x 298x 140x 145x 111x 111x 131x 26x 26x 145x 72x 298x 180x 305x 373x 113x 210x 210x 210x 210x 210x 53x 53x 157x 157x 210x 106x 106x 131x 131x 131x 108x 115x 35x 35x 80x 80x 108x 131x 106x 157x 210x 113x 63x 63x 5x 5x 58x 58x 58x 130x 130x 58x 58x 63x 113x 63x 63x 137x 137x 122x 136x 136x 122x 137x 63x 63x 113x 42x 1x 1x 41x 41x 70x 70x 1x 1x 69x 70x 26x 70x 43x 43x 68x 68x 42x 113x 42x 16x 16x 16x 16x 26x 42x 113x | import { ActionHandler } from './action-handler';
import { ActionCreator } from './action';
import { ActionMapExtended, ActionMapInternal } from './action-map';
import { FlowfulError } from './error';
import { IActionHandlerMiddlewareRunner } from './action-handler-middleware';
import { IDispatchMiddlewareRunner } from './dispatch-middleware';
import { checkMode, getGlobalMode, MODE, MODE_DEV } from './mode';
export interface FlowfulOptions {
mode?: MODE;
dispatchMiddleware?: IDispatchMiddlewareRunner;
handlerMiddleware?: IActionHandlerMiddlewareRunner;
}
export interface IFlowful {
readonly mode: MODE;
hasHandler(handler: ActionHandler): boolean;
addHandler(handler: ActionHandler): boolean;
addHandlers(handlers: ActionHandler[]): boolean;
removeHandler(handler: ActionHandler): boolean;
removeHandlers(handlers: ActionHandler[]): boolean;
dispatchActionMap(actionMap: ActionMapExtended): unknown;
}
export class Flowful implements IFlowful {
protected _className = 'Flowful';
protected _handlers: Set<ActionHandler>;
protected _handlersByInput: Map<ActionCreator, Set<ActionHandler>>;
protected _dispatchMiddleware?: IDispatchMiddlewareRunner;
protected _handlerMiddleware?: IActionHandlerMiddlewareRunner;
protected _mode: MODE;
constructor(options?: FlowfulOptions) {
this._mode = options?.mode ?? getGlobalMode();
checkMode(this._mode, 'constructor');
this._handlers = new Set();
this._handlersByInput = new Map();
this._dispatchMiddleware = options?.dispatchMiddleware;
this._handlerMiddleware = options?.handlerMiddleware;
}
get mode() {
return this._mode;
}
hasHandler(handler: ActionHandler) {
return this._handlers.has(handler);
}
protected _addHandlerInputs(handler: ActionHandler) {
const inputs = handler.getInputs();
for (const input of inputs) {
if (!this._handlersByInput.has(input)) {
this._handlersByInput.set(input, new Set());
}
this._handlersByInput.get(input)!.add(handler);
}
}
protected _addHandlerOutputs(handler: ActionHandler) {
// placeholder for extensions
}
protected _addHandlerActions(handler: ActionHandler) {
this._addHandlerInputs(handler);
this._addHandlerOutputs(handler);
}
addHandler(handler: ActionHandler) {
if (!handler || !handler.invoke) {
throw new FlowfulError(`[${this._className}][addHandler/s] Invalid action handler.`, handler);
}
if (this._handlers.has(handler)) {
return false;
}
this._addHandlerActions(handler);
this._handlers.add(handler);
if (this._mode === MODE_DEV) {
this._checkCircular(handler);
}
return true;
}
addHandlers(handlers: ActionHandler[]) {
if (!Array.isArray(handlers)) {
throw new FlowfulError(
`[${this._className}][addHandlers] Invalid function argument. Action handlers should be an array.`,
handlers
);
}
const len = handlers.length;
if (!len) return true;
let result = true;
for (let i = 0; i < len; i++) {
const handler = handlers[i];
result = this.addHandler(handler) && result;
}
return result;
}
protected _removeHandlerInputs(handler: ActionHandler) {
const inputs = handler.getInputs();
for (let idx = 0, len = inputs.length; idx < len; idx++) {
const ActionCreator = inputs[idx];
const current = this._handlersByInput.get(ActionCreator)!;
current.delete(handler);
if (current.size === 0) {
this._handlersByInput.delete(ActionCreator);
}
}
}
protected _removeHandlerOutputs(handler: ActionHandler) {
// placeholder for extensions
}
protected _removeHandlerActions(handler: ActionHandler) {
this._removeHandlerInputs(handler);
this._removeHandlerOutputs(handler);
}
removeHandler(handler: ActionHandler) {
if (!this._handlers.has(handler)) {
return false;
}
this._removeHandlerActions(handler);
this._handlers.delete(handler);
return true;
}
removeHandlers(handlers: ActionHandler[]) {
if (!Array.isArray(handlers)) {
throw new FlowfulError(
`[${this._className}][removeHandlers] Invalid function argument. Action handlers should be an array.`,
handlers
);
}
const handlersLen = handlers.length;
if (!handlersLen) return true;
let result = true;
for (let handlerIndex = 0; handlerIndex < handlersLen; handlerIndex++) {
const handler = handlers[handlerIndex];
result = this.removeHandler(handler) && result;
}
return result;
}
protected _isHandlerInterested(handler: ActionHandler, actionMap: ActionMapInternal) {
const handlerInputSet = handler.getInputSet();
const handlerInputSize = handlerInputSet.size;
// we want as few lookups as possible
if (handlerInputSize <= actionMap.size) {
const handlerInputArr = handler.getInputs();
for (let i = 0; i < handlerInputSize; i++) {
if (actionMap.has(handlerInputArr[i])) {
return true;
}
}
} else {
const allActionCreators = actionMap.getActionCreators();
const handlerInputSet = handler.getInputSet();
for (const actionCreator of allActionCreators) {
if (handlerInputSet.has(actionCreator)) {
return true;
}
}
}
return false;
}
protected _throwCircular(handler: ActionHandler, stack: Set<ActionHandler>) {
let _stack = [...stack];
const index = _stack.indexOf(handler);
_stack = _stack.slice(index);
_stack.push(handler);
throw new FlowfulError(`[${this._className}] Circular dependency detected.`, {
handler,
stack: _stack,
});
}
protected _checkCircular(
handler: ActionHandler,
visited: WeakSet<ActionHandler> = new WeakSet(),
stack: Set<ActionHandler> = new Set()
) {
stack.add(handler);
visited.add(handler);
const outputs = handler.getOutputs();
if (outputs) {
const size = outputs.length;
for (let idx = 0; idx < size; idx++) {
const Output = outputs[idx];
const handlersByInput = this._handlersByInput.get(Output);
if (handlersByInput) {
for (const nextHandler of handlersByInput) {
if (!visited.has(nextHandler)) {
this._checkCircular(nextHandler, visited, stack);
}
if (stack.has(nextHandler)) {
this._throwCircular(nextHandler, stack);
}
}
}
}
}
stack.delete(handler);
}
protected _collectDependentHandlers(
handler: ActionHandler,
handlers: ActionHandler[],
visited: WeakSet<ActionHandler>
) {
if (visited.has(handler)) {
return;
}
visited.add(handler);
const outputs = handler.getOutputs();
if (outputs) {
const size = outputs.length;
for (let idx = 0; idx < size; idx++) {
const Output = outputs[idx];
const handlersByInput = this._handlersByInput.get(Output);
if (handlersByInput) {
for (const nextHandler of handlersByInput) {
if (visited.has(nextHandler)) {
continue;
}
this._collectDependentHandlers(nextHandler, handlers, visited);
}
}
}
}
handlers.push(handler);
}
protected _collectHandlers(actionMap: ActionMapInternal): ActionHandler[] {
const directHandlers = this._findDirectHandlers(actionMap);
if (!directHandlers.size) {
return [];
}
const visited: WeakSet<ActionHandler> = new WeakSet();
const handlers: ActionHandler[] = [];
for (const handler of directHandlers) {
this._collectDependentHandlers(handler, handlers, visited);
}
handlers.reverse();
return handlers;
}
protected _findDirectHandlers(actionMap: ActionMapInternal): Set<ActionHandler> {
const handlers: Set<ActionHandler> = new Set();
for (const actionCreator of actionMap.getActionCreators()) {
const current = this._handlersByInput.get(actionCreator);
if (current) {
for (const handler of current) {
handlers.add(handler);
}
}
}
return handlers;
}
protected _performDispatch(actionMap: ActionMapInternal) {
if (actionMap.size === 0) {
return;
}
const handlers = this._collectHandlers(actionMap);
for (let handlerIndex = 0, len = handlers.length; handlerIndex < len; handlerIndex++) {
const handler = handlers[handlerIndex];
if (!this._isHandlerInterested(handler, actionMap)) {
continue;
}
actionMap._attachHandler(handler);
if (this._handlerMiddleware) {
this._handlerMiddleware.run(actionMap, handler);
} else {
handler.invoke(actionMap);
}
actionMap._detachHandler();
}
// actionMap shouldn't be returned because we can't rely on it
// the middlewares might dispatch something and won't know which actionMap to return
// middlewares may return something else than actionMap too
}
dispatchActionMap(actionMap: ActionMapExtended) {
if (this._dispatchMiddleware) {
return this._dispatchMiddleware.run(actionMap, (actionMap) => {
return this._performDispatch(actionMap as ActionMapInternal);
});
}
return this._performDispatch(actionMap as unknown as ActionMapInternal);
}
}
|