package samples; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsDate; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONParser; import com.google.gwt.json.client.JSONString; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window;
public class JsBus extends JavaScriptObject { public static class JsHandlerRegistration extends JavaScriptObject implements HandlerRegistration { protected JsHandlerRegistration() {} @Override public native final void removeHandler() /*-{ this.removeHandler(); }-*/; } public static interface Callback { void call(JavaScriptObject event); } public static final native JsBus getInstance() /*-{ var name = "jsBusSingletonInstance"; return $wnd[name] || ($wnd[name] = (function(){ var _h = {}; return { fire:function(key, evt){ if(_h[key]){ for(var i=0;i<_h[key].length;i++){ try { _h[key][i](evt); } catch(e) {} } } }, addHandler:function(key, handler){ (_h[key] || (_h[key] = [])).push(handler); return { removeHandler:function(){ if(_h[key]){ for(var i=_h[key].length-1; i>-1; i--){ if(_h[key][i] === handler){ _h[key].splice(i, 1); if(_h[key].length == 0){ _h[key] = null; delete _h[key]; } return; } } } } }; } } })()); }-*/; protected JsBus() {} public void fire(String key, JSONObject event) { fire(key, event.getJavaScriptObject()); } public native void fire(String key, JavaScriptObject event) /*-{ this.fire(key, event); }-*/; public native JsHandlerRegistration addHandler(String key, Callback callback) /*-{ return this.addHandler(key, function(evt){ callback.@samples.JsBus.Callback::call(Lcom/google/gwt/core/client/ JavaScriptObject;)(evt); }); }-*/; } class A implements EntryPoint { @Override public void onModuleLoad() { final JsBus jsBus = JsBus.getInstance(); jsBus.addHandler("timer_in_b", new JsBus.Callback() { @Override public void call(JavaScriptObject event) { Window.alert(event.<JsDate> cast().toLocaleTimeString()); } }); new Timer() { @Override public void run() { JSONObject event = new JSONObject(); event.put("message", new JSONString("Message from class A!")); event.put("data", JSONParser .parseStrict("{\"someData\":[1,2,3]}")); jsBus.fire("event_from_A", event); } }.schedule(10000); } } class B implements EntryPoint { @Override public void onModuleLoad() { final Timer timer = new Timer() { @Override public void run() { JsBus.getInstance().fire("timer_in_b", JsDate.create()); } }; timer.scheduleRepeating(1500); JsBus.getInstance().addHandler("event_from_A", new JsBus.Callback() { @Override public void call(JavaScriptObject event) { timer.cancel(); Window.alert(new JSONObject(event).get("message") .isString().stringValue()); } }); } } -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-toolkit@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.