Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restored DotPath #214

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ When in doubt simplify.
* [Unified Turing Machine](https://github.com/Phuire-Research/Stratimux/blob/main/The-Unified-Turing-Machine.md) - The governing concept for this entire framework.

## Change Log ![Tests](https://github.com/Phuire-Research/Stratimux/actions/workflows/node.js.yml/badge.svg)
### **BREAKING** Strong Fast Lock Step v0.1.62
### Patch v0.1.62 5/09/2024
* Restored DotPath, a type used in the selector creators used to guide the creation of a dot path string.
### **BREAKING** Strong Fast Lock Step v0.1.62 5/08/2024
* Devised a means to ensure a lock step execution of incoming actions
* Due to each stage being ran once regardless of their selector being changed, some plans may receive the wrong value if not determining if that stage has been ran for the first time. See priority.test.ts for the example: if (changes.length > 0) {//}
* This also impacted the *axiumWaitForOpenThenIterate* helper function, but now works as intended via no longer checking for the latest lastStrategy change.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stratimux",
"license": "GPL-3.0",
"version": "0.1.62",
"version": "0.1.63",
"description": "Unified Turing Machine",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ export {
createUnifiedKeyedSelector,
createAdvancedKeys,
select,
DotPath
} from './model/selector';
export type {
DotPath
} from './model/dotPath';
export { PrincipleFunction, principle } from './model/principle';
export { createActionController$, actionController } from './model/actionController';
export type { dispatchOptions, Staging, UnifiedSubject, StagePlanner, NamedStagePlanner } from './model/stagePlanner';
Expand Down
183 changes: 183 additions & 0 deletions src/model/dotPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*<$
For the asynchronous graph programming framework Stratimux, define the DotPath model file.
$>*/
/*<#*/
type Key = string | number | symbol;

type Join<L extends Key | undefined, R extends Key | undefined> = L extends
| string
| number
? R extends string | number
? `${L}.${R}`
: L
: R extends string | number
? R
: undefined;

type Union<
L extends unknown | undefined,
R extends unknown | undefined
> = L extends undefined
? R extends undefined
? undefined
: R
: R extends undefined
? L
: L | R;

// Use this type to define object types you want to skip (no path-scanning)
type ObjectsToIgnore = { new(...parms: any[]): any } | Date | Array<any>

type ValidObject<T> = T extends object
? T extends ObjectsToIgnore
? false & 1
: T
: false & 1;

export type DotPath<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
// T[K] is a type already checked?
T[K] extends PrevTypes | T
// Return all previous paths.
? Union<Union<Prev, Path>, Join<Path, K>>
: // T[K] is an object?.
Required<T>[K] extends ValidObject<Required<T>[K]>
? // Continue extracting
DotPathOne<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
: // Return all previous paths, including current key.
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathOne<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathTwo<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathTwo<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathThree<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathThree<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathFour<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathFour<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathFive<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathFive<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathSix<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

type DotPathSix<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
DotPathSevenEnd<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];

// Beyond this point will trigger TS excessively deep error or circular reference.
type DotPathSevenEnd<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
T[K] extends PrevTypes | T
? Union<Union<Prev, Path>, Join<Path, K>>
:
Required<T>[K] extends ValidObject<Required<T>[K]>
?
Union<Union<Prev, Path>, Join<Path, K>>
:
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];
/*#>*/
131 changes: 42 additions & 89 deletions src/model/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ This file will contain a series of selectors that can be used to engage with dif
$>*/
/*<#*/
import { Action } from './action';
import { Concept, Concepts, createConcept } from './concept';
import { Concept, Concepts } from './concept';
import { DotPath } from './dotPath';

/**
* Will have such be a list of state keys separated by spaces until someone yells at me to change this.
Expand All @@ -18,8 +19,11 @@ export type KeyedSelector = {
setKeys?: (number | string)[]
setSelector?: SelectorFunction
};

/**
* For usage outside of the Axium, or when subscribed to other Axiums
* Will create a new KeyedSelector based on a concept name comparison during runtime, mainly used for external usage
* @param keys - type string - Format is 'key0.key1.key3' for deep nested key values
* Originally used a DotPath<T> parameter to ease the developer experience, but recent versions made the approach unfeasible
*/
export const createConceptKeyedSelector =
<T extends Record<string, unknown>>(conceptName: string, keys: DotPath<T>, setKeys?: (number|string)[]): KeyedSelector => {
Expand All @@ -44,93 +48,11 @@ export const createConceptKeyedSelector =
};

