Skip to content

Update Asyn.java #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 137 additions & 129 deletions Java/Loon-Neo/src/loon/Asyn.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Copyright 2008 - 2015 The Loon Game Engine Authors
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*
* @project loon
* @author cping
* @email:[email protected]
Expand All @@ -26,129 +26,137 @@
import loon.utils.reply.Port;

public abstract class Asyn {

public static class Default extends Asyn {

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private class CallDefaultPort<T> extends Port<T>{

private Default _def;

CallDefaultPort(Default d){
this._def = d;
}

@Override
public void onEmit(Object event) {
_def.dispatch();
}

}

private final TArray<Runnable> pending = new TArray<>();
private final TArray<Runnable> running = new TArray<>();
protected final Log log;

public Default(Log log, Act<? extends Object> frame) {
this.log = log;
frame.connect(new CallDefaultPort<Object>(this)).setPriority(Short.MAX_VALUE);
}

@Override
public boolean isAsyncSupported() {
return false;
}

@Override
public void invokeAsync(Runnable action) {
throw new UnsupportedOperationException();
}

@Override
public synchronized void invokeLater(Runnable action) {
pending.add(action);
}

private void dispatch() {
synchronized (this) {
running.addAll(pending);
pending.clear();
}

for (int ii = 0, ll = running.size; ii < ll; ii++) {
Runnable action = running.get(ii);
try {
action.run();
} catch (Exception e) {
log.warn("invokeLater Runnable failed: " + action, e);
}
}
running.clear();
}
}

public abstract void invokeLater(Runnable action);

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private static class DeferredPromiseRunnable<T> implements Runnable {

private GoPromise<T> _promise;

private int _mode = 0;

private T _value;

private Throwable _cause;

public DeferredPromiseRunnable(int m, GoPromise<T> p, T val, Throwable c) {
this._mode = m;
this._promise = p;
this._value = val;
this._cause = c;
}

@Override
public void run() {
switch (_mode) {
case 0:
_promise.succeed(_value);
break;
default:
_promise.fail(_cause);
break;
}
}
}

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private class CallDeferredPromise<T> extends GoPromise<T> {

private Asyn _asyn;

public CallDeferredPromise(Asyn a) {
this._asyn = a;
}

@Override
public void succeed(final T value) {
_asyn.invokeLater(new DeferredPromiseRunnable<T>(0, this, value,
null));
}

@Override
public void fail(final Throwable cause) {
_asyn.invokeLater(new DeferredPromiseRunnable<T>(1, this, null,
cause));
}
}

public <T> GoPromise<T> deferredPromise() {
return new CallDeferredPromise<T>(this);
}

public abstract boolean isAsyncSupported();

public void invokeAsync(Runnable action) {
throw new UnsupportedOperationException();
}

public static class Default extends Asyn {

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private class CallDefaultPort<T> extends Port<T>{

private Default _def;

CallDefaultPort(Default d){
this._def = d;
}

@Override
public void onEmit(Object event) {
_def.dispatch();
}

}

private final TArray<Runnable> pending = new TArray<>();
private final TArray<Runnable> running = new TArray<>();
protected final Log log;

public Default(Log log, Act<? extends Object> frame) {
this.log = log;
frame.connect(new CallDefaultPort<Object>(this)).setPriority(Short.MAX_VALUE);
}

@Override
public boolean isAsyncSupported() {
return false;
}

@Override
public void invokeAsync(Runnable action) {
throw new UnsupportedOperationException();
}

@Override
public synchronized void invokeLater(Runnable action) {
pending.add(action);
}

private void dispatch() {
synchronized (this) {
running.addAll(pending);
pending.clear();
}

for (int ii = 0, ll = running.size; ii < ll; ii++) {
Runnable action = running.get(ii);
try {
action.run();
} catch (Exception e) {
log.warn("invokeLater Runnable failed: " + action, e);
}
}
running.clear();
}
}

public abstract void invokeLater(Runnable action);

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private static class DeferredPromiseRunnable<T> implements Runnable {

private CallDeferredPromise<T> _promise;

private int _mode = 0;

private T _value;

private Throwable _cause;

public DeferredPromiseRunnable(int m, CallDeferredPromise<T> p, T val, Throwable c) {
this._mode = m;
this._promise = p;
this._value = val;
this._cause = c;
}

@Override
public void run() {
switch (_mode) {
case 0:
_promise.invokeSucceed(_value);
break;
default:
_promise.invokeFail(_cause);
break;
}
}
}

/** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/
private class CallDeferredPromise<T> extends GoPromise<T> {

private Asyn _asyn;

public CallDeferredPromise(Asyn a) {
this._asyn = a;
}

@Override
public void succeed(final T value) {
_asyn.invokeLater(new DeferredPromiseRunnable<T>(0, this, value,
null));
}

@Override
public void fail(final Throwable cause) {
_asyn.invokeLater(new DeferredPromiseRunnable<T>(1, this, null,
cause));
}

public void invokeSucceed(final T value) {
super.succeed(value);
}

public void invokeFail(final Throwable cause) {
super.fail(cause);
}
}

public <T> GoPromise<T> deferredPromise() {
return new CallDeferredPromise<T>(this);
}

public abstract boolean isAsyncSupported();

public void invokeAsync(Runnable action) {
throw new UnsupportedOperationException();
}
}