Skip to content

Commit

Permalink
+new
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed Nov 2, 2024
1 parent f767426 commit b723a97
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ For the library, a class implementing another without going through the `Diamond
### Abstraction
`new Diamond(...)` is not possible as it is declared abstract (even if it has no abstract member) - This is so that it can inherit abstract classes!
`new Diamond(...)` is possible even if it still has abstract members. Yes, it can inherit abstract classes though the typings are not always possible there, and some `//@ts-ignore` might be needed.

The only problem still worked on is that if a class who has no implementation for an abstract method appears before another one who has an implementation, the method will be considered abstract (so the order of arguments for `Diamond(...)` matters here), even though a `//@ts-ignore` does the job.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "flat-diamond",
"version": "1.0.4",
"types": "./lib/index.d.ts",
"exports": {
".": {
"import": "./lib/esm.js",
Expand Down
4 changes: 2 additions & 2 deletions src/diamond.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ctor, HasBases } from './types'
import { Ctor, HasBases, Newable } from './types'
import { allFLegs, bottomLeg, fLegs, nextInFLeg, temporaryBuiltObjects } from './utils'

type BuildingStrategy = Map<Ctor, Ctor[]>
Expand Down Expand Up @@ -36,7 +36,7 @@ export const diamondHandler: {

export default function Diamond<TBases extends Ctor[]>(
...baseClasses: TBases
): Ctor<HasBases<TBases>> {
): Newable<HasBases<TBases>> {
const bases: Ctor[] = []
for (const base of baseClasses) {
let fLeg = [base, ...(fLegs(base) || [])]
Expand Down
5 changes: 3 additions & 2 deletions src/seclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function Seclude<TBase extends Ctor, Keys extends (keyof InstanceType<TBa
initPropertiesBasket: BasketBall[] = []
/**
* In order to integrate well in diamonds, we need to be a diamond
* When we create a diamond between the Secludeded and the base, the private properties of the base *have to*
* When we create a diamond between the Secluded and the base, the private properties of the base *have to*
* be collected before the diamond propagate them to the `constructedObject`
*/
abstract class PropertyCollector extends base {
Expand All @@ -47,7 +47,8 @@ export function Seclude<TBase extends Ctor, Keys extends (keyof InstanceType<TBa
}
const privates = new WeakMap<GateKeeper, TBase>(),
diamondSecluded = !fLegs(base),
diamond = diamondSecluded ? (Diamond(PropertyCollector) as TBase) : PropertyCollector
// any: abstract -> newable
diamond = diamondSecluded ? (Diamond(PropertyCollector) as any) : PropertyCollector
class GateKeeper extends (diamond as any) {
static secluded(obj: TBase): TBase | undefined {
return privates.get(obj)
Expand Down
3 changes: 1 addition & 2 deletions test/animal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Duck extends D(FlyingAnimal, SwimmingAnimal, WalkingAnimal) {
return [...super.actions, 'quack']
}
}
class Beaver extends D(WalkingAnimal, SwimmingAnimal) {}

beforeEach(() => {
logs() //make sure logs are cleared
Expand All @@ -62,7 +61,7 @@ test('inheritance', () => {
duck.doIt()
expect(logs()).toEqual(['KWAK', 'woosh', 'fshhh', 'picpoc', 'mniom'])

const beaver = new Beaver()
const beaver = new (D(WalkingAnimal, SwimmingAnimal))()
expect(beaver.actions).toEqual(['eat', 'sleep', 'swim', 'walk'])
beaver.doIt()
expect(logs()).toEqual(['picpoc', 'fshhh', 'mniom'])
Expand Down

0 comments on commit b723a97

Please sign in to comment.