Skip to content

Commit

Permalink
Merge pull request #3 from rambler-digital-solutions/feature/after-ef…
Browse files Browse the repository at this point in the history
…fects

feature afterEffects
  • Loading branch information
asapovk authored Jul 27, 2024
2 parents af12569 + 4a9bb64 commit 267f825
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
#yarn lint-staged
2 changes: 1 addition & 1 deletion packages/core-v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
export {Slice} from './lib/Slice';
export {Bite} from './lib/Bite';
export {Script} from './lib/Script';
export {EffectiveScript} from './lib/interfaces/EffectiveScript';
export {useSystem, type System} from './lib/System';
export {configureRoot} from './lib/configureRoot';
export {getTriggerAndStatus, getActionType} from './lib/utils';
export type {
UpdateOnType,
Expand Down
95 changes: 95 additions & 0 deletions packages/core-v1/lib/AfterEffects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { getTriggerAndStatus } from "./utils";
import { UpdateOnType } from "./types";

/**
*
* key object (__ISALL__: boolean)
* other keys are statuses from array
* value of status keys or __ISALL__ key are biteNames array (to which trigger __afterEffects__ event)
*/

/*
**
*/

export class AfterEffects {
private finalMap: any = {};
private removeCheckMap: any = {};
constructor(private getCurrentTask: () => {type: string, payload: string}) {}

Check failure on line 18 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

Useless constructor

public handleAfterEffect = (dispather: (action) => void) => {
const currentTask = this.getCurrentTask();
const dispatchPayload = currentTask;
if(currentTask) {

Check failure on line 23 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

Expected blank line before this statement
const {trigger, status} = getTriggerAndStatus(currentTask.type);
if(this.finalMap[trigger]) {

Check failure on line 25 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

Expected blank line before this statement
for( let key in this.finalMap[trigger]) {

Check failure on line 26 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

'key' is never reassigned. Use 'const' instead
if(this.finalMap[trigger][key] === '_ALLSTATUSES_') {

Check failure on line 27 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

Blocks are nested too deeply (4). Maximum allowed is 3
setTimeout(()=> {
dispather({
type: `${key}/__AFTEREFFECTS__`,
payload: dispatchPayload
})
})
}
else if(this.finalMap[trigger][key] === status ) {

Check failure on line 35 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

This branch's code block is the same as the block for the branch on line 27
setTimeout(()=> {
dispather({
type: `${key}/__AFTEREFFECTS__`,
payload: dispatchPayload
})})
}
else if(this.finalMap[trigger][key].includes(status)) {

Check failure on line 42 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

This branch's code block is the same as the block for the branch on line 35
setTimeout(() => {
dispather({
type: `${key}/__AFTEREFFECTS__`,
payload: dispatchPayload
})
})
}
}
}
//TODO

Check failure on line 52 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

'TODO' doesn't match the pattern /^((TODO|FIXME|BUG)\(\w+-\d+\)|(NOTE|COMMENT|HACK|INFO)(\(\w+-\d+\))?)\: (.*)$/
}
//check if current task has after effects
//if is => dispatch after effects

}

public removeAfterEffect = (biteName:string) => {
if(this.removeCheckMap[biteName]) {
for(let r of this.removeCheckMap[biteName]) {

Check failure on line 61 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

'r' is never reassigned. Use 'const' instead
if(typeof r === 'string') {
delete this.finalMap[r][biteName]
}
else {
const objectkey = Object.keys(r)[0];
delete this.finalMap[objectkey][biteName]

Check failure on line 67 in packages/core-v1/lib/AfterEffects.ts

View workflow job for this annotation

GitHub Actions / lint

Expected blank line before this statement
}
}
}
delete this.removeCheckMap[biteName]
}

public addAfterEffect = <Tr>(updateOn: UpdateOnType<Tr>, biteName: string) => {

//add to remove map array
this.removeCheckMap[biteName] = updateOn;
for (let uo of updateOn) {
if(!this.finalMap[uo]) {
this.finalMap[uo] = {}
}
if(typeof uo === 'string') {
this.finalMap[uo][biteName] = '_ALLSTATUSES_'
}
else {
const objectkey = Object.keys(uo)[0];
const objectValues = uo[objectkey];
this.finalMap[objectkey][biteName] = objectValues;
}
}
console.log(this.finalMap);
console.log(this.removeCheckMap);

}
}
12 changes: 8 additions & 4 deletions packages/core-v1/lib/System.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TaskQueue} from './TaskQueue';
import type {SystemConfig} from './types';
import { AfterEffects } from "./AfterEffects";
import { TaskQueue } from "./TaskQueue";
import { SystemConfig } from "./types";

