Skip to content

Commit 5f69537

Browse files
committed
feat: stash
1 parent a62861c commit 5f69537

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

packages/core2/aa.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { bodyParser } from '@koa/bodyparser';
2+
import debug from 'debug';
3+
import Koa from 'koa';
4+
import compose from 'koa-compose';
5+
6+
class Context {
7+
/**
8+
* @type {Strategy} The Context maintains a reference to one of the Strategy
9+
* objects. The Context does not know the concrete class of a strategy. It
10+
* should work with all strategies via the Strategy interface.
11+
*/
12+
private plugins: Strategy[];
13+
app;
14+
use;
15+
/**
16+
* Usually, the Context accepts a strategy through the constructor, but also
17+
* provides a setter to change it at runtime.
18+
*/
19+
constructor() {
20+
// init
21+
const app = new Koa();
22+
app.use(bodyParser());
23+
24+
this.app = app;
25+
this.use = app.use;
26+
}
27+
28+
/**
29+
* Usually, the Context allows replacing a Strategy object at runtime.
30+
*/
31+
public plugin(strategy: Strategy) {
32+
this.plugins.push(strategy);
33+
}
34+
35+
public mount() {
36+
console.dir('s');
37+
// mount
38+
for (const plugin of this.plugins) {
39+
console.log(plugin);
40+
}
41+
}
42+
43+
/**
44+
* The Context delegates some work to the Strategy object instead of
45+
* implementing multiple versions of the algorithm on its own.
46+
*/
47+
public start(): void {
48+
this.mount();
49+
50+
console.log("Context: Sorting data using the strategy (not sure how it'll do it)");
51+
// const result = this.strategy.doAlgorithm(['a', 'b', 'c', 'd', 'e']);
52+
// console.log(result.join(','));
53+
54+
// ...
55+
// return listenerCount()
56+
}
57+
}
58+
59+
/**
60+
* The Strategy interface declares operations common to all supported versions
61+
* of some algorithm.
62+
*
63+
* The Context uses this interface to call the algorithm defined by Concrete
64+
* Strategies.
65+
*/
66+
interface Strategy {
67+
doAlgorithm(data: string[]): string[];
68+
}
69+
70+
/**
71+
* Concrete Strategies implement the algorithm while following the base Strategy
72+
* interface. The interface makes them interchangeable in the Context.
73+
*/
74+
class ConcreteStrategyA implements Strategy {
75+
public doAlgorithm(data: string[]): string[] {
76+
return data.sort();
77+
}
78+
}
79+
80+
class ConcreteStrategyB implements Strategy {
81+
public doAlgorithm(data: string[]): string[] {
82+
return data.reverse();
83+
}
84+
}
85+
86+
/**
87+
* The client code picks a concrete strategy and passes it to the context. The
88+
* client should be aware of the differences between strategies in order to make
89+
* the right choice.
90+
*/
91+
// const context = new Context(new ConcreteStrategyA());
92+
// console.log('Client: Strategy is set to normal sorting.');
93+
// context.start();
94+
95+
// console.log('');
96+
97+
// console.log('Client: Strategy is set to reverse sorting.');
98+
// context.plugin(new ConcreteStrategyB());
99+
// context.start();

packages/core2/app.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createServer } from './src/index';
2+
3+
const rpc = createServer({});
4+
5+
rpc.plugin();
6+
rpc.plugin();
7+
8+
rpc.listen(3000);

packages/core2/fn.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { F } from 'vitest/dist/reporters-5f784f42';
2+
3+
export class Fn {
4+
public name: string;
5+
public init: Array<Function>;
6+
public load: Array<Function>;
7+
8+
constructor() {
9+
this.name = 'fn';
10+
this.init = [];
11+
this.load = [];
12+
}
13+
14+
async render(ctx, next) {
15+
console.dir('');
16+
await next();
17+
}
18+
}

0 commit comments

Comments
 (0)