const testLayer = <I, S extends object>(tag: Context.Tag<I, S>, impl = {} as Partial<S>): Layer.Layer<I> =>
Layer.succeed(tag, makeProxy(tag.key, impl));
const makeProxy = <T extends object>(
service: string,
impl: Partial<T>
): T =>
new Proxy({ ...impl } as T, {
get(target, prop, _receiver) {
if (prop in target) {
return target[prop as keyof T];
}
return ((target as any)[prop] = unimplementedMethod(service, prop));
},
has: () => true
});
const unimplementedMethod = (id: string, prop: PropertyKey) => {
const dead = Effect.die(`${id}: Unimplemented method "${prop.toString()}"`);
function method() { return dead; }
const yieldable = Object.assign(method, dead);
Object.setPrototypeOf(yieldable, Object.getPrototypeOf(dead));
return yieldable;
};
const testLayer = <I, S extends object>(tag: Context.Tag<I, S>, impl = {} as Partial<S>): Layer.Layer<I> =>
Layer.succeed(tag, makeProxy(tag.key, impl));
const makeProxy = <T extends object>(
service: string,
impl: Partial<T>
): T =>
new Proxy({ ...impl } as T, {
get(target, prop, _receiver) {
if (prop in target) {
return target[prop as keyof T];
}
return ((target as any)[prop] = unimplementedMethod(service, prop));
},
has: () => true
});
const unimplementedMethod = (id: string, prop: PropertyKey) => {
const dead = Effect.die(`${id}: Unimplemented method "${prop.toString()}"`);
function method() { return dead; }
const yieldable = Object.assign(method, dead);
Object.setPrototypeOf(yieldable, Object.getPrototypeOf(dead));
return yieldable;
};