-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathapi-internal.ts
More file actions
142 lines (129 loc) · 3.86 KB
/
Copy pathapi-internal.ts
File metadata and controls
142 lines (129 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// deno-lint-ignore-file ban-types no-explicit-any
import { createContext } from "./context.ts";
import { useScope } from "./scope.ts";
import type {
Api,
Around,
Context,
Middleware,
Operation,
Scope,
} from "./types.ts";
import type { ScopeInternal } from "./scope-internal.ts";
export interface ApiInternal<A> extends Api<A> {
context: Context<{
max: Partial<Around<A>>[];
min: Partial<Around<A>>[];
}>;
core: A;
}
export function createApiInternal<A extends {}>(
name: string,
core: A,
): ApiInternal<A> {
let fields = Object.keys(core) as (keyof A)[];
let context = createContext(`api::${name}`) as ApiInternal<A>["context"];
let api: ApiInternal<A> = {
core,
context,
invoke: (scope, key, args) => {
let handle = createHandle(api, scope);
let member = handle[key];
if (typeof member === "function") {
return member(...args);
} else {
return member;
}
},
around: (decorator, options = { at: "max" }) => ({
*[Symbol.iterator]() {
let scope = yield* useScope();
scope.around(api, decorator, options);
},
}),
operations: fields.reduce((sum, field) => {
if (typeof core[field] === "function") {
return Object.assign(sum, {
[field]: (...args: any) => ({
*[Symbol.iterator]() {
let scope = yield* useScope();
let target = api.invoke(scope, field, args);
return isOperation(target) ? yield* target : target;
},
}),
});
} else {
return Object.assign(sum, {
[field]: {
*[Symbol.iterator]() {
let scope = yield* useScope();
let target = api.invoke(scope, field, [] as any);
return isOperation(target) ? yield* target : target;
},
},
});
}
}, {} as Api<A>["operations"]),
};
return api;
}
function createHandle<A extends {}>(api: ApiInternal<A>, scope: Scope): A {
let handle = Object.create(api.core);
let $scope = scope as ScopeInternal;
for (let key of Object.keys(api.core) as Array<keyof A>) {
let dispatch = (args: unknown[], next: (...args: unknown[]) => unknown) => {
let { min, max } = $scope.reduce(api.context, (sum, current) => {
let min = current.min.flatMap((around) =>
around[key] ? [around[key]] : []
);
let max = current.max.flatMap((around) =>
around[key] ? [around[key]] : []
);
sum.min.push(...min);
sum.max.unshift(...max);
return sum;
}, {
min: [] as Around<A>[typeof key][],
max: [] as Around<A>[typeof key][],
});
let stack = combine(
max.concat(min) as Middleware<unknown[], unknown>[],
);
return stack(args, next);
};
if (typeof api.core[key] === "function") {
handle[key] = (...args: unknown[]) =>
dispatch(args, api.core[key] as (...args: unknown[]) => unknown);
} else {
Object.defineProperty(handle, key, {
enumerable: true,
get() {
return dispatch([], () => api.core[key]);
},
});
}
}
return handle;
}
function isOperation<T>(
target: Operation<T> | T,
): target is Operation<T> {
return target && !isNativeIterable(target) &&
typeof (target as Operation<T>)[Symbol.iterator] === "function";
}
function isNativeIterable(target: unknown): boolean {
return (
typeof target === "string" || Array.isArray(target) ||
target instanceof Map || target instanceof Set
);
}
function combine<TArgs extends unknown[], TReturn>(
middlewares: Middleware<TArgs, TReturn>[],
): Middleware<TArgs, TReturn> {
if (middlewares.length === 0) {
return (args, next) => next(...args);
}
return middlewares.reduceRight((sum, middleware) => (args, next) =>
middleware(args, (...args) => sum(args, next))
);
}