Skip to content

Commit

Permalink
feat: stash
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Oct 31, 2023
1 parent a62861c commit 5f69537
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
99 changes: 99 additions & 0 deletions packages/core2/aa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { bodyParser } from '@koa/bodyparser';
import debug from 'debug';
import Koa from 'koa';
import compose from 'koa-compose';

class Context {
/**
* @type {Strategy} The Context maintains a reference to one of the Strategy
* objects. The Context does not know the concrete class of a strategy. It
* should work with all strategies via the Strategy interface.
*/
private plugins: Strategy[];
app;
use;
/**
* Usually, the Context accepts a strategy through the constructor, but also
* provides a setter to change it at runtime.
*/
constructor() {
// init
const app = new Koa();
app.use(bodyParser());

this.app = app;
this.use = app.use;
}

/**
* Usually, the Context allows replacing a Strategy object at runtime.
*/
public plugin(strategy: Strategy) {
this.plugins.push(strategy);
}

public mount() {
console.dir('s');
// mount
for (const plugin of this.plugins) {
console.log(plugin);
}
}

/**
* The Context delegates some work to the Strategy object instead of
* implementing multiple versions of the algorithm on its own.
*/
public start(): void {
this.mount();

console.log("Context: Sorting data using the strategy (not sure how it'll do it)");
// const result = this.strategy.doAlgorithm(['a', 'b', 'c', 'd', 'e']);
// console.log(result.join(','));

// ...
// return listenerCount()
}
}

/**
* The Strategy interface declares operations common to all supported versions
* of some algorithm.
*
* The Context uses this interface to call the algorithm defined by Concrete
* Strategies.
*/
interface Strategy {
doAlgorithm(data: string[]): string[];
}

/**
* Concrete Strategies implement the algorithm while following the base Strategy
* interface. The interface makes them interchangeable in the Context.
*/
class ConcreteStrategyA implements Strategy {
public doAlgorithm(data: string[]): string[] {
return data.sort();
}
}

class ConcreteStrategyB implements Strategy {
public doAlgorithm(data: string[]): string[] {
return data.reverse();
}
}

/**
* The client code picks a concrete strategy and passes it to the context. The
* client should be aware of the differences between strategies in order to make
* the right choice.
*/
// const context = new Context(new ConcreteStrategyA());
// console.log('Client: Strategy is set to normal sorting.');
// context.start();

// console.log('');

// console.log('Client: Strategy is set to reverse sorting.');
// context.plugin(new ConcreteStrategyB());
// context.start();
8 changes: 8 additions & 0 deletions packages/core2/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createServer } from './src/index';

const rpc = createServer({});

rpc.plugin();
rpc.plugin();

rpc.listen(3000);
18 changes: 18 additions & 0 deletions packages/core2/fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { F } from 'vitest/dist/reporters-5f784f42';

export class Fn {
public name: string;
public init: Array<Function>;
public load: Array<Function>;

constructor() {
this.name = 'fn';
this.init = [];
this.load = [];
}

async render(ctx, next) {
console.dir('');
await next();
}
}

0 comments on commit 5f69537

Please sign in to comment.