promise.pony

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use "time"

actor Promise[A: Any #share]
  """
  A promise to eventually produce a result of type A. This promise can either
  be fulfilled or rejected.

  Any number of promises can be chained after this one.
  """
  var _value: (_Pending | _Reject | A) = _Pending
  embed _list: Array[_IThen[A]] = _list.create()

  be apply(value: A) =>
    """
    Fulfill the promise.
    """
    if _value isnt _Pending then
      return
    end

    _value = value

    for f in _list.values() do
      f(value)
    end

    _list.clear()

  be reject() =>
    """
    Reject the promise.
    """
    if _value isnt _Pending then
      return
    end

    _value = _Reject

    for f in _list.values() do
      f.reject()
    end

    _list.clear()

  fun tag next[B: Any #share](
    fulfill: Fulfill[A, B],
    rejected: Reject[B] = RejectAlways[B])
    : Promise[B]
  =>
    """
    Chain a promise after this one.

    When this promise is fulfilled, the result of type A is passed to the
    fulfill function, generating in an intermediate result of type B. This
    is then used to fulfill the next promise in the chain.

    If there is no fulfill function, or if the fulfill function raises an
    error, then the next promise in the chain will be rejected.

    If this promise is rejected, this step's reject function is called with no
    input, generating an intermediate result of type B which is used to
    fulfill the next promise in the chain.

    If there is no reject function, of if the reject function raises an error,
    then the next promise in the chain will be rejected.
    """
    let attach = _Then[A, B](consume fulfill, consume rejected)
    let promise = attach.promise()
    _attach(consume attach)
    promise

  fun tag flatten_next[B: Any #share](
    fulfill: Fulfill[A, Promise[B]],
    rejected: Reject[Promise[B]] = RejectAlways[Promise[B]])
    : Promise[B]
  =>
    """
    Chain a promise after this one and unwrap the promise returned from this
    one.

    `flatten_next` is a companion to `next`. It operates in an identical
    fashion except for the type of the fulfilled promise. Whereas `next` takes
    a function that returns a type `B`, `flatten_next` takes a function that
    returns `Promise[B]`.

    Why is `flatten_next` valuable given that next could take a `B` that is of
    a type like `Promise[String]`? Let's start with some code to demonstrate the
    problem that arises when returning `Promise[Promise[B]]` from `next`.

    Let's say we have a library for accessing the GitHub REST API:

    ```pony
    class GitHub
      new create(personal_access_token: String)

      fun get_repo(repo: String): Promise[Repository]

    class Repository
      fun get_issue(number: I64): Promise[Issue]

    class Issue
      fun title(): String
    ```

    And we want to use this promise based API to look up the title of an issue.
    Without `flatten_next`, we could attempt to do the following using `next`:

    ```pony
    actor Main
      new create(env: Env) =>
        let repo: Promise[Repository] =
          GitHub("my token").get_repo("ponylang/ponyc")

        //
        // do something with the repo once the promise is fulfilled
        // in our case, get the issue
        //

        let issue = Promise[Promise[Issue]] =
          repo.next[Promise[Issue]](FetchIssue~apply(1))

        // once we get the issue, print the title
        issue.next[None](PrintIssueTitle~apply(env.out))

    primitive FetchIssue
      fun apply(number: I64, repo: Repository): Promise[Issue] =>
        repo.get_issue(number)

    primitive PrintIssueTitle
      fun apply(out: OutStream, issue: Promise[Issue]) =>
        // O NO! We can't print the title
        // We don't have an issue, we have a promise for an issue
    ```

    Take a look at what happens in the example, when we get to
    `PrintIssueTitle`, we can't print anything because we "don't have anything".
    In order to print the issue title, we need an `Issue` not a
    `Promise[Issue]`.

    We could solve this by doing something like this:

    ```pony
    primitive PrintIssueTitle
      fun apply(out: OutStream, issue: Promise[Issue]) =>
        issue.next[None](ActuallyPrintIssueTitle~apply(out))

    primitive ActuallyPrintIssueTitle
      fun apply(out: OutStream, issue: Issue) =>
        out.print(issue.title())
    ```

    That will work, however, it is kind of awful. When looking at:

    ```pony
        let repo: Promise[Repoository] =
          GitHub("my token").get_repo("ponylang/ponyc")
        let issue = Promise[Promise[Issue]] =
          repo.next[Promise[Issue]](FetchIssue~apply(1))
        issue.next[None](PrintIssueTitle~apply(env.out))
    ```

    it can be hard to follow what is going on. We can only tell what is
    happening because we gave `PrintIssueTitle` a very misleading name; it
    doesn't print an issue title.

    `flatten_next` addresses the problem of "we want the `Issue`, not the
    intermediate `Promise`". `flatten_next` takes an intermediate promise and
    unwraps it into the fulfilled type. You get to write your promise chain
    without having to worry about intermediate promises.

    Updated to use `flatten_next`, our API example becomes:

    ```pony
    actor Main
      new create(env: Env) =>
        let repo: Promise[Repository] =
          GitHub("my token").get_repo("ponylang/ponyc")

        let issue = Promise[Issue] =
          repo.flatten_next[Issue](FetchIssue~apply(1))

        issue.next[None](PrintIssueTitle~apply(env.out))

    primitive FetchIssue
      fun apply(number: I64, repo: Repository): Promise[Issue] =>
        repo.get_issue(number)

    primitive PrintIssueTitle
      fun apply(out: OutStream, issue: Issue) =>
        out.print(issue.title())
    ```

    Our promise `Issue`, is no longer a `Promise[Promise[Issue]]`. By using
    `flatten_next`, we have a much more manageable `Promise[Issue]` instead.

    Other than unwrapping promises for you, `flatten_next` otherwise acts the
    same as `next` so all the same rules apply to fulfillment and rejection.
    """
    let outer = Promise[B]

    next[None](object iso
      var f: Fulfill[A, Promise[B]] = consume fulfill
      let p: Promise[B] = outer

      fun ref apply(value: A) =>
        let fulfill' = f = _PromiseFulFill[A, B]

        try
          let inner = (consume fulfill').apply(value)?

          inner.next[None](
            {(fulfilled: B) => p(fulfilled)},
            {()? => p.reject(); error})
        else
          p.reject()
        end
    end,
    object iso
      var r: Reject[Promise[B]] = consume rejected
      let p: Promise[B] = outer

      fun ref apply() =>
        let rejected' = r = RejectAlways[Promise[B]]

        try
          (consume rejected').apply()?
        else
          p.reject()
        end
    end)

    outer

  fun tag add[B: Any #share = A](p: Promise[B]): Promise[(A, B)] =>
    """
    Add two promises into one promise that returns the result of both when
    they are fulfilled. If either of the promises is rejected then the new
    promise is also rejected.
    """
    let p' = Promise[(A, B)]

    let c =
      object
        var _a: (A | _None) = _None
        var _b: (B | _None) = _None

        be fulfill_a(a: A) =>
          match _b
          | let b: B => p'((a, b))
          else _a = a
          end

        be fulfill_b(b: B) =>
          match _a
          | let a: A => p'((a, b))
          else _b = b
          end
      end

    next[None](
      {(a) => c.fulfill_a(a) },
      {() => p'.reject() })

    p.next[None](
      {(b) => c.fulfill_b(b) },
      {() => p'.reject() })

    p'

  fun tag join(ps: Iterator[Promise[A]]): Promise[Array[A] val] =>
    """
    Create a promise that is fulfilled when the receiver and all promises in
    the given iterator are fulfilled. If the receiver or any promise in the
    sequence is rejected then the new promise is also rejected.

    Join `p1` and `p2` with an existing promise, `p3`.
    ```pony
    use "promises"

    actor Main
      new create(env: Env) =>

        let p1 = Promise[String val]
        let p2 = Promise[String val]
        let p3 = Promise[String val]

        p3.join([p1; p2].values())
          .next[None]({(a: Array[String val] val) =>
            for s in a.values() do
              env.out.print(s)
            end
          })

        p2("second")
        p3("third")
        p1("first")
    ```
    """
    Promises[A].join(
      [this]
        .> concat(ps)
        .values())

  fun tag select(p: Promise[A]): Promise[(A, Promise[A])] =>
    """
    Return a promise that is fulfilled when either promise is fulfilled,
    resulting in a tuple of its value and the other promise.
    """
    let p' = Promise[(A, Promise[A])]

    let s =
      object tag
        var _complete: Bool = false
        let _p: Promise[(A, Promise[A])] = p'

        be apply(a: A, p: Promise[A]) =>
          if not _complete then
            _p((a, p))
            _complete = true
          end
      end

    next[None]({(a) => s(a, p) })
    p.next[None]({(a)(p = this) => s(a, p) })

    p'

  fun tag timeout(expiration: U64) =>
    """
    Reject the promise after the given expiration in nanoseconds.
    """
    Timers.apply(Timer(
      object iso is TimerNotify
        let _p: Promise[A] = this
        fun ref apply(timer: Timer, count: U64): Bool =>
          _p.reject()
          false
      end,
      expiration))

  be _attach(attach: _IThen[A] iso) =>
    """
    Attaches a step asynchronously. If this promise has already been fulfilled
    or rejected, immediately fulfill or reject the incoming step. Otherwise,
    keep it in a list.
    """
    if _value is _Pending then
      _list.push(consume attach)
    elseif _value is _Reject then
      attach.reject()
    else
      try attach(_value as A) end
    end

primitive Promises[A: Any #share]
  fun join(ps: Iterator[Promise[A]]): Promise[Array[A] val] =>
    """
    Create a promise that is fulfilled when all promises in the given sequence
    are fulfilled. If any promise in the sequence is rejected then the new
    promise is also rejected. The order that values appear in the final array
    is based on when each promise is fulfilled and not the order that they are
    given.

    Join three existing promises to make a fourth.
    ```pony
    use "promises"

    actor Main
      new create(env: Env) =>

        let p1 = Promise[String val]
        let p2 = Promise[String val]
        let p3 = Promise[String val]

        Promises[String val].join([p1; p2; p3].values())
          .next[None]({(a: Array[String val] val) =>
            for s in a.values() do
              env.out.print(s)
            end
          })

        p2("second")
        p3("third")
        p1("first")
    ```
    """
    let p' = Promise[Array[A] val]
    let ps' = Array[Promise[A]] .> concat(consume ps)

    if ps'.size() == 0 then
      p'(recover Array[A] end)
      return p'
    end

    let j = _Join[A](p', ps'.size())
    for p in ps'.values() do
      p.next[None]({(a)(j) => j(a)}, {() => p'.reject()})
    end

    p'

actor _Join[A: Any #share]
  embed _xs: Array[A]
  let _space: USize
  let _p: Promise[Array[A] val]

  new create(p: Promise[Array[A] val], space: USize) =>
    (_xs, _space, _p) = (Array[A](space), space, p)

  be apply(a: A) =>
    _xs.push(a)
    if _xs.size() == _space then
      let len = _xs.size()
      let xs = recover Array[A](len) end
      for x in _xs.values() do
        xs.push(x)
      end
      _p(consume xs)
    end

primitive _None


class iso _PromiseFulFill[A: Any #share, B: Any #share] is Fulfill[A, Promise[B]]
  """
  Fulfill discarding its input value of `A` and returning a promise of type `B`.
  """
  new iso create() => None
  fun ref apply(value: A): Promise[B] => Promise[B]