Skip to content

Commit

Permalink
Tests Passing: New Stage Options Tests(3/3)
Browse files Browse the repository at this point in the history
  • Loading branch information
REllEK-IO committed Mar 28, 2024
1 parent c20351d commit c2239c0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/concepts/axium/axium.close.principle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const axiumClosePrinciple: PrincipleFunction = (
});
plan.conclude();
}
}, [axiumSelectPrepareClose], Infinity),
}, { selectors: [axiumSelectPrepareClose], priority: Infinity}),
createStage(() => {
//
})
Expand Down
4 changes: 2 additions & 2 deletions src/concepts/axium/axium.principle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const axiumPrinciple: PrincipleFunction = (
throttle: 50
});
}
}, [axiumSelectAddConceptQue], Infinity - 1),
}, { selectors: [axiumSelectAddConceptQue], priority: Infinity - 1}),
]);

const removeConceptsPlan = concepts$.plan('Remove Concepts Plan', [
Expand Down Expand Up @@ -171,7 +171,7 @@ export const axiumPrinciple: PrincipleFunction = (
throttle: 50
});
}
}, [axiumSelectRemoveConceptQue], Infinity - 2)
}, { selectors: [axiumSelectRemoveConceptQue], priority: Infinity - 2})
]);
observer.next(strategy.begin(strategy.create({
topic: 'Register Axium Add/Remove Plans',
Expand Down
45 changes: 29 additions & 16 deletions src/model/stagePlanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const stageWaitForOpenThenIterate = (func: () => Action): Staging => (cre
iterateStage: true
});
}
}, [axiumSelectOpen]));
}, { selectors: [axiumSelectOpen] }));

export const stageWaitForOwnershipThenIterate =
(func: () => Action): Staging => (createStage((concepts: Concepts, dispatch: Dispatcher) => {
Expand All @@ -96,7 +96,7 @@ export const stageWaitForOwnershipThenIterate =
iterateStage: true
});
}
}, [ownershipSelectInitialized]));
}, { selectors: [ownershipSelectInitialized] }));

/**
* Helper function to aid readability of composing plans, otherwise you may directly create a Staging Entity, selectors non optional
Expand All @@ -106,13 +106,20 @@ export const stageWaitForOwnershipThenIterate =
* @param beat - Will fire once, then if informed again within your supplied beat, will fire after such time
* @returns stage: Stage, selectors: KeyedSelector[], priority?: number, beat?: number
*/
export const createStage = (stage: Stage, selectors?: KeyedSelector[], priority?: number, beat?: number): Staging => {
return {
stage,
selectors: selectors ? selectors : [],
priority,
beat
};
export const createStage = (stage: Stage, options?: { selectors?: KeyedSelector[], priority?: number, beat?: number}): Staging => {
if (options) {
return {
stage,
selectors: options.selectors ? options.selectors : [],
priority: options.priority,
beat: options.beat
};
} else {
return {
stage,
selectors: []
};
}
};

// Token to denote ALL, using a selector that utilizes this token should return undefined
Expand Down Expand Up @@ -200,7 +207,7 @@ const handleStageDelimiter =
];
};

