Skip to content

Commit

Permalink
fix: deprecation warnings
Browse files Browse the repository at this point in the history
remove same_thread from spawn as it is no longer used
  • Loading branch information
stakach committed Oct 14, 2024
1 parent 31803d7 commit 7d50243
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: promise
version: 3.1.0
version: 3.2.0
crystal: ">= 1.1.1"

development_dependencies:
Expand Down
26 changes: 13 additions & 13 deletions spec/promise_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ describe Promise do

p1.resolve(:fin)

spawn(same_thread: true) do
spawn(same_thread: true) do
spawn(same_thread: true) do
spawn do
spawn do
spawn do
LOG << :resolving
p2.resolve(:foo)
end
Expand Down Expand Up @@ -402,7 +402,7 @@ describe Promise do

describe "deferred code" do
it "should run some code concurrently" do
result = Promise.defer(same_thread: true) {
result = Promise.defer {
# Tuple
{123, "string"}
}.get
Expand All @@ -412,7 +412,7 @@ describe Promise do

it "should return errors concurrently" do
begin
Promise.defer(same_thread: true) {
Promise.defer {
raise "an error occured"
}.get
raise "no go"
Expand Down Expand Up @@ -446,7 +446,7 @@ describe Promise do
it "should be able to asynchronously map over a collection" do
collection = [1, 2, 3, 4, 5]
promise_collection = Promise.map(collection) do |v|
sleep 0.002
sleep 0.002.seconds
v + 1
end
promise_collection.get.should eq [2, 3, 4, 5, 6]
Expand All @@ -466,15 +466,15 @@ describe Promise do
it "should return the first promise to be resolved" do
p1 = Promise.new(Symbol).resolve(:foo)
p2 = Promise.new(String)
spawn(same_thread: true) { p2.resolve("testing") }
spawn { p2.resolve("testing") }
val = Promise.race(p1, p2).get
val.should eq :foo

p1 = Promise.new(Symbol)
p2 = Promise.new(String)
spawn(same_thread: true) { p2.resolve("testing") }
spawn { p2.resolve("testing") }
spawn do
sleep 0.002
sleep 0.002.seconds
p1.resolve(:foo)
end
val = Promise.race(p1, p2).get
Expand All @@ -484,7 +484,7 @@ describe Promise do
it "should return the first promise to be rejected" do
p1 = Promise.new(Symbol).reject("err")
p2 = Promise.new(String)
spawn(same_thread: true) { p2.resolve("testing") }
spawn { p2.resolve("testing") }

begin
val = Promise.race(p1, p2).get
Expand All @@ -495,9 +495,9 @@ describe Promise do

p1 = Promise.new(Symbol)
p2 = Promise.new(String)
spawn(same_thread: true) { p2.reject("testing") }
spawn { p2.reject("testing") }
spawn do
sleep 0.002
sleep 0.002.seconds
p1.resolve(:foo)
end

Expand All @@ -511,7 +511,7 @@ describe Promise do

it "should work with different types" do
Promise.race(
Promise.defer { sleep 1; 1.3 },
Promise.defer { sleep 1.seconds; 1.3 },
Promise.defer { "string" }
).get.should eq "string"
end
Expand Down
6 changes: 3 additions & 3 deletions spec/timeout_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe "promise timeouts" do
end

it "should timeout a defer" do
p = Promise.defer(timeout: 1.millisecond) { sleep 1; "p1 wins" }
p = Promise.defer(timeout: 1.millisecond) { sleep 1.seconds; "p1 wins" }
expect_raises(Promise::Timeout) { p.get }
end

it "should timeout a race" do
expect_raises(Promise::Timeout) do
Promise.race(
Promise.defer(timeout: 1.millisecond) { sleep 1; "p1 wins" },
Promise.defer { sleep 1; "p2 wins" }
Promise.defer(timeout: 1.millisecond) { sleep 1.seconds; "p1 wins" },
Promise.defer { sleep 1.seconds; "p2 wins" }
).get
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ abstract class Promise(Input)

# Execute code in the next tick of the event loop
# and return a promise for obtaining the value
def self.defer(same_thread = false, timeout = nil, &block : -> _)
promise = ::Promise::ImplicitDefer.new(same_thread, &block).execute!
def self.defer(timeout = nil, &block : -> _)
promise = ::Promise::ImplicitDefer.new(&block).execute!
spawn { ::Promise.timeout(promise, timeout) } if timeout
promise
end
Expand Down Expand Up @@ -93,9 +93,9 @@ abstract class Promise(Input)
end

# Asynchronously map an `Enumerable`
def self.map(collection : Enumerable(T), same_thread = false, &block : T -> V) forall T, V
def self.map(collection : Enumerable(T), &block : T -> V) forall T, V
promise_collection = collection.map do |element|
::Promise.defer(same_thread: same_thread) do
::Promise.defer do
block.call(element)
end
end
Expand All @@ -122,7 +122,7 @@ abstract class Promise(Input)

def self.all(*promises)
result = DeferredPromise(typeof(static_map_type_var(*promises))).new
spawn(same_thread: true) do
spawn do
begin
result.resolve(static_map_get(*promises))
rescue error
Expand All @@ -140,7 +140,7 @@ abstract class Promise(Input)
end

result = DeferredPromise(typeof(promises.map(&.type_var))).new
spawn(same_thread: true) do
spawn do
begin
result.resolve(promises.map(&.get))
rescue error
Expand Down
2 changes: 1 addition & 1 deletion src/promise/deferred_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Promise::DeferredPromise(Input) < Promise(Input)
def raw_value
channel = Channel(Input | Exception).new

spawn(same_thread: true) do
spawn do
self.then(->(result : Input) {
channel.send(result)
nil
Expand Down
4 changes: 2 additions & 2 deletions src/promise/implicit_defer.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "./deferred_promise"

class Promise::ImplicitDefer(Output)
def initialize(@same_thread = false, &@block : -> Output)
def initialize(&@block : -> Output)
end

def execute!
# Replace NoReturn with Nil if the block will always `raise` an error
promise = DeferredPromise(typeof(Generic(Output).new.type_var)).new

spawn(same_thread: @same_thread) do
spawn do
begin
promise.resolve(@block.call)
rescue error
Expand Down
2 changes: 1 addition & 1 deletion src/promise/rejected_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Promise::RejectedPromise(Input) < Promise::DeferredPromise(Input)
reason = @rejection

# Execute next tick
spawn(same_thread: true) do
spawn do
begin
ret = errback.call(reason)
ret.is_a?(Input | DeferredPromise(Input) | RejectedPromise(Input) | ResolvedPromise(Input)) ? result.resolve(ret) : result.reject(ret)
Expand Down
2 changes: 1 addition & 1 deletion src/promise/resolved_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Promise::ResolvedPromise(Input) < Promise::DeferredPromise(Input)
# Replace NoReturn with Nil if the block will always `raise` an error
promise = DeferredPromise(typeof(Generic(Output).new.type_var)).new

spawn(same_thread: true) do
spawn do
begin
promise.resolve(@block.call(@input))
rescue error
Expand Down

0 comments on commit 7d50243

Please sign in to comment.