Closed Bug 85109 Opened 25 years ago Closed 20 years ago

[RFE]: Rhino should support for setting change listeners of objects in scopes.

Categories

(Rhino Graveyard :: Core, enhancement)

x86
Windows 2000
enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED WONTFIX

People

(Reporter: Bill.T.Wang, Assigned: norrisboyd)

Details

Attachments

(1 file)

I'm developing a project using embeded javascript engine. It requires notification when a piece of javascript changes or creates variables in top- level scope. The desired codeing pattern like the following: Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(new ImporterTopLevel()); scope.addPropertyChangeListener(new MyChangeListener()); cx.evaluateString(scope,"var1 = 1;","<<change test>>",1,null); When create variable var1 or change the value of var1, the javascript engine will call the change listener automatically.
Enhancement of Rhino 1.5 By Bill Wang E-mail: Bill_T_Wang@acersoftech.com.cn Company: Acer Softech (Shanghai) Inc. Added features: You may add PropertyChangeListeners to a scope to monitor the operations on variables in the scope. You can monitor the ADD, DELETE, and MODIFY operations. A property change listener receives java.beans.PropertyChangeEvent objects. Your program can get the property name, old value, and new value from the received event objects. So you may determine the operation type from the old value and the new value. The Following table describes how to judge which operation occurred. old value new value ADD null not null DELETE not null null MODIFY not null not null Affected source files in package org.mozilla.javascript are: Scriptable.java ScriptableObject.java Undefined.java NativeJavaObject.java NativeWith.java All the modified segments were marked with: /*Bill Wang Modified --- begin*/ ADDED CODE BLOCK /*Bill Wang Modified --- end*/ If you need the enhanced feature, please feel free to contact me thru Bill_T_Wang@acersoftech.com.cn. /************************************************************************/ Code example: /************************************************************************/ /** This example shows the coding pattern for monitoring the variable changes in scopes. WhiteBoard is a variable pool. When evaluating a piece of javascript code, any changes made in localScope will reflect into the whiteBoard. */ package jstest; /** * Copyright: Copyright (c) 2001 * Company: Acer Softech (Shanghai) Inc. * @author Bill Wang * @version 1.0 * E-mail: Bill_T_Wang@acersoftech.com.cn */ import org.mozilla.javascript.*; import java.beans.*; import java.util.Hashtable; public class JsTest1 { public JsTest1() { } public static void main(String[] args) throws JavaScriptException { Context cx = Context.enter(); try { cx.setCompileFunctionsWithDynamicScope(true); Scriptable sharedScope = cx.initStandardObjects(null); Scriptable localScope = cx.newObject(sharedScope); localScope.setPrototype(sharedScope); localScope.setParentScope(null); WhiteBoard whiteBoard = new WhiteBoard(); localScope.addPropertyChangeListener( new VarChangeListener(whiteBoard)); cx.evaluateString(localScope, "intvalue = 50; intvalue=60; str = 'sssssss';" , "<TestScript>", 1, null); System.out.println(whiteBoard.getValue("intvalue")); System.out.println(whiteBoard.getValue("str")); }catch(Exception ex){ ex.printStackTrace(); } finally { cx.exit(); } } } class WhiteBoard { Hashtable varList = new Hashtable(); public WhiteBoard() { varList.put("str",""); varList.put("intvalue",new Integer(0)); } public void changeVarValue(String varName, Object value) { varList.put(varName,value); } public Object getValue(String varName) { return varList.get(varName); } } class VarChangeListener implements java.beans.PropertyChangeListener { WhiteBoard whiteBoard = null; public VarChangeListener(WhiteBoard wb) { whiteBoard = wb; } public void propertyChange(PropertyChangeEvent evt) { System.out.println("call propertyChange in listener"); String varName = evt.getPropertyName(); Object oldValue = evt.getOldValue(); Object newValue = evt.getNewValue(); System.out.println("("+varName+": "+oldValue+", "+newValue+")"); whiteBoard.changeVarValue(varName,newValue); } } /********************/ Running Result /********************/ call propertyChange in listener (intvalue: null, 50) call propertyChange in listener (intvalue: 50, 60) call propertyChange in listener (str: null, sssssss) 60 sssssss
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Bill, did you really mean to close the bug out? Don't you want it to stay open for consideration?
Reopening for consideration -
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
Summary: Requests for Enhancement: Rhino should support for setting change listeners of objects in scopes. → [RFE]: Rhino should support for setting change listeners of objects in scopes.
[RFE] is deprecated in favor of severity: enhancement. They have the same meaning.
Severity: critical → enhancement
I can only imagine this in core Rhino as a separate class. It is relatively easy to create a Scriptable implementation that wraps another Scriptable and allows addition of property change listeners to itself and emits the events on modifications ("decorator" pattern), i.e. a PropertyChangeObservableScriptable, or to avoid too long names, just ObservableScriptable. So you'd use it as: Scriptable localScope = cx.newObject(sharedScope); Scriptable observableScope = new ObservableScriptable(localScope); observableScope.addPropertyChangeListener(...); cx.evaluateString(observableScope, ...);
Is there anyone out there still interested in this? If not, I'd not burden the js.jar with an observable decorator for scriptables and just be done with it with a WONTFIX. So, if you're out there using Rhino and want this to be finally implemented (after 5 years of waiting for it...), please speak up.
Won't fix. Don't think this provides significant added value to burden js.jar with it -- last call for speaking up in favor of this feature remained without a reply for two weeks now. It is relatively easy to build such code as an independent decorator implementing Scriptable.
Status: REOPENED → RESOLVED
Closed: 25 years ago → 20 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: