Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider revisiting this with the current decorator proposal #9

Open
Jamesernator opened this issue Aug 26, 2019 · 0 comments
Open

Comments

@Jamesernator
Copy link

Jamesernator commented Aug 26, 2019

Since the decorator proposal changed to a hook-like approach, it's more extensible to more syntax. I think it would be worth revisiting this proposal by defining some decorators that hook into await.

For example a task library might look something like:

class Task {
    static iterator = Symbol('iterator');

    #initialize;
    constructor(initialize) {
        this.#initialize = initialize;
    }

    subscribe(subscriber) {
        let settled = false;
        const resolve = (value) => {
            if (settled) {
                 return;
            }
            settled = true;
            subscriber.value(value);
        }

        const reject = (error) => {
            if (settled) {
                return;
            } 
            settled = true;
            subscriber.error(error);
        }

        const cancel = this.#initialize(resolve, reject);

        return {
            unsubscribe() {
                if (settled) {
                    return;
                }
                settled = true;
                cancel();
            }
        }
    }
}

decorator @task {
  @coroutine({
    getAsyncIterator: value => value[Task.iterator](),
    onCall: (coroutine, asyncLocalState) => {
      const task = new Task((resolve, reject) => {
        asyncLocalState.resolve = resolve;
        asyncLocalState.reject = reject; 
        return () => asyncLocalState.currentSubscription?.unsubscribe();
      });
      coroutine.continue();
      return task;
    },
    onAwait: (coroutine, value, asyncLocalState) => {
      asyncLocalState.currentSubscription = value.subscribe({
        value: value => coroutine.continue(value),
        error: error => coroutine.throw(value),
      });
    },
    onThrow: (coroutine, error, asyncLocalState) => {
      asyncLocalState.reject(error);
    },
    onReturn: (coroutine, value, asyncLocalState) => {
      asyncLocalState.resolve(value);
    },
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant