svecosystem / runed

Magical utilities for your Svelte applications (WIP)
https://runed.dev
MIT License
461 stars 23 forks source link

Alternative api #48

Closed TGlide closed 4 months ago

TGlide commented 4 months ago

Exploring some alternative APIs for our existing utilities

changeset-bot[bot] commented 4 months ago

🦋 Changeset detected

Latest commit: fd7a511591ea535b2c9295f7841f1af39494b84c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ----- | ----- | | runed | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

github-actions[bot] commented 4 months ago
built with Refined Cloudflare Pages Action

âš¡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview fd7a511591ea535b2c9295f7841f1af39494b84c
abdel-17 commented 4 months ago

I suggest we keep the external API using functions and keep classes an implementation detail. Classes have a predetermined shape and cannot be overloaded, so something like #50 would have to be separated to ReadableStore and WritableStore.

An alternative is to have a static method attached to the class to handle overloads.

abstract class Store<T> {
  abstract value: T;

  static from<T>(readable: Readable<T>): Readonly<Store<T>>

  static from<T>(writable: Writable<T>): Store<T>

  static from<T>(store: Readable<T> | Writable<T>): Store<T> {
    if (isWritable(store)) {
      return new WritableStore(store);
    }
    return new ReadableStore(store);
  }
}

class ReadableStore<T> extends Store<T> { ... }

class WritableStore<T> extends Store<T> { ... }
niemyjski commented 3 months ago

I'm curious why box was removed? Just trying to learn more