Skip to content

Tree-shakeable protocol definitions #95

@legendecas

Description

@legendecas

The current design installs symbol-keyed methods onto the class prototype via implement. Even if a consumer never calls a particular protocol method, it is bundled and attached to the class because a bundle would not be able to infer if it would be used or not. Protocol implementations are non-tree-shakeable.

A library may define many protocols and methods, but a given consumer may only use a few. The current design forces shipping all of them.

Sparked by the recent TC39 tree-shaking discussion, I'm wondering if we could make protocols not mixing into the implementing class. Instead, the consumer imports the protocol and applies it via extension-call or call-this syntax:

import { ProtocolA } from 'some-protocols';
const instance = new MyClass();
instance::(ProtocolA.methodFoo)();

In this case,

  • the protocol is applied by the user, not the class author (this could conflict with the proposal motivation, but could be a bonus point as extending existing classes does not need patching the target class prototype).
  • the class prototype does not mix-in new properties (this could also conflict with the proposal motivation, but would be necessary for tree-shaking).
  • since protocols are frozen objects, bundlers can also eliminate individual unused methods within an imported protocol.

This enforces explicit references to each protocol and each protocol method used in user code.

Might be related: #46

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions