Skip to content

Commit

Permalink
refactor: moved some packages around
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Aug 5, 2021
1 parent 1ed86cd commit 0afd28a
Show file tree
Hide file tree
Showing 24 changed files with 146 additions and 121 deletions.
3 changes: 3 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"build": "tsup",
"prepublish": "yarn build"
},
"dependencies": {
"@harlem/utilities": "^1.3.2"
},
"peerDependencies": {
"vue": "^3.2.0-beta.7"
},
Expand Down
2 changes: 1 addition & 1 deletion core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export const EVENTS = {
devtools: {
update: 'devtools:update',
},
};
} as const;
9 changes: 4 additions & 5 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
} from './constants';

import {
lockObject,
raiseOverwriteError,
} from './utilities';
lock,
} from '@harlem/utilities';

import type {
App,
Expand Down Expand Up @@ -43,7 +42,7 @@ function validateStoreCreation(name: string): void {
const store = stores.get(name);

if (store && !store.allowsOverwrite) {
raiseOverwriteError('store', name);
throw new Error(`A store named ${name} has already been registered.`);
}
}

Expand Down Expand Up @@ -88,7 +87,7 @@ function installPlugin(plugin: HarlemPlugin, app: App): void {
install,
} = plugin;

const lockedStores = lockObject(stores, [
const lockedStores = lock(stores, [
'set',
'delete',
'clear',
Expand Down
24 changes: 8 additions & 16 deletions core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
} from 'vue';

import {
raiseOverwriteError,
} from './utilities';
clone,
} from '@harlem/utilities';

import type {
BaseState,
Expand Down Expand Up @@ -66,21 +66,17 @@ export default class Store<TState extends BaseState = any> implements InternalSt
providers: {
read: value => value,
write: value => value,
payload: value => value,
payload: value => clone(value),
...options?.providers,
},
};

this.name = name;
this.registrations = {};
this.stack = new Set();
this.scope = effectScope();
this.writeState = reactive(state) as WriteState<TState>;
this.readState = readonly(this.writeState) as ReadState<TState>;

this.registrations = {
getters: new Map(),
mutations: new Map(),
};
}

public get allowsOverwrite(): boolean {
Expand Down Expand Up @@ -143,6 +139,10 @@ export default class Store<TState extends BaseState = any> implements InternalSt
this.registrations[group] = new Map();
}

if (!this.allowsOverwrite && this.hasRegistration(group, name)) {
throw new Error(`A ${group} named ${name} has already been registered on this store`);
}

this.registrations[group].set(name, {
type,
producer,
Expand All @@ -154,10 +154,6 @@ export default class Store<TState extends BaseState = any> implements InternalSt
}

public getter<TResult>(name: string, getter: Getter<TState, TResult>): ComputedRef<TResult> {
if (!this.allowsOverwrite && this.hasRegistration('getters', name)) {
raiseOverwriteError('getter', name);
}

const output = this.track(() => computed(() => getter(this.state)));

this.register('getters', name, () => output.value, 'computed');
Expand Down Expand Up @@ -208,10 +204,6 @@ export default class Store<TState extends BaseState = any> implements InternalSt
}

public mutation<TPayload, TResult = void>(name: string, mutator: Mutator<TState, TPayload, TResult>): Mutation<TPayload, TResult> {
if (!this.allowsOverwrite && this.hasRegistration('mutations', name)) {
raiseOverwriteError('mutation', name);
}

const mutation = ((payload: TPayload) => {
return this.mutate(name, SENDER, mutator, payload);
}) as Mutation<TPayload, TResult>;
Expand Down
7 changes: 1 addition & 6 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type BaseState = object;
export type StoreProvider<TState extends BaseState> = keyof StoreProviders<TState>;
export type ReadState<TState extends BaseState> = DeepReadonly<TState>;
export type WriteState<TState extends BaseState> = TState;
export type StoreRegistrations = Record<string, Map<string, StoreRegistration>>;
export type RegistrationType = 'ref' | 'reactive' | 'computed' | 'other';
export type RegistrationValueProducer = () => unknown;
export type Getter<TState extends BaseState, TResult> = (state: ReadState<TState>) => TResult;
Expand Down Expand Up @@ -107,9 +108,3 @@ export interface StoreRegistration {
type: RegistrationType;
producer: RegistrationValueProducer;
}

export interface StoreRegistrations {
[key: string]: Map<string, StoreRegistration>;
getters: Map<string, StoreRegistration>;
mutations: Map<string, StoreRegistration>;
}
4 changes: 3 additions & 1 deletion extensions/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"build": "tsup",
"prepublish": "yarn build"
},
"dependencies": {
"@harlem/task": "^1.1.2"
},
"peerDependencies": {
"@harlem/core": "^1.1.2",
"@harlem/utilities": "^1.1.2",
"vue": "^3.2.0-beta.7"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions extensions/actions/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SENDER = 'extension:action';
71 changes: 27 additions & 44 deletions extensions/actions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Task from '@harlem/task';

import {
Task,
} from '@harlem/utilities';
SENDER,
} from './constants';

import {
watchEffect,
Expand Down Expand Up @@ -28,46 +30,34 @@ export default function actionsExtension<TState extends BaseState>() {
return (store: InternalStore<TState>) => {
const _store = store as unknown as InternalStore<TState & ActionStoreState>;

_store.write('$add-actions', '$actions-extension', state => {
_store.write('$action-init', SENDER, state => {
state.$actions = {};
});

const registerAction = _store.mutation('$register-action', (state, name: string) => {
state.$actions[name] = {
runCount: 0,
instances: new Map<symbol, unknown>(),
};
});

const clearActionRunCount = _store.mutation('$clear-run-count', (state, name: string) => state.$actions[name].runCount = 0);
const incrementRunCount = _store.mutation('$increment-run-count', (state, name: string) => state.$actions[name].runCount += 1);

const addInstance = _store.mutation<AddActionInstancePayload>('$add-action-instance', (state, payload) => {
const {
actionName,
instanceId,
instancePayload,
} = payload;

if (!state.$actions[actionName]) {
return;
}
function registerAction(name: string) {
_store.write('$action-register', SENDER, state => {
state.$actions[name] = {
runCount: 0,
instances: new Map<symbol, unknown>(),
};
});
}

state.$actions[actionName].instances.set(instanceId, instancePayload);
});
function clearActionRunCount(name: string) {
_store.write('$action-clear-run-count', SENDER, state => state.$actions[name].runCount = 0);
}

const removeInstance = _store.mutation<RemoveActionInstancePayload>('$remove-action-instance', (state, payload) => {
const {
actionName,
instanceId,
} = payload;
function incrementRunCount(name: string) {
_store.write('$action-increment-run-count', SENDER, state => state.$actions[name].runCount += 1);
}

if (!state.$actions[actionName]) {
return;
}
function addInstance(name: string, instanceId: symbol, payload: unknown) {
_store.write('$action-add-instance', SENDER, state => state.$actions[name]?.instances.set(instanceId, payload));
}

state.$actions[actionName].instances.delete(instanceId);
});
function removeInstance(name: string, instanceId: symbol) {
_store.write('$action-remove-instance', SENDER, state => state.$actions[name]?.instances.delete(instanceId));
}

function action<TPayload, TResult = void>(name: string, body: ActionBody<TState, TPayload, TResult>, options?: Partial<ActionOptions>): Action<TPayload, TResult> {
registerAction(name);
Expand All @@ -94,17 +84,10 @@ export default function actionsExtension<TState extends BaseState>() {
const task = new Task<TResult>(async (resolve, reject, controller, onAbort) => {
const id = Symbol(name);

const complete = () => (tasks.delete(task), removeInstance({
actionName: name,
instanceId: id,
}));
const complete = () => (tasks.delete(task), removeInstance(name, id));

onAbort(() => (complete(), reject()));
addInstance({
actionName: name,
instanceId: id,
instancePayload: payload,
});
addInstance(name, id, payload);

try {
const result = await body(payload, mutate, controller, onAbort);
Expand Down
5 changes: 2 additions & 3 deletions extensions/actions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import type {
Mutator,
} from '@harlem/core';

import type {
Task,
import Task, {
TaskAbortCallback,
} from '@harlem/utilities';
} from '@harlem/task';

export type ActionBody<TState extends BaseState, TPayload = undefined, TResult = void> = (payload: TPayload, mutator: (mutate: Mutator<TState, undefined, void>) => void, controller: AbortController, onAbort: (callback: TaskAbortCallback) => void) => Promise<TResult>;
export type Action<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload, controller?: AbortController) => Task<TResult> : (payload: TPayload, controller?: AbortController) => Task<TResult>;
Expand Down
4 changes: 1 addition & 3 deletions extensions/actions/test/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
Task,
} from '@harlem/utilities';
import Task from '@harlem/task';

import {
getStore,
Expand Down
6 changes: 3 additions & 3 deletions extensions/history/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
"build": "tsup",
"prepublish": "yarn build"
},
"peerDependencies": {
"@harlem/core": "^1.1.2"
},
"dependencies": {
"@harlem/extension-trace": "^1.3.2",
"@harlem/utilities": "^1.3.2"
},
"peerDependencies": {
"@harlem/core": "^1.1.2"
},
"devDependencies": {
"@harlem/core": "^1.3.2",
"@harlem/testing": "^1.3.2"
Expand Down
2 changes: 2 additions & 0 deletions extensions/history/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {
CommandTasks,
} from './types';

export const SENDER = 'extension:history';

export const COMMAND_MAP = {
exec: {
set: (target, prop, newValue) => target[prop] = newValue,
Expand Down
31 changes: 15 additions & 16 deletions extensions/history/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
COMMAND_MAP,
SENDER,
} from './constants';

import {
Expand All @@ -21,7 +22,6 @@ import {
import type {
CommandType,
HistoryCommand,
MutationPayload,
Options,
} from './types';

Expand Down Expand Up @@ -53,21 +53,23 @@ export default function historyExtension<TState extends BaseState>(options?: Par
let commands = [] as HistoryCommand[];
let results = [] as TraceResult<any>[];

const mutation = store.mutation('$history', (state, { type, command }: MutationPayload) => {
const tasks = COMMAND_MAP[type];
function executeCommand(type: CommandType, command: HistoryCommand) {
store.write('$history', SENDER, state => {
const tasks = COMMAND_MAP[type];

const {
results,
} = command;
const {
results,
} = command;

results.forEach(({ gate, nodes, prop, newValue, oldValue }) => {
const target = fromPath(state, nodes);
results.forEach(({ gate, nodes, prop, newValue, oldValue }) => {
const target = fromPath(state, nodes);

if (target && prop) {
tasks[gate]?.(target, prop, newValue, oldValue);
}
if (target && prop) {
tasks[gate]?.(target, prop, newValue, oldValue);
}
});
});
});
}

function processResults(name: string) {
if (results.length === 0) {
Expand Down Expand Up @@ -114,10 +116,7 @@ export default function historyExtension<TState extends BaseState>(options?: Par
return;
}

mutation({
type,
command,
});
executeCommand(type, command);

position = Math.max(0, Math.min(commands.length - 1, position + offset));
}
Expand Down
35 changes: 35 additions & 0 deletions packages/task/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@harlem/task",
"amdName": "harlemTask",
"version": "1.3.2",
"license": "MIT",
"author": "Andrew Courtice <[email protected]>",
"description": "Harlem task package",
"homepage": "https://harlemjs.com",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": "./dist/index.js",
"unpkg": "./dist/index.global.js",
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"keywords": [
"vue",
"state",
"harlem",
"task"
],
"repository": {
"type": "git",
"url": "https://github.com/andrewcourtice/harlem.git",
"directory": "packages/task"
},
"bugs": {
"url": "https://github.com/andrewcourtice/harlem/issues"
},
"scripts": {
"dev": "tsup --watch src",
"build": "tsup",
"prepublish": "yarn build"
}
}
Loading

0 comments on commit 0afd28a

Please sign in to comment.