| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 // Generated file. Do not edit. | |
| 6 | |
| 7 package immi; | |
| 8 | |
| 9 import java.util.concurrent.ExecutorService; | |
| 10 import java.util.concurrent.Executors; | |
| 11 | |
| 12 import dartino.ImmiServiceLayer; | |
| 13 import dartino.ImmiServiceLayer.RefreshCallback; | |
| 14 import dartino.PatchData; | |
| 15 | |
| 16 public final class ImmiRoot { | |
| 17 | |
| 18 // Public interface. | |
| 19 | |
| 20 public void refresh() { | |
| 21 refreshThread.submit(requestRefresh); | |
| 22 } | |
| 23 | |
| 24 public void reset() { | |
| 25 refreshThread.submit(requestReset); | |
| 26 } | |
| 27 | |
| 28 // Package private implementation. | |
| 29 | |
| 30 // For construction see ImmiService.registerPresenter. | |
| 31 ImmiRoot(int id, AnyNodePresenter presenter) { | |
| 32 assert id > 0; | |
| 33 this.id = id; | |
| 34 this.presenter = presenter; | |
| 35 } | |
| 36 | |
| 37 void dispatch(Runnable event) { | |
| 38 event.run(); | |
| 39 // TODO(zerny): Consider other strategies than eagerly requesting a refresh | |
| 40 // on each dispatch. | |
| 41 refresh(); | |
| 42 } | |
| 43 | |
| 44 // Private implementation. | |
| 45 | |
| 46 // No assumptions are made on which thread patch application is performed on. | |
| 47 // Initiating a refresh is controlled by requestRefresh and finishRefresh belo
w. | |
| 48 // Single thread executor guaranties mutual exclusion of requestRefresh and fi
nishRefresh. | |
| 49 private RefreshCallback applyPatch = new RefreshCallback() { | |
| 50 @Override | |
| 51 public void handle(PatchData data) { | |
| 52 if (data.isNode()) { | |
| 53 AnyNodePatch patch = new AnyNodePatch(data.getNode(), previous, ImmiRoot
.this); | |
| 54 previous = patch.getCurrent(); | |
| 55 patch.applyTo(presenter); | |
| 56 } | |
| 57 refreshThread.submit(finishRefresh); | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 private Runnable requestRefresh = new Runnable() { | |
| 62 @Override | |
| 63 public void run() { | |
| 64 if (refreshPending) { | |
| 65 // Request a refresh once the pending refresh finishes. | |
| 66 refreshRequired = true; | |
| 67 } else { | |
| 68 // Initiate a new refresh. | |
| 69 refreshPending = true; | |
| 70 ImmiServiceLayer.refreshAsync(id, applyPatch); | |
| 71 } | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 private Runnable finishRefresh = new Runnable() { | |
| 76 @Override | |
| 77 public void run() { | |
| 78 assert refreshPending; | |
| 79 if (refreshRequired) { | |
| 80 // A refresh request is outstanding so immediately initiate a new refres
h. | |
| 81 refreshRequired = false; | |
| 82 ImmiServiceLayer.refreshAsync(id, applyPatch); | |
| 83 } else { | |
| 84 refreshPending = false; | |
| 85 } | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 // To guarantee order of refresh and reset we schedule reset on the same execu
tor. | |
| 90 private Runnable requestReset = new Runnable() { | |
| 91 @Override | |
| 92 public void run() { | |
| 93 ImmiServiceLayer.resetAsync(id, null); | |
| 94 } | |
| 95 }; | |
| 96 | |
| 97 private int id; | |
| 98 private AnyNodePresenter presenter; | |
| 99 private AnyNode previous; | |
| 100 private boolean refreshPending = false; | |
| 101 private boolean refreshRequired = false; | |
| 102 private final static ExecutorService refreshThread = Executors.newSingleThread
Executor(); | |
| 103 } | |
| OLD | NEW |