Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion packages/core/src/agile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Agile {
* @param deps Array - An array of state items to depend on
* @param computeFunction Function - A function where the return value is the state, ran every time a dep changes
*/
public Computed = <ComputedValueType = any>(computeFunction: () => ComputedValueType, deps?: Array<State>) => new Computed<ComputedValueType>(this, computeFunction, deps);
public Computed = <ComputedValueType = any>(computeFunction: () => ComputedValueType, deps?: Array<State>) => new Computed<ComputedValueType>(this, computeFunction, deps?.map(state => state.observer));


//=========================================================================================================
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/collection/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export class Group<DataType = DefaultDataItem> extends State<Array<ItemKey>> {

public get output(): Array<DataType> {
// Add state(group) to foundState (for auto tracking used states in computed functions)
if (this.agileInstance().runtime.trackState)
this.agileInstance().runtime.foundStates.add(this);
if (this.agileInstance().runtime.trackObserver)
this.agileInstance().runtime.foundObservers.add(this.observer);

return this._output;
}

public get states(): Array<State<DataType>> {
// Add state(group) to foundState (for auto tracking used states in computed functions)
if (this.agileInstance().runtime.trackState)
this.agileInstance().runtime.foundStates.add(this);
if (this.agileInstance().runtime.trackObserver)
this.agileInstance().runtime.foundObservers.add(this.observer);

return this._states.map(state => state());
}
Expand Down Expand Up @@ -225,4 +225,17 @@ export class Group<DataType = DefaultDataItem> extends State<Array<ItemKey>> {
this._states = finalStates.map(state => (() => state));
this._output = finalOutput;
}


//=========================================================================================================
// Get Public Value
//=========================================================================================================
/**
* @internal
* Returns the public value
*/
// @ts-ignore
public getPublicValue(): Array<DataType> {
return this.output;
}
}
4 changes: 2 additions & 2 deletions packages/core/src/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ export class Collection<DataType = DefaultDataItem> {
return undefined;

// Add state to foundState (for auto tracking used states in computed functions)
if (this.agileInstance().runtime.trackState)
this.agileInstance().runtime.foundStates.add(this.data[id]);
if (this.agileInstance().runtime.trackObserver)
this.agileInstance().runtime.foundObservers.add(this.data[id].observer);

// Return data by id
return this.data[id];
Expand Down
33 changes: 18 additions & 15 deletions packages/core/src/computed/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {
State,
Agile,
defineConfig
defineConfig,
Observer
} from '../internal';

export class Computed<ComputedValueType = any> extends State<ComputedValueType> {
public agileInstance: () => Agile;

public computeFunction: () => ComputedValueType;
public deps: Array<State> = [];
public hardCodedDeps: Array<State> = [];
public deps: Array<Observer> = [];
public hardCodedDeps: Array<Observer> = [];

constructor(agileInstance: Agile, computeFunction: () => ComputedValueType, deps: Array<State> = []) {
constructor(agileInstance: Agile, computeFunction: () => ComputedValueType, deps: Array<Observer> = []) {
super(agileInstance, computeFunction());
this.agileInstance = () => agileInstance;
this.computeFunction = computeFunction;
Expand All @@ -29,8 +30,8 @@ export class Computed<ComputedValueType = any> extends State<ComputedValueType>
// Note can't use 'super.value' because of 'https://github.com/Microsoft/TypeScript/issues/338'

// Add state to foundState (for auto tracking used states in computed functions)
if (this.agileInstance().runtime.trackState)
this.agileInstance().runtime.foundStates.add(this);
if (this.agileInstance().runtime.trackObserver)
this.agileInstance().runtime.foundObservers.add(this.observer);

return this._value;
}
Expand Down Expand Up @@ -62,7 +63,7 @@ export class Computed<ComputedValueType = any> extends State<ComputedValueType>
*/
public updateComputeFunction(computeFunction: () => ComputedValueType, deps: Array<State> = [], options?: { background?: boolean, sideEffects?: boolean }) {
this.computeFunction = computeFunction;
this.hardCodedDeps = deps;
this.hardCodedDeps = deps.map(state => state.observer);

// Recompute for setting initial state value and adding missing dependencies
this.recompute(options);
Expand All @@ -78,28 +79,30 @@ export class Computed<ComputedValueType = any> extends State<ComputedValueType>
*/
public computeValue(): ComputedValueType {
// Set tracking state to true which will than track all states which for instance call state.value
this.agileInstance().runtime.trackState = true;
this.agileInstance().runtime.trackObserver = true;

// Call computeFunction
const computedValue = this.computeFunction();

// Get tracked states and set trackSate to false
let foundStates = this.agileInstance().runtime.getTrackedStates();
let foundObservers = this.agileInstance().runtime.getTrackedObservers();

// Handle foundStates dependencies
const newDeps: Array<State> = [];
foundStates.forEach(state => {
const newDeps: Array<Observer> = [];
foundObservers.forEach(observer => {
if(!observer) return;

// Add the state to newDeps
newDeps.push(state);
newDeps.push(observer);

// Add this as dependency of the state
state.dep.depend(this);
observer.dep.depend(this.observer);
});

// Handle hardCoded dependencies
this.hardCodedDeps.forEach(state => {
this.hardCodedDeps.forEach(observer => {
// Add this as dependency of the state
state.dep.depend(this);
observer.dep.depend(this.observer);
});

// Set deps
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/event/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,4 @@ export class Event<PayloadType = DefaultEventPayload> {

return;
}

}
17 changes: 12 additions & 5 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@
// Agile
export * from './agile';

// Runtime
export * from './runtime';
export * from './runtime/observer';
export * from './runtime/dep';
export * from './runtime/job';
export * from './runtime/subscription/SubscriptionContainer';
export * from './runtime/subscription/CallbackSubscriptionContainer';
export * from './runtime/subscription/ComponentSubscriptionContainer';
export * from './runtime/subscription/sub';


// State
export * from './state';
export * from './state/dep';
export * from './state/state.observer';

// Computed
export {Computed} from './computed';
Expand All @@ -25,12 +36,8 @@ export * from './collection/selector';
export * from './event';

// Internal Classes
export * from './runtime';
export * from './storage';
export * from './integrations';
export * from './runtime/subscription/sub';
export * from './runtime/subscription/CallbackSubscriptionContainer';
export * from './runtime/subscription/ComponentSubscriptionContainer';

// Utils
export * from './utils';
24 changes: 12 additions & 12 deletions packages/core/src/state/dep.ts → packages/core/src/runtime/dep.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import {
State,
SubscriptionContainer
SubscriptionContainer,
Observer
} from '../internal';

export class Dep {
public deps: Set<any> = new Set(); // Dependencies from the State
public deps: Set<Observer> = new Set(); // Dependencies from the State
public subs: Set<SubscriptionContainer> = new Set(); // Subscriptions (for instance a component subscribes to a state to get rerendered if the state changes)

constructor(initialDeps?: Array<Dep>) {
constructor(initialDeps?: Array<Observer>) {
if (!initialDeps) return;

// Add Initial Dependencies to Deps
initialDeps.forEach(dep => this.deps.add(dep));
initialDeps.forEach(observable => this.deps.add(observable));
}


//=========================================================================================================
// Depend
//=========================================================================================================
/**
* Add new Dependency to the State
* Add new Dependency to the Observer
*/
public depend(state: State) {
if (state.dep !== this && !this.deps.has(state))
this.deps.add(state);
public depend(observable: Observer) {
if (observable.dep !== this && !this.deps.has(observable))
this.deps.add(observable);
}


//=========================================================================================================
// Subscribe
//=========================================================================================================
/**
* Add new Subscription to the State
* Add new Subscription to the Observer
*/
public subscribe(subscriptionContainer: SubscriptionContainer){
if(!this.subs.has(subscriptionContainer))
Expand All @@ -43,10 +43,10 @@ export class Dep {
// Unsubscribe
//=========================================================================================================
/**
* Delete Subscription from the State
* Delete Subscription from the Observer
*/
public unsubscribe(subscriptionContainer: SubscriptionContainer){
if(!this.subs.has(subscriptionContainer))
if(this.subs.has(subscriptionContainer))
this.subs.delete(subscriptionContainer);
}
}
Loading