const handleNewStageOptions = (plan: Plan, options: dispatchOptions): boolean => {
const handleNewStageOptions = (plan: Plan, options: dispatchOptions, next: number): boolean => {
let evaluate = false;
if (options.newPriority) {
plan.stages[plan.stage].priority = options.newPriority;
Expand All @@ -212,6 +219,9 @@ const handleNewStageOptions = (plan: Plan, options: dispatchOptions): boolean =>
}
if (options.newBeat) {
plan.stages[plan.stage].beat = options.newBeat;
if (next === -1) {
plan.beat = options.newBeat;
}
evaluate = true;
}
return evaluate;
Expand Down Expand Up @@ -280,7 +290,7 @@ export class UnifiedSubject extends Subject<Concepts> {
}
}

protected createPlan(title: string, stages: PartialStaging[], beat?: number): Plan {
protected createPlan(title: string, stages: PartialStaging[]): Plan {
// [TODO Unify Streams]
// protected createPlan(title: string, stages: PartialStaging[], outer: boolean, beat?: number): Plan {
const planId = this.planId;
Expand All @@ -293,7 +303,8 @@ export class UnifiedSubject extends Subject<Concepts> {
beat: s.beat
};
});
return {id: planId, title, stages: staged, stage: 0, stageFailed: -1, beat: beat ? beat : -1, offBeat: -1, timer: []};
const beat = staged[0].beat;
return {id: planId, title, stages: staged, stage: 0, stageFailed: -1, beat: beat !== undefined ? beat : -1, offBeat: -1, timer: []};
// [TODO Unify Streams]
// return {id: planId, outer, title, stages: staged, stage: 0, stageFailed: -1, beat: beat ? beat : -1, offBeat: -1, timer: []};
}
Expand All @@ -317,10 +328,10 @@ export class UnifiedSubject extends Subject<Concepts> {
// return this.initPlan(this.createPlan(title, stages, true, beat));
// }

plan(title: string, stages: Staging[], beat?: number): StagePlanner {
plan(title: string, stages: Staging[]): StagePlanner {
// [TODO Unify Streams]
// return this.initPlan(this.createPlan(title, stages, false, beat));
return this.initPlan(this.createPlan(title, stages, beat));
return this.initPlan(this.createPlan(title, stages));
}

protected deletePlan(planId: number) {
Expand Down Expand Up @@ -513,19 +524,20 @@ export class UnifiedSubject extends Subject<Concepts> {
// Double check this logic while writing the unit test.
if (plan.stages[plan.stage]) {
this.handleRemoveSelector(plan.stages[plan.stage].selectors, plan.id);
handleNewStageOptions(plan, options);
}
plan.stage = next;
this.manageQues();
if (plan.stages[plan.stage]) {
this.handleAddSelector(plan.stages[plan.stage].selectors, plan.id);
}
const beat = plan.stages[plan.stage].beat;
plan.beat = beat !== undefined ? beat : -1;
stageDelimiter.prevActions = [];
stageDelimiter.unionExpiration = [];
stageDelimiter.runOnceMap = new Map();
this.stageDelimiters.set(plan.id, stageDelimiter);
}
const evaluate = handleNewStageOptions(plan, options);
const evaluate = handleNewStageOptions(plan, options, next);
if (evaluate && next === -1) {
this.handleRemoveSelector(plan.stages[plan.stage].selectors, plan.id);
this.handleAddSelector(plan.stages[plan.stage].selectors, plan.id);
Expand Down Expand Up @@ -571,6 +583,7 @@ export class UnifiedSubject extends Subject<Concepts> {
if (index < plan.stages.length) {
const timer = plan.timer;
const now = Date.now();
// console.log('CHECK', plan.title, plan.beat);
if (plan.beat > -1) {
if (plan.offBeat < now) {
plan.offBeat = Date.now() + plan.beat;
Expand Down
2 changes: 1 addition & 1 deletion src/test/addConcepts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('Axium add Concepts Strategy Test', (done) => {
}
});
}
}, [axiumSelectOpen]),
}, { selectors: [axiumSelectOpen] }),
createStage((concepts) => {
const axiumState = concepts[0].state as AxiumState;
console.log('Check for final counting topic', axiumState.lastStrategy, concepts[1]?.state);
Expand Down
6 changes: 3 additions & 3 deletions src/test/newPlanOptions/newPlanOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test('New Plan Options Priority Test', (done) => {
});
}
}
}, undefined, 100),
}, { priority: 100 }),
createStage(() => {
planOne.conclude();
setTimeout(() => {
Expand Down Expand Up @@ -170,7 +170,7 @@ test('New Plan Options Priority Test', (done) => {
});
}
}
}, undefined, 50),
}, { priority: 50 }),
createStage(() => {
planTwo.conclude();
})
Expand Down Expand Up @@ -203,7 +203,7 @@ test('New Plan Options Priority Test', (done) => {
});
}
}
}, undefined, 5),
}, { priority: 5 }),
createStage(() => {
planThree.conclude();
})
Expand Down
6 changes: 3 additions & 3 deletions src/test/priority/priority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('Priority Test', (done) => {
iterateStage: true
});
}
}, [experimentPriorityReadySelector], priority);
}, {selectors: [experimentPriorityReadySelector], priority});
const secondStage = (name: string, newValue: number, priority: number) => createStage((concepts, dispatch) => {
const priorityState = select.state<ExperimentPriorityState>(concepts, experimentName);
if (priorityState) {
Expand All @@ -46,7 +46,7 @@ test('Priority Test', (done) => {
iterateStage: true
});
}
}, undefined, priority);
}, {priority});
const thirdStage = (name: string, expected: number, priority: number) => createStage((concepts, dispatch) => {
const priorityState = select.state<ExperimentPriorityState>(concepts, experimentName);
if (priorityState) {
Expand All @@ -56,7 +56,7 @@ test('Priority Test', (done) => {
iterateStage: true
});
}
}, [experimentPriorityValueSelector], priority);
}, {selectors: [experimentPriorityValueSelector], priority});
const concludePlan = (name: string, func: () => StagePlanner) => createStage(() => {
console.log(`${name} Priority END`);
func().conclude();
Expand Down
16 changes: 11 additions & 5 deletions src/test/stagePlannerBeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,29 @@ test('Stage Planner Beat Test', (done) => {
const axium = createAxium('axium test stage planner beat', [
createCounterConcept()
], true, true);
const beat = 105;
const plan = axium.plan('Stage Planner Beat Test', [
createStage((concepts, dispatch) => {
console.log('HIT 1');
timerActive = true;
setTimeout(() => {
console.log('FIRE');
timerActive = false;
}, 1000);
if (selectSlice(concepts, axiumSelectOpen)) {
dispatch(axiumKick(), {
iterateStage: true,
});
}
}),
}, {beat}),
createStage((___, dispatch) => {
console.log('HIT 2');
dispatch(counterAdd(), {
iterateStage: true
});
}),
}, {beat}),
createStage((concepts, dispatch) => {
// console.log('HIT 3', timerActive, selectState<CounterState>(concepts, counterName));
if (!timerActive) {
const state = selectState<CounterState>(concepts, counterName);
if (state) {
Expand All @@ -55,10 +60,11 @@ test('Stage Planner Beat Test', (done) => {
throttle: 1
});
}
}),
}, {beat}),
createStage(() => {
console.log('HIT 4');
//
})
], 105);
}, {beat})
]);
});
/*#>*/

0 comments on commit c2239c0

Please sign in to comment.