Skip to content

Commit

Permalink
Constistency, debounce fires most recent action after duration.
Browse files Browse the repository at this point in the history
  • Loading branch information
REllEK-IO committed Oct 20, 2023
1 parent 341841d commit fb6c5ff
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 26 deletions.
6 changes: 3 additions & 3 deletions ActionStrategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ This was a purposeful design choice, if you find yourself doing such. Known this
export const createMethod =
(method: (action: Action) => Action): [Method, Subject<Action>] => {}
export const createMethodWithConcepts =
(method: (action: Action) => Action, concepts$: UnifiedSubject): [Method, Subject<Action>] => {}
(method: (action: Action, concepts: Concept[]) => Action, concepts$: UnifiedSubject): [Method, Subject<Action>] => {}
export const createAsyncMethod =
(asyncMethod: (controller: ActionController, action: Action) => void): [Method, Subject<Action>] => {}
export const createAsyncMethodWithConcepts =
Expand All @@ -153,7 +153,7 @@ export const createAsyncMethodDebounceWithConcepts =
* createAsyncMethodWithConcepts - Will also have access to the most recent concepts.

*Note if you are implementing your own debounceAction, pay attention to how these method helpers work. They are handling a passed conclude from debounceAction within their map/switchMap*
* createMethodDebounce - After the first action, this will filter actions within the duration to be set to the conclude action.
* createMethodDebounce - Will fire after the set duration with the most recent action, and filter previous actions within the duration to be set to the conclude action.
* createMethodDebounceWithConcepts - Will filter actions within the duration while providing access to the most recent concepts.
* createAsyncMethodDebounce - Will not disengage the initial ActionController, but will allow debounced actions to pass through when filtered as conclude actions. And will fire the first action upon its own conditions are met asynchronously.
* createAsyncMethodDebounce - Will not disengage the initial ActionController, but will allow debounced actions to pass through when filtered as conclude actions. And will fire the most recent action upon its own conditions are met asynchronously after the set duration.
* createAsyncMethodDebounceWithConcepts - Filters and then first the first action once conditions are met, and provides access to the most recent concepts.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@phuire/strx",
"version": "0.0.39",
"version": "0.0.40",
"description": "Unified Turing Machine",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
4 changes: 3 additions & 1 deletion src/concepts/experiment/experiment.concept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { PrincipleFunction } from '../../model/principle';
export type ExperimentState = {
actionQue: Action[],
mock: boolean,
id: number
}

export const experimentName = 'experiment';

