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

Experimenting with proxies for the Effect API #30

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 37 additions & 0 deletions lib/task/effect.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from '~/test-utils';
import { Effect, WithIO, WithoutIO } from './effect';

describe('Effect', () => {
it('calculates changes to the given object', () => {
const effect = Effect.of((s: { foo: string }) => {
s.foo = 'bar';
});

const o = { foo: 'baz' };
effect(o);
expect(effect.changes).to.deep.equal([
{ op: 'replace', path: '/foo', value: 'bar' },
]);

expect(o).to.deep.equal({ foo: 'baz' });
});

it('it can perform async operations depending on the execution context', async () => {
const effect = Effect.of(async (s: { foo: string }, withIO: WithIO) => {
s.foo = await withIO(async () => 'bazbaz', 'bar');
});

const o = { foo: 'baz' };
await effect(o, WithoutIO);
expect(effect.changes).to.deep.equal([
{ op: 'replace', path: '/foo', value: 'bar' },
]);

await effect(o, WithIO);
expect(effect.changes).to.deep.equal([
{ op: 'replace', path: '/foo', value: 'bazbaz' },
]);

expect(o).to.deep.equal({ foo: 'baz' });
});
});
99 changes: 99 additions & 0 deletions lib/task/effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Path } from '../path';
import { Pointer } from '../pointer';

interface AddOperation<T, P extends Path> {
op: 'add';
path: P;
value: Pointer<T, P>;
}
interface RemoveOperation<P extends Path> {
op: 'remove';
path: P;
}
interface ReplaceOperation<T, P extends Path> {
op: 'replace';
path: P;
value: Pointer<T, P>;
}
export type Operation<T = any, P extends Path = '/'> =
| AddOperation<T, P>
| RemoveOperation<P>
| ReplaceOperation<T, P>;

type Tuple<T extends any[]> = T extends [infer THead, ...infer TTail]
? [THead, ...Tuple<TTail>]
: T;

export type EffectFn<T, A extends any[]> = (
s: T,
...args: Tuple<A>
) => void | Promise<void>;

interface AsyncEffect<T, A extends any[]> {
readonly changes: Array<Operation<T, any>>;
(s: T, ...args: Tuple<A>): Promise<void>;
}
interface PureEffect<T, A extends any[]> {
readonly changes: Array<Operation<T, any>>;
(s: T, ...args: Tuple<A>): void;
}
export type Effect<T, A extends any[]> = AsyncEffect<T, A> | PureEffect<T, A>;

export type WithIO = <T>(fn: () => Promise<T>, fallback: T) => T | Promise<T>;

function set<T extends object>(
target: T,
prop: string | symbol,
value: any,
changes: Array<Operation<T, any>>,
) {
if (prop in target) {
changes.push({
op: 'replace',
path: `/${prop as any}`,
value: value as any,
});
} else {
changes.push({ op: 'add', path: `/${prop as any}`, value: value as any });
}
// If value is a promise then all this will be called in the then() callback
return Reflect.set(target, prop, value);
}

function of<T extends object, A extends any[]>(
fn: (s: T, ...args: Tuple<A>) => Promise<void>,
): AsyncEffect<T, A>;
function of<T extends object, A extends any[]>(
fn: (s: T, ...args: Tuple<A>) => void,
): PureEffect<T, A>;
function of<T extends object, A extends any[]>(
fn: EffectFn<T, A>,
): Effect<T, A> {
const changes: Array<Operation<T, any>> = [];
const self = Object.assign(
function (s: T, ...args: Tuple<A>) {
self.changes = [];
s = new Proxy(structuredClone(s), {
set(target, prop, value) {
return set(target, prop, value, self.changes);
},
});
return fn(s, ...args);
},
{ changes },
);
return self;
}

export const Effect = {
of,
};

export function WithoutIO<T>(_: () => Promise<T>, d: T): T {
return d;
}

// eslint-disable-next-line
export async function WithIO<T>(f: () => Promise<T>, _: T): Promise<T> {
return await f();
}