interface ProcessorOpts {
propagate: boolean;
Expand All @@ -19,16 +20,19 @@ export class System {
};

public taksQueue: TaskQueue;
public afterEffects: AfterEffects;

constructor() {
this.taksQueue = new TaskQueue();
this.taksQueue = new TaskQueue()
this.afterEffects = new AfterEffects(() => this.taksQueue.getCurrentTask())
}

public setConfig(conf: SystemConfig) {
this.config = conf;
}

public afterHandlers: Array<any> = [];

//public afterHandlers: Array<any> = []

public context: {[triggerer: string]: any} = {};

Expand Down
33 changes: 0 additions & 33 deletions packages/core-v1/lib/configureRoot.ts

This file was deleted.

21 changes: 17 additions & 4 deletions packages/core-v1/lib/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {matchInitTrigger} from './processor/matchInitTrigger';
import {matchUpdateTrigger} from './processor/matchUpdateTrigger';
import {prepareOpts} from './processor/prepareInstanceOpts';
import {useSystem} from './System';
import { AfterEffects } from './processor/lifecycle/AfterEffects';
import { getTriggerAndStatus } from './utils';

export const makeProcMiddleware = (
configs,
Expand Down Expand Up @@ -35,11 +37,11 @@ export const makeProcMiddleware = (

if (instance) {
onInit(instance, actionPayload);
if(instance.afterEffects) {
system.afterEffects.addAfterEffect(initConfig.config.watchScope, initConfig.trigger)
}
}

if (instance?.watchAfter) {
system.afterHandlers.push(() => instance.watchAfter);
}
};

const handleUpdate = (store, action, skipUpdate) => {
Expand Down Expand Up @@ -79,6 +81,7 @@ export const makeProcMiddleware = (
opts,
} = action;
const isBiteHit = matchBiteName(configs, actionType);
const { trigger, status } = getTriggerAndStatus(actionType);
const ignore =
sliceConfig?.ignoreExternal &&
(sliceConfig.ignoreExternal === 'ignoreAll' ||
Expand All @@ -92,7 +95,17 @@ export const makeProcMiddleware = (

handleInit(store, actionType, actionPayload, opts?.noInit);

const forceStopPropagate = handleUpdate(store, action, opts?.noUpdate);
let forceStopPropagate = handleUpdate(store, action, opts?.noUpdate);

if(status === '__AFTEREFFECTS__' && isBiteHit) {
forceStopPropagate = true;
const afInstances = getInstance(configs[trigger], trigger, system);
if(afInstances) {
afInstances.forEach( afi =>
AfterEffects(afi, action, sliceName)
)
}
}
const processorOpts = system.getProcessorInfo(action.type);

system.resolveWait(action.type, actionPayload);
Expand Down
19 changes: 19 additions & 0 deletions packages/core-v1/lib/interfaces/EffectiveScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { WatchArgsType,ScriptOptsType, InitArgsType, TriggerPhaseKeys} from "../types";


export abstract class EffectiveScript<RTg, RSt, Bitename extends keyof RTg, PhK extends TriggerPhaseKeys<RTg, Bitename>, Inj=unknown> {

constructor (opts) {
this.opts = opts
}

protected opts: ScriptOptsType<RTg, RSt, Bitename, Inj>;

abstract watch?(args: WatchArgsType<RTg, Bitename>): void

abstract afterEffects?(args: WatchArgsType<RTg, Bitename>): void

abstract init(args: InitArgsType<RTg, Bitename, PhK>): void


}
29 changes: 29 additions & 0 deletions packages/core-v1/lib/processor/createProcessorInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,32 @@ function refreshingMode(system, config, opt, actionType) {

return newInstance; //.init(actionPayload);
}

///on init instance => add to system configure {trigger => []}
///on drop instance => remove from system's map
// for s of config.config.watchScope,
// s.
//
/**
* store.subscribe(() => {
* /// call for check =>
* const currentTask = system.taskQueue.currentTask
* const afterEffectsMap = system.afterEffectsMap
* if(match(currentTask, afterEffectsMap)) {
* store.dispatch({
* type: biteName/__AFTER_EFFECTS__,
* payload: {
* taskTrigger:
* taskStatus:
* taskPayload:
* taskSourceBite:
* taskSourceSlice:
* }
* })
* }
* })
*
*
*
*
*/
20 changes: 20 additions & 0 deletions packages/core-v1/lib/processor/lifecycle/AfterEffects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getTriggerAndStatus } from "../../utils";

export function AfterEffects (instance, action, sliceName) {
const actionPayload = action.payload;
const effectType = actionPayload.type;
const effectPayload = actionPayload.payload;
if(instance.afterEffects) {
const { trigger, status } = getTriggerAndStatus(effectType);
const afterEffectsArgs = {
payload: effectPayload,
trigger,
status,
source: action.source,
sourceSlice: sliceName,
};
instance.afterEffects(afterEffectsArgs)

}

}
3 changes: 2 additions & 1 deletion packages/core-v1/lib/processor/opts/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export function Drop(system, config) {
const actionType = getActionType(config.trigger, config.config.initOn);

return () => {
system.afterEffects.removeAfterEffect(config.trigger)
system.downProcess(actionType);
};
}
}
20 changes: 20 additions & 0 deletions packages/core-v1/lib/processor/opts/setState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getActionType } from '../../utils';

export function SetState(store, config, system, uid, sourceSlice) {

return (status, args) => {
const process = system.findProcessByUid(uid);
if (process.length) {
store.dispatch({
type: getActionType(config.trigger, status),
payload: args,
source: `${config.trigger}:${uid}`,
sourceSlice,
opts: {
noInit: true,
noUpdate: true,
},
});
}
};
}
22 changes: 22 additions & 0 deletions packages/core-v1/lib/processor/opts/setStateNoEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import { getActionType } from '../../utils';

export function SetStateNoEffect(store, config, system, uid, sourceSlice) {

return (status, args) => {
const process = system.findProcessByUid(uid);
if (process.length) {
store.dispatch({
type: getActionType(config.trigger, status),
payload: args,
source: `${config.trigger}:${uid}`,
sourceSlice,
opts: {
notEffect: true,
noInit: true,
noUpdate: true,
},
});
}
};
}
15 changes: 10 additions & 5 deletions packages/core-v1/lib/processor/prepareInstanceOpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import {v4} from 'uuid';
import {Drop} from './opts/drop';
import {SetStatus} from './opts/setStatus';
import {Trigger} from './opts/trigger';

//
// import { Save } from './opts/save';
// import { TriggerOnly } from './opts/triggerOnly';
import {Wait} from './opts/wait';
import {Hook} from './opts/hook';
import {Bind} from './opts/bind';
import { Wait } from './opts/wait';
import { Hook } from './opts/hook';
import { Bind } from './opts/bind';
import { SetStateNoEffect } from './opts/setStateNoEffect';
import { SetState } from './opts/setState';

export function prepareOpts(config, store, system, sliceName, injected) {
const processUid = v4();
const trigger = Trigger(store, config, system, processUid, sliceName);

const setStatus = SetStatus(store, config, system, processUid, sliceName);
//const save = Save(store, config, system, processUid);
//const triggerOnly = TriggerOnly(store, config, system, processUid);
const setStateNoEffect = SetStateNoEffect(store, config, system, processUid, sliceName);
const setState = SetState(store, config, system, processUid, sliceName);
const drop = Drop(system, config);
//const state = store.getState();
const getCurrentState = store.getState;
Expand All @@ -39,6 +42,8 @@ export function prepareOpts(config, store, system, sliceName, injected) {
injected,
addOpts: config.config.addOpts,
bind,
setState,
setStateNoEffect,
catchStatus: (status, args) => {
if (status === args.status && config.trigger === args.trigger) {
return {
Expand Down
Loading

0 comments on commit 267f825

Please sign in to comment.