{"action":"created","comment":{"url":"https://api.github.com/repos/benski/BAPromise/pulls/comments/211835979","pull_request_review_id":148340476,"id":211835979,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxMTgzNTk3OQ==","diff_hunk":"@@ -0,0 +1,269 @@\n+//\n+//  BAPromise.swift\n+//  OS X\n+//\n+//  Created by Ben Allison on 6/15/18.\n+//  Copyright © 2018 Ben Allison. All rights reserved.\n+//\n+\n+import Foundation\n+\n+public enum PromiseResult<ValueType> {\n+    case success(ValueType)\n+    case promise(Promise<ValueType>)\n+    case failure(Error)\n+    \n+    var resolved: Bool {\n+        switch(self) {\n+        case .success, .failure:\n+            return true\n+        default:\n+            return false\n+        }\n+    }\n+    \n+    init(error: Error) {\n+        self = .failure(error)\n+    }\n+    \n+    init(promise: Promise<ValueType>) {\n+        self = .promise(promise)\n+    }\n+    \n+    init(value: ValueType) {\n+        self = .success(value)\n+    }\n+    \n+}\n+\n+extension PromiseResult where ValueType == Void {\n+    static var success: PromiseResult {\n+        return .success(())\n+    }\n+    \n+    init() {\n+        self = .success(())\n+    }\n+}\n+\n+public class PromiseCancelToken {\n+    var cancelled: Bool = false\n+    static let queue = DispatchQueue(label: \"com.github.benski.promise\")\n+    var onCancel : (() -> Void)?\n+    \n+    func cancelled(_ onCancel: @escaping () -> Void, on queue: DispatchQueue) {\n+        let wrappedBlock = {\n+            queue.async {\n+                onCancel()\n+            }\n+        }\n+        \n+        PromiseCancelToken.queue.async {\n+            if self.cancelled {\n+                wrappedBlock()\n+            } else {\n+                self.onCancel = wrappedBlock\n+            }\n+        }\n+    }\n+    \n+    func cancel() {\n+        cancelled = true\n+        OSMemoryBarrier()\n+        PromiseCancelToken.queue.async {\n+            self.onCancel?()\n+            self.onCancel = nil\n+        }\n+    }\n+}\n+\n+public class Promise<ValueType> : PromiseCancelToken {\n+    \n+    var fulfilledObject: PromiseResult<ValueType>?\n+    \n+    public class PromiseBlock {\n+        var done : ((ValueType) -> Void)?\n+        var observed : ((ValueType) -> Void)?\n+        var rejected : ((Error) -> Void)?\n+        var always : (() -> Void)?\n+        var queue : DispatchQueue?\n+        let cancellationToken : PromiseCancelToken\n+        \n+        init(cancellationToken: PromiseCancelToken) {\n+            self.cancellationToken = cancellationToken\n+        }\n+        var shouldKeepPromise: Bool {\n+            get {\n+                return done != nil || always != nil || rejected != nil\n+            }\n+        }\n+        \n+        private func internalCall(with object: PromiseResult<ValueType>) {\n+            if !self.cancellationToken.cancelled {\n+                if case let .failure(error) = object {\n+                    rejected?(error)\n+                } else if case let .success(value) = object {\n+                    done?(value)\n+                    observed?(value)\n+                }\n+                always?()\n+            }\n+        }\n+        \n+        func call(with object: PromiseResult<ValueType>) {\n+            if let queue = queue {\n+                queue.async {\n+                    self.internalCall(with: object)\n+                }\n+            } else {\n+                internalCall(with: object)\n+            }\n+        }\n+    }\n+    \n+    @discardableResult func then(_ onFulfilled: ((ValueType) -> Void)? = nil,\n+                                 observed: ((ValueType) -> Void)? = nil,\n+                                 rejected : ((Error) -> Void)? = nil,\n+                                 always : (() -> Void)? = nil,\n+                                 queue : DispatchQueue) -> PromiseCancelToken {\n+        let cancellationToken = PromiseCancelToken()\n+        let blocks = PromiseBlock(cancellationToken: cancellationToken)\n+        blocks.queue = queue\n+        blocks.done = onFulfilled\n+        blocks.observed = observed\n+        blocks.rejected = rejected\n+        blocks.always = always\n+        \n+        cancellationToken.cancelled({ [weak self, unowned blocks] in\n+            Promise.queue.async {\n+                if let `self` = self {                    \n+                    blocks.done = nil\n+                    blocks.observed = nil\n+                    blocks.rejected = nil\n+                    blocks.always = nil\n+                    \n+                    var strongCount = false\n+                    for block in `self`.blocks {\n+                        if block.shouldKeepPromise {\n+                            strongCount = true\n+                            break","path":"Classes/Promise.swift","position":149,"original_position":149,"commit_id":"6fe22cbdaa26778e10be4fa3143d4a600d90c120","original_commit_id":"6fe22cbdaa26778e10be4fa3143d4a600d90c120","user":{"login":"mvyrko","id":11045737,"node_id":"MDQ6VXNlcjExMDQ1NzM3","avatar_url":"https://avatars3.githubusercontent.com/u/11045737?v=4","gravatar_id":"","url":"https://api.github.com/users/mvyrko","html_url":"https://github.com/mvyrko","followers_url":"https://api.github.com/users/mvyrko/followers","following_url":"https://api.github.com/users/mvyrko/following{/other_user}","gists_url":"https://api.github.com/users/mvyrko/gists{/gist_id}","starred_url":"https://api.github.com/users/mvyrko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mvyrko/subscriptions","organizations_url":"https://api.github.com/users/mvyrko/orgs","repos_url":"https://api.github.com/users/mvyrko/repos","events_url":"https://api.github.com/users/mvyrko/events{/privacy}","received_events_url":"https://api.github.com/users/mvyrko/received_events","type":"User","site_admin":false},"body":"from my point of view would be more readable one line conditional\r\n`for block in blocks where block.shouldKeepPromise`","created_at":"2018-08-22T05:55:47Z","updated_at":"2018-08-22T05:55:47Z","html_url":"https://github.com/benski/BAPromise/pull/13#discussion_r211835979","pull_request_url":"https://api.github.com/repos/benski/BAPromise/pulls/13","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/comments/211835979"},"html":{"href":"https://github.com/benski/BAPromise/pull/13#discussion_r211835979"},"pull_request":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/benski/BAPromise/pulls/13","id":209934987,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA5OTM0OTg3","html_url":"https://github.com/benski/BAPromise/pull/13","diff_url":"https://github.com/benski/BAPromise/pull/13.diff","patch_url":"https://github.com/benski/BAPromise/pull/13.patch","issue_url":"https://api.github.com/repos/benski/BAPromise/issues/13","number":13,"state":"open","locked":false,"title":"Re-write in Swift. WORK IN PROGRESS","user":{"login":"benski","id":702119,"node_id":"MDQ6VXNlcjcwMjExOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/702119?v=4","gravatar_id":"","url":"https://api.github.com/users/benski","html_url":"https://github.com/benski","followers_url":"https://api.github.com/users/benski/followers","following_url":"https://api.github.com/users/benski/following{/other_user}","gists_url":"https://api.github.com/users/benski/gists{/gist_id}","starred_url":"https://api.github.com/users/benski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benski/subscriptions","organizations_url":"https://api.github.com/users/benski/orgs","repos_url":"https://api.github.com/users/benski/repos","events_url":"https://api.github.com/users/benski/events{/privacy}","received_events_url":"https://api.github.com/users/benski/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-08-21T19:44:28Z","updated_at":"2018-08-22T05:55:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cc722f0bbf1f167922a0c14b7c1ab1ee20f47ab3","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/benski/BAPromise/pulls/13/commits","review_comments_url":"https://api.github.com/repos/benski/BAPromise/pulls/13/comments","review_comment_url":"https://api.github.com/repos/benski/BAPromise/pulls/comments{/number}","comments_url":"https://api.github.com/repos/benski/BAPromise/issues/13/comments","statuses_url":"https://api.github.com/repos/benski/BAPromise/statuses/6fe22cbdaa26778e10be4fa3143d4a600d90c120","head":{"label":"benski:swift-port","ref":"swift-port","sha":"6fe22cbdaa26778e10be4fa3143d4a600d90c120","user":{"login":"benski","id":702119,"node_id":"MDQ6VXNlcjcwMjExOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/702119?v=4","gravatar_id":"","url":"https://api.github.com/users/benski","html_url":"https://github.com/benski","followers_url":"https://api.github.com/users/benski/followers","following_url":"https://api.github.com/users/benski/following{/other_user}","gists_url":"https://api.github.com/users/benski/gists{/gist_id}","starred_url":"https://api.github.com/users/benski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benski/subscriptions","organizations_url":"https://api.github.com/users/benski/orgs","repos_url":"https://api.github.com/users/benski/repos","events_url":"https://api.github.com/users/benski/events{/privacy}","received_events_url":"https://api.github.com/users/benski/received_events","type":"User","site_admin":false},"repo":{"id":37872147,"node_id":"MDEwOlJlcG9zaXRvcnkzNzg3MjE0Nw==","name":"BAPromise","full_name":"benski/BAPromise","owner":{"login":"benski","id":702119,"node_id":"MDQ6VXNlcjcwMjExOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/702119?v=4","gravatar_id":"","url":"https://api.github.com/users/benski","html_url":"https://github.com/benski","followers_url":"https://api.github.com/users/benski/followers","following_url":"https://api.github.com/users/benski/following{/other_user}","gists_url":"https://api.github.com/users/benski/gists{/gist_id}","starred_url":"https://api.github.com/users/benski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benski/subscriptions","organizations_url":"https://api.github.com/users/benski/orgs","repos_url":"https://api.github.com/users/benski/repos","events_url":"https://api.github.com/users/benski/events{/privacy}","received_events_url":"https://api.github.com/users/benski/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benski/BAPromise","description":"Objective C Promise Library","fork":false,"url":"https://api.github.com/repos/benski/BAPromise","forks_url":"https://api.github.com/repos/benski/BAPromise/forks","keys_url":"https://api.github.com/repos/benski/BAPromise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benski/BAPromise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benski/BAPromise/teams","hooks_url":"https://api.github.com/repos/benski/BAPromise/hooks","issue_events_url":"https://api.github.com/repos/benski/BAPromise/issues/events{/number}","events_url":"https://api.github.com/repos/benski/BAPromise/events","assignees_url":"https://api.github.com/repos/benski/BAPromise/assignees{/user}","branches_url":"https://api.github.com/repos/benski/BAPromise/branches{/branch}","tags_url":"https://api.github.com/repos/benski/BAPromise/tags","blobs_url":"https://api.github.com/repos/benski/BAPromise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benski/BAPromise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benski/BAPromise/git/refs{/sha}","trees_url":"https://api.github.com/repos/benski/BAPromise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benski/BAPromise/statuses/{sha}","languages_url":"https://api.github.com/repos/benski/BAPromise/languages","stargazers_url":"https://api.github.com/repos/benski/BAPromise/stargazers","contributors_url":"https://api.github.com/repos/benski/BAPromise/contributors","subscribers_url":"https://api.github.com/repos/benski/BAPromise/subscribers","subscription_url":"https://api.github.com/repos/benski/BAPromise/subscription","commits_url":"https://api.github.com/repos/benski/BAPromise/commits{/sha}","git_commits_url":"https://api.github.com/repos/benski/BAPromise/git/commits{/sha}","comments_url":"https://api.github.com/repos/benski/BAPromise/comments{/number}","issue_comment_url":"https://api.github.com/repos/benski/BAPromise/issues/comments{/number}","contents_url":"https://api.github.com/repos/benski/BAPromise/contents/{+path}","compare_url":"https://api.github.com/repos/benski/BAPromise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benski/BAPromise/merges","archive_url":"https://api.github.com/repos/benski/BAPromise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benski/BAPromise/downloads","issues_url":"https://api.github.com/repos/benski/BAPromise/issues{/number}","pulls_url":"https://api.github.com/repos/benski/BAPromise/pulls{/number}","milestones_url":"https://api.github.com/repos/benski/BAPromise/milestones{/number}","notifications_url":"https://api.github.com/repos/benski/BAPromise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benski/BAPromise/labels{/name}","releases_url":"https://api.github.com/repos/benski/BAPromise/releases{/id}","deployments_url":"https://api.github.com/repos/benski/BAPromise/deployments","created_at":"2015-06-22T18:10:22Z","updated_at":"2018-06-15T16:26:29Z","pushed_at":"2018-08-21T21:23:21Z","git_url":"git://github.com/benski/BAPromise.git","ssh_url":"git@github.com:benski/BAPromise.git","clone_url":"https://github.com/benski/BAPromise.git","svn_url":"https://github.com/benski/BAPromise","homepage":null,"size":153,"stargazers_count":2,"watchers_count":2,"language":"Objective-C","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"archived":false,"open_issues_count":2,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":2,"open_issues":2,"watchers":2,"default_branch":"master"}},"base":{"label":"benski:master","ref":"master","sha":"6f15afd8c0350ef44ac09aab3a0b4fc258b09392","user":{"login":"benski","id":702119,"node_id":"MDQ6VXNlcjcwMjExOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/702119?v=4","gravatar_id":"","url":"https://api.github.com/users/benski","html_url":"https://github.com/benski","followers_url":"https://api.github.com/users/benski/followers","following_url":"https://api.github.com/users/benski/following{/other_user}","gists_url":"https://api.github.com/users/benski/gists{/gist_id}","starred_url":"https://api.github.com/users/benski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benski/subscriptions","organizations_url":"https://api.github.com/users/benski/orgs","repos_url":"https://api.github.com/users/benski/repos","events_url":"https://api.github.com/users/benski/events{/privacy}","received_events_url":"https://api.github.com/users/benski/received_events","type":"User","site_admin":false},"repo":{"id":37872147,"node_id":"MDEwOlJlcG9zaXRvcnkzNzg3MjE0Nw==","name":"BAPromise","full_name":"benski/BAPromise","owner":{"login":"benski","id":702119,"node_id":"MDQ6VXNlcjcwMjExOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/702119?v=4","gravatar_id":"","url":"https://api.github.com/users/benski","html_url":"https://github.com/benski","followers_url":"https://api.github.com/users/benski/followers","following_url":"https://api.github.com/users/benski/following{/other_user}","gists_url":"https://api.github.com/users/benski/gists{/gist_id}","starred_url":"https://api.github.com/users/benski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benski/subscriptions","organizations_url":"https://api.github.com/users/benski/orgs","repos_url":"https://api.github.com/users/benski/repos","events_url":"https://api.github.com/users/benski/events{/privacy}","received_events_url":"https://api.github.com/users/benski/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benski/BAPromise","description":"Objective C Promise Library","fork":false,"url":"https://api.github.com/repos/benski/BAPromise","forks_url":"https://api.github.com/repos/benski/BAPromise/forks","keys_url":"https://api.github.com/repos/benski/BAPromise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benski/BAPromise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benski/BAPromise/teams","hooks_url":"https://api.github.com/repos/benski/BAPromise/hooks","issue_events_url":"https://api.github.com/repos/benski/BAPromise/issues/events{/number}","events_url":"https://api.github.com/repos/benski/BAPromise/events","assignees_url":"https://api.github.com/repos/benski/BAPromise/assignees{/user}","branches_url":"https://api.github.com/repos/benski/BAPromise/branches{/branch}","tags_url":"https://api.github.com/repos/benski/BAPromise/tags","blobs_url":"https://api.github.com/repos/benski/BAPromise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benski/BAPromise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benski/BAPromise/git/refs{/sha}","trees_url":"https://api.github.com/repos/benski/BAPromise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benski/BAPromise/statuses/{sha}","languages_url":"https://api.github.com/repos/benski/BAPromise/languages","stargazers_url":"https://api.github.com/repos/benski/BAPromise/stargazers","contributors_url":"https://api.github.com/repos/benski/BAPromise/contributors","subscribers_url":"https://api.github.com/repos/benski/BAPromise/subscribers","subscription_url":"https://api.github.com/repos/benski/BAPromise/subscription","commits_url":"https://api.github.com/repos/benski/BAPromise/commits{/sha}","git_commits_url":"https://api.github.com/repos/benski/BAPromise/git/commits{/sha}","comments_url":"https://api.github.com/repos/benski/BAPromise/comments{/number}","issue_comment_url":"https://api.github.com/repos/benski/BAPromise/issues/comments{/number}","contents_url":"https://api.github.com/repos/benski/BAPromise/contents/{+path}","compare_url":"https://api.github.com/repos/benski/BAPromise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benski/BAPromise/merges","archive_url":"https://api.github.com/repos/benski/BAPromise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benski/BAPromise/downloads","issues_url":"https://api.github.com/repos/benski/BAPromise/issues{/number}","pulls_url":"https://api.github.com/repos/benski/BAPromise/pulls{/number}","milestones_url":"https://api.github.com/repos/benski/BAPromise/milestones{/number}","notifications_url":"https://api.github.com/repos/benski/BAPromise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benski/BAPromise/labels{/name}","releases_url":"https://api.github.com/repos/benski/BAPromise/releases{/id}","deployments_url":"https://api.github.com/repos/benski/BAPromise/deployments","created_at":"2015-06-22T18:10:22Z","updated_at":"2018-06-15T16:26:29Z","pushed_at":"2018-08-21T21:23:21Z","git_url":"git://github.com/benski/BAPromise.git","ssh_url":"git@github.com:benski/BAPromise.git","clone_url":"https://github.com/benski/BAPromise.git","svn_url":"https://github.com/benski/BAPromise","homepage":null,"size":153,"stargazers_count":2,"watchers_count":2,"language":"Objective-C","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"archived":false,"open_issues_count":2,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":2,"open_issues":2,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/13"},"html":{"href":"https://github.com/benski/BAPromise/pull/13"},"issue":{"href":"https://api.github.com/repos/benski/BAPromise/issues/13"},"comments":{"href":"https://api.github.com/repos/benski/BAPromise/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/benski/BAPromise/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/benski/BAPromise/statuses/6fe22cbdaa26778e10be4fa3143d4a600d90c120"}},"author_association":"OWNER"}}
{"actor":{"display_login":"mvyrko"}}