export const createExperimentState = (): ExperimentState => {
return {
actionQue: [],
mock: false
mock: false,
id: 0,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MethodCreator, defaultMethodCreator, defaultReducer } from '../../../model/concept';
import { Action, prepareActionCreator } from '../../../model/action';
import { createQuality } from '../../../model/concept';
import { ExperimentState, experimentName } from '../experiment.concept';
import { UnifiedSubject } from '../../../model/stagePlanner';
import { createAsyncMethodWithConcepts, createMethodWithConcepts } from '../../../model/method';
import { selectState } from '../../../model/selector';
import { strategySuccess } from '../../../model/actionStrategy';
import { strategyData_unifyData } from '../../../model/actionStrategyData';

export const experimentAsyncIterateIdThenReceiveInMethodType
= 'Experiment asynchronously iterate ID then receive in Method via Concept select';

export const experimentAsyncIterateIdThenReceiveInMethod = prepareActionCreator(experimentAsyncIterateIdThenReceiveInMethodType);

const experimentAsyncIterateIdThenReceiveInMethodCreator: MethodCreator = (concepts$?: UnifiedSubject) =>
createAsyncMethodWithConcepts((controller, action, concepts) => {
console.log('HIT');
setTimeout(() => {
const experimentState = selectState<ExperimentState>(concepts, experimentName);
if (action.strategy) {
const data = strategyData_unifyData<ExperimentState>(action.strategy, {id: experimentState.id});
const strategy = strategySuccess(action.strategy, data);
console.log('FIRE');
controller.fire(strategy);
}
controller.fire(action);
}, 50);
}, concepts$ as UnifiedSubject);

function experimentAsyncIterateIdThenReceiveInMethodReducer(state: ExperimentState, _: Action): ExperimentState {
return {
...state,
id: state.id + 1
};
}

export const experimentAsyncIterateIdThenReceiveInMethodQuality = createQuality(
experimentAsyncIterateIdThenReceiveInMethodType,
experimentAsyncIterateIdThenReceiveInMethodReducer,
experimentAsyncIterateIdThenReceiveInMethodCreator
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MethodCreator } from '../../../model/concept';
import { Action, prepareActionWithPayloadCreator } from '../../../model/action';
import { createQuality } from '../../../model/concept';
import { ExperimentState, experimentName } from '../experiment.concept';
import { UnifiedSubject } from '../../../model/stagePlanner';
import { createAsyncMethodDebounceWithConcepts } from '../../../model/method';
import { selectPayload, selectState } from '../../../model/selector';
import { strategySuccess } from '../../../model/actionStrategy';
import { strategyData_unifyData } from '../../../model/actionStrategyData';

export type DebounceAsyncIterateIdThenReceiveInMethodPayload = {
setId: number;
}
export const experimentDebounceAsyncIterateIdThenReceiveInMethodType
= 'Experiment asynchronously iterate ID then receive in Method via Concept select';
export const experimentDebounceAsyncIterateIdThenReceiveInMethod
= prepareActionWithPayloadCreator<DebounceAsyncIterateIdThenReceiveInMethodPayload>(
experimentDebounceAsyncIterateIdThenReceiveInMethodType
);

const experimentDebounceAsyncIterateIdThenReceiveInMethodCreator: MethodCreator = (concepts$?: UnifiedSubject) =>
createAsyncMethodDebounceWithConcepts((controller, action, concepts) => {
setTimeout(() => {
const payload = selectPayload<DebounceAsyncIterateIdThenReceiveInMethodPayload>(action);
const experimentState = selectState<ExperimentState>(concepts, experimentName);
if (action.strategy) {
const data = strategyData_unifyData<ExperimentState & DebounceAsyncIterateIdThenReceiveInMethodPayload>(
action.strategy,
{
id: experimentState.id,
setId: payload.setId
}
);
const strategy = strategySuccess(action.strategy, data);
controller.fire(strategy);
}
controller.fire(action);
}, 50);
}, concepts$ as UnifiedSubject, 500);

function experimentDebounceAsyncIterateIdThenReceiveInMethodReducer(state: ExperimentState, _: Action): ExperimentState {
return {
...state,
id: state.id + 1
};
}

export const experimentDebounceAsyncIterateIdThenReceiveInMethodQuality = createQuality(
experimentDebounceAsyncIterateIdThenReceiveInMethodType,
experimentDebounceAsyncIterateIdThenReceiveInMethodReducer,
experimentDebounceAsyncIterateIdThenReceiveInMethodCreator
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MethodCreator, defaultMethodCreator, defaultReducer } from '../../../model/concept';
import { Action, prepareActionCreator, prepareActionWithPayloadCreator } from '../../../model/action';
import { createQuality } from '../../../model/concept';
import { ExperimentState, experimentName } from '../experiment.concept';
import { UnifiedSubject } from '../../../model/stagePlanner';
import { createMethodDebounceWithConcepts, createMethodWithConcepts } from '../../../model/method';
import { selectPayload, selectState } from '../../../model/selector';
import { strategySuccess } from '../../../model/actionStrategy';
import { strategyData_unifyData } from '../../../model/actionStrategyData';

export type DebounceIterateIdThenReceiveInMethodPayload = {
setId: number;
}
export const experimentDebounceIterateIdThenReceiveInMethodType =
'Experiment debounce iterate ID then receive in Method via Concept select';

export const experimentDebounceIterateIdThenReceiveInMethod =
prepareActionWithPayloadCreator<DebounceIterateIdThenReceiveInMethodPayload>(experimentDebounceIterateIdThenReceiveInMethodType);

const experimentDebounceIterateIdThenReceiveInMethodCreator: MethodCreator = (concepts$?: UnifiedSubject) =>
createMethodDebounceWithConcepts((action, concepts) => {
const payload = selectPayload<DebounceIterateIdThenReceiveInMethodPayload>(action);
const experimentState = selectState<ExperimentState>(concepts, experimentName);
if (action.strategy) {
const data = strategyData_unifyData<ExperimentState & DebounceIterateIdThenReceiveInMethodPayload>(action.strategy, {
id: experimentState.id,
setId: payload.setId
});
const strategy = strategySuccess(action.strategy, data);
return strategy;
}
return action;
}, concepts$ as UnifiedSubject, 500);

function experimentDebounceIterateIdThenReceiveInMethodReducer(state: ExperimentState, _: Action): ExperimentState {
return {
...state,
id: state.id + 1
};
}

export const experimentDebounceIterateIdThenReceiveInMethodQuality = createQuality(
experimentDebounceIterateIdThenReceiveInMethodType,
experimentDebounceIterateIdThenReceiveInMethodReducer,
experimentDebounceIterateIdThenReceiveInMethodCreator
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MethodCreator, defaultMethodCreator, defaultReducer } from '../../../model/concept';
import { Action, prepareActionCreator } from '../../../model/action';
import { createQuality } from '../../../model/concept';
import { ExperimentState, experimentName } from '../experiment.concept';
import { UnifiedSubject } from '../../../model/stagePlanner';
import { createMethodWithConcepts } from '../../../model/method';
import { selectState } from '../../../model/selector';
import { strategySuccess } from '../../../model/actionStrategy';
import { strategyData_unifyData } from '../../../model/actionStrategyData';

export const experimentIterateIdThenReceiveInMethodType = 'Experiment iterate ID then receive in Method via Concept select';

export const experimentIterateIdThenReceiveInMethod = prepareActionCreator(experimentIterateIdThenReceiveInMethodType);

const experimentIterateIdThenReceiveInMethodCreator: MethodCreator = (concepts$?: UnifiedSubject) =>
createMethodWithConcepts((action, concepts) => {
const experimentState = selectState<ExperimentState>(concepts, experimentName);
if (action.strategy) {
const data = strategyData_unifyData<ExperimentState>(action.strategy, {id: experimentState.id});
const strategy = strategySuccess(action.strategy, data);
return strategy;
}
return action;
}, concepts$ as UnifiedSubject);

function experimentIterateIdThenReceiveInMethodReducer(state: ExperimentState, _: Action): ExperimentState {
return {
...state,
id: state.id + 1
};
}

export const experimentIterateIdThenReceiveInMethodQuality = createQuality(
experimentIterateIdThenReceiveInMethodType,
experimentIterateIdThenReceiveInMethodReducer,
experimentIterateIdThenReceiveInMethodCreator
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ActionStrategy, ActionStrategyParameters, createActionNode, createStrategy } from '../../../model/actionStrategy';
import { experimentAsyncIterateIdThenReceiveInMethod } from '../qualities/asyncIterateIdThenReceiveInMethod.quality copy';

export const asyncIterateIdThenAddToDataTopic = 'Async iterate experiment ID then add to strategy data';
export function asyncIterateIdThenAddToData(): ActionStrategy {
const stepOne = createActionNode(experimentAsyncIterateIdThenReceiveInMethod(), {
successNode: null,
failureNode: null,
});

const params: ActionStrategyParameters = {
topic: asyncIterateIdThenAddToDataTopic,
initialNode: stepOne,
};

return createStrategy(params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ActionStrategy, ActionStrategyParameters, createActionNode, createStrategy } from '../../../model/actionStrategy';
import { experimentDebounceAsyncIterateIdThenReceiveInMethod } from '../qualities/debounceAsyncIterateIdThenReceiveInMethod.quality copy 2';

export const debounceAsyncIterateIdThenAddToDataTopic = 'Debounce async iterate experiment ID then add to strategy data';
export function debounceAsyncIterateIdThenAddToData(setId: number): ActionStrategy {
const stepOne = createActionNode(experimentDebounceAsyncIterateIdThenReceiveInMethod({setId}), {
successNode: null,
failureNode: null,
});

const params: ActionStrategyParameters = {
topic: debounceAsyncIterateIdThenAddToDataTopic,
initialNode: stepOne,
};

return createStrategy(params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ActionStrategy, ActionStrategyParameters, createActionNode, createStrategy } from '../../../model/actionStrategy';
import { experimentDebounceIterateIdThenReceiveInMethod } from '../qualities/debounceIterateIdThenReceiveInMethod.quality';

export const debounceIterateIdThenAddToDataTopic = 'Debounce iterate experiment ID then add to strategy data';
export function debounceIterateIdThenAddToData(setId: number): ActionStrategy {
const stepOne = createActionNode(experimentDebounceIterateIdThenReceiveInMethod({setId}), {
successNode: null,
failureNode: null,
});

const params: ActionStrategyParameters = {
topic: debounceIterateIdThenAddToDataTopic,
initialNode: stepOne,
};

return createStrategy(params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ActionStrategy, ActionStrategyParameters, createActionNode, createStrategy } from '../../../model/actionStrategy';
import { experimentIterateIdThenReceiveInMethod } from '../qualities/iterateIdThenReceiveInMethod.quality';

export const iterateIdThenAddToDataTopic = 'Iterate experiment ID then add to strategy data';
export function iterateIdThenAddToData(): ActionStrategy {
const stepOne = createActionNode(experimentIterateIdThenReceiveInMethod(), {
successNode: null,
failureNode: null,
});

const params: ActionStrategyParameters = {
topic: iterateIdThenAddToDataTopic,
initialNode: stepOne,
};

return createStrategy(params);
}
5 changes: 4 additions & 1 deletion src/model/actionStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export const strategySuccess = (_strategy: ActionStrategy, data?: Record<string,
conclude.action.strategy = {
...strategy,
currentNode: conclude,
data: data ? data : strategy.data
};
return conclude.action;
}
Expand All @@ -250,7 +251,7 @@ export const strategySuccess = (_strategy: ActionStrategy, data?: Record<string,
* If no failureNode is found, will return EndOfActionStrategy instead.
* @param data - OPTIONAL, if used will override the ActionStrategy's payload
*/
export function strategyFailed(_strategy: ActionStrategy, data?: unknown) {
export function strategyFailed(_strategy: ActionStrategy, data?: Record<string, unknown>) {
const strategy = {..._strategy};
let nextAction: Action;
const actionListEntry = createSentence(
Expand Down Expand Up @@ -310,6 +311,7 @@ export function strategyFailed(_strategy: ActionStrategy, data?: unknown) {
conclude.action.strategy = {
...strategy,
currentNode: conclude,
data: data ? data : strategy.data
};
return conclude.action;
}
Expand Down Expand Up @@ -388,6 +390,7 @@ export const strategyDecide = (
conclude.action.strategy = {
...strategy,
currentNode: conclude,
data: data ? data : strategy.data
};
return conclude.action;
};
Expand Down
21 changes: 11 additions & 10 deletions src/model/actionStrategyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export const strategyData_select = <T>(strategy: ActionStrategy): T | undefined
}
};

export const strategyData_unifyData = (strategy: ActionStrategy, data: Record<string,unknown>): Record<string,unknown> => {
if (strategy.data) {
return {
...strategy.data,
...data
};
} else {
return {...data};
}
};
export const strategyData_unifyData =
<T extends Record<string, unknown>>(strategy: ActionStrategy, data: Record<string,unknown> | T): Record<string,unknown> => {
if (strategy.data) {
return {
...strategy.data,
...data
};
} else {
return {...data};
}
};
11 changes: 5 additions & 6 deletions src/model/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export const createMethod =
return [defaultMethod, defaultSubject];
};
export const createMethodWithConcepts =
(method: (action: Action) => Action, concepts$: UnifiedSubject): [Method, Subject<Action>] => {
(methodWithConcepts: (action: Action, concepts: Concept[]) => Action, concepts$: UnifiedSubject): [Method, Subject<Action>] => {
const defaultSubject = new Subject<Action>();
const defaultMethod: Method = defaultSubject.pipe(
withLatestFrom(concepts$ as UnifiedSubject),
map(([act, concepts] : [Action, Concept[]]) => {
const methodAction = method(act);
const methodAction = methodWithConcepts(act, concepts);
if (methodAction.strategy) {
return methodAction;
}
Expand Down Expand Up @@ -71,10 +71,9 @@ export const createAsyncMethodWithConcepts =
const defaultSubject = new Subject<Action>();
const defaultMethod: Method = defaultSubject.pipe(
withLatestFrom(concepts$ as UnifiedSubject),
switchMap(([act, concepts] : [Action, Concept[]]) =>
createActionController$(act, (controller: ActionController, action: Action) => {
asyncMethodWithConcepts(controller, action, concepts);
})),
switchMap(([act, concepts] : [Action, Concept[]]) => createActionController$(act, (controller: ActionController, action: Action) => {
asyncMethodWithConcepts(controller, action, concepts);
})),
);
return [defaultMethod, defaultSubject];
};
Expand Down
Loading

0 comments on commit fb6c5ff

Please sign in to comment.