Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Apr 30, 2024
1 parent 6b13448 commit 6794d71
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 154 deletions.
44 changes: 23 additions & 21 deletions src/main/ExecutorImpl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AbortablePromise, PubSub } from 'parallel-universe';
import type { ExecutorManager } from './ExecutorManager';
import type { Executor, ExecutorEvent, ExecutorState, ExecutorTask } from './types';
import { AbortError, definePrivateProperty } from './utils';
import { AbortError } from './utils';

/**
* The {@link Executor} implementation returned by the {@link ExecutorManager}.
Expand All @@ -21,17 +21,17 @@ export class ExecutorImpl<Value = any> implements Executor {
/**
* The promise of the pending task execution, or `null` if there's no pending task execution.
*/
declare _promise: AbortablePromise<Value> | null;
_promise: AbortablePromise<Value> | null = null;

/**
* The number times the executor was activated.
*/
declare _activeCount: number;
_activeCount: number = 0;

/**
* The pubsub that handles the executor subscriptions.
*/
declare _pubSub: PubSub<ExecutorEvent>;
_pubSub = new PubSub<ExecutorEvent>();

get isSettled(): boolean {
return this.isFulfilled || this.isRejected;
Expand All @@ -48,11 +48,7 @@ export class ExecutorImpl<Value = any> implements Executor {
constructor(
public readonly key: string,
public readonly manager: ExecutorManager
) {
definePrivateProperty(this, '_promise', null);
definePrivateProperty(this, '_activeCount', 0);
definePrivateProperty(this, '_pubSub', new PubSub());
}
) {}

get(): Value {
if (this.isFulfilled) {
Expand Down Expand Up @@ -101,8 +97,9 @@ export class ExecutorImpl<Value = any> implements Executor {
signal.addEventListener('abort', () => {
if (this._promise === promise) {
this._promise = null;
this.version++;
}
notify(this, 'aborted');
this._publish('aborted');
});

new Promise<Value>(resolve => {
Expand Down Expand Up @@ -134,11 +131,13 @@ export class ExecutorImpl<Value = any> implements Executor {

if (prevPromise !== null) {
prevPromise.abort(AbortError('The task was replaced: ' + this.key));
} else {
this.version++;
}

if (this._promise === promise) {
this.latestTask = task;
notify(this, 'pending');
this._publish('pending');
}

return promise;
Expand All @@ -155,7 +154,8 @@ export class ExecutorImpl<Value = any> implements Executor {
this.isFulfilled = this.isRejected = this.isStale = false;
this.value = this.reason = undefined;
this.timestamp = 0;
notify(this, 'cleared');
this.version++;
this._publish('cleared');
}
}

Expand All @@ -167,7 +167,8 @@ export class ExecutorImpl<Value = any> implements Executor {

invalidate(): void {
if (this.isStale !== (this.isStale = this.isSettled)) {
notify(this, 'invalidated');
this.version++;
this._publish('invalidated');
}
}

Expand All @@ -189,7 +190,8 @@ export class ExecutorImpl<Value = any> implements Executor {
this.value = value;
this.timestamp = timestamp;

notify(this, 'fulfilled');
this.version++;
this._publish('fulfilled');
}

reject(reason: any, timestamp = Date.now()): void {
Expand All @@ -205,22 +207,23 @@ export class ExecutorImpl<Value = any> implements Executor {
this.reason = reason;
this.timestamp = timestamp;

notify(this, 'rejected');
this.version++;
this._publish('rejected');
}

activate(): () => void {
let isActive = true;

if (this._activeCount++ === 0) {
notify(this, 'activated');
this._publish('activated');
}

return () => {
if (isActive) {
isActive = false;

if (--this._activeCount === 0) {
notify(this, 'deactivated');
this._publish('deactivated');
}
}
};
Expand All @@ -241,9 +244,8 @@ export class ExecutorImpl<Value = any> implements Executor {
timestamp: this.timestamp,
};
}
}

function notify(executor: ExecutorImpl, eventType: ExecutorEvent['type']): void {
executor.version++;
executor._pubSub.publish({ type: eventType, target: executor });
_publish(eventType: ExecutorEvent['type']): void {
this._pubSub.publish({ type: eventType, target: this, version: this.version });
}
}
4 changes: 2 additions & 2 deletions src/main/ExecutorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ExecutorManager implements Iterable<Executor> {

this._executors.set(key, executor);

executor._pubSub.publish({ type: 'configured', target: executor });
executor._publish('configured');

if (initialValue === undefined || executor.isSettled || executor.isPending) {
return executor;
Expand Down Expand Up @@ -149,7 +149,7 @@ export class ExecutorManager implements Iterable<Executor> {

this._executors.delete(key);

executor._pubSub.publish({ type: 'disposed', target: executor });
executor._publish('disposed');
executor._pubSub.unsubscribeAll();

return true;
Expand Down
8 changes: 7 additions & 1 deletion src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export interface ExecutorEvent<Value = any> {
* The executor for which the lifecycle event has occurred.
*/
target: Executor<Value>;

/**
* The version of the executor for which this event was published.
*/
version: number;
}

/**
Expand Down Expand Up @@ -204,7 +209,8 @@ export interface Executor<Value = any> extends ExecutorState<Value> {
readonly latestTask: ExecutorTask<Value> | null;

/**
* The integer version that is incremented every time the executor is mutated.
* The integer version of {@link ExecutorState the state of this executor} that is incremented every time it is
* mutated.
*/
readonly version: number;

Expand Down
4 changes: 0 additions & 4 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ export function isMatchingPeerKey(keys: Array<RegExp | string>, peerKey: string)
}
return false;
}

export function definePrivateProperty<T, K extends keyof T>(executor: T, key: K, value: T[K]): void {
Object.defineProperty(executor, key, { value, configurable: true, writable: true });
}
Loading

0 comments on commit 6794d71

Please sign in to comment.