| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package clock | 5 package clock |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // Context key for clock. | |
| 14 type clockKeyType int | |
| 15 | |
| 16 // Unique value for clock key. | 13 // Unique value for clock key. |
| 17 var clockKey clockKeyType | 14 var clockKey = "clock.Clock" |
| 18 | 15 |
| 19 // Factory is a generator function that produces a Clock instnace. | 16 // Factory is a generator function that produces a Clock instnace. |
| 20 type Factory func(context.Context) Clock | 17 type Factory func(context.Context) Clock |
| 21 | 18 |
| 22 // SetFactory creates a new Context using the supplied Clock factory. | 19 // SetFactory creates a new Context using the supplied Clock factory. |
| 23 func SetFactory(ctx context.Context, f Factory) context.Context { | 20 func SetFactory(ctx context.Context, f Factory) context.Context { |
| 24 » return context.WithValue(ctx, clockKey, f) | 21 » return context.WithValue(ctx, &clockKey, f) |
| 25 } | 22 } |
| 26 | 23 |
| 27 // Set creates a new Context using the supplied Clock. | 24 // Set creates a new Context using the supplied Clock. |
| 28 func Set(ctx context.Context, c Clock) context.Context { | 25 func Set(ctx context.Context, c Clock) context.Context { |
| 29 return SetFactory(ctx, func(context.Context) Clock { return c }) | 26 return SetFactory(ctx, func(context.Context) Clock { return c }) |
| 30 } | 27 } |
| 31 | 28 |
| 32 // Get returns the Clock set in the supplied Context, defaulting to | 29 // Get returns the Clock set in the supplied Context, defaulting to |
| 33 // SystemClock() if none is set. | 30 // SystemClock() if none is set. |
| 34 func Get(ctx context.Context) (clock Clock) { | 31 func Get(ctx context.Context) (clock Clock) { |
| 35 » if v := ctx.Value(clockKey); v != nil { | 32 » if v := ctx.Value(&clockKey); v != nil { |
| 36 if f, ok := v.(Factory); ok { | 33 if f, ok := v.(Factory); ok { |
| 37 clock = f(ctx) | 34 clock = f(ctx) |
| 38 } | 35 } |
| 39 } | 36 } |
| 40 if clock == nil { | 37 if clock == nil { |
| 41 clock = GetSystemClock() | 38 clock = GetSystemClock() |
| 42 } | 39 } |
| 43 return | 40 return |
| 44 } | 41 } |
| 45 | 42 |
| 46 // Now calls Clock.Now on the Clock instance stored in the supplied Context. | 43 // Now calls Clock.Now on the Clock instance stored in the supplied Context. |
| 47 func Now(ctx context.Context) time.Time { | 44 func Now(ctx context.Context) time.Time { |
| 48 return Get(ctx).Now() | 45 return Get(ctx).Now() |
| 49 } | 46 } |
| 50 | 47 |
| 51 // Sleep calls Clock.Sleep on the Clock instance stored in the supplied Context. | 48 // Sleep calls Clock.Sleep on the Clock instance stored in the supplied Context. |
| 52 func Sleep(ctx context.Context, d time.Duration) { | 49 func Sleep(ctx context.Context, d time.Duration) { |
| 53 » Get(ctx).Sleep(d) | 50 » Get(ctx).Sleep(ctx, d) |
| 54 } | 51 } |
| 55 | 52 |
| 56 // NewTimer calls Clock.NewTimer on the Clock instance stored in the supplied | 53 // NewTimer calls Clock.NewTimer on the Clock instance stored in the supplied |
| 57 // Context. | 54 // Context. |
| 58 func NewTimer(ctx context.Context) Timer { | 55 func NewTimer(ctx context.Context) Timer { |
| 59 » return Get(ctx).NewTimer() | 56 » return Get(ctx).NewTimer(ctx) |
| 60 } | 57 } |
| 61 | 58 |
| 62 // After calls Clock.After on the Clock instance stored in the supplied Context. | 59 // After calls Clock.After on the Clock instance stored in the supplied Context. |
| 63 func After(ctx context.Context, d time.Duration) <-chan time.Time { | 60 func After(ctx context.Context, d time.Duration) <-chan TimerResult { |
| 64 » return Get(ctx).After(d) | 61 » return Get(ctx).After(ctx, d) |
| 65 } | 62 } |
| OLD | NEW |