Skip to content

Commit

Permalink
Add detailed history and tests; Optional callback to syncTime
Browse files Browse the repository at this point in the history
  • Loading branch information
superguineapig committed Dec 13, 2018
1 parent 897d0e2 commit c085c7d
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 136 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,24 @@ NetInfo.isConnected.addEventListener('connectionChange', handleConnectivityChang

**NOTE:** The example above does not account for rapid changes in network state. You may wish to add additional handling to 'de-bounce' such changes. Also, remember to remove the listener and set your clockSync instance to *offline* when done (un-mounting components, shutting down, etc.)

### syncTime()
### syncTime( *[callback]* )

An on-demand method that will force a sync with an NTP server. Will not sync or update when *offline*.

**NOTE:** You generally do not need to invoke a manual sync since *clockSync* automatically runs sync according to the specified `syncDelay` interval (or its default).

An optional callback function may be supplied to monitor completion/failure of the requested sync. Callback will accept a single `boolean` parameter that indicates success or failure of the requested sync. Callback will always be given `false` when instance is *offline*.

```javascript
clock.syncTime();
```

or

```javascript
function cb(success) {
console.log("sync was" + (success ? "" : " not") + " a success");
}

clock.syncTime(cb);
```
10 changes: 8 additions & 2 deletions __mocks__/react-native-ntp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let __offset_ms = 0;

// custom method to allow tests to set a server delta time
// can be +/- (values in milliseconds)
function __setOffsetMs(ms) {
function __setOffsetMS(ms) {
__offset_ms = ms;
}

Expand All @@ -24,6 +24,11 @@ function __useJitter(j) {
__jitter = j;
}

function __reset() {
__offset_ms = 0;
__jitter = false;
}

// custom getNetworkTime that simply calls callback
// after generating a time value, or error
function getNetworkTime(s, p, cb) {
Expand All @@ -42,8 +47,9 @@ function getNetworkTime(s, p, cb) {

/**** mocked API ****/
client.MOCK_FAILING_SERVER = MOCK_FAILING_SERVER;
client.__setOffsetMs = __setOffsetMs;
client.__setOffsetMS = __setOffsetMS;
client.__useJitter = __useJitter;
client.__resetForTest = __reset;
// overrides
client.getNetworkTime = getNetworkTime;

Expand Down
Loading

0 comments on commit c085c7d

Please sign in to comment.