/**
* This will update a concepts KeyedSelector to its currently unified concept.
* @Note Use this in place of createUnifiedSelector if you find yourself needing to lock deep values.
*/
export const updateUnifiedKeyedSelector =
(concepts: Concepts, semaphore: number, keyedSelector: KeyedSelector): KeyedSelector | undefined => {
if (concepts[semaphore]) {
const selectorBase = keyedSelector.keys.split('.');
selectorBase[0] = concepts[semaphore].name;
const selector = creation(selectorBase, selectorBase.length - 1, selectorBase.length) as SelectorFunction;
if (keyedSelector.setKeys) {
return {
conceptName: concepts[semaphore].name,
conceptSemaphore: semaphore,
selector,
keys: selectorBase.join('.'),
setKeys: keyedSelector.setKeys,
setSelector: keyedSelector.setSelector
};
}
return {
conceptName: concepts[semaphore].name,
conceptSemaphore: semaphore,
selector,
keys: selectorBase.join('.')
};
} else {
return undefined;
}
};

type Key = string | number | symbol;

type Join<L extends Key | undefined, R extends Key | undefined> = L extends
| string
| number
? R extends string | number
? `${L}.${R}`
: L
: R extends string | number
? R
: undefined;

type Union<
L extends unknown | undefined,
R extends unknown | undefined
> = L extends undefined
? R extends undefined
? undefined
: R
: R extends undefined
? L
: L | R;

// Use this type to define object types you want to skip (no path-scanning)
type ObjectsToIgnore = { new(...parms: any[]): any } | Date | Array<any>

type ValidObject<T> = T extends object
? T extends ObjectsToIgnore
? false & 1
: T
: false & 1;

export type DotPath<
T extends object,
Prev extends Key | undefined = undefined,
Path extends Key | undefined = undefined,
PrevTypes extends object = T
> = string &
{
[K in keyof T]:
// T[K] is a type already checked?
T[K] extends PrevTypes | T
// Return all previous paths.
? Union<Union<Prev, Path>, Join<Path, K>>
: // T[K] is an object?.
Required<T>[K] extends ValidObject<Required<T>[K]>
? // Continue extracting
DotPath<Required<T>[K], Union<Prev, Path>, Join<Path, K>, PrevTypes | T>
: // Return all previous paths, including current key.
Union<Union<Prev, Path>, Join<Path, K>>;
}[keyof T];
/**
* Will create a new KeyedSelector during runtime, for usage within your principles.
* @Note Will want to expand this later, so that we can select into objects and arrays.
* This would allow us to lock parts of such in later revisions, not an immediate concern.
* Will create a new KeyedSelector during runtime, for usage throughout Stratimux
* @param keys - type string - Format is 'key0.key1.key3' for deep nested key values
* Originally used a DotPath<T> parameter to ease the developer experience, but recent versions made the approach unfeasible
*/
export const createUnifiedKeyedSelector = <T extends object>(
export const createUnifiedKeyedSelector = <T extends Record<string, unknown>>(
concepts: Concepts,
semaphore: number,
keys: DotPath<T>,
Expand Down Expand Up @@ -164,6 +86,37 @@ export const createUnifiedKeyedSelector = <T extends object>(
return undefined;
};

/**
* This will update a concepts KeyedSelector to its currently unified concept.
* @Note Use this in place of createUnifiedSelector if you find yourself needing to lock deep values.
*/
export const updateUnifiedKeyedSelector =
(concepts: Concepts, semaphore: number, keyedSelector: KeyedSelector): KeyedSelector | undefined => {
if (concepts[semaphore]) {
const selectorBase = keyedSelector.keys.split('.');
selectorBase[0] = concepts[semaphore].name;
const selector = creation(selectorBase, selectorBase.length - 1, selectorBase.length) as SelectorFunction;
if (keyedSelector.setKeys) {
return {
conceptName: concepts[semaphore].name,
conceptSemaphore: semaphore,
selector,
keys: selectorBase.join('.'),
setKeys: keyedSelector.setKeys,
setSelector: keyedSelector.setSelector
};
}
return {
conceptName: concepts[semaphore].name,
conceptSemaphore: semaphore,
selector,
keys: selectorBase.join('.')
};
} else {
return undefined;
}
};

const recordReturn = (key: string, previous: SelectorFunction) => {
return (obj: Record<string, unknown>) => {
if (obj[key] !== undefined) {
Expand Down Expand Up @@ -326,7 +279,7 @@ export function selectConcept(concepts: Concepts, name: string): Concept | undef

/**
* Advanced functionality, set a custom key path that may include array indexes.
* @example createAdvancedKeys('some', 1, 'once', 2, 'me', 7, 'world', 4) : some.1.once.2.m.7.world.4
* @example createAdvancedKeys(['some', 1, 'once', 2, 'me', 7, 'world', 4]) : some.1.once.2.m.7.world.4
* @param arr a series of keys that points to your targeted slice
* @returns DotPath<T extends object>
*/
Expand Down
Loading